/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/errors/MAPErrorMessageSystemFailureImpl.java

http://mobicents.googlecode.com/ · Java · 324 lines · 243 code · 55 blank · 26 comment · 42 complexity · e571dec368b2bbc273741a6108392f7c 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.errors;
  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.errors.AdditionalNetworkResource;
  32. import org.mobicents.protocols.ss7.map.api.errors.MAPErrorCode;
  33. import org.mobicents.protocols.ss7.map.api.errors.MAPErrorMessageSystemFailure;
  34. import org.mobicents.protocols.ss7.map.api.errors.NetworkResource;
  35. import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
  36. import org.mobicents.protocols.ss7.map.primitives.MAPExtensionContainerImpl;
  37. /**
  38. *
  39. * @author sergey vetyutnev
  40. *
  41. */
  42. public class MAPErrorMessageSystemFailureImpl extends MAPErrorMessageImpl implements MAPErrorMessageSystemFailure {
  43. public static final int additionalNetworkResource_TAG = 0x00;
  44. private long mapProtocolVersion;
  45. private NetworkResource networkResource;
  46. private AdditionalNetworkResource additionalNetworkResource;
  47. private MAPExtensionContainer extensionContainer;
  48. public MAPErrorMessageSystemFailureImpl(long mapProtocolVersion, NetworkResource networkResource, AdditionalNetworkResource additionalNetworkResource,
  49. MAPExtensionContainer extensionContainer) {
  50. super((long) MAPErrorCode.systemFailure);
  51. this.mapProtocolVersion = mapProtocolVersion;
  52. this.networkResource = networkResource;
  53. this.additionalNetworkResource = additionalNetworkResource;
  54. this.extensionContainer = extensionContainer;
  55. }
  56. protected MAPErrorMessageSystemFailureImpl() {
  57. super((long) MAPErrorCode.systemFailure);
  58. }
  59. @Override
  60. public boolean isEmSystemFailure() {
  61. return true;
  62. }
  63. @Override
  64. public MAPErrorMessageSystemFailure getEmSystemFailure() {
  65. return this;
  66. }
  67. @Override
  68. public NetworkResource getNetworkResource() {
  69. return this.networkResource;
  70. }
  71. @Override
  72. public AdditionalNetworkResource getAdditionalNetworkResource() {
  73. return this.additionalNetworkResource;
  74. }
  75. @Override
  76. public MAPExtensionContainer getExtensionContainer() {
  77. return this.extensionContainer;
  78. }
  79. @Override
  80. public long getMapProtocolVersion() {
  81. return this.mapProtocolVersion;
  82. }
  83. @Override
  84. public void setNetworkResource(NetworkResource networkResource) {
  85. this.networkResource = networkResource;
  86. }
  87. @Override
  88. public void setAdditionalNetworkResource(AdditionalNetworkResource additionalNetworkResource) {
  89. this.additionalNetworkResource = additionalNetworkResource;
  90. }
  91. @Override
  92. public void setExtensionContainer(MAPExtensionContainer extensionContainer) {
  93. this.extensionContainer = extensionContainer;
  94. }
  95. @Override
  96. public void setMapProtocolVersion(long mapProtocolVersion) {
  97. this.mapProtocolVersion = mapProtocolVersion;
  98. }
  99. @Override
  100. public int getTag() throws MAPException {
  101. if (this.mapProtocolVersion < 3)
  102. return Tag.ENUMERATED;
  103. else
  104. return Tag.SEQUENCE;
  105. }
  106. @Override
  107. public int getTagClass() {
  108. return Tag.CLASS_UNIVERSAL;
  109. }
  110. @Override
  111. public boolean getIsPrimitive() {
  112. if (this.mapProtocolVersion < 3)
  113. return true;
  114. else
  115. return false;
  116. }
  117. @Override
  118. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  119. try {
  120. int length = ansIS.readLength();
  121. this._decode(ansIS, length);
  122. } catch (IOException e) {
  123. throw new MAPParsingComponentException("IOException when decoding MAPErrorMessageSystemFailure: " + e.getMessage(), e,
  124. MAPParsingComponentExceptionReason.MistypedParameter);
  125. } catch (AsnException e) {
  126. throw new MAPParsingComponentException("AsnException when decoding MAPErrorMessageSystemFailure: " + e.getMessage(), e,
  127. MAPParsingComponentExceptionReason.MistypedParameter);
  128. }
  129. }
  130. @Override
  131. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  132. try {
  133. this._decode(ansIS, length);
  134. } catch (IOException e) {
  135. throw new MAPParsingComponentException("IOException when decoding MAPErrorMessageSystemFailure: " + e.getMessage(), e,
  136. MAPParsingComponentExceptionReason.MistypedParameter);
  137. } catch (AsnException e) {
  138. throw new MAPParsingComponentException("AsnException when decoding MAPErrorMessageSystemFailure: " + e.getMessage(), e,
  139. MAPParsingComponentExceptionReason.MistypedParameter);
  140. }
  141. }
  142. private void _decode(AsnInputStream localAis, int length) throws MAPParsingComponentException, IOException, AsnException {
  143. this.networkResource = null;
  144. this.additionalNetworkResource = null;
  145. this.extensionContainer = null;
  146. if (localAis.getTagClass() != Tag.CLASS_UNIVERSAL)
  147. throw new MAPParsingComponentException("Error decoding MAPErrorMessageSystemFailure: bad tag class",
  148. MAPParsingComponentExceptionReason.MistypedParameter);
  149. switch (localAis.getTag()) {
  150. case Tag.ENUMERATED:
  151. if (!localAis.isTagPrimitive())
  152. throw new MAPParsingComponentException("Error decoding MAPErrorMessageAbsentSubscriberSM: ENUMERATED tag but data is not primitive",
  153. MAPParsingComponentExceptionReason.MistypedParameter);
  154. int code = (int) localAis.readIntegerData(length);
  155. this.networkResource = NetworkResource.getInstance(code);
  156. if (this.networkResource == null)
  157. throw new MAPParsingComponentException("Error decoding MAPErrorMessageAbsentSubscriberSM.networkResource: bad code value",
  158. MAPParsingComponentExceptionReason.MistypedParameter);
  159. this.mapProtocolVersion = 2;
  160. break;
  161. case Tag.SEQUENCE:
  162. if (localAis.isTagPrimitive())
  163. throw new MAPParsingComponentException("Error decoding MAPErrorMessageAbsentSubscriberSM: SEQUENCE tag but data is primitive",
  164. MAPParsingComponentExceptionReason.MistypedParameter);
  165. AsnInputStream ais = localAis.readSequenceStreamData(length);
  166. while (true) {
  167. if (ais.available() == 0)
  168. break;
  169. int tag = ais.readTag();
  170. switch (ais.getTagClass()) {
  171. case Tag.CLASS_UNIVERSAL:
  172. switch (tag) {
  173. case Tag.ENUMERATED:
  174. code = (int) ais.readInteger();
  175. this.networkResource = NetworkResource.getInstance(code);
  176. if (this.networkResource == null)
  177. throw new MAPParsingComponentException("Error decoding MAPErrorMessageAbsentSubscriberSM.networkResource: bad code value",
  178. MAPParsingComponentExceptionReason.MistypedParameter);
  179. break;
  180. case Tag.SEQUENCE:
  181. this.extensionContainer = new MAPExtensionContainerImpl();
  182. ((MAPExtensionContainerImpl)this.extensionContainer).decodeAll(ais);
  183. break;
  184. default:
  185. ais.advanceElement();
  186. break;
  187. }
  188. break;
  189. case Tag.CLASS_CONTEXT_SPECIFIC:
  190. switch (tag) {
  191. case additionalNetworkResource_TAG:
  192. code = (int) ais.readInteger();
  193. this.additionalNetworkResource = AdditionalNetworkResource.getInstance(code);
  194. break;
  195. default:
  196. ais.advanceElement();
  197. break;
  198. }
  199. break;
  200. default:
  201. ais.advanceElement();
  202. break;
  203. }
  204. }
  205. this.mapProtocolVersion = 3;
  206. break;
  207. default:
  208. throw new MAPParsingComponentException("Error decoding MAPErrorMessageSystemFailure: bad tag",
  209. MAPParsingComponentExceptionReason.MistypedParameter);
  210. }
  211. }
  212. @Override
  213. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  214. if (this.mapProtocolVersion < 3)
  215. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.ENUMERATED);
  216. else
  217. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  218. }
  219. @Override
  220. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  221. try {
  222. if (this.mapProtocolVersion < 3)
  223. asnOs.writeTag(tagClass, true, tag);
  224. else
  225. asnOs.writeTag(tagClass, false, tag);
  226. int pos = asnOs.StartContentDefiniteLength();
  227. this.encodeData(asnOs);
  228. asnOs.FinalizeContent(pos);
  229. } catch (AsnException e) {
  230. throw new MAPException("AsnException when encoding MAPErrorMessageSystemFailure: " + e.getMessage(), e);
  231. }
  232. }
  233. @Override
  234. public void encodeData(AsnOutputStream aos) throws MAPException {
  235. if (this.networkResource == null && this.additionalNetworkResource == null && this.extensionContainer == null)
  236. return;
  237. if (this.networkResource == null && this.mapProtocolVersion < 3)
  238. return;
  239. try {
  240. if (this.mapProtocolVersion < 3) {
  241. aos.writeIntegerData(this.networkResource.getCode());
  242. } else {
  243. if (this.networkResource != null)
  244. aos.writeInteger(Tag.CLASS_UNIVERSAL, Tag.ENUMERATED, this.networkResource.getCode());
  245. if (this.extensionContainer != null)
  246. ((MAPExtensionContainerImpl)this.extensionContainer).encodeAll(aos);
  247. if (this.additionalNetworkResource != null)
  248. aos.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, additionalNetworkResource_TAG, this.additionalNetworkResource.getCode());
  249. }
  250. } catch (IOException e) {
  251. throw new MAPException("IOException when encoding MAPErrorMessageSystemFailure: " + e.getMessage(), e);
  252. } catch (AsnException e) {
  253. throw new MAPException("AsnException when encoding MAPErrorMessageSystemFailure: " + e.getMessage(), e);
  254. }
  255. }
  256. @Override
  257. public String toString() {
  258. StringBuilder sb = new StringBuilder();
  259. sb.append("MAPErrorMessageSystemFailure [");
  260. if (this.networkResource != null)
  261. sb.append("networkResource=" + this.networkResource.toString());
  262. if (this.extensionContainer != null)
  263. sb.append(", extensionContainer=" + this.extensionContainer.toString());
  264. if (this.additionalNetworkResource != null)
  265. sb.append(", additionalNetworkResource=" + this.additionalNetworkResource.toString());
  266. sb.append("]");
  267. return sb.toString();
  268. }
  269. }