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

http://mobicents.googlecode.com/ · Java · 338 lines · 252 code · 60 blank · 26 comment · 49 complexity · b20b79eb7a49213194e9db97d9c98509 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.primitives;
  23. import java.io.IOException;
  24. import java.util.Arrays;
  25. import org.mobicents.protocols.asn.AsnException;
  26. import org.mobicents.protocols.asn.AsnInputStream;
  27. import org.mobicents.protocols.asn.AsnOutputStream;
  28. import org.mobicents.protocols.asn.Tag;
  29. import org.mobicents.protocols.ss7.map.api.MAPException;
  30. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  31. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  32. import org.mobicents.protocols.ss7.map.api.primitives.CellGlobalIdOrServiceAreaIdFixedLength;
  33. /**
  34. *
  35. * @author sergey vetyutnev
  36. *
  37. */
  38. public class CellGlobalIdOrServiceAreaIdFixedLengthImpl implements CellGlobalIdOrServiceAreaIdFixedLength, MAPAsnPrimitive {
  39. private byte[] data;
  40. public CellGlobalIdOrServiceAreaIdFixedLengthImpl() {
  41. }
  42. public CellGlobalIdOrServiceAreaIdFixedLengthImpl(byte[] data) {
  43. this.data = data;
  44. }
  45. public CellGlobalIdOrServiceAreaIdFixedLengthImpl(int mcc, int mnc, int lac, int cellId) throws MAPException {
  46. if (mcc < 1 || mcc > 999)
  47. throw new MAPException("Bad mcc value");
  48. if (mnc < 0 || mnc > 999)
  49. throw new MAPException("Bad mnc value");
  50. this.data = new byte[7];
  51. StringBuilder sb = new StringBuilder();
  52. StringBuilder sb2 = new StringBuilder();
  53. if (mcc < 100)
  54. sb.append("0");
  55. if (mcc < 10)
  56. sb.append("0");
  57. sb.append(mcc);
  58. if (mnc < 100) {
  59. if (mnc < 10)
  60. sb2.append("0");
  61. sb2.append(mnc);
  62. } else {
  63. sb.append(mnc % 10);
  64. sb2.append(mnc / 10);
  65. }
  66. AsnOutputStream asnOs = new AsnOutputStream();
  67. TbcdString.encodeString(asnOs, sb.toString());
  68. System.arraycopy(asnOs.toByteArray(), 0, this.data, 0, 2);
  69. asnOs = new AsnOutputStream();
  70. TbcdString.encodeString(asnOs, sb2.toString());
  71. System.arraycopy(asnOs.toByteArray(), 0, this.data, 2, 1);
  72. data[3] = (byte)(lac / 256);
  73. data[4] = (byte)(lac % 256);
  74. data[5] = (byte)(cellId / 256);
  75. data[6] = (byte)(cellId % 256);
  76. }
  77. @Override
  78. public byte[] getData() {
  79. return data;
  80. }
  81. @Override
  82. public int getMCC() throws MAPException {
  83. if (data == null)
  84. throw new MAPException("Data must not be empty");
  85. if (data.length != 7)
  86. throw new MAPException("Data length must be equal 7");
  87. AsnInputStream ansIS = new AsnInputStream(data);
  88. String res = null;
  89. try {
  90. res = TbcdString.decodeString(ansIS, 3);
  91. } catch (IOException e) {
  92. throw new MAPException("IOException when decoding TbcdString: " + e.getMessage(), e);
  93. } catch (MAPParsingComponentException e) {
  94. throw new MAPException("MAPParsingComponentException when decoding TbcdString: " + e.getMessage(), e);
  95. }
  96. if (res.length() < 5 || res.length() > 6)
  97. throw new MAPException("Decoded TbcdString must be equal 5 or 6");
  98. String sMcc = res.substring(0, 3);
  99. return Integer.parseInt(sMcc);
  100. }
  101. @Override
  102. public int getMNC() throws MAPException {
  103. if (data == null)
  104. throw new MAPException("Data must not be empty");
  105. if (data.length != 7)
  106. throw new MAPException("Data length must be equal 7");
  107. AsnInputStream ansIS = new AsnInputStream(data);
  108. String res = null;
  109. try {
  110. res = TbcdString.decodeString(ansIS, 3);
  111. } catch (IOException e) {
  112. throw new MAPException("IOException when decoding TbcdString: " + e.getMessage(), e);
  113. } catch (MAPParsingComponentException e) {
  114. throw new MAPException("MAPParsingComponentException when decoding TbcdString: " + e.getMessage(), e);
  115. }
  116. if (res.length() < 5 || res.length() > 6)
  117. throw new MAPException("Decoded TbcdString must be equal 5 or 6");
  118. String sMnc;
  119. if (res.length() == 5) {
  120. sMnc = res.substring(3);
  121. } else {
  122. sMnc = res.substring(4) + res.substring(3, 4);
  123. }
  124. return Integer.parseInt(sMnc);
  125. }
  126. @Override
  127. public int getLac() throws MAPException {
  128. if (data == null)
  129. throw new MAPException("Data must not be empty");
  130. if (data.length != 7)
  131. throw new MAPException("Data length must be equal 7");
  132. int res = (data[3] & 0xFF) * 256 + (data[4] & 0xFF);
  133. return res;
  134. }
  135. @Override
  136. public int getCellId() throws MAPException {
  137. if (data == null)
  138. throw new MAPException("Data must not be empty");
  139. if (data.length != 7)
  140. throw new MAPException("Data length must be equal 7");
  141. int res = (data[5] & 0xFF) * 256 + (data[6] & 0xFF);
  142. return res;
  143. }
  144. @Override
  145. public int getTag() throws MAPException {
  146. return Tag.STRING_OCTET;
  147. }
  148. @Override
  149. public int getTagClass() {
  150. return Tag.CLASS_UNIVERSAL;
  151. }
  152. @Override
  153. public boolean getIsPrimitive() {
  154. return false;
  155. }
  156. @Override
  157. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  158. try {
  159. int length = ansIS.readLength();
  160. this._decode(ansIS, length);
  161. } catch (IOException e) {
  162. throw new MAPParsingComponentException("IOException when decoding CellGlobalIdOrServiceAreaIdFixedLength: " + e.getMessage(), e,
  163. MAPParsingComponentExceptionReason.MistypedParameter);
  164. } catch (AsnException e) {
  165. throw new MAPParsingComponentException("AsnException when decoding CellGlobalIdOrServiceAreaIdFixedLength: " + e.getMessage(), e,
  166. MAPParsingComponentExceptionReason.MistypedParameter);
  167. }
  168. }
  169. @Override
  170. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  171. try {
  172. this._decode(ansIS, length);
  173. } catch (IOException e) {
  174. throw new MAPParsingComponentException("IOException when decoding CellGlobalIdOrServiceAreaIdFixedLength: " + e.getMessage(), e,
  175. MAPParsingComponentExceptionReason.MistypedParameter);
  176. } catch (AsnException e) {
  177. throw new MAPParsingComponentException("AsnException when decoding CellGlobalIdOrServiceAreaIdFixedLength: " + e.getMessage(), e,
  178. MAPParsingComponentExceptionReason.MistypedParameter);
  179. }
  180. }
  181. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  182. try {
  183. this.data = ansIS.readOctetStringData(length);
  184. if (this.data.length != 7)
  185. throw new MAPParsingComponentException("Error decoding CellGlobalIdOrServiceAreaIdFixedLength: the CellGlobalIdOrServiceAreaIdFixedLength field must contain from 7 to 7 octets. Contains: " + length,
  186. MAPParsingComponentExceptionReason.MistypedParameter);
  187. } catch (IOException e) {
  188. throw new MAPParsingComponentException("IOException when decoding CellGlobalIdOrServiceAreaIdFixedLength: " + e.getMessage(), e,
  189. MAPParsingComponentExceptionReason.MistypedParameter);
  190. }
  191. }
  192. @Override
  193. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  194. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
  195. }
  196. @Override
  197. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  198. try {
  199. asnOs.writeTag(tagClass, true, tag);
  200. int pos = asnOs.StartContentDefiniteLength();
  201. this.encodeData(asnOs);
  202. asnOs.FinalizeContent(pos);
  203. } catch (AsnException e) {
  204. throw new MAPException("AsnException when encoding CellGlobalIdOrServiceAreaIdFixedLength: " + e.getMessage(), e);
  205. }
  206. }
  207. @Override
  208. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  209. if (this.data == null)
  210. throw new MAPException("Error while encoding the CellGlobalIdOrServiceAreaIdFixedLength: data is not defined");
  211. if (this.data.length != 7)
  212. throw new MAPException("Error while encoding the CellGlobalIdOrServiceAreaIdFixedLength: field length must be equal 7");
  213. asnOs.writeOctetStringData(this.data);
  214. }
  215. @Override
  216. public String toString() {
  217. int mcc = 0;
  218. int mnc = 0;
  219. int lac = 0;
  220. int cellId = 0;
  221. boolean goodData = false;
  222. try {
  223. mcc = this.getMCC();
  224. mnc = this.getMNC();
  225. lac = this.getLac();
  226. cellId = this.getCellId();
  227. goodData = true;
  228. } catch (MAPException e) {
  229. }
  230. StringBuilder sb = new StringBuilder();
  231. sb.append("CellGlobalIdOrServiceAreaIdFixedLength [");
  232. if (goodData) {
  233. sb.append("MCC=");
  234. sb.append(mcc);
  235. sb.append(", MNC=");
  236. sb.append(mnc);
  237. sb.append(", Lac=");
  238. sb.append(lac);
  239. sb.append(", CellId(SAC)=");
  240. sb.append(cellId);
  241. } else {
  242. sb.append("Data=");
  243. sb.append(this.printDataArr());
  244. }
  245. sb.append("]");
  246. return sb.toString();
  247. }
  248. private String printDataArr() {
  249. StringBuilder sb = new StringBuilder();
  250. if( this.data!=null ) {
  251. for( int b : this.data ) {
  252. sb.append(b);
  253. sb.append(" ");
  254. }
  255. }
  256. return sb.toString();
  257. }
  258. @Override
  259. public int hashCode() {
  260. final int prime = 31;
  261. int result = 1;
  262. result = prime * result + Arrays.hashCode(data);
  263. return result;
  264. }
  265. @Override
  266. public boolean equals(Object obj) {
  267. if (this == obj)
  268. return true;
  269. if (obj == null)
  270. return false;
  271. if (getClass() != obj.getClass())
  272. return false;
  273. CellGlobalIdOrServiceAreaIdFixedLengthImpl other = (CellGlobalIdOrServiceAreaIdFixedLengthImpl) obj;
  274. if (!Arrays.equals(data, other.data))
  275. return false;
  276. return true;
  277. }
  278. }