PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 155 lines | 84 code | 30 blank | 41 comment | 24 complexity | c213ae1a5cbe578a339de8a73e254fc8 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
 39/**
 40 * 
 41 * @author amit bhayani
 42 *
 43 */
 44public class RemAsTransActToPendRemAspDwn implements TransitionHandler {
 45
 46    private static final Logger logger = Logger.getLogger(RemAsTransActToPendRemAspDwn.class);
 47
 48    private RemAsImpl as = null;
 49    private FSM fsm;
 50
 51    private int lbCount = 0;
 52
 53    public RemAsTransActToPendRemAspDwn(RemAsImpl as, FSM fsm) {
 54        this.as = as;
 55        this.fsm = fsm;
 56    }
 57
 58    public boolean process(State state) {
 59        try {
 60            RemAspImpl remAsp = (RemAspImpl) this.fsm.getAttribute(RemAsImpl.ATTRIBUTE_ASP);
 61
 62            if (this.as.getTrafficModeType().getMode() == TrafficModeType.Broadcast) {
 63                // We don't support this
 64                return false;
 65
 66            }
 67
 68            if (this.as.getTrafficModeType().getMode() == TrafficModeType.Loadshare) {
 69                this.lbCount = 0;
 70
 71                for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList()
 72                        .tail(); (n = n.getNext()) != end;) {
 73                    RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
 74                    if (remAspImpl.getState() == AspState.ACTIVE) {
 75                        this.lbCount++;
 76                    }
 77                }// for
 78
 79                // TODO : yet not sure. May be the communication died with this
 80                // ASP who is causing this Transition; so the state will still
 81                // be ACTIVE for it
 82                if (remAsp.getState() == AspState.ACTIVE) {
 83                    this.lbCount--;
 84                }
 85
 86                if (this.lbCount >= this.as.getMinAspActiveForLb()) {
 87                    // we still have more ASP's ACTIVE for lb. Don't change
 88                    // state
 89                    return false;
 90                }
 91
 92                if (this.lbCount > 0) {
 93                    // In any case if we have at least one ASP that can take
 94                    // care of traffic, don't change state
 95
 96                    // But we are below threshold. Send "Ins. ASPs" to INACTIVE
 97                    // ASP's but not to ASP that caused this transition as it is
 98                    // already DOWN
 99                    for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as
100                            .getAspList().tail(); (n = n.getNext()) != end;) {
101                        RemAspImpl remAspTemp = (RemAspImpl) n.getValue();
102
103                        // TODO : needed? The state should be changed before
104                        // signalling AS transition
105                        if (remAspTemp.getName().compareTo(remAsp.getName()) == 0) {
106                            continue;
107                        }
108                        if (remAspTemp.getState() == AspState.INACTIVE) {
109                            Notify notify = this.createNotify(remAsp, Status.STATUS_Other,
110                                    Status.INFO_Insufficient_ASP_Resources_Active);
111                            remAspTemp.getAspFactory().write(notify);
112                        }
113                    }
114
115                    return false;
116                }
117
118            }//If Loadshare
119
120            // We have reached here means AS is transitioning to be PENDING.
121            // Send new AS STATUS to all INACTIVE APS's
122
123            for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList()
124                    .tail(); (n = n.getNext()) != end;) {
125                remAsp = (RemAspImpl) n.getValue();
126                if (remAsp.getState() == AspState.INACTIVE) {
127                    Notify notify = this.createNotify(remAsp, Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING);
128                    remAsp.getAspFactory().write(notify);
129                }
130            }
131
132        } catch (Exception e) {
133            logger.error(String.format("Error while translating Rem AS to PENDING. %s", this.fsm.toString()), e);
134        }
135
136        return true;
137    }
138
139    private Notify createNotify(RemAspImpl remAsp, int type, int info) {
140        Notify msg = (Notify) this.as.getM3UAProvider().getMessageFactory().createMessage(MessageClass.MANAGEMENT,
141                MessageType.NOTIFY);
142
143        Status status = this.as.getM3UAProvider().getParameterFactory().createStatus(type, info);
144        msg.setStatus(status);
145
146        if (remAsp.getASPIdentifier() != null) {
147            msg.setASPIdentifier(remAsp.getASPIdentifier());
148        }
149
150        msg.setRoutingContext(this.as.getRoutingContext());
151
152        return msg;
153    }
154
155}