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

http://mobicents.googlecode.com/ · Java · 337 lines · 209 code · 59 blank · 69 comment · 95 complexity · 10cd3b2d1dcde1766655d311284019a3 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.sg;
  23. import java.io.FileNotFoundException;
  24. import javolution.util.FastList;
  25. import org.apache.log4j.Logger;
  26. import org.mobicents.protocols.ss7.m3ua.impl.As;
  27. import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
  28. import org.mobicents.protocols.ss7.m3ua.impl.M3UAManagement;
  29. import org.mobicents.protocols.ss7.m3ua.impl.oam.M3UAOAMMessages;
  30. import org.mobicents.protocols.ss7.m3ua.impl.router.ServerM3UARouter;
  31. import org.mobicents.protocols.ss7.m3ua.parameter.DestinationPointCode;
  32. import org.mobicents.protocols.ss7.m3ua.parameter.OPCList;
  33. import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
  34. import org.mobicents.protocols.ss7.m3ua.parameter.RoutingKey;
  35. import org.mobicents.protocols.ss7.m3ua.parameter.ServiceIndicators;
  36. import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
  37. /**
  38. * @author amit bhayani
  39. *
  40. */
  41. public class ServerM3UAManagement extends M3UAManagement {
  42. private static final Logger logger = Logger.getLogger(ServerM3UAManagement.class);
  43. protected ServerM3UARouter m3uaRouter = new ServerM3UARouter();
  44. /**
  45. *
  46. */
  47. public ServerM3UAManagement() {
  48. this.PERSIST_FILE_NAME = "m3ua-server.xml";
  49. }
  50. /**
  51. * Expected command is m3ua ras create rc <rc> rk dpc <dpc> opc <opc-list>
  52. * si <si-list> traffic-mode {broadcast|loadshare|override} <ras-name>
  53. *
  54. * opc, si and traffic-mode is not compulsory
  55. *
  56. * @param args
  57. * @return
  58. * @throws Exception
  59. */
  60. public As createAppServer(String args[]) throws Exception {
  61. if (args.length < 9) {
  62. // minimum 8 args needed
  63. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  64. }
  65. String rcKey = args[3];
  66. if (rcKey == null || rcKey.compareTo("rc") != 0) {
  67. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  68. }
  69. String rc = args[4];
  70. if (rc == null) {
  71. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  72. }
  73. RoutingContext rcObj = m3uaProvider.getParameterFactory().createRoutingContext(
  74. new long[] { Long.parseLong(rc) });
  75. // Routing Key
  76. if (args[5] == null || args[5].compareTo("rk") != 0) {
  77. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  78. }
  79. if (args[6] == null || args[6].compareTo("dpc") != 0) {
  80. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  81. }
  82. int dpc = Integer.parseInt(args[7]);
  83. DestinationPointCode dspobj = m3uaProvider.getParameterFactory().createDestinationPointCode(dpc, (short) 0);
  84. OPCList opcListObj = null;
  85. ServiceIndicators si = null;
  86. TrafficModeType trMode = null;
  87. String name = null;
  88. if (args[8] == null) {
  89. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  90. }
  91. if (args.length >= 11) {
  92. // OPC also defined
  93. if (args[8].compareTo("opc") == 0) {
  94. // comma separated OPC list
  95. String opcListStr = args[9];
  96. if (opcListStr == null) {
  97. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  98. }
  99. String[] opcListArr = opcListStr.split(",");
  100. int[] opcs = new int[opcListArr.length];
  101. short[] masks = new short[opcListArr.length];
  102. for (int count = 0; count < opcListArr.length; count++) {
  103. opcs[count] = Integer.parseInt(opcListArr[count]);
  104. masks[count] = 0; // TODO mask should be sent in command
  105. }
  106. opcListObj = m3uaProvider.getParameterFactory().createOPCList(opcs, masks);
  107. if (args.length >= 13) {
  108. if (args[10].compareTo("si") == 0) {
  109. si = this.createSi(args, 11);
  110. if (args[12] == null) {
  111. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  112. }
  113. if (args.length == 15) {
  114. if (args[12].compareTo("traffic-mode") != 0) {
  115. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  116. }
  117. trMode = createTrMode(args, 13);
  118. name = args[14];
  119. } else {
  120. name = args[12];
  121. }
  122. } else if (args[10].compareTo("traffic-mode") == 0) {
  123. trMode = createTrMode(args, 11);
  124. name = args[12];
  125. } else {
  126. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  127. }
  128. } else {
  129. name = args[10];
  130. }
  131. } else if (args[8].compareTo("si") == 0) {
  132. si = this.createSi(args, 9);
  133. if (args[10] == null) {
  134. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  135. }
  136. if (args.length == 13) {
  137. if (args[10].compareTo("traffic-mode") != 0) {
  138. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  139. }
  140. trMode = createTrMode(args, 11);
  141. name = args[12];
  142. }
  143. } else if (args[8].compareTo("traffic-mode") == 0) {
  144. trMode = this.createTrMode(args, 9);
  145. name = args[10];
  146. } else {
  147. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  148. }
  149. } else {
  150. name = args[8];
  151. }
  152. for (FastList.Node<As> n = appServers.head(), end = appServers.tail(); (n = n.getNext()) != end;) {
  153. As as = n.getValue();
  154. if (as.getName().compareTo(name) == 0) {
  155. throw new Exception(String.format(M3UAOAMMessages.CREATE_AS_FAIL_NAME_EXIST, name));
  156. }
  157. // TODO : Check for duplication of RoutingKey
  158. }
  159. RoutingKey rk = m3uaProvider.getParameterFactory().createRoutingKey(null, rcObj, trMode, null,
  160. new DestinationPointCode[] { dspobj }, si != null ? new ServiceIndicators[] { si } : null,
  161. opcListObj != null ? new OPCList[] { opcListObj } : null);
  162. RemAsImpl as = new RemAsImpl(name, rcObj, rk, trMode, this.m3uaProvider);
  163. m3uaRouter.addRk(rk, as);
  164. m3uaScheduler.execute(as.getFSM());
  165. appServers.add(as);
  166. this.store();
  167. return as;
  168. }
  169. /**
  170. * Command to create ASPFactory is "m3ua rasp create ip <ip> port <port>
  171. * <asp-name>"
  172. *
  173. * @param args
  174. * @return
  175. * @throws Exception
  176. */
  177. public AspFactory createAspFactory(String[] args) throws Exception {
  178. if (args.length != 8) {
  179. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  180. }
  181. if (args[3] == null || args[3].compareTo("ip") != 0) {
  182. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  183. }
  184. if (args[4] == null) {
  185. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  186. }
  187. String ip = args[4];
  188. if (args[5] == null || args[5].compareTo("port") != 0) {
  189. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  190. }
  191. int port = Integer.parseInt(args[6]);
  192. if (args[7] == null) {
  193. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  194. }
  195. String name = args[7];
  196. for (FastList.Node<AspFactory> n = aspfactories.head(), end = aspfactories.tail(); (n = n.getNext()) != end;) {
  197. AspFactory fact = n.getValue();
  198. if (fact.getName().compareTo(name) == 0) {
  199. throw new Exception(String.format(M3UAOAMMessages.CREATE_ASP_FAIL_NAME_EXIST, name));
  200. }
  201. if (fact.getIp().compareTo(ip) == 0 && fact.getPort() == port) {
  202. throw new Exception(String.format(M3UAOAMMessages.CREATE_ASP_FAIL_IPPORT_EXIST, ip, port));
  203. }
  204. }
  205. AspFactory factory = new RemAspFactory(name, ip, port, this.m3uaProvider, this);
  206. aspfactories.add(factory);
  207. this.store();
  208. return factory;
  209. }
  210. /*
  211. * (non-Javadoc)
  212. *
  213. * @see
  214. * org.mobicents.protocols.ss7.m3ua.impl.M3UAManagement#startAsp(java.lang
  215. * .String)
  216. */
  217. @Override
  218. public void managementStartAsp(String aspName) throws Exception {
  219. throw new UnsupportedOperationException("Start ASP not supported in SGW");
  220. }
  221. /*
  222. * (non-Javadoc)
  223. *
  224. * @see
  225. * org.mobicents.protocols.ss7.m3ua.impl.M3UAManagement#stopAsp(java.lang
  226. * .String)
  227. */
  228. @Override
  229. public void managementStopAsp(String aspName) throws Exception {
  230. throw new UnsupportedOperationException("Stop ASP not supported in SGW");
  231. }
  232. private ServiceIndicators createSi(String args[], int index) throws Exception {
  233. ServiceIndicators si = null;
  234. String siStr = args[index++];
  235. if (siStr == null) {
  236. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  237. }
  238. String[] sitArr = siStr.split(",");
  239. short[] sis = new short[sitArr.length];
  240. for (int count = 0; count < sis.length; count++) {
  241. sis[count] = Short.parseShort(sitArr[count]);
  242. }
  243. si = m3uaProvider.getParameterFactory().createServiceIndicators(sis);
  244. return si;
  245. }
  246. private TrafficModeType createTrMode(String[] args, int index) throws Exception {
  247. if (args[index] == null) {
  248. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  249. }
  250. if (args[index].compareTo("broadcast") == 0) {
  251. return this.m3uaProvider.getParameterFactory().createTrafficModeType(TrafficModeType.Broadcast);
  252. } else if (args[index].compareTo("override") == 0) {
  253. return this.m3uaProvider.getParameterFactory().createTrafficModeType(TrafficModeType.Override);
  254. } else if (args[index].compareTo("loadshare") == 0) {
  255. return this.m3uaProvider.getParameterFactory().createTrafficModeType(TrafficModeType.Loadshare);
  256. }
  257. throw new Exception(M3UAOAMMessages.INVALID_COMMAND);
  258. }
  259. @Override
  260. public void load() throws FileNotFoundException {
  261. super.load();
  262. for (FastList.Node<As> n = appServers.head(), end = appServers.tail(); (n = n.getNext()) != end;) {
  263. As as = n.getValue();
  264. try {
  265. this.m3uaRouter.addRk(as.getRoutingKey(), as);
  266. } catch (Exception e) {
  267. logger.error("Error while pupoulating Server Router loading from xml file", e);
  268. }
  269. }
  270. }
  271. /* (non-Javadoc)
  272. * @see org.mobicents.protocols.ss7.m3ua.impl.M3UAManagement#startAsp(org.mobicents.protocols.ss7.m3ua.impl.AspFactory)
  273. */
  274. @Override
  275. public void startAsp(AspFactory aspFactory) {
  276. //We do nothing here
  277. }
  278. }