PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/CellGlobalIdOrServiceAreaIdOrLAIImpl.java

http://mobicents.googlecode.com/
Java | 260 lines | 163 code | 32 blank | 65 comment | 38 complexity | 71df229b691cdd536c9e76bdba7af011 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. package org.mobicents.protocols.ss7.map.primitives;
  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.CellGlobalIdOrServiceAreaIdFixedLength;
  32. import org.mobicents.protocols.ss7.map.api.primitives.CellGlobalIdOrServiceAreaIdOrLAI;
  33. import org.mobicents.protocols.ss7.map.api.primitives.LAIFixedLength;
  34. /**
  35. * @author amit bhayani
  36. *
  37. */
  38. public class CellGlobalIdOrServiceAreaIdOrLAIImpl implements CellGlobalIdOrServiceAreaIdOrLAI, MAPAsnPrimitive {
  39. private static final int _TAG_CELL_GLOBAL_ID_OR_SERVICE_AREAR_ID = 0;
  40. private static final int _TAG_LAI = 1;
  41. private CellGlobalIdOrServiceAreaIdFixedLength cellGlobalIdOrServiceAreaIdFixedLength = null;
  42. private LAIFixedLength laiFixedLength = null;
  43. /**
  44. *
  45. */
  46. public CellGlobalIdOrServiceAreaIdOrLAIImpl() {
  47. super();
  48. }
  49. public CellGlobalIdOrServiceAreaIdOrLAIImpl(CellGlobalIdOrServiceAreaIdFixedLength cellGlobalIdOrServiceAreaIdFixedLength) {
  50. this.cellGlobalIdOrServiceAreaIdFixedLength = cellGlobalIdOrServiceAreaIdFixedLength;
  51. }
  52. public CellGlobalIdOrServiceAreaIdOrLAIImpl(LAIFixedLength laiFixedLength) {
  53. this.laiFixedLength = laiFixedLength;
  54. }
  55. /*
  56. * (non-Javadoc)
  57. *
  58. * @see org.mobicents.protocols.ss7.map.api.service.lsm.
  59. * CellGlobalIdOrServiceAreaIdOrLAI
  60. * #getCellGlobalIdOrServiceAreaIdFixedLength()
  61. */
  62. @Override
  63. public CellGlobalIdOrServiceAreaIdFixedLength getCellGlobalIdOrServiceAreaIdFixedLength() {
  64. return this.cellGlobalIdOrServiceAreaIdFixedLength;
  65. }
  66. /*
  67. * (non-Javadoc)
  68. *
  69. * @see org.mobicents.protocols.ss7.map.api.service.lsm.
  70. * CellGlobalIdOrServiceAreaIdOrLAI#getLAIFixedLength()
  71. */
  72. @Override
  73. public LAIFixedLength getLAIFixedLength() {
  74. return this.laiFixedLength;
  75. }
  76. /* (non-Javadoc)
  77. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  78. */
  79. @Override
  80. public int getTag() throws MAPException {
  81. if (this.cellGlobalIdOrServiceAreaIdFixedLength != null) {
  82. return _TAG_CELL_GLOBAL_ID_OR_SERVICE_AREAR_ID;
  83. } else {
  84. return _TAG_LAI;
  85. }
  86. }
  87. /* (non-Javadoc)
  88. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass()
  89. */
  90. @Override
  91. public int getTagClass() {
  92. return Tag.CLASS_CONTEXT_SPECIFIC;
  93. }
  94. /* (non-Javadoc)
  95. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive()
  96. */
  97. @Override
  98. public boolean getIsPrimitive() {
  99. return true;
  100. }
  101. /* (non-Javadoc)
  102. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll(org.mobicents.protocols.asn.AsnInputStream)
  103. */
  104. @Override
  105. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  106. try {
  107. int length = ansIS.readLength();
  108. this._decode(ansIS, length);
  109. } catch (IOException e) {
  110. throw new MAPParsingComponentException("IOException when decoding CellGlobalIdOrServiceAreaIdOrLAI: " + e.getMessage(), e,
  111. MAPParsingComponentExceptionReason.MistypedParameter);
  112. } catch (AsnException e) {
  113. throw new MAPParsingComponentException("AsnException when decoding CellGlobalIdOrServiceAreaIdOrLAI: " + e.getMessage(), e,
  114. MAPParsingComponentExceptionReason.MistypedParameter);
  115. }
  116. }
  117. /* (non-Javadoc)
  118. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData(org.mobicents.protocols.asn.AsnInputStream, int)
  119. */
  120. @Override
  121. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  122. try {
  123. this._decode(ansIS, length);
  124. } catch (IOException e) {
  125. throw new MAPParsingComponentException("IOException when decoding CellGlobalIdOrServiceAreaIdOrLAI: " + e.getMessage(), e,
  126. MAPParsingComponentExceptionReason.MistypedParameter);
  127. } catch (AsnException e) {
  128. throw new MAPParsingComponentException("AsnException when decoding CellGlobalIdOrServiceAreaIdOrLAI: " + e.getMessage(), e,
  129. MAPParsingComponentExceptionReason.MistypedParameter);
  130. }
  131. }
  132. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  133. if (asnIS.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !asnIS.isTagPrimitive())
  134. throw new MAPParsingComponentException("Error while decoding CellGlobalIdOrServiceAreaIdOrLAI: bad tag class or is not primitive: TagClass=" + asnIS.getTagClass(),
  135. MAPParsingComponentExceptionReason.MistypedParameter);
  136. switch (asnIS.getTag()) {
  137. case _TAG_CELL_GLOBAL_ID_OR_SERVICE_AREAR_ID:
  138. this.cellGlobalIdOrServiceAreaIdFixedLength = new CellGlobalIdOrServiceAreaIdFixedLengthImpl();
  139. ((CellGlobalIdOrServiceAreaIdFixedLengthImpl)this.cellGlobalIdOrServiceAreaIdFixedLength).decodeData(asnIS, length);
  140. break;
  141. case _TAG_LAI:
  142. this.laiFixedLength = new LAIFixedLengthImpl();
  143. ((LAIFixedLengthImpl)this.laiFixedLength).decodeData(asnIS, length);
  144. break;
  145. default:
  146. throw new MAPParsingComponentException(
  147. "Error while decoding AdditionalNumber: Expexted msc-Number [0] ISDN-AddressString or sgsn-Number [1] ISDN-AddressString, but found "
  148. + asnIS.getTag(), MAPParsingComponentExceptionReason.MistypedParameter);
  149. }
  150. }
  151. /* (non-Javadoc)
  152. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll(org.mobicents.protocols.asn.AsnOutputStream)
  153. */
  154. @Override
  155. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  156. this.encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, this.getTag());
  157. }
  158. /* (non-Javadoc)
  159. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll(org.mobicents.protocols.asn.AsnOutputStream, int, int)
  160. */
  161. @Override
  162. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  163. try {
  164. asnOs.writeTag(tagClass, true, tag);
  165. int pos = asnOs.StartContentDefiniteLength();
  166. this.encodeData(asnOs);
  167. asnOs.FinalizeContent(pos);
  168. } catch (AsnException e) {
  169. throw new MAPException("AsnException when encoding CellGlobalIdOrServiceAreaIdOrLAI: " + e.getMessage(), e);
  170. }
  171. }
  172. /* (non-Javadoc)
  173. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData(org.mobicents.protocols.asn.AsnOutputStream)
  174. */
  175. @Override
  176. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  177. if (this.cellGlobalIdOrServiceAreaIdFixedLength == null && this.laiFixedLength == null)
  178. throw new MAPException("Error while encoding the CellGlobalIdOrServiceAreaIdOrLAI: both cellGlobalIdOrServiceAreaIdFixedLength and laiFixedLength are not defined");
  179. if(this.cellGlobalIdOrServiceAreaIdFixedLength != null){
  180. ((CellGlobalIdOrServiceAreaIdFixedLengthImpl)this.cellGlobalIdOrServiceAreaIdFixedLength).encodeData(asnOs);
  181. } else {
  182. ((LAIFixedLengthImpl)this.laiFixedLength).encodeData(asnOs);
  183. }
  184. }
  185. @Override
  186. public String toString() {
  187. StringBuilder sb = new StringBuilder();
  188. sb.append("CellGlobalIdOrServiceAreaIdOrLAI [");
  189. if (this.cellGlobalIdOrServiceAreaIdFixedLength != null)
  190. sb.append(this.cellGlobalIdOrServiceAreaIdFixedLength.toString());
  191. if (this.laiFixedLength != null)
  192. sb.append(this.laiFixedLength.toString());
  193. sb.append("]");
  194. return sb.toString();
  195. }
  196. @Override
  197. public int hashCode() {
  198. final int prime = 31;
  199. int result = 1;
  200. result = prime
  201. * result
  202. + ((cellGlobalIdOrServiceAreaIdFixedLength == null) ? 0 : cellGlobalIdOrServiceAreaIdFixedLength
  203. .hashCode());
  204. result = prime * result + ((laiFixedLength == null) ? 0 : laiFixedLength.hashCode());
  205. return result;
  206. }
  207. @Override
  208. public boolean equals(Object obj) {
  209. if (this == obj)
  210. return true;
  211. if (obj == null)
  212. return false;
  213. if (getClass() != obj.getClass())
  214. return false;
  215. CellGlobalIdOrServiceAreaIdOrLAIImpl other = (CellGlobalIdOrServiceAreaIdOrLAIImpl) obj;
  216. if (cellGlobalIdOrServiceAreaIdFixedLength == null) {
  217. if (other.cellGlobalIdOrServiceAreaIdFixedLength != null)
  218. return false;
  219. } else if (!cellGlobalIdOrServiceAreaIdFixedLength.equals(other.cellGlobalIdOrServiceAreaIdFixedLength))
  220. return false;
  221. if (laiFixedLength == null) {
  222. if (other.laiFixedLength != null)
  223. return false;
  224. } else if (!laiFixedLength.equals(other.laiFixedLength))
  225. return false;
  226. return true;
  227. }
  228. }