PageRenderTime 72ms CodeModel.GetById 46ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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