/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/oam/M3UAShellExecutor.java

http://mobicents.googlecode.com/ · Java · 449 lines · 310 code · 77 blank · 62 comment · 117 complexity · feb8f673105ddf549e8e3b0b0c8df812 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.oam;
  23. import java.util.Arrays;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import org.apache.log4j.Logger;
  27. import org.mobicents.protocols.api.Management;
  28. import org.mobicents.protocols.ss7.m3ua.ExchangeType;
  29. import org.mobicents.protocols.ss7.m3ua.Functionality;
  30. import org.mobicents.protocols.ss7.m3ua.IPSPType;
  31. import org.mobicents.protocols.ss7.m3ua.impl.As;
  32. import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
  33. import org.mobicents.protocols.ss7.m3ua.impl.M3UAManagement;
  34. import org.mobicents.protocols.ss7.m3ua.impl.parameter.ParameterFactoryImpl;
  35. import org.mobicents.protocols.ss7.m3ua.parameter.NetworkAppearance;
  36. import org.mobicents.protocols.ss7.m3ua.parameter.ParameterFactory;
  37. import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
  38. import org.mobicents.protocols.ss7.m3ua.parameter.TrafficModeType;
  39. import org.mobicents.ss7.management.console.ShellExecutor;
  40. /**
  41. *
  42. * @author amit bhayani
  43. *
  44. */
  45. public class M3UAShellExecutor implements ShellExecutor {
  46. private static final Logger logger = Logger.getLogger(M3UAShellExecutor.class);
  47. private M3UAManagement m3uaManagement;
  48. protected ParameterFactory parameterFactory = new ParameterFactoryImpl();
  49. private SCTPShellExecutor sctpShellExecutor = new SCTPShellExecutor();
  50. public M3UAShellExecutor() {
  51. }
  52. public M3UAManagement getM3uaManagement() {
  53. return m3uaManagement;
  54. }
  55. public void setM3uaManagement(M3UAManagement m3uaManagement) {
  56. this.m3uaManagement = m3uaManagement;
  57. }
  58. public Management getSctpManagement() {
  59. return this.sctpShellExecutor.getSctpManagement();
  60. }
  61. public void setSctpManagement(Management sctpManagement) {
  62. this.sctpShellExecutor.setSctpManagement(sctpManagement);
  63. }
  64. /**
  65. * m3ua as create <as-name> <AS | SGW | IPSP> mode <SE | DE> ipspType <
  66. * client | server > rc <routing-context> traffic-mode <traffic mode>
  67. * network-appearance <network appearance>
  68. *
  69. * @param args
  70. * @return
  71. */
  72. private String createAs(String[] args) throws Exception {
  73. if (args.length < 5 || args.length > 15) {
  74. return M3UAOAMMessages.INVALID_COMMAND;
  75. }
  76. // Create new Rem AS
  77. String asName = args[3];
  78. if (asName == null) {
  79. return M3UAOAMMessages.INVALID_COMMAND;
  80. }
  81. Functionality functionlaity = Functionality.getFunctionality(args[4]);
  82. ExchangeType exchangeType = null;
  83. IPSPType ipspType = null;
  84. RoutingContext rc = null;
  85. TrafficModeType trafficModeType = null;
  86. NetworkAppearance na = null;
  87. if (functionlaity == null) {
  88. return M3UAOAMMessages.INVALID_COMMAND;
  89. }
  90. int count = 5;
  91. while (count < args.length) {
  92. String key = args[count++];
  93. if (key == null) {
  94. return M3UAOAMMessages.INVALID_COMMAND;
  95. }
  96. if (key.equals("mode")) {
  97. exchangeType = ExchangeType.getExchangeType(args[count++]);
  98. if (exchangeType == null) {
  99. return M3UAOAMMessages.INVALID_COMMAND;
  100. }
  101. } else if (key.equals("ipspType")) {
  102. ipspType = IPSPType.getIPSPType(args[count++]);
  103. } else if (key.equals("rc")) {
  104. long rcLong = Long.parseLong(args[count++]);
  105. rc = parameterFactory.createRoutingContext(new long[] { rcLong });
  106. } else if (key.equals("traffic-mode")) {
  107. trafficModeType = getTrafficModeType(args[count++]);
  108. } else if (key.equals("network-appearance")) {
  109. na = parameterFactory.createNetworkAppearance(Long.parseLong(args[count++]));
  110. } else {
  111. return M3UAOAMMessages.INVALID_COMMAND;
  112. }
  113. }
  114. As as = this.m3uaManagement.createAs(asName, functionlaity, exchangeType, ipspType, rc, trafficModeType, na);
  115. return String.format(M3UAOAMMessages.CREATE_AS_SUCESSFULL, as.getName());
  116. }
  117. /**
  118. * m3ua as destroy <as-name>
  119. *
  120. * @param args
  121. * @return
  122. * @throws Exception
  123. */
  124. private String destroyAs(String[] args) throws Exception {
  125. if (args.length < 4) {
  126. return M3UAOAMMessages.INVALID_COMMAND;
  127. }
  128. String asName = args[3];
  129. if (asName == null) {
  130. return M3UAOAMMessages.INVALID_COMMAND;
  131. }
  132. As as = this.m3uaManagement.destroyAs(asName);
  133. return String.format("Successfully destroyed AS name=%s", asName);
  134. }
  135. /**
  136. * m3ua as add <as-name> <asp-name>
  137. *
  138. * @param args
  139. * @return
  140. * @throws Exception
  141. */
  142. private String addAspToAs(String[] args) throws Exception {
  143. if (args.length < 5) {
  144. return M3UAOAMMessages.INVALID_COMMAND;
  145. }
  146. // Add Rem ASP to Rem AS
  147. if (args[3] == null || args[4] == null) {
  148. return M3UAOAMMessages.INVALID_COMMAND;
  149. }
  150. this.m3uaManagement.assignAspToAs(args[3], args[4]);
  151. return String.format(M3UAOAMMessages.ADD_ASP_TO_AS_SUCESSFULL, args[4], args[3]);
  152. }
  153. /**
  154. * m3ua as remove <as-name> <asp-name>
  155. *
  156. * @param args
  157. * @return
  158. * @throws Exception
  159. */
  160. private String removeAspFromAs(String[] args) throws Exception {
  161. if (args.length < 5) {
  162. return M3UAOAMMessages.INVALID_COMMAND;
  163. }
  164. // Add Rem ASP to Rem AS
  165. if (args[3] == null || args[4] == null) {
  166. return M3UAOAMMessages.INVALID_COMMAND;
  167. }
  168. this.m3uaManagement.unassignAspFromAs(args[3], args[4]);
  169. return String.format("Successfully removed ASP name=%s to AS name=%s", args[4], args[3]);
  170. }
  171. private TrafficModeType getTrafficModeType(String mode) {
  172. int iMode = -1;
  173. if (mode == null) {
  174. return null;
  175. } else if (mode.equals("loadshare")) {
  176. iMode = TrafficModeType.Loadshare;
  177. } else if (mode.equals("override")) {
  178. iMode = TrafficModeType.Override;
  179. } else if (mode.equals("broadcast")) {
  180. iMode = TrafficModeType.Broadcast;
  181. } else {
  182. return null;
  183. }
  184. return this.parameterFactory.createTrafficModeType(iMode);
  185. }
  186. private String showAspFactories() {
  187. FastList<AspFactory> aspfactories = this.m3uaManagement.getAspfactories();
  188. if (aspfactories.size() == 0) {
  189. return M3UAOAMMessages.NO_ASP_DEFINED_YET;
  190. }
  191. StringBuffer sb = new StringBuffer();
  192. for (FastList.Node<AspFactory> n = aspfactories.head(), end = aspfactories.tail(); (n = n.getNext()) != end;) {
  193. AspFactory aspFactory = n.getValue();
  194. sb.append(M3UAOAMMessages.NEW_LINE);
  195. aspFactory.show(sb);
  196. sb.append(M3UAOAMMessages.NEW_LINE);
  197. }
  198. return sb.toString();
  199. }
  200. private String showRoutes() {
  201. FastMap<String, As[]> route = this.m3uaManagement.getRoute();
  202. if (route.size() == 0) {
  203. return M3UAOAMMessages.NO_ROUTE_DEFINED_YET;
  204. }
  205. StringBuffer sb = new StringBuffer();
  206. for (FastMap.Entry<String, As[]> e = route.head(), end = route.tail(); (e = e.getNext()) != end;) {
  207. String key = e.getKey();
  208. As[] asList = e.getValue();
  209. sb.append(M3UAOAMMessages.NEW_LINE);
  210. sb.append(key);
  211. sb.append(M3UAOAMMessages.TAB);
  212. for (int i = 0; i < asList.length; i++) {
  213. As as = asList[i];
  214. if (as != null) {
  215. sb.append(as.getName());
  216. sb.append(M3UAOAMMessages.COMMA);
  217. }
  218. }
  219. sb.append(M3UAOAMMessages.NEW_LINE);
  220. }
  221. return sb.toString();
  222. }
  223. private String showAs() {
  224. FastList<As> appServers = this.m3uaManagement.getAppServers();
  225. if (appServers.size() == 0) {
  226. return M3UAOAMMessages.NO_AS_DEFINED_YET;
  227. }
  228. StringBuffer sb = new StringBuffer();
  229. for (FastList.Node<As> n = appServers.head(), end = appServers.tail(); (n = n.getNext()) != end;) {
  230. As as = n.getValue();
  231. sb.append(M3UAOAMMessages.NEW_LINE);
  232. as.show(sb);
  233. sb.append(M3UAOAMMessages.NEW_LINE);
  234. }
  235. return sb.toString();
  236. }
  237. private String executeM3UA(String[] args) {
  238. try {
  239. if (args.length < 3 || args.length > 15) {
  240. // any command will have atleast 3 args
  241. return M3UAOAMMessages.INVALID_COMMAND;
  242. }
  243. if (args[1] == null) {
  244. return M3UAOAMMessages.INVALID_COMMAND;
  245. }
  246. if (args[1].equals("as")) {
  247. // related to rem AS for SigGatewayImpl
  248. String rasCmd = args[2];
  249. if (rasCmd == null) {
  250. return M3UAOAMMessages.INVALID_COMMAND;
  251. }
  252. if (rasCmd.equals("create")) {
  253. return this.createAs(args);
  254. } else if (rasCmd.equals("destroy")) {
  255. return this.destroyAs(args);
  256. } else if (rasCmd.equals("add")) {
  257. return this.addAspToAs(args);
  258. } else if (rasCmd.equals("remove")) {
  259. return this.removeAspFromAs(args);
  260. } else if (rasCmd.equals("show")) {
  261. return this.showAs();
  262. }
  263. return M3UAOAMMessages.INVALID_COMMAND;
  264. } else if (args[1].equals("asp")) {
  265. if (args.length > 5) {
  266. return M3UAOAMMessages.INVALID_COMMAND;
  267. }
  268. // related to rem AS for SigGatewayImpl
  269. String raspCmd = args[2];
  270. if (raspCmd == null) {
  271. return M3UAOAMMessages.INVALID_COMMAND;
  272. } else if (raspCmd.equals("create")) {
  273. // Create new ASP
  274. if (args.length < 5) {
  275. return M3UAOAMMessages.INVALID_COMMAND;
  276. }
  277. String aspname = args[3];
  278. String assocName = args[4];
  279. if (aspname == null || assocName == null) {
  280. return M3UAOAMMessages.INVALID_COMMAND;
  281. }
  282. AspFactory factory = this.m3uaManagement.createAspFactory(aspname, assocName);
  283. return String.format(M3UAOAMMessages.CREATE_ASP_SUCESSFULL, factory.getName());
  284. } else if (raspCmd.equals("destroy")) {
  285. if (args.length < 4) {
  286. return M3UAOAMMessages.INVALID_COMMAND;
  287. }
  288. String aspName = args[3];
  289. this.m3uaManagement.destroyAspFactory(aspName);
  290. return String.format("Successfully destroyed ASP name=%s", aspName);
  291. } else if (raspCmd.equals("show")) {
  292. return this.showAspFactories();
  293. } else if (raspCmd.equals("start")) {
  294. if (args.length < 4) {
  295. return M3UAOAMMessages.INVALID_COMMAND;
  296. }
  297. String aspName = args[3];
  298. this.m3uaManagement.startAsp(aspName);
  299. return String.format(M3UAOAMMessages.ASP_START_SUCESSFULL, aspName);
  300. } else if (raspCmd.equals("stop")) {
  301. if (args.length < 4) {
  302. return M3UAOAMMessages.INVALID_COMMAND;
  303. }
  304. String aspName = args[3];
  305. this.m3uaManagement.stopAsp(aspName);
  306. return String.format(M3UAOAMMessages.ASP_STOP_SUCESSFULL, aspName);
  307. }
  308. return M3UAOAMMessages.INVALID_COMMAND;
  309. } else if (args[1].equals("route")) {
  310. String routeCmd = args[2];
  311. if (routeCmd == null) {
  312. return M3UAOAMMessages.INVALID_COMMAND;
  313. }
  314. if (routeCmd.equals("add")) {
  315. if (args.length < 5 || args.length > 7) {
  316. return M3UAOAMMessages.INVALID_COMMAND;
  317. }
  318. int count = 3;
  319. String asName = args[count++];
  320. int dpc = -1;
  321. int opc = -1;
  322. int si = -1;
  323. if (asName == null) {
  324. return M3UAOAMMessages.INVALID_COMMAND;
  325. }
  326. dpc = Integer.parseInt(args[count++]);
  327. while (count < args.length) {
  328. opc = Integer.parseInt(args[count++]);
  329. si = Integer.parseInt(args[count++]);
  330. }
  331. this.m3uaManagement.addRoute(dpc, opc, si, asName);
  332. return String.format(M3UAOAMMessages.ADD_ROUTE_AS_FOR_DPC_SUCCESSFULL, asName, dpc);
  333. }
  334. if (routeCmd.equals("remove")) {
  335. if (args.length < 5 || args.length > 7) {
  336. return M3UAOAMMessages.INVALID_COMMAND;
  337. }
  338. int count = 3;
  339. String asName = args[count++];
  340. int dpc = -1;
  341. int opc = -1;
  342. int si = -1;
  343. if (asName == null) {
  344. return M3UAOAMMessages.INVALID_COMMAND;
  345. }
  346. dpc = Integer.parseInt(args[count++]);
  347. while (count < args.length) {
  348. opc = Integer.parseInt(args[count++]);
  349. si = Integer.parseInt(args[count++]);
  350. }
  351. this.m3uaManagement.removeRoute(dpc, opc, si, asName);
  352. return String.format(M3UAOAMMessages.REMOVE_AS_ROUTE_FOR_DPC_SUCCESSFULL, args[4], dpc);
  353. }
  354. if (routeCmd.equals("show")) {
  355. return this.showRoutes();
  356. }
  357. }
  358. return M3UAOAMMessages.INVALID_COMMAND;
  359. } catch (Exception e) {
  360. logger.error(String.format("Error while executing comand %s", Arrays.toString(args)), e);
  361. return e.getMessage();
  362. }
  363. }
  364. public String execute(String[] args) {
  365. if (args[0].equals("m3ua")) {
  366. return this.executeM3UA(args);
  367. } else if (args[0].equals("sctp")) {
  368. return this.sctpShellExecutor.execute(args);
  369. }
  370. return M3UAOAMMessages.INVALID_COMMAND;
  371. }
  372. }