PageRenderTime 31ms CodeModel.GetById 19ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/LocationTypeImpl.java

http://mobicents.googlecode.com/
Java | 284 lines | 157 code | 31 blank | 96 comment | 32 complexity | c8cae24cba6c60429d115d88119522da 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.lsm;
 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.service.lsm.DeferredLocationEventType;
 35import org.mobicents.protocols.ss7.map.api.service.lsm.LocationEstimateType;
 36import org.mobicents.protocols.ss7.map.api.service.lsm.LocationType;
 37import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 38
 39/**
 40 * @author amit bhayani
 41 * 
 42 */
 43public class LocationTypeImpl implements LocationType, MAPAsnPrimitive {
 44	private static final int _TAG_LOCATION_ESTIMATE_TYPE = 0;
 45	private static final int _TAG_DEFERRED_LOCATION_EVET_TYPE = 1;
 46
 47	private LocationEstimateType locationEstimateType;
 48	private DeferredLocationEventType deferredLocationEventType;
 49
 50	public LocationTypeImpl() {
 51
 52	}
 53
 54	/**
 55	 * 
 56	 */
 57	public LocationTypeImpl(final LocationEstimateType locationEstimateType, final DeferredLocationEventType deferredLocationEventType) {
 58		this.locationEstimateType = locationEstimateType;
 59		this.deferredLocationEventType = deferredLocationEventType;
 60	}
 61
 62	/*
 63	 * (non-Javadoc)
 64	 * 
 65	 * @see org.mobicents.protocols.ss7.map.api.service.lsm.LocationType#
 66	 * getLocationEstimateType()
 67	 */
 68	@Override
 69	public LocationEstimateType getLocationEstimateType() {
 70		return this.locationEstimateType;
 71	}
 72
 73	/*
 74	 * (non-Javadoc)
 75	 * 
 76	 * @see org.mobicents.protocols.ss7.map.api.service.lsm.LocationType#
 77	 * getDeferredLocationEventType()
 78	 */
 79	@Override
 80	public DeferredLocationEventType getDeferredLocationEventType() {
 81		return this.deferredLocationEventType;
 82	}
 83
 84	/*
 85	 * (non-Javadoc)
 86	 * 
 87	 * @see
 88	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
 89	 */
 90	@Override
 91	public int getTag() throws MAPException {
 92		return Tag.SEQUENCE;
 93	}
 94
 95	/*
 96	 * (non-Javadoc)
 97	 * 
 98	 * @see
 99	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
100	 * ()
101	 */
102	@Override
103	public int getTagClass() {
104		return Tag.CLASS_UNIVERSAL;
105	}
106
107	/*
108	 * (non-Javadoc)
109	 * 
110	 * @see
111	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
112	 * ()
113	 */
114	@Override
115	public boolean getIsPrimitive() {
116		return false;
117	}
118
119	/*
120	 * (non-Javadoc)
121	 * 
122	 * @see
123	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
124	 * (org.mobicents.protocols.asn.AsnInputStream)
125	 */
126	@Override
127	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
128		try {
129			int length = ansIS.readLength();
130			this._decode(ansIS, length);
131		} catch (IOException e) {
132			throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
133					MAPParsingComponentExceptionReason.MistypedParameter);
134		} catch (AsnException e) {
135			throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
136					MAPParsingComponentExceptionReason.MistypedParameter);
137		}
138	}
139
140	/*
141	 * (non-Javadoc)
142	 * 
143	 * @see
144	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
145	 * (org.mobicents.protocols.asn.AsnInputStream, int)
146	 */
147	@Override
148	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
149		try {
150			this._decode(ansIS, length);
151		} catch (IOException e) {
152			throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
153					MAPParsingComponentExceptionReason.MistypedParameter);
154		} catch (AsnException e) {
155			throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
156					MAPParsingComponentExceptionReason.MistypedParameter);
157		}
158	}
159
160	private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
161
162		AsnInputStream ais = asnIS.readSequenceStreamData(length);
163
164		int tag = ais.readTag();
165
166		if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_LOCATION_ESTIMATE_TYPE) {
167			throw new MAPParsingComponentException(
168					"Error while decoding LocationType: Parameter 0[locationEstimateType [0] LocationEstimateType] bad tag class, tag or not primitive",
169					MAPParsingComponentExceptionReason.MistypedParameter);
170		}
171
172		int length1 = ais.readLength();
173		int type = (int) ais.readIntegerData(length1);
174		this.locationEstimateType = LocationEstimateType.getLocationEstimateType(type);
175
176		while (true) {
177			if (ais.available() == 0)
178				break;
179
180			switch (ais.readTag()) {
181			case _TAG_DEFERRED_LOCATION_EVET_TYPE:
182				if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive()) {
183					throw new MAPParsingComponentException(
184							"Error while decoding LocationType: Parameter 1 [deferredLocationEventType [1] DeferredLocationEventType] bad tag class, tag or not primitive",
185							MAPParsingComponentExceptionReason.MistypedParameter);
186				}
187
188				this.deferredLocationEventType = new DeferredLocationEventTypeImpl();
189				((DeferredLocationEventTypeImpl)this.deferredLocationEventType).decodeAll(ais);
190				break;
191			default:
192				// Do we care?
193				ais.advanceElement();
194				break;
195			}
196		}
197	}
198
199	/*
200	 * (non-Javadoc)
201	 * 
202	 * @see
203	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
204	 * (org.mobicents.protocols.asn.AsnOutputStream)
205	 */
206	@Override
207	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
208		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
209	}
210
211	/*
212	 * (non-Javadoc)
213	 * 
214	 * @see
215	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
216	 * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
217	 */
218	@Override
219	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
220		try {
221			asnOs.writeTag(tagClass, false, tag);
222			int pos = asnOs.StartContentDefiniteLength();
223			this.encodeData(asnOs);
224			asnOs.FinalizeContent(pos);
225		} catch (AsnException e) {
226			throw new MAPException("AsnException when encoding reportSMDeliveryStatusRequest: " + e.getMessage(), e);
227		}
228	}
229
230	/*
231	 * (non-Javadoc)
232	 * 
233	 * @see
234	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
235	 * (org.mobicents.protocols.asn.AsnOutputStream)
236	 */
237	@Override
238	public void encodeData(AsnOutputStream asnOs) throws MAPException {
239		if (this.locationEstimateType == null) {
240			throw new MAPException("Error while encoding LocationType the mandatory parameter LocationEstimateType is not defined");
241		}
242
243		try {
244			asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_LOCATION_ESTIMATE_TYPE, this.locationEstimateType.getType());
245		} catch (IOException e) {
246			throw new MAPException("IOException when encoding parameter locationEstimateType: ", e);
247		} catch (AsnException e) {
248			throw new MAPException("AsnException when encoding parameter locationEstimateType: ", e);
249		}
250
251		if (this.deferredLocationEventType != null) {
252			((DeferredLocationEventTypeImpl)this.deferredLocationEventType).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_DEFERRED_LOCATION_EVET_TYPE);
253		}
254	}
255
256	@Override
257	public int hashCode() {
258		final int prime = 31;
259		int result = 1;
260		result = prime * result + ((deferredLocationEventType == null) ? 0 : deferredLocationEventType.hashCode());
261		result = prime * result + ((locationEstimateType == null) ? 0 : locationEstimateType.hashCode());
262		return result;
263	}
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		LocationTypeImpl other = (LocationTypeImpl) obj;
274		if (deferredLocationEventType == null) {
275			if (other.deferredLocationEventType != null)
276				return false;
277		} else if (!deferredLocationEventType.equals(other.deferredLocationEventType))
278			return false;
279		if (locationEstimateType != other.locationEstimateType)
280			return false;
281		return true;
282	}
283
284}