PageRenderTime 35ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 2ms app.codeStats 0ms

/protocols/ss7/map/map-api/src/main/java/org/mobicents/protocols/ss7/map/api/MAPApplicationContext.java

http://mobicents.googlecode.com/
Java | 169 lines | 101 code | 27 blank | 41 comment | 34 complexity | a14d8151b632c52fddc62d73581eacf9 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.api;
 24
 25import java.util.Arrays;
 26
 27/**
 28 * 
 29 * @author amit bhayani
 30 * @author sergey vetyutnev
 31 * 
 32 */
 33public class MAPApplicationContext {
 34	
 35	private static long[] oidTemplate = new long[] { 0, 4, 0, 0, 1, 0, 0, 0 };
 36
 37	private MAPApplicationContextName contextName;
 38	private MAPApplicationContextVersion contextVersion;
 39
 40	private MAPApplicationContext(MAPApplicationContextName contextName, MAPApplicationContextVersion contextVersion) {
 41		this.contextName = contextName;
 42		this.contextVersion = contextVersion;
 43	}
 44
 45	public long[] getOID() {
 46		long[] res = Arrays.copyOf(oidTemplate, oidTemplate.length);
 47		res[6] = this.contextName.getApplicationContextCode();
 48		res[7] = this.contextVersion.getVersion();
 49		
 50		return res;
 51	}
 52
 53	public MAPApplicationContextName getApplicationContextName() {
 54		return this.contextName;
 55	}
 56
 57	public MAPApplicationContextVersion getApplicationContextVersion() {
 58		return this.contextVersion;
 59	}
 60
 61	public static MAPApplicationContext getInstance(MAPApplicationContextName contextName, MAPApplicationContextVersion contextVersion) {
 62		if (MAPApplicationContext.availableApplicationContextVersion(contextName, contextVersion.getVersion()))
 63			return new MAPApplicationContext(contextName, contextVersion);
 64		else
 65			return null;
 66	}
 67
 68	public static MAPApplicationContext getInstance(long[] oid) {
 69		
 70		if (oid == null || oid.length != oidTemplate.length)
 71			return null;
 72		for (int i1 = 0; i1 < oidTemplate.length - 2; i1++) {
 73			if (oid[i1] != oidTemplate[i1])
 74				return null;
 75		}
 76		
 77		MAPApplicationContextName contextName = MAPApplicationContextName.getInstance(oid[6]);
 78		MAPApplicationContextVersion contextVersion = MAPApplicationContextVersion.getInstance(oid[7]);
 79
 80		if (contextName == null || contextVersion == null)
 81			return null;
 82		if (!MAPApplicationContext.availableApplicationContextVersion(contextName, (int) oid[7]))
 83			return null;
 84		
 85		return new MAPApplicationContext(contextName, contextVersion);
 86	}
 87	
 88	/**
 89	 * Return if the contextVersion is available for the contextName
 90	 * 
 91	 * @param contextName
 92	 * @param version
 93	 * @return
 94	 */
 95	public static boolean availableApplicationContextVersion(MAPApplicationContextName contextName, int contextVersion) {
 96		switch (contextName) {
 97		case networkUnstructuredSsContext:
 98		case shortMsgAlertContext:
 99			if (contextVersion >= 1 && contextVersion <= 2)
100				return true;
101			else
102				return false;
103
104		case shortMsgMORelayContext:
105		case shortMsgMTRelayContext:
106		case shortMsgGatewayContext:
107			if (contextVersion >= 1 && contextVersion <= 3)
108				return true;
109			else
110				return false;
111		case locationSvcEnquiryContext:
112			//for locationSvcEnquiryContext only version 3 is supported
113			if (contextVersion == 3) 
114				return true;
115			else
116				return false;	
117		case anyTimeEnquiryContext:
118			if (contextVersion == 3){
119				return true;
120			}
121		}
122		
123		return false;
124	}
125
126	/**
127	 * Get ApplicationContext version. If oid is bad 0 will be received
128	 * 
129	 * @param oid
130	 * @return
131	 */
132	public static int getProtocolVersion(long[] oid) {
133		if (oid == null || oid.length != 8)
134			return 0;
135		else
136			return (int) oid[7];
137	}
138
139	@Override
140	public boolean equals(Object obj) {
141		if (obj == null || !(obj instanceof MAPApplicationContext))
142			return false;
143		
144		MAPApplicationContext x = (MAPApplicationContext)obj;
145		if (this.contextName == x.contextName && this.contextVersion == x.contextVersion)
146			return true;
147		else
148			return false;
149	}
150
151	
152	@Override
153	public String toString() {
154		StringBuffer s = new StringBuffer();
155
156		s.append("MAPApplicationContext [Name=");
157		s.append(this.contextName.toString());
158		s.append(", Version=");
159		s.append(this.contextVersion.toString());
160		s.append(", Oid=");
161		for (long l : this.getOID()) {
162			s.append(l).append(", ");
163		}
164		s.append("]");
165
166		return s.toString();
167	}
168
169}