PageRenderTime 31ms CodeModel.GetById 8ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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