PageRenderTime 45ms CodeModel.GetById 12ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/isup/isup-impl/src/main/java/org/mobicents/protocols/ss7/isup/impl/ISUPProviderImpl.java

http://mobicents.googlecode.com/
Java | 577 lines | 369 code | 77 blank | 131 comment | 70 complexity | 7d00fe9efc98966f5a0c6438389987f6 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
 23/**
 24 * 
 25 */
 26package org.mobicents.protocols.ss7.isup.impl;
 27
 28import java.io.IOException;
 29import java.util.List;
 30import java.util.Properties;
 31import java.util.concurrent.Executors;
 32import java.util.concurrent.ScheduledExecutorService;
 33
 34import javolution.util.FastList;
 35import javolution.util.FastMap;
 36
 37import org.apache.log4j.Level;
 38import org.apache.log4j.Logger;
 39import org.mobicents.protocols.ss7.isup.CircuitManager;
 40import org.mobicents.protocols.ss7.isup.ISUPEvent;
 41import org.mobicents.protocols.ss7.isup.ISUPListener;
 42import org.mobicents.protocols.ss7.isup.ISUPMessageFactory;
 43import org.mobicents.protocols.ss7.isup.ISUPParameterFactory;
 44import org.mobicents.protocols.ss7.isup.ISUPProvider;
 45import org.mobicents.protocols.ss7.isup.ISUPTimeoutEvent;
 46import org.mobicents.protocols.ss7.isup.ParameterException;
 47import org.mobicents.protocols.ss7.isup.impl.message.ISUPMessageFactoryImpl;
 48import org.mobicents.protocols.ss7.isup.impl.message.parameter.ISUPParameterFactoryImpl;
 49import org.mobicents.protocols.ss7.isup.message.ISUPMessage;
 50import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
 51
 52/**
 53 * @author baranowb
 54 * 
 55 */
 56public class ISUPProviderImpl implements ISUPProvider {
 57
 58	private static final Logger logger = Logger.getLogger(ISUPProviderImpl.class);
 59
 60	protected final List<ISUPListener> listeners = new FastList<ISUPListener>();
 61	protected ScheduledExecutorService executors[]; // assign executor to
 62													// circuit?
 63	protected ISUPStackImpl stack;
 64	protected ISUPMessageFactory messageFactory;
 65	protected ISUPParameterFactory parameterFactory;
 66	protected final FastMap<Integer, Circuit> cic2Circuit = new FastMap<Integer, Circuit>();
 67	protected int ni, localSpc;
 68	public ISUPProviderImpl(ISUPStackImpl isupStackImpl, Properties props) {
 69		this.stack = isupStackImpl;
 70		this.T1Timeout = Long.parseLong(props.getProperty(T1, this.T1Timeout + ""));
 71		this.T5Timeout = Long.parseLong(props.getProperty(T5, this.T5Timeout + ""));
 72		this.T7Timeout = Long.parseLong(props.getProperty(T7, this.T7Timeout + ""));
 73		this.T12Timeout = Long.parseLong(props.getProperty(T12, this.T12Timeout + ""));
 74		this.T13Timeout = Long.parseLong(props.getProperty(T13, this.T13Timeout + ""));
 75		this.T14Timeout = Long.parseLong(props.getProperty(T14, this.T14Timeout + ""));
 76		this.T15Timeout = Long.parseLong(props.getProperty(T15, this.T15Timeout + ""));
 77		this.T16Timeout = Long.parseLong(props.getProperty(T16, this.T16Timeout + ""));
 78		this.T17Timeout = Long.parseLong(props.getProperty(T17, this.T17Timeout + ""));
 79		this.T18Timeout = Long.parseLong(props.getProperty(T18, this.T18Timeout + ""));
 80		this.T19Timeout = Long.parseLong(props.getProperty(T19, this.T19Timeout + ""));
 81		this.T20Timeout = Long.parseLong(props.getProperty(T20, this.T20Timeout + ""));
 82		this.T21Timeout = Long.parseLong(props.getProperty(T21, this.T21Timeout + ""));
 83		this.T22Timeout = Long.parseLong(props.getProperty(T22, this.T22Timeout + ""));
 84		this.T23Timeout = Long.parseLong(props.getProperty(T23, this.T23Timeout + ""));
 85		// this.T28Timeout = Long.parseLong(props.getProperty(T28,
 86		// this.T28Timeout + ""));
 87		this.T33Timeout = Long.parseLong(props.getProperty(T33, this.T33Timeout + ""));
 88		
 89		if(!props.containsKey(NI))
 90		{
 91			throw new IllegalArgumentException("No definition of local NI!("+NI+")");
 92		}else
 93		{
 94			this.ni = Integer.parseInt(props.getProperty(NI));
 95		}
 96		
 97		if(!props.containsKey(LOCAL_SPC))
 98		{
 99			throw new IllegalArgumentException("No definition of localSPC!("+LOCAL_SPC+")");
100		}else
101		{
102			this.localSpc = Integer.parseInt(props.getProperty(LOCAL_SPC));
103		}
104		
105		// check bounds for timers.... trick might be that... for
106		// national/internationl those are different... ech
107		if (this.T1Timeout < 5000 || this.T1Timeout > 60000) {
108			if (logger.isEnabledFor(Level.WARN)) {
109				logger.warn("Wrong value of T1 timeout: " + this.T1Timeout + ", using default value.");
110			}
111			this.T1Timeout = ISUPTimeoutEvent.T1_DEFAULT;
112		}
113
114		if (this.T5Timeout < 5 * 60 * 1000 || this.T5Timeout > 15 * 60 * 1000) {
115			if (logger.isEnabledFor(Level.WARN)) {
116				logger.warn("Wrong value of T5 timeout: " + this.T5Timeout + ", using default value.");
117			}
118			this.T5Timeout = ISUPTimeoutEvent.T5_DEFAULT;
119		}
120
121		if (this.T7Timeout < 20 * 1000 || this.T7Timeout > 30 * 1000) {
122			if (logger.isEnabledFor(Level.WARN)) {
123				logger.warn("Wrong value of T7 timeout: " + this.T7Timeout + ", using default value.");
124			}
125			this.T7Timeout = ISUPTimeoutEvent.T7_DEFAULT;
126		}
127
128		if (this.T12Timeout < 15 * 1000 || this.T12Timeout > 60 * 1000) {
129			if (logger.isEnabledFor(Level.WARN)) {
130				logger.warn("Wrong value of T12 timeout: " + this.T12Timeout + ", using default value.");
131			}
132			this.T12Timeout = ISUPTimeoutEvent.T12_DEFAULT;
133		}
134		if (this.T13Timeout < 5 * 60 * 1000 || this.T13Timeout > 15 * 60 * 1000) {
135			if (logger.isEnabledFor(Level.WARN)) {
136				logger.warn("Wrong value of T13 timeout: " + this.T13Timeout + ", using default value.");
137			}
138			//this.T13Timeout = ISUPTimeoutEvent.T13_DEFAULT;
139		}
140
141		if (this.T14Timeout < 15 * 1000 || this.T14Timeout > 60 * 1000) {
142			if (logger.isEnabledFor(Level.WARN)) {
143				logger.warn("Wrong value of T14 timeout: " + this.T14Timeout + ", using default value.");
144			}
145			this.T14Timeout = ISUPTimeoutEvent.T14_DEFAULT;
146		}
147		if (this.T15Timeout < 5 * 60 * 1000 || this.T15Timeout > 15 * 60 * 1000) {
148			if (logger.isEnabledFor(Level.WARN)) {
149				logger.warn("Wrong value of T15 timeout: " + this.T15Timeout + ", using default value.");
150			}
151			this.T15Timeout = ISUPTimeoutEvent.T15_DEFAULT;
152		}
153
154		if (this.T16Timeout < 15 * 1000 || this.T16Timeout > 60 * 1000) {
155			if (logger.isEnabledFor(Level.WARN)) {
156				logger.warn("Wrong value of T16 timeout: " + this.T16Timeout + ", using default value.");
157			}
158			this.T16Timeout = ISUPTimeoutEvent.T16_DEFAULT;
159		}
160		if (this.T17Timeout < 5 * 60 * 1000 || this.T17Timeout > 15 * 60 * 1000) {
161			if (logger.isEnabledFor(Level.WARN)) {
162				logger.warn("Wrong value of T17 timeout: " + this.T17Timeout + ", using default value.");
163			}
164			this.T17Timeout = ISUPTimeoutEvent.T17_DEFAULT;
165		}
166
167		if (this.T18Timeout < 15 * 1000 || this.T18Timeout > 60 * 1000) {
168			if (logger.isEnabledFor(Level.WARN)) {
169				logger.warn("Wrong value of T18 timeout: " + this.T18Timeout + ", using default value.");
170			}
171			this.T18Timeout = ISUPTimeoutEvent.T18_DEFAULT;
172		}
173		if (this.T19Timeout < 5 * 60 * 1000 || this.T19Timeout > 15 * 60 * 1000) {
174			if (logger.isEnabledFor(Level.WARN)) {
175				logger.warn("Wrong value of T19 timeout: " + this.T19Timeout + ", using default value.");
176			}
177			this.T19Timeout = ISUPTimeoutEvent.T19_DEFAULT;
178		}
179
180		if (this.T20Timeout < 15 * 1000 || this.T20Timeout > 60 * 1000) {
181			if (logger.isEnabledFor(Level.WARN)) {
182				logger.warn("Wrong value of T20 timeout: " + this.T20Timeout + ", using default value.");
183			}
184			this.T20Timeout = ISUPTimeoutEvent.T20_DEFAULT;
185		}
186		if (this.T21Timeout < 5 * 60 * 1000 || this.T21Timeout > 15 * 60 * 1000) {
187			if (logger.isEnabledFor(Level.WARN)) {
188				logger.warn("Wrong value of T21 timeout: " + this.T21Timeout + ", using default value.");
189			}
190			this.T21Timeout = ISUPTimeoutEvent.T21_DEFAULT;
191		}
192
193		if (this.T22Timeout < 15 * 1000 || this.T22Timeout > 60 * 1000) {
194			if (logger.isEnabledFor(Level.WARN)) {
195				logger.warn("Wrong value of T22 timeout: " + this.T22Timeout + ", using default value.");
196			}
197			this.T22Timeout = ISUPTimeoutEvent.T22_DEFAULT;
198		}
199		if (this.T23Timeout < 5 * 60 * 1000 || this.T23Timeout > 15 * 60 * 1000) {
200			if (logger.isEnabledFor(Level.WARN)) {
201				logger.warn("Wrong value of T23 timeout: " + this.T23Timeout + ", using default value.");
202			}
203			this.T23Timeout = ISUPTimeoutEvent.T23_DEFAULT;
204		}
205
206		if (this.T33Timeout < 12 * 1000 || this.T33Timeout > 15 * 60 * 1000) {
207			if (logger.isEnabledFor(Level.WARN)) {
208				logger.warn("Wrong value of T33 timeout: " + this.T33Timeout + ", using default value.");
209			}
210			this.T33Timeout = ISUPTimeoutEvent.T33_DEFAULT;
211		}
212		
213	
214		this.parameterFactory = new ISUPParameterFactoryImpl();
215		this.messageFactory = new ISUPMessageFactoryImpl(this.parameterFactory);
216	}
217
218	@Override
219	public int getNi() {
220		return this.ni;
221	}
222
223	@Override
224	public int getLocalSpc() {
225		return this.localSpc;
226	}
227
228	/*
229	 * (non-Javadoc)
230	 * 
231	 * @see
232	 * org.mobicents.isup.ISUPProvider#addListener(org.mobicents.isup.ISUPListener
233	 * )
234	 */
235	public void addListener(ISUPListener listener) {
236		if (listener == null) {
237			throw new NullPointerException("Listener must not be null!");
238		}
239		if(this.listeners.contains(listener))
240		{
241			throw new IllegalArgumentException("Listener already present: "+listener+" !");
242		}else
243		{
244			this.listeners.add(listener);
245		}
246
247	}
248
249	/*
250	 * (non-Javadoc)
251	 * 
252	 * @seeorg.mobicents.isup.ISUPProvider#removeListener(org.mobicents.isup.
253	 * ISUPListener)
254	 */
255	public void removeListener(ISUPListener listener) {
256		if (listener == null) {
257			throw new NullPointerException("Listener must not be null!");
258		}
259		this.listeners.remove(listener);
260
261	}
262
263	/*
264	 * (non-Javadoc)
265	 * 
266	 * @see org.mobicents.protocols.ss7.isup.ISUPProvider#getMessageFactory()
267	 */
268	public ISUPMessageFactory getMessageFactory() {
269		return this.messageFactory;
270	}
271
272	/*
273	 * (non-Javadoc)
274	 * 
275	 * @see org.mobicents.protocols.ss7.isup.ISUPProvider#getParameterFactory()
276	 */
277	public ISUPParameterFactory getParameterFactory() {
278		return this.parameterFactory;
279	}
280
281	/*
282	 * (non-Javadoc)
283	 * 
284	 * @see
285	 * org.mobicents.protocols.ss7.isup.ISUPProvider#sendMessage(org.mobicents
286	 * .protocols.ss7.isup.message.ISUPMessage)
287	 */
288
289	public void sendMessage(ISUPMessage msg) throws ParameterException, IOException {
290		if (!msg.hasAllMandatoryParameters()) {
291			throw new ParameterException("Message does not have all required parameters!");
292		}
293		getCircuit(msg).send(msg);
294	}
295
296	public boolean cancelTimer(int cic, int timerId) {
297		if (this.cic2Circuit.containsKey(cic)) {
298			Circuit c = this.cic2Circuit.get(cic);
299			return c.cancelTimer(timerId);
300		} else {
301			return false;
302		}
303	}
304
305	// ---------------------- non interface methods ----------------
306
307	public void start() {
308		CircuitManager cm = this.stack.getCircuitManager(); 
309		int[] cics = cm.getCircuits();
310		this.cic2Circuit.clear();
311		this.executors = new ScheduledExecutorService[5];
312		for (int index = 0; index < this.executors.length; index++) {
313			this.executors[index] = Executors.newScheduledThreadPool(1);
314		}
315		for(int cic:cics)
316		{
317			Circuit c = new Circuit(cic, cm.getDpc(cic),this);
318			this.cic2Circuit.put(cic, c);
319		}
320		
321	}
322
323	public void stop() {
324		for (FastMap.Entry<Integer, Circuit> e = this.cic2Circuit.head(), end = this.cic2Circuit
325                .tail(); (e = e.getNext()) != end;) {
326			try{
327				e.getValue().onStop();
328			}catch(Exception ex)
329			{
330				ex.printStackTrace();
331			}
332		}
333		for (int index = 0; index < this.executors.length; index++) {
334			this.executors[index].shutdown();
335		}
336		executors = null;
337	}
338
339	// --------- private methods and class defs.
340
341	/**
342	 * @param message
343	 */
344	void receive(ISUPMessage message) {
345		Circuit c = getCircuit(message);
346		c.receive(message);
347
348	}
349
350	/**
351	 * @param message
352	 * @return
353	 */
354	private Circuit getCircuit(ISUPMessage message) {
355		Circuit c = null;
356		int cic = message.getCircuitIdentificationCode().getCIC();
357		if (!this.stack.getCircuitManager().isCircuitPresent(cic)) {
358			if (this.cic2Circuit.containsKey(cic)) {
359				this.cic2Circuit.remove(cic).onStop();
360			}
361			throw new IllegalArgumentException("Curcuit not defined, no route definition present!");
362
363		} else {
364			c = this.cic2Circuit.get(message.getCircuitIdentificationCode().getCIC());
365		}
366		return c;
367	}
368
369	ScheduledExecutorService getExecutor(int cic) {
370		int index = cic % this.executors.length;
371		return this.executors[index];
372	}
373
374	void send(Mtp3TransferPrimitive encoded) throws IOException {
375		this.stack.send(encoded);
376	}
377
378	/**
379	 * @param request
380	 */
381	public void deliver(ISUPEvent event) {
382		for (int index = 0; index < listeners.size(); index++) {
383			try {
384				listeners.get(index).onEvent(event);
385			} catch (Exception e) {
386				if (logger.isEnabledFor(Level.ERROR)) {
387					logger.error("Exception thrown from listener.", e);
388				}
389			}
390		}
391
392	}
393
394	/**
395	 * @param timeoutEvent
396	 */
397	public void deliver(ISUPTimeoutEvent timeoutEvent) {
398		for (int index = 0; index < listeners.size(); index++) {
399			try {
400				listeners.get(index).onTimeout(timeoutEvent);
401			} catch (Exception e) {
402				if (logger.isEnabledFor(Level.ERROR)) {
403					logger.error("Exception thrown from listener.", e);
404				}
405			}
406		}
407	}
408	
409	protected static final String NI = "ni";
410	protected static final String LOCAL_SPC = "localspc";
411
412	protected final static String T1 = "t1";
413	protected final static String T5 = "t5";
414	protected final static String T7 = "t7";
415	protected final static String T12 = "t12";
416	protected final static String T13 = "t13";
417	protected final static String T14 = "t14";
418	protected final static String T15 = "t15";
419	protected final static String T16 = "t16";
420	protected final static String T17 = "t17";
421	protected final static String T18 = "t18";
422	protected final static String T19 = "t19";
423	protected final static String T20 = "t20";
424	protected final static String T21 = "t21";
425	protected final static String T22 = "t22";
426	protected final static String T23 = "t23";
427	// protected final static String T28 = "t28";
428	protected final static String T33 = "t33";
429	private long T1Timeout = ISUPTimeoutEvent.T1_DEFAULT;
430	private long T5Timeout = ISUPTimeoutEvent.T5_DEFAULT;
431	private long T7Timeout = ISUPTimeoutEvent.T7_DEFAULT;
432	private long T12Timeout = ISUPTimeoutEvent.T12_DEFAULT;
433	private long T13Timeout = ISUPTimeoutEvent.T13_DEFAULT;
434	private long T14Timeout = ISUPTimeoutEvent.T14_DEFAULT;
435	private long T15Timeout = ISUPTimeoutEvent.T15_DEFAULT;
436	private long T16Timeout = ISUPTimeoutEvent.T16_DEFAULT;
437	private long T17Timeout = ISUPTimeoutEvent.T17_DEFAULT;
438	private long T18Timeout = ISUPTimeoutEvent.T18_DEFAULT;
439	private long T19Timeout = ISUPTimeoutEvent.T19_DEFAULT;
440	private long T20Timeout = ISUPTimeoutEvent.T20_DEFAULT;
441	private long T21Timeout = ISUPTimeoutEvent.T21_DEFAULT;
442	private long T22Timeout = ISUPTimeoutEvent.T22_DEFAULT;
443	private long T23Timeout = ISUPTimeoutEvent.T23_DEFAULT;
444	private long T28Timeout = ISUPTimeoutEvent.T28_DEFAULT;
445	private long T33Timeout = ISUPTimeoutEvent.T33_DEFAULT;
446
447	/**
448	 * @return
449	 */
450	long getT1Timeout() {
451		return T1Timeout;
452	}
453
454	/**
455	 * @return
456	 */
457	long getT5Timeout() {
458		return T5Timeout;
459	}
460
461	/**
462	 * @return
463	 */
464	long getT7Timeout() {
465		return T7Timeout;
466	}
467
468	/**
469	 * @return
470	 */
471	long getT12Timeout() {
472		return T12Timeout;
473	}
474
475	/**
476	 * @return
477	 */
478	long getT13Timeout() {
479		return T13Timeout;
480	}
481
482	/**
483	 * @return
484	 */
485	long getT14Timeout() {
486		return T14Timeout;
487	}
488
489	/**
490	 * @return
491	 */
492	long getT15Timeout() {
493
494		return T15Timeout;
495	}
496
497	/**
498	 * @return
499	 */
500	long getT16Timeout() {
501
502		return T16Timeout;
503	}
504
505	/**
506	 * @return
507	 */
508	long getT17Timeout() {
509
510		return T17Timeout;
511	}
512
513	/**
514	 * @return
515	 */
516	long getT18Timeout() {
517
518		return T18Timeout;
519	}
520
521	/**
522	 * @return
523	 */
524	long getT19Timeout() {
525
526		return T19Timeout;
527	}
528
529	/**
530	 * @return
531	 */
532	long getT20Timeout() {
533
534		return T20Timeout;
535	}
536
537	/**
538	 * @return
539	 */
540	long getT21Timeout() {
541
542		return T21Timeout;
543	}
544
545	/**
546	 * @return
547	 */
548	long getT22Timeout() {
549
550		return T22Timeout;
551	}
552
553	/**
554	 * @return
555	 */
556	long getT23Timeout() {
557
558		return T23Timeout;
559	}
560
561	/**
562	 * @return
563	 */
564	long getT28Timeout() {
565
566		return T28Timeout;
567	}
568
569	/**
570	 * @return
571	 */
572	long getT33Timeout() {
573
574		return T33Timeout;
575	}
576
577}