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