PageRenderTime 50ms CodeModel.GetById 14ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/sms/LocationInfoWithLMSIImpl.java

http://mobicents.googlecode.com/
Java | 316 lines | 233 code | 55 blank | 28 comment | 55 complexity | ea6b88057cc90db033454d28e2440d57 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.service.sms;
 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.primitives.AdditionalNumberType;
 35import org.mobicents.protocols.ss7.map.api.primitives.ISDNAddressString;
 36import org.mobicents.protocols.ss7.map.api.primitives.LMSI;
 37import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
 38import org.mobicents.protocols.ss7.map.api.service.sms.LocationInfoWithLMSI;
 39import org.mobicents.protocols.ss7.map.primitives.ISDNAddressStringImpl;
 40import org.mobicents.protocols.ss7.map.primitives.LMSIImpl;
 41import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 42import org.mobicents.protocols.ss7.map.primitives.MAPExtensionContainerImpl;
 43
 44/**
 45*
 46* @author sergey vetyutnev
 47* 
 48*/
 49public class LocationInfoWithLMSIImpl implements LocationInfoWithLMSI, MAPAsnPrimitive {
 50
 51	private static final int _TAG_NetworkNodeNumber = 1;
 52	private static final int _TAG_GprsNodeIndicator = 5;
 53	private static final int _TAG_AdditionalNumber = 6;
 54	
 55	private ISDNAddressString networkNodeNumber;
 56	private LMSI lmsi;
 57	private MAPExtensionContainer extensionContainer;
 58	private AdditionalNumberType additionalNumberType;
 59	private ISDNAddressString additionalNumber;
 60
 61	
 62	public LocationInfoWithLMSIImpl() {
 63	}
 64	
 65	public LocationInfoWithLMSIImpl(ISDNAddressString networkNodeNumber, LMSI lmsi, MAPExtensionContainer extensionContainer,
 66			AdditionalNumberType additionalNumberType, ISDNAddressString additionalNumber) {
 67		this.networkNodeNumber = networkNodeNumber;
 68		this.lmsi = lmsi;
 69		this.extensionContainer = extensionContainer;
 70		this.additionalNumberType = additionalNumberType;
 71		this.additionalNumber = additionalNumber;
 72	}
 73
 74	
 75	@Override
 76	public ISDNAddressString getNetworkNodeNumber() {
 77		return this.networkNodeNumber;
 78	}
 79
 80	@Override
 81	public LMSI getLMSI() {
 82		return this.lmsi;
 83	}
 84
 85	@Override
 86	public MAPExtensionContainer getExtensionContainer() {
 87		return this.extensionContainer;
 88	}
 89
 90	@Override
 91	public AdditionalNumberType getAdditionalNumberType() {
 92		return this.additionalNumberType;
 93	}
 94
 95	@Override
 96	public ISDNAddressString getAdditionalNumber() {
 97		return this.additionalNumber;
 98	}
 99
100
101	public int getTag() {
102		return Tag.SEQUENCE;
103	}
104
105	@Override
106	public int getTagClass() {
107		return Tag.CLASS_UNIVERSAL;
108	}
109
110	@Override
111	public boolean getIsPrimitive() {
112		return false;
113	}
114
115	@Override
116	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
117
118		try {
119			int length = ansIS.readLength();
120			this._decode(ansIS, length);
121		} catch (IOException e) {
122			throw new MAPParsingComponentException("IOException when decoding LocationInfoWithLMSI: " + e.getMessage(), e,
123					MAPParsingComponentExceptionReason.MistypedParameter);
124		} catch (AsnException e) {
125			throw new MAPParsingComponentException("AsnException when decoding LocationInfoWithLMSI: " + e.getMessage(), e,
126					MAPParsingComponentExceptionReason.MistypedParameter);
127		}
128	}
129
130	@Override
131	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
132
133		try {
134			this._decode(ansIS, length);
135		} catch (IOException e) {
136			throw new MAPParsingComponentException("IOException when decoding LocationInfoWithLMSI: " + e.getMessage(), e,
137					MAPParsingComponentExceptionReason.MistypedParameter);
138		} catch (AsnException e) {
139			throw new MAPParsingComponentException("AsnException when decoding LocationInfoWithLMSI: " + e.getMessage(), e,
140					MAPParsingComponentExceptionReason.MistypedParameter);
141		}
142	}
143
144	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
145		
146		this.networkNodeNumber = null;
147		this.lmsi = null;
148		this.extensionContainer = null;
149		this.additionalNumberType = null;
150		this.additionalNumber = null;
151		
152		AsnInputStream ais = ansIS.readSequenceStreamData(length);
153		
154		int num = 0;
155		while( true ) {
156			if( ais.available()==0 )
157				break;
158			
159			int tag = ais.readTag();
160			
161			if (num == 0) {
162				// first parameter is mandatory - networkNode-Number
163				if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_NetworkNodeNumber)
164					throw new MAPParsingComponentException(
165							"Error when decoding LocationInfoWithLMSI: networkNode-Number: tagClass or tag is bad or element is not primitive: tagClass="
166									+ ais.getTagClass() + ", Tag=" + tag, MAPParsingComponentExceptionReason.MistypedParameter);
167				this.networkNodeNumber = new ISDNAddressStringImpl();
168				((ISDNAddressStringImpl) this.networkNodeNumber).decodeAll(ais);
169			} else {
170				// optional parameters
171				if(ais.getTagClass() == Tag.CLASS_UNIVERSAL) {
172					
173					switch (tag) {
174					case Tag.STRING_OCTET:
175						if (!ais.isTagPrimitive() || this.lmsi != null)
176							throw new MAPParsingComponentException(
177									"Error when decoding LocationInfoWithLMSI: lmsi: double element or element is not primitive",
178									MAPParsingComponentExceptionReason.MistypedParameter);
179						this.lmsi = new LMSIImpl();
180						((LMSIImpl)this.lmsi).decodeAll(ais);
181						break;
182						
183					case Tag.SEQUENCE:
184						if (ais.isTagPrimitive() || this.extensionContainer != null)
185							throw new MAPParsingComponentException(
186									"Error when decoding LocationInfoWithLMSI: extensionContainer: double element or element is primitive",
187									MAPParsingComponentExceptionReason.MistypedParameter);
188						this.extensionContainer = new MAPExtensionContainerImpl();
189						((MAPExtensionContainerImpl)this.extensionContainer).decodeAll(ais);
190						break;
191						
192					default:
193						ais.advanceElement();
194						break;
195					}
196				} else if (ais.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
197					
198					switch (tag) {
199					case _TAG_GprsNodeIndicator:
200						if (!ais.isTagPrimitive() || this.additionalNumberType != null)
201							throw new MAPParsingComponentException(
202									"Error when decoding LocationInfoWithLMSI: gprsNodeIndicator: double element or element is not primitive",
203									MAPParsingComponentExceptionReason.MistypedParameter);
204						ais.readNull();
205						this.additionalNumberType = AdditionalNumberType.sgsn;
206						break;
207						
208					case _TAG_AdditionalNumber:
209						if (!ais.isTagPrimitive() || this.additionalNumber != null)
210							throw new MAPParsingComponentException(
211									"Error when decoding LocationInfoWithLMSI: additionalNumber: double element or element is not primitive",
212									MAPParsingComponentExceptionReason.MistypedParameter);
213						this.additionalNumber = new ISDNAddressStringImpl();
214						((ISDNAddressStringImpl)this.additionalNumber).decodeAll(ais);
215						break;
216						
217					default:
218						ais.advanceElement();
219						break;
220					}
221				} else {
222					ais.advanceElement();
223				}
224			}
225			
226			num++;
227		}
228		
229		if (this.networkNodeNumber == null)
230			throw new MAPParsingComponentException("Error while decoding LocationInfoWithLMSI: 1 parameter is mandatory, found " + num,
231					MAPParsingComponentExceptionReason.MistypedParameter);
232		
233		if (this.additionalNumberType == null && this.additionalNumber != null)
234			this.additionalNumberType = AdditionalNumberType.msc;
235	}
236
237	@Override
238	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
239		
240		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
241	}
242
243	@Override
244	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
245		
246		try {
247			asnOs.writeTag(tagClass, false, tag);
248			int pos = asnOs.StartContentDefiniteLength();
249			this.encodeData(asnOs);
250			asnOs.FinalizeContent(pos);
251		} catch (AsnException e) {
252			throw new MAPException("AsnException when encoding LocationInfoWithLMSI: " + e.getMessage(), e);
253		}
254	}
255
256	@Override
257	public void encodeData(AsnOutputStream asnOs) throws MAPException {
258
259		try {
260			if (this.networkNodeNumber == null)
261				throw new MAPException("Error while decoding LocationInfoWithLMSI: networkNodeNumber must not be null");
262
263			((ISDNAddressStringImpl)this.networkNodeNumber).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_NetworkNodeNumber);
264
265			if (this.lmsi != null)
266				((LMSIImpl)this.lmsi).encodeAll(asnOs);
267
268			if (this.extensionContainer != null)
269				((MAPExtensionContainerImpl)this.extensionContainer).encodeAll(asnOs);
270
271			if (this.additionalNumber != null) {
272				if (this.additionalNumberType == AdditionalNumberType.sgsn)
273					asnOs.writeNull(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_GprsNodeIndicator);
274
275				((ISDNAddressStringImpl)this.additionalNumber).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_AdditionalNumber);
276			}
277		} catch (IOException e) {
278			throw new MAPException("IOException when encoding LocationInfoWithLMSI: " + e.getMessage(), e);
279		} catch (AsnException e) {
280			throw new MAPException("AsnException when encoding LocationInfoWithLMSI: " + e.getMessage(), e);
281		}
282	}
283
284	
285	@Override
286	public String toString() {
287		StringBuilder sb = new StringBuilder();
288		sb.append("LocationInfoWithLMSIImpl [");
289
290		if (this.networkNodeNumber != null) {
291			sb.append("networkNodeNumber=");
292			sb.append(this.networkNodeNumber.toString());
293		}
294		if (this.lmsi != null) {
295			sb.append(", lmsi=");
296			sb.append(this.lmsi.toString());
297		}
298		if (this.extensionContainer != null) {
299			sb.append(", extensionContainer=");
300			sb.append(this.extensionContainer.toString());
301		}
302		if (this.additionalNumberType != null) {
303			sb.append(", additionalNumberType=");
304			sb.append(this.additionalNumberType.toString());
305		}
306		if (this.additionalNumber != null) {
307			sb.append(", additionalNumber=");
308			sb.append(this.additionalNumber.toString());
309		}
310
311		sb.append("]");
312
313		return sb.toString();
314	}
315}
316