PageRenderTime 41ms CodeModel.GetById 23ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

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