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