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