/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/LCSQoSImpl.java

http://mobicents.googlecode.com/ · Java · 403 lines · 240 code · 32 blank · 131 comment · 64 complexity · 10cf5f4d015b11a0ba43ec3cbe8a4828 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.map.service.lsm;
  23. import java.io.IOException;
  24. import org.mobicents.protocols.asn.AsnException;
  25. import org.mobicents.protocols.asn.AsnInputStream;
  26. import org.mobicents.protocols.asn.AsnOutputStream;
  27. import org.mobicents.protocols.asn.Tag;
  28. import org.mobicents.protocols.ss7.map.api.MAPException;
  29. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  30. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  31. import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
  32. import org.mobicents.protocols.ss7.map.api.service.lsm.LCSQoS;
  33. import org.mobicents.protocols.ss7.map.api.service.lsm.ResponseTime;
  34. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  35. import org.mobicents.protocols.ss7.map.primitives.MAPExtensionContainerImpl;
  36. /**
  37. * TODO introduce all params used by LCSQoS and test
  38. *
  39. * @author amit bhayani
  40. *
  41. */
  42. public class LCSQoSImpl implements LCSQoS, MAPAsnPrimitive {
  43. private static final int _TAG_HORIZONTAL_ACCURACY = 0;
  44. private static final int _TAG_VERTICAL_COORDINATE_REQUEST = 1;
  45. private static final int _TAG_VERTICAL_ACCURACY = 2;
  46. private static final int _TAG_RESPONSE_TIME = 3;
  47. private static final int _TAG_EXTENSION_CONTAINER = 4;
  48. private Integer horizontalAccuracy = null;
  49. private Integer verticalAccuracy = null;
  50. private Boolean verticalCoordinateRequest = false;
  51. private ResponseTime responseTime = null;
  52. private MAPExtensionContainer extensionContainer = null;
  53. /**
  54. *
  55. */
  56. public LCSQoSImpl() {
  57. super();
  58. }
  59. /**
  60. * @param horizontalAccuracy
  61. * @param verticalAccuracy
  62. * @param verticalCoordinateRequest
  63. * @param responseTime
  64. * @param extensionContainer
  65. */
  66. public LCSQoSImpl(Integer horizontalAccuracy, Integer verticalAccuracy, Boolean verticalCoordinateRequest, ResponseTime responseTime,
  67. MAPExtensionContainer extensionContainer) {
  68. super();
  69. this.horizontalAccuracy = horizontalAccuracy;
  70. this.verticalAccuracy = verticalAccuracy;
  71. this.verticalCoordinateRequest = verticalCoordinateRequest;
  72. this.responseTime = responseTime;
  73. this.extensionContainer = extensionContainer;
  74. }
  75. /*
  76. * (non-Javadoc)
  77. *
  78. * @see
  79. * org.mobicents.protocols.ss7.map.api.service.lsm.LCSQoS#getHorizontalAccuracy
  80. * ()
  81. */
  82. @Override
  83. public Integer getHorizontalAccuracy() {
  84. return this.horizontalAccuracy;
  85. }
  86. /*
  87. * (non-Javadoc)
  88. *
  89. * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSQoS#
  90. * getVerticalCoordinateRequest()
  91. */
  92. @Override
  93. public Boolean getVerticalCoordinateRequest() {
  94. return this.verticalCoordinateRequest;
  95. }
  96. /*
  97. * (non-Javadoc)
  98. *
  99. * @see
  100. * org.mobicents.protocols.ss7.map.api.service.lsm.LCSQoS#getVerticalAccuracy
  101. * ()
  102. */
  103. @Override
  104. public Integer getVerticalAccuracy() {
  105. return this.verticalAccuracy;
  106. }
  107. /*
  108. * (non-Javadoc)
  109. *
  110. * @see
  111. * org.mobicents.protocols.ss7.map.api.service.lsm.LCSQoS#getResponseTime()
  112. */
  113. @Override
  114. public ResponseTime getResponseTime() {
  115. return this.responseTime;
  116. }
  117. /*
  118. * (non-Javadoc)
  119. *
  120. * @see
  121. * org.mobicents.protocols.ss7.map.api.service.lsm.LCSQoS#getExtensionContainer
  122. * ()
  123. */
  124. @Override
  125. public MAPExtensionContainer getExtensionContainer() {
  126. return this.extensionContainer;
  127. }
  128. /*
  129. * (non-Javadoc)
  130. *
  131. * @see
  132. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  133. */
  134. @Override
  135. public int getTag() throws MAPException {
  136. return Tag.SEQUENCE;
  137. }
  138. /*
  139. * (non-Javadoc)
  140. *
  141. * @see
  142. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
  143. * ()
  144. */
  145. @Override
  146. public int getTagClass() {
  147. return Tag.CLASS_CONTEXT_SPECIFIC;
  148. }
  149. /*
  150. * (non-Javadoc)
  151. *
  152. * @see
  153. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
  154. * ()
  155. */
  156. @Override
  157. public boolean getIsPrimitive() {
  158. return false;
  159. }
  160. /*
  161. * (non-Javadoc)
  162. *
  163. * @see
  164. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
  165. * (org.mobicents.protocols.asn.AsnInputStream)
  166. */
  167. @Override
  168. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  169. try {
  170. int length = ansIS.readLength();
  171. this._decode(ansIS, length);
  172. } catch (IOException e) {
  173. throw new MAPParsingComponentException("IOException when decoding MWStatus: " + e.getMessage(), e,
  174. MAPParsingComponentExceptionReason.MistypedParameter);
  175. } catch (AsnException e) {
  176. throw new MAPParsingComponentException("AsnException when decoding MWStatus: " + e.getMessage(), e,
  177. MAPParsingComponentExceptionReason.MistypedParameter);
  178. }
  179. }
  180. /*
  181. * (non-Javadoc)
  182. *
  183. * @see
  184. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
  185. * (org.mobicents.protocols.asn.AsnInputStream, int)
  186. */
  187. @Override
  188. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  189. try {
  190. this._decode(ansIS, length);
  191. } catch (IOException e) {
  192. throw new MAPParsingComponentException("IOException when decoding MWStatus: " + e.getMessage(), e,
  193. MAPParsingComponentExceptionReason.MistypedParameter);
  194. } catch (AsnException e) {
  195. throw new MAPParsingComponentException("AsnException when decoding MWStatus: " + e.getMessage(), e,
  196. MAPParsingComponentExceptionReason.MistypedParameter);
  197. }
  198. }
  199. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  200. AsnInputStream ais = asnIS.readSequenceStreamData(length);
  201. while (true) {
  202. if (ais.available() == 0)
  203. break;
  204. switch (ais.readTag()) {
  205. case _TAG_HORIZONTAL_ACCURACY:
  206. // horizontal-accuracy [0] Horizontal-Accuracy OPTIONAL,
  207. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive()) {
  208. throw new MAPParsingComponentException(
  209. "Decoding LCSQoS failed. Error while decoding horizontal-accuracy [0] Horizontal-Accuracy: bad tag class or not primitive",
  210. MAPParsingComponentExceptionReason.MistypedParameter);
  211. }
  212. int length1 = ais.readLength();
  213. this.horizontalAccuracy = new Integer(ais.readOctetStringData(length1)[0]);
  214. break;
  215. case _TAG_VERTICAL_COORDINATE_REQUEST:
  216. // verticalCoordinateRequest [1] NULL OPTIONAL,
  217. length1 = ais.readLength();
  218. this.verticalCoordinateRequest = true;
  219. break;
  220. case _TAG_VERTICAL_ACCURACY:
  221. // vertical-accuracy [2] Vertical-Accuracy OPTIONAL,
  222. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive()) {
  223. throw new MAPParsingComponentException(
  224. "Decoding LCSQoS failed. Error while decoding vertical-accuracy [2] Vertical-Accuracy: bad tag class or not primitive",
  225. MAPParsingComponentExceptionReason.MistypedParameter);
  226. }
  227. length1 = ais.readLength();
  228. this.verticalAccuracy = new Integer(ais.readOctetStringData(length1)[0]);
  229. break;
  230. case _TAG_RESPONSE_TIME:
  231. // responseTime [3] ResponseTime OPTIONAL,
  232. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || ais.isTagPrimitive()) {
  233. throw new MAPParsingComponentException(
  234. "Decoding LCSQoS failed. Error while decoding responseTime [3] ResponseTime: bad tag class, tag or not constructed",
  235. MAPParsingComponentExceptionReason.MistypedParameter);
  236. }
  237. this.responseTime = new ResponseTimeImpl();
  238. ((ResponseTimeImpl)this.responseTime).decodeAll(ais);
  239. break;
  240. case _TAG_EXTENSION_CONTAINER:
  241. // extensionContainer [4] ExtensionContainer OPTIONAL
  242. this.extensionContainer = new MAPExtensionContainerImpl();
  243. ((MAPExtensionContainerImpl)this.extensionContainer).decodeAll(ais);
  244. break;
  245. default:
  246. // Do we care?
  247. ais.advanceElement();
  248. break;
  249. }
  250. }// while
  251. }
  252. /*
  253. * (non-Javadoc)
  254. *
  255. * @see
  256. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  257. * (org.mobicents.protocols.asn.AsnOutputStream)
  258. */
  259. @Override
  260. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  261. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  262. }
  263. /*
  264. * (non-Javadoc)
  265. *
  266. * @see
  267. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  268. * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
  269. */
  270. @Override
  271. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  272. try {
  273. asnOs.writeTag(tagClass, false, tag);
  274. int pos = asnOs.StartContentDefiniteLength();
  275. this.encodeData(asnOs);
  276. asnOs.FinalizeContent(pos);
  277. } catch (AsnException e) {
  278. throw new MAPException("AsnException when encoding InformServiceCentreRequest: " + e.getMessage(), e);
  279. }
  280. }
  281. /*
  282. * (non-Javadoc)
  283. *
  284. * @see
  285. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
  286. * (org.mobicents.protocols.asn.AsnOutputStream)
  287. */
  288. @Override
  289. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  290. if (this.horizontalAccuracy != null) {
  291. try {
  292. asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_HORIZONTAL_ACCURACY, new byte[] { this.horizontalAccuracy.byteValue() });
  293. } catch (IOException e) {
  294. throw new MAPException("IOException when encoding parameter horizontalAccuracy: ", e);
  295. } catch (AsnException e) {
  296. throw new MAPException("AsnException when encoding parameter horizontalAccuracy: ", e);
  297. }
  298. }
  299. if (this.verticalCoordinateRequest != null) {
  300. try {
  301. asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_VERTICAL_COORDINATE_REQUEST);
  302. } catch (IOException e) {
  303. throw new MAPException("IOException when encoding parameter verticalCoordinateRequest: ", e);
  304. } catch (AsnException e) {
  305. throw new MAPException("IOException when encoding parameter verticalCoordinateRequest: ", e);
  306. }
  307. }
  308. if (this.verticalAccuracy != null) {
  309. try {
  310. asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_VERTICAL_ACCURACY, new byte[] { this.verticalAccuracy.byteValue() });
  311. } catch (IOException e) {
  312. throw new MAPException("IOException when encoding parameter verticalAccuracy: ", e);
  313. } catch (AsnException e) {
  314. throw new MAPException("AsnException when encoding parameter verticalAccuracy: ", e);
  315. }
  316. }
  317. if (this.responseTime != null) {
  318. ((ResponseTimeImpl)this.responseTime).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_RESPONSE_TIME);
  319. }
  320. if (this.extensionContainer != null) {
  321. ((MAPExtensionContainerImpl)this.extensionContainer).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_EXTENSION_CONTAINER);
  322. }
  323. }
  324. @Override
  325. public int hashCode() {
  326. final int prime = 31;
  327. int result = 1;
  328. result = prime * result + ((extensionContainer == null) ? 0 : extensionContainer.hashCode());
  329. result = prime * result + ((horizontalAccuracy == null) ? 0 : horizontalAccuracy.hashCode());
  330. result = prime * result + ((responseTime == null) ? 0 : responseTime.hashCode());
  331. result = prime * result + ((verticalAccuracy == null) ? 0 : verticalAccuracy.hashCode());
  332. result = prime * result + ((verticalCoordinateRequest == null) ? 0 : verticalCoordinateRequest.hashCode());
  333. return result;
  334. }
  335. @Override
  336. public boolean equals(Object obj) {
  337. if (this == obj)
  338. return true;
  339. if (obj == null)
  340. return false;
  341. if (getClass() != obj.getClass())
  342. return false;
  343. LCSQoSImpl other = (LCSQoSImpl) obj;
  344. if (extensionContainer == null) {
  345. if (other.extensionContainer != null)
  346. return false;
  347. } else if (!extensionContainer.equals(other.extensionContainer))
  348. return false;
  349. if (horizontalAccuracy == null) {
  350. if (other.horizontalAccuracy != null)
  351. return false;
  352. } else if (!horizontalAccuracy.equals(other.horizontalAccuracy))
  353. return false;
  354. if (responseTime == null) {
  355. if (other.responseTime != null)
  356. return false;
  357. } else if (!responseTime.equals(other.responseTime))
  358. return false;
  359. if (verticalAccuracy == null) {
  360. if (other.verticalAccuracy != null)
  361. return false;
  362. } else if (!verticalAccuracy.equals(other.verticalAccuracy))
  363. return false;
  364. if (verticalCoordinateRequest == null) {
  365. if (other.verticalCoordinateRequest != null)
  366. return false;
  367. } else if (!verticalCoordinateRequest.equals(other.verticalCoordinateRequest))
  368. return false;
  369. return true;
  370. }
  371. }