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

http://mobicents.googlecode.com/ · Java · 284 lines · 157 code · 31 blank · 96 comment · 32 complexity · c8cae24cba6c60429d115d88119522da 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.service.lsm.DeferredLocationEventType;
  32. import org.mobicents.protocols.ss7.map.api.service.lsm.LocationEstimateType;
  33. import org.mobicents.protocols.ss7.map.api.service.lsm.LocationType;
  34. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  35. /**
  36. * @author amit bhayani
  37. *
  38. */
  39. public class LocationTypeImpl implements LocationType, MAPAsnPrimitive {
  40. private static final int _TAG_LOCATION_ESTIMATE_TYPE = 0;
  41. private static final int _TAG_DEFERRED_LOCATION_EVET_TYPE = 1;
  42. private LocationEstimateType locationEstimateType;
  43. private DeferredLocationEventType deferredLocationEventType;
  44. public LocationTypeImpl() {
  45. }
  46. /**
  47. *
  48. */
  49. public LocationTypeImpl(final LocationEstimateType locationEstimateType, final DeferredLocationEventType deferredLocationEventType) {
  50. this.locationEstimateType = locationEstimateType;
  51. this.deferredLocationEventType = deferredLocationEventType;
  52. }
  53. /*
  54. * (non-Javadoc)
  55. *
  56. * @see org.mobicents.protocols.ss7.map.api.service.lsm.LocationType#
  57. * getLocationEstimateType()
  58. */
  59. @Override
  60. public LocationEstimateType getLocationEstimateType() {
  61. return this.locationEstimateType;
  62. }
  63. /*
  64. * (non-Javadoc)
  65. *
  66. * @see org.mobicents.protocols.ss7.map.api.service.lsm.LocationType#
  67. * getDeferredLocationEventType()
  68. */
  69. @Override
  70. public DeferredLocationEventType getDeferredLocationEventType() {
  71. return this.deferredLocationEventType;
  72. }
  73. /*
  74. * (non-Javadoc)
  75. *
  76. * @see
  77. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  78. */
  79. @Override
  80. public int getTag() throws MAPException {
  81. return Tag.SEQUENCE;
  82. }
  83. /*
  84. * (non-Javadoc)
  85. *
  86. * @see
  87. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
  88. * ()
  89. */
  90. @Override
  91. public int getTagClass() {
  92. return Tag.CLASS_UNIVERSAL;
  93. }
  94. /*
  95. * (non-Javadoc)
  96. *
  97. * @see
  98. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
  99. * ()
  100. */
  101. @Override
  102. public boolean getIsPrimitive() {
  103. return false;
  104. }
  105. /*
  106. * (non-Javadoc)
  107. *
  108. * @see
  109. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
  110. * (org.mobicents.protocols.asn.AsnInputStream)
  111. */
  112. @Override
  113. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  114. try {
  115. int length = ansIS.readLength();
  116. this._decode(ansIS, length);
  117. } catch (IOException e) {
  118. throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
  119. MAPParsingComponentExceptionReason.MistypedParameter);
  120. } catch (AsnException e) {
  121. throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
  122. MAPParsingComponentExceptionReason.MistypedParameter);
  123. }
  124. }
  125. /*
  126. * (non-Javadoc)
  127. *
  128. * @see
  129. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
  130. * (org.mobicents.protocols.asn.AsnInputStream, int)
  131. */
  132. @Override
  133. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  134. try {
  135. this._decode(ansIS, length);
  136. } catch (IOException e) {
  137. throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
  138. MAPParsingComponentExceptionReason.MistypedParameter);
  139. } catch (AsnException e) {
  140. throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
  141. MAPParsingComponentExceptionReason.MistypedParameter);
  142. }
  143. }
  144. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  145. AsnInputStream ais = asnIS.readSequenceStreamData(length);
  146. int tag = ais.readTag();
  147. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_LOCATION_ESTIMATE_TYPE) {
  148. throw new MAPParsingComponentException(
  149. "Error while decoding LocationType: Parameter 0[locationEstimateType [0] LocationEstimateType] bad tag class, tag or not primitive",
  150. MAPParsingComponentExceptionReason.MistypedParameter);
  151. }
  152. int length1 = ais.readLength();
  153. int type = (int) ais.readIntegerData(length1);
  154. this.locationEstimateType = LocationEstimateType.getLocationEstimateType(type);
  155. while (true) {
  156. if (ais.available() == 0)
  157. break;
  158. switch (ais.readTag()) {
  159. case _TAG_DEFERRED_LOCATION_EVET_TYPE:
  160. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive()) {
  161. throw new MAPParsingComponentException(
  162. "Error while decoding LocationType: Parameter 1 [deferredLocationEventType [1] DeferredLocationEventType] bad tag class, tag or not primitive",
  163. MAPParsingComponentExceptionReason.MistypedParameter);
  164. }
  165. this.deferredLocationEventType = new DeferredLocationEventTypeImpl();
  166. ((DeferredLocationEventTypeImpl)this.deferredLocationEventType).decodeAll(ais);
  167. break;
  168. default:
  169. // Do we care?
  170. ais.advanceElement();
  171. break;
  172. }
  173. }
  174. }
  175. /*
  176. * (non-Javadoc)
  177. *
  178. * @see
  179. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  180. * (org.mobicents.protocols.asn.AsnOutputStream)
  181. */
  182. @Override
  183. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  184. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  185. }
  186. /*
  187. * (non-Javadoc)
  188. *
  189. * @see
  190. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  191. * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
  192. */
  193. @Override
  194. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  195. try {
  196. asnOs.writeTag(tagClass, false, tag);
  197. int pos = asnOs.StartContentDefiniteLength();
  198. this.encodeData(asnOs);
  199. asnOs.FinalizeContent(pos);
  200. } catch (AsnException e) {
  201. throw new MAPException("AsnException when encoding reportSMDeliveryStatusRequest: " + e.getMessage(), e);
  202. }
  203. }
  204. /*
  205. * (non-Javadoc)
  206. *
  207. * @see
  208. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
  209. * (org.mobicents.protocols.asn.AsnOutputStream)
  210. */
  211. @Override
  212. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  213. if (this.locationEstimateType == null) {
  214. throw new MAPException("Error while encoding LocationType the mandatory parameter LocationEstimateType is not defined");
  215. }
  216. try {
  217. asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_LOCATION_ESTIMATE_TYPE, this.locationEstimateType.getType());
  218. } catch (IOException e) {
  219. throw new MAPException("IOException when encoding parameter locationEstimateType: ", e);
  220. } catch (AsnException e) {
  221. throw new MAPException("AsnException when encoding parameter locationEstimateType: ", e);
  222. }
  223. if (this.deferredLocationEventType != null) {
  224. ((DeferredLocationEventTypeImpl)this.deferredLocationEventType).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_DEFERRED_LOCATION_EVET_TYPE);
  225. }
  226. }
  227. @Override
  228. public int hashCode() {
  229. final int prime = 31;
  230. int result = 1;
  231. result = prime * result + ((deferredLocationEventType == null) ? 0 : deferredLocationEventType.hashCode());
  232. result = prime * result + ((locationEstimateType == null) ? 0 : locationEstimateType.hashCode());
  233. return result;
  234. }
  235. @Override
  236. public boolean equals(Object obj) {
  237. if (this == obj)
  238. return true;
  239. if (obj == null)
  240. return false;
  241. if (getClass() != obj.getClass())
  242. return false;
  243. LocationTypeImpl other = (LocationTypeImpl) obj;
  244. if (deferredLocationEventType == null) {
  245. if (other.deferredLocationEventType != null)
  246. return false;
  247. } else if (!deferredLocationEventType.equals(other.deferredLocationEventType))
  248. return false;
  249. if (locationEstimateType != other.locationEstimateType)
  250. return false;
  251. return true;
  252. }
  253. }