PageRenderTime 54ms CodeModel.GetById 16ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

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