PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/Asp.java

http://mobicents.googlecode.com/
Java | 364 lines | 185 code | 73 blank | 106 comment | 13 complexity | ba39c255228aed91fc32701f8020f693 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;
 24
 25import javolution.xml.XMLFormat;
 26import javolution.xml.XMLSerializable;
 27import javolution.xml.stream.XMLStreamException;
 28
 29import org.mobicents.protocols.ss7.m3ua.ExchangeType;
 30import org.mobicents.protocols.ss7.m3ua.IPSPType;
 31import org.mobicents.protocols.ss7.m3ua.impl.fsm.FSM;
 32import org.mobicents.protocols.ss7.m3ua.impl.message.MessageFactoryImpl;
 33import org.mobicents.protocols.ss7.m3ua.message.MessageFactory;
 34import org.mobicents.protocols.ss7.m3ua.parameter.ASPIdentifier;
 35
 36/**
 37 * 
 38 * @author amit bhayani
 39 * 
 40 */
 41public class Asp implements XMLSerializable {
 42
 43	protected static final String NAME = "name";
 44
 45	protected String name;
 46
 47	/**
 48	 * Local FSM is such that it sends ASP_UP to other side
 49	 **/
 50	protected FSM localFSM;
 51
 52	/**
 53	 * Peer FSM is such that it receives ASP_UP from other side
 54	 **/
 55	protected FSM peerFSM;
 56
 57	protected AspFactory aspFactory;
 58
 59	protected As as;
 60
 61	protected ASPIdentifier aSPIdentifier;
 62
 63	private MessageFactory messageFactory = new MessageFactoryImpl();
 64
 65	public Asp() {
 66
 67	}
 68
 69	public Asp(String name, AspFactory aspFactroy) {
 70		this.name = name;
 71		this.aspFactory = aspFactroy;
 72		this.init();
 73	}
 74
 75	private void init() {
 76
 77		switch (this.aspFactory.functionality) {
 78		case IPSP:
 79			if (this.aspFactory.exchangeType == ExchangeType.SE) {
 80				if (this.aspFactory.ipspType == IPSPType.CLIENT) {
 81					// If this ASP is IPSP and client side, it should send the
 82					// ASP_UP to other side
 83					this.initLocalFSM();
 84				} else {
 85					// else this will receive ASP_UP from other side
 86					this.initPeerFSM();
 87				}
 88			} else {
 89				// If this is IPSP and DE, it should maintain two states. One
 90				// for sending ASP_UP to other side and other for receiving
 91				// ASP_UP form
 92				// other side
 93				this.initPeerFSM();
 94				this.initLocalFSM();
 95			}
 96			break;
 97		case AS:
 98			if (this.aspFactory.exchangeType == ExchangeType.SE) {
 99				// If this is AS side, it should always send the ASP_UP to other
100				// side
101				this.initLocalFSM();
102			} else {
103				// If DE it should maintain two states
104				this.initPeerFSM();
105				this.initLocalFSM();
106			}
107			break;
108		case SGW:
109			if (this.aspFactory.exchangeType == ExchangeType.SE) {
110				// If this is SGW, it should always receive ASP_UP from other
111				// side
112				this.initPeerFSM();
113			} else {
114				// If DE it should maintain two states
115				this.initPeerFSM();
116				this.initLocalFSM();
117			}
118			break;
119		}
120	}
121
122	private void initLocalFSM() {
123		this.localFSM = new FSM(this.name + "_LOCAL");
124		// Define states
125		this.localFSM.createState(AspState.DOWN_SENT.toString());
126		this.localFSM.createState(AspState.DOWN.toString());
127		this.localFSM.createState(AspState.UP_SENT.toString());
128		this.localFSM.createState(AspState.INACTIVE.toString());
129		this.localFSM.createState(AspState.ACTIVE_SENT.toString());
130		this.localFSM.createState(AspState.ACTIVE.toString());
131		this.localFSM.createState(AspState.INACTIVE_SENT.toString());
132
133		this.localFSM.setStart(AspState.DOWN.toString());
134		this.localFSM.setEnd(AspState.DOWN.toString());
135
136		// Define Transitions
137
138		// ******************************************************************/
139		// DOWN /
140		// ******************************************************************/
141		this.localFSM.createTransition(TransitionState.COMM_UP, AspState.DOWN.toString(), AspState.UP_SENT.toString());
142		// .setHandler(new AspTransDwnToAspUpSnt(this, this.fsm));
143
144		this.localFSM.createTransition(TransitionState.COMM_DOWN, AspState.DOWN.toString(), AspState.DOWN.toString());
145
146		// ******************************************************************/
147		// UP_SENT/
148		// ******************************************************************/
149		// Keep sending ASP_UP if timeout occurs.
150		this.localFSM.createTimeoutTransition(AspState.UP_SENT.toString(), AspState.UP_SENT.toString(), 2000)
151				.setHandler(new THLocalAspDwnToAspUpSnt(this, this.localFSM));
152
153		this.localFSM.createTransition(TransitionState.ASP_INACTIVE, AspState.UP_SENT.toString(),
154				AspState.INACTIVE.toString());
155
156		this.localFSM.createTransition(TransitionState.ASP_ACTIVE_SENT, AspState.UP_SENT.toString(),
157				AspState.ACTIVE_SENT.toString());
158
159		this.localFSM.createTransition(TransitionState.ASP_DOWN_SENT, AspState.UP_SENT.toString(),
160				AspState.DOWN_SENT.toString());
161
162		this.localFSM
163				.createTransition(TransitionState.COMM_DOWN, AspState.UP_SENT.toString(), AspState.DOWN.toString());
164
165		// ******************************************************************/
166		// ACTIVE_SENT/
167		// ******************************************************************/
168		// TODO Keep sending ASP_ACTIVE ?
169		this.localFSM.createTimeoutTransition(AspState.ACTIVE_SENT.toString(), AspState.ACTIVE_SENT.toString(), 2000);
170
171		this.localFSM.createTransition(TransitionState.ASP_ACTIVE_ACK, AspState.ACTIVE_SENT.toString(),
172				AspState.ACTIVE.toString());
173		// .setHandler(new AspTransActSntToAct(this, this.fsm));
174
175		this.localFSM.createTransition(TransitionState.ASP_DOWN_SENT, AspState.ACTIVE_SENT.toString(),
176				AspState.DOWN_SENT.toString());
177
178		this.localFSM.createTransition(TransitionState.COMM_DOWN, AspState.ACTIVE_SENT.toString(),
179				AspState.DOWN.toString());
180
181		// ******************************************************************/
182		// ACTIVE/
183		// ******************************************************************/
184		this.localFSM.createTransition(TransitionState.ASP_INACTIVE_SENT, AspState.ACTIVE.toString(),
185				AspState.INACTIVE_SENT.toString());
186		// .setHandler(new AspTransActToInactSnt(this, this.fsm));
187
188		this.localFSM.createTransition(TransitionState.OTHER_ALTERNATE_ASP_ACTIVE, AspState.ACTIVE.toString(),
189				AspState.INACTIVE.toString());
190
191		this.localFSM.createTransition(TransitionState.ASP_DOWN_SENT, AspState.ACTIVE.toString(),
192				AspState.DOWN_SENT.toString());
193
194		this.localFSM.createTransition(TransitionState.COMM_DOWN, AspState.ACTIVE.toString(), AspState.DOWN.toString());
195		// .setHandler(new AspTransActToDwn(this, this.fsm));
196
197		// ******************************************************************/
198		// INACTIVE/
199		// ******************************************************************/
200		this.localFSM.createTransition(TransitionState.COMM_DOWN, AspState.INACTIVE.toString(),
201				AspState.DOWN.toString());
202		// .setHandler(new AspTransInactToDwn(this, this.fsm));
203
204		this.localFSM.createTransition(TransitionState.ASP_ACTIVE_SENT, AspState.INACTIVE.toString(),
205				AspState.ACTIVE_SENT.toString());
206
207		this.localFSM.createTransition(TransitionState.ASP_DOWN_SENT, AspState.INACTIVE.toString(),
208				AspState.DOWN_SENT.toString());
209
210		// ******************************************************************/
211		// INACTIVE_SENT/
212		// ******************************************************************/
213		// TODO keep sending INACTIVE ASP ?
214		this.localFSM.createTimeoutTransition(AspState.INACTIVE_SENT.toString(), AspState.INACTIVE_SENT.toString(),
215				2000);
216		// TODO Take care of this .setHandler(new
217		// AspTransActToInactSnt(this,
218		// this.fsm));
219
220		this.localFSM.createTransition(TransitionState.ASP_INACTIVE_ACK, AspState.INACTIVE_SENT.toString(),
221				AspState.INACTIVE.toString());
222
223		this.localFSM.createTransition(TransitionState.ASP_DOWN_SENT, AspState.INACTIVE_SENT.toString(),
224				AspState.DOWN_SENT.toString());
225
226		this.localFSM.createTransition(TransitionState.COMM_DOWN, AspState.INACTIVE_SENT.toString(),
227				AspState.DOWN.toString());
228
229		// ******************************************************************/
230		// DOWN_SENT/
231		// ******************************************************************/
232		this.localFSM.createTransition(TransitionState.ASP_DOWN_ACK, AspState.DOWN_SENT.toString(),
233				AspState.DOWN.toString());
234
235		this.localFSM.createTransition(TransitionState.COMM_DOWN, AspState.DOWN_SENT.toString(),
236				AspState.DOWN.toString());
237	}
238
239	private void initPeerFSM() {
240		this.peerFSM = new FSM(this.name + "_PEER");
241		// Define states
242		this.peerFSM.createState(AspState.DOWN.toString());
243		this.peerFSM.createState(AspState.ACTIVE.toString());
244		this.peerFSM.createState(AspState.INACTIVE.toString());
245
246		this.peerFSM.setStart(AspState.DOWN.toString());
247		this.peerFSM.setEnd(AspState.DOWN.toString());
248
249		// Define Transitions
250
251		// ******************************************************************/
252		// STATE DOWN /
253		// ******************************************************************/
254		this.peerFSM.createTransition(TransitionState.COMM_UP, AspState.DOWN.toString(), AspState.DOWN.toString());
255		this.peerFSM.createTransition(TransitionState.COMM_DOWN, AspState.DOWN.toString(), AspState.DOWN.toString());
256		this.peerFSM.createTransition(TransitionState.ASP_UP, AspState.DOWN.toString(), AspState.INACTIVE.toString());
257
258		// If the SGP receives any other M3UA messages before an ASP Up message
259		// is received (other than ASP Down; see Section 4.3.4.2), the SGP MAY
260		// discard them.
261		this.peerFSM.createTransition(TransitionState.DAUD, AspState.DOWN.toString(), AspState.DOWN.toString());
262		this.peerFSM.createTransition(TransitionState.ASP_ACTIVE, AspState.DOWN.toString(), AspState.DOWN.toString());
263		this.peerFSM.createTransition(TransitionState.ASP_INACTIVE, AspState.DOWN.toString(), AspState.DOWN.toString());
264		this.peerFSM.createTransition(TransitionState.PAYLOAD, AspState.DOWN.toString(), AspState.DOWN.toString());
265
266		this.peerFSM.createTransition(TransitionState.ASP_DOWN, AspState.DOWN.toString(), AspState.DOWN.toString());
267		// TODO : For REG_REQ/DREG_REQ we don't support dynamic adding of
268		// key.
269		// Handle this
270
271		// ******************************************************************/
272		// STATE INACTIVE /
273		// ******************************************************************/
274		this.peerFSM
275				.createTransition(TransitionState.COMM_DOWN, AspState.INACTIVE.toString(), AspState.DOWN.toString());
276		// Mo transition needed? .setHandler(new RemAspTransInactToDwn(this,
277		// this.fsm));
278
279		this.peerFSM.createTransition(TransitionState.ASP_UP, AspState.INACTIVE.toString(),
280				AspState.INACTIVE.toString());
281
282		this.peerFSM.createTransition(TransitionState.ASP_DOWN, AspState.INACTIVE.toString(), AspState.DOWN.toString());
283
284		this.peerFSM.createTransition(TransitionState.ASP_ACTIVE, AspState.INACTIVE.toString(),
285				AspState.ACTIVE.toString());
286
287		// TODO: Ignore payload if Remote ASP is still INACTIVE. may be log
288		// it?
289		this.peerFSM.createTransition(TransitionState.PAYLOAD, AspState.INACTIVE.toString(),
290				AspState.INACTIVE.toString());
291
292		// ******************************************************************/
293		// STATE ACTIVE /
294		// ******************************************************************/
295		this.peerFSM.createTransition(TransitionState.COMM_DOWN, AspState.ACTIVE.toString(), AspState.DOWN.toString());
296
297		this.peerFSM.createTransition(TransitionState.ASP_UP, AspState.ACTIVE.toString(), AspState.INACTIVE.toString());
298
299		this.peerFSM.createTransition(TransitionState.ASP_DOWN, AspState.ACTIVE.toString(), AspState.DOWN.toString());
300
301		this.peerFSM.createTransition(TransitionState.ASP_INACTIVE, AspState.ACTIVE.toString(),
302				AspState.INACTIVE.toString());
303		// No transition handler .setHandler(new RemAspTransActToInact(this,
304		// this.fsm));
305
306		this.peerFSM.createTransition(TransitionState.PAYLOAD, AspState.ACTIVE.toString(), AspState.ACTIVE.toString());
307
308		// This transition will be signaled from SGW
309		this.peerFSM.createTransition(TransitionState.OTHER_ALTERNATE_ASP_ACTIVE, AspState.ACTIVE.toString(),
310				AspState.INACTIVE.toString());
311	}
312
313	public String getName() {
314		return this.name;
315	}
316
317	public FSM getLocalFSM() {
318		return localFSM;
319	}
320
321	public FSM getPeerFSM() {
322		return peerFSM;
323	}
324
325	public As getAs() {
326		return as;
327	}
328
329	public void setAs(As as) {
330		this.as = as;
331	}
332
333	public AspFactory getAspFactory() {
334		return this.aspFactory;
335	}
336
337	public ASPIdentifier getASPIdentifier() {
338		return aSPIdentifier;
339	}
340
341	public void setASPIdentifier(ASPIdentifier identifier) {
342		aSPIdentifier = identifier;
343	}
344
345	public MessageFactory getMessageFactory() {
346		return messageFactory;
347	}
348
349	/**
350	 * XML Serialization/Deserialization
351	 */
352	protected static final XMLFormat<Asp> ASP_XML = new XMLFormat<Asp>(Asp.class) {
353
354		@Override
355		public void read(javolution.xml.XMLFormat.InputElement xml, Asp asp) throws XMLStreamException {
356			asp.name = xml.getAttribute(NAME, "");
357		}
358
359		@Override
360		public void write(Asp asp, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
361			xml.setAttribute(NAME, asp.name);
362		}
363	};
364}