PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/sg/RemAsTransInactToAct.java

http://mobicents.googlecode.com/
Java | 119 lines | 67 code | 22 blank | 30 comment | 17 complexity | 2ef41f4a0d992252ba01894220928400 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.ss7.m3ua.impl.sg;
 24
 25import javolution.util.FastList;
 26
 27import org.apache.log4j.Logger;
 28import org.mobicents.protocols.ss7.m3ua.impl.Asp;
 29import org.mobicents.protocols.ss7.m3ua.impl.AspState;
 30import org.mobicents.protocols.ss7.m3ua.impl.fsm.FSM;
 31import org.mobicents.protocols.ss7.m3ua.impl.fsm.State;
 32import org.mobicents.protocols.ss7.m3ua.impl.fsm.TransitionHandler;
 33import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
 34import org.mobicents.protocols.ss7.m3ua.message.MessageType;
 35import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
 36import org.mobicents.protocols.ss7.m3ua.parameter.Status;
 37import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
 38
 39public class RemAsTransInactToAct implements TransitionHandler {
 40
 41    private static final Logger logger = Logger.getLogger(RemAsTransInactToAct.class);
 42
 43    private RemAsImpl as = null;
 44    private FSM fsm;
 45
 46    private int lbCount = 0;
 47
 48    public RemAsTransInactToAct(RemAsImpl as, FSM fsm) {
 49        this.as = as;
 50        this.fsm = fsm;
 51    }
 52
 53    public boolean process(State state) {
 54        try {
 55
 56            if (this.as.getTrafficModeType().getMode() == TrafficModeType.Broadcast) {
 57                // We don't handle this
 58                return false;
 59            }
 60
 61            // For Traffic Mode Type = load-balancing, need to check policy to
 62            // have 'minAspActiveForLb' ASP's ACTIVE before AS_ACTIVE NOTIFY is
 63            // sent.
 64            if (this.as.getTrafficModeType().getMode() == TrafficModeType.Loadshare) {
 65                lbCount = this.as.getMinAspActiveForLb();
 66
 67                // Find out how many ASP's are ACTIVE now
 68                for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList()
 69                        .tail(); (n = n.getNext()) != end;) {
 70                    RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
 71                    if (remAspImpl.getState() == AspState.ACTIVE) {
 72                        lbCount--;
 73                    }
 74                }
 75
 76                if (lbCount > 0) {
 77                    // We still need more ASP ACTIVE before AS is ACTIVE
 78                    return false;
 79                }
 80            }
 81
 82            // Iterate through ASP's and send AS_ACTIVE to ASP's who
 83            // are INACTIVE or ACTIVE
 84            for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList()
 85                    .tail(); (n = n.getNext()) != end;) {
 86                RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
 87
 88                if (remAspImpl.getState() == AspState.INACTIVE || remAspImpl.getState() == AspState.ACTIVE) {
 89                    Notify msg = createNotify(remAspImpl);
 90                    remAspImpl.getAspFactory().write(msg);
 91                }
 92            }
 93
 94            return true;
 95        } catch (Exception e) {
 96            logger.error(String.format("Error while translating Rem AS to INACTIVE. %s", this.fsm.toString()), e);
 97        }
 98        // something wrong
 99        return false;
100    }
101
102    private Notify createNotify(RemAspImpl remAsp) {
103        Notify msg = (Notify) this.as.getM3UAProvider().getMessageFactory().createMessage(MessageClass.MANAGEMENT,
104                MessageType.NOTIFY);
105
106        Status status = this.as.getM3UAProvider().getParameterFactory().createStatus(Status.STATUS_AS_State_Change,
107                Status.INFO_AS_ACTIVE);
108        msg.setStatus(status);
109
110        if (remAsp.getASPIdentifier() != null) {
111            msg.setASPIdentifier(remAsp.getASPIdentifier());
112        }
113
114        msg.setRoutingContext(this.as.getRoutingContext());
115
116        return msg;
117    }
118
119}