PageRenderTime 109ms CodeModel.GetById 82ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 173 lines | 79 code | 25 blank | 69 comment | 20 complexity | 0bc74a9a002a5c52bd45e150ebb785f3 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.AsState;
 29import org.mobicents.protocols.ss7.m3ua.impl.Asp;
 30import org.mobicents.protocols.ss7.m3ua.impl.AspState;
 31import org.mobicents.protocols.ss7.m3ua.impl.fsm.FSM;
 32import org.mobicents.protocols.ss7.m3ua.impl.fsm.State;
 33import org.mobicents.protocols.ss7.m3ua.impl.fsm.TransitionHandler;
 34import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
 35import org.mobicents.protocols.ss7.m3ua.message.MessageType;
 36import org.mobicents.protocols.ss7.m3ua.message.mgmt.Notify;
 37import org.mobicents.protocols.ss7.m3ua.parameter.Status;
 38import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
 39
 40/**
 41 * <p>
 42 * Transitions the {@link RemAsImpl} from {@link AsState.ACTIVE} to
 43 * {@link AsState.PENDING}. The transition depends on {@link TrafficModeType}
 44 * defined for this {@link RemAsImpl} and {@link AspState} of enclosed
 45 * {@link RemAsImpl}
 46 * </p>
 47 * 
 48 * 
 49 *   <ul>
 50 *
 51 *     <li>
 52 *     	<p> If the {@link TrafficModeType} for this {@link RemAsImpl} is Loadshare and 
 53 *     	there are {@link RemAsImpl} that are still {@link AspState.ACTIVE}, {@link RemAsImpl}
 54 *      remains {@link AsState.ACTIVE}. If number of {@link RemAsImpl} in {@link AspState.ACTIVE}
 55 *      is greater than minimum Asp required for load balancing, it just returns false and 
 56 *      transition doesn't happen. However if number of {@link RemAsImpl} in {@link AspState.ACTIVE}
 57 *      is less than minimum Asp required for load balancing, it sends {@link Status.INFO_Insufficient_ASP_Resources_Active}
 58 *      {@link Notify} to remote ASP who are {@link AspState.INACTIVE} and transition doesn't happen.
 59 *    	</p>
 60 *    
 61 *    	<p>
 62 *    		If there are no {@link RemAsImpl} in {@link AspState.ACTIVE}, it transitions to {@link AsState.PENDIING} and
 63 *    		sends {@link Status.INFO_AS_PENDING} {@link Notify} to remote ASP who are {@link AspState.INACTIVE}.
 64 *    	</p>
 65 *     </li>
 66 *
 67 *     <li>
 68 *     	<p> If the {@link TrafficModeType} for this {@link RemAsImpl} is Override 
 69 *     		it transitions to {@link AsState.PENDIING} and sends {@link Status.INFO_AS_PENDING} {@link Notify} 
 70 *     		to remote ASP who are {@link AspState.INACTIVE}.
 71 *    	</p>
 72 *     </li>
 73 *
 74 *   </ul>
 75 * 
 76 * @author amit bhayani
 77 * 
 78 */
 79public class RemAsTransActToPendRemAspInac implements TransitionHandler {
 80
 81	private static final Logger logger = Logger.getLogger(RemAsTransActToPendRemAspInac.class);
 82
 83	private RemAsImpl as = null;
 84	private FSM fsm;
 85
 86	private int lbCount = 0;
 87
 88	public RemAsTransActToPendRemAspInac(RemAsImpl as, FSM fsm) {
 89		this.as = as;
 90		this.fsm = fsm;
 91	}
 92
 93	public boolean process(State state) {
 94		try {
 95			RemAspImpl remAsp = (RemAspImpl) this.fsm.getAttribute(RemAsImpl.ATTRIBUTE_ASP);
 96
 97			if (this.as.getTrafficModeType().getMode() == TrafficModeType.Broadcast) {
 98				// We don't support this
 99				return false;
100
101			}
102
103			if (this.as.getTrafficModeType().getMode() == TrafficModeType.Loadshare) {
104				this.lbCount = 0;
105
106				for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n
107						.getNext()) != end;) {
108					RemAspImpl remAspImpl = (RemAspImpl) n.getValue();
109					if (remAspImpl.getState() == AspState.ACTIVE) {
110						this.lbCount++;
111					}
112				}// for
113
114				if (this.lbCount >= this.as.getMinAspActiveForLb()) {
115					// we still have more ASP's ACTIVE for lb. Don't change
116					// state
117					return false;
118				}
119
120				// We are below minAspActiveForLb required for LB
121				if (this.lbCount > 0) {
122					// But In any case if we have at least one ASP that can take
123					// care of traffic, don't change state but send the "Ins.
124					// ASPs" to INACTIVE ASP's
125
126					for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n
127							.getNext()) != end;) {
128						remAsp = (RemAspImpl) n.getValue();
129						if (remAsp.getState() == AspState.INACTIVE) {
130							Notify notify = this.createNotify(remAsp, Status.STATUS_Other,
131									Status.INFO_Insufficient_ASP_Resources_Active);
132							remAsp.getAspFactory().write(notify);
133						}
134					}
135
136					return false;
137				}
138			}// If Loadshare
139
140			// We have reached here means AS is transitioning to be PENDING.
141			// Send new AS STATUS to all INACTIVE APS's
142
143			for (FastList.Node<Asp> n = this.as.getAspList().head(), end = this.as.getAspList().tail(); (n = n
144					.getNext()) != end;) {
145				remAsp = (RemAspImpl) n.getValue();
146				if (remAsp.getState() == AspState.INACTIVE) {
147					Notify notify = this.createNotify(remAsp, Status.STATUS_AS_State_Change, Status.INFO_AS_PENDING);
148					remAsp.getAspFactory().write(notify);
149				}
150			}
151		} catch (Exception e) {
152			logger.error(String.format("Error while translating Rem AS to PENDING. %s", this.fsm.toString()), e);
153		}
154		return true;
155	}
156
157	private Notify createNotify(RemAspImpl remAsp, int type, int info) {
158		Notify msg = (Notify) this.as.getM3UAProvider().getMessageFactory()
159				.createMessage(MessageClass.MANAGEMENT, MessageType.NOTIFY);
160
161		Status status = this.as.getM3UAProvider().getParameterFactory().createStatus(type, info);
162		msg.setStatus(status);
163
164		if (remAsp.getASPIdentifier() != null) {
165			msg.setASPIdentifier(remAsp.getASPIdentifier());
166		}
167
168		msg.setRoutingContext(this.as.getRoutingContext());
169
170		return msg;
171	}
172
173}