/*
* TeleStax, Open Source Cloud Communications Copyright 2012.
* and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.mobicents.protocols.ss7.map.service.pdpContextActivation;
import org.apache.log4j.Logger;
import org.mobicents.protocols.asn.AsnInputStream;
import org.mobicents.protocols.asn.Tag;
import org.mobicents.protocols.ss7.map.MAPDialogImpl;
import org.mobicents.protocols.ss7.map.MAPProviderImpl;
import org.mobicents.protocols.ss7.map.MAPServiceBaseImpl;
import org.mobicents.protocols.ss7.map.api.MAPApplicationContext;
import org.mobicents.protocols.ss7.map.api.MAPApplicationContextName;
import org.mobicents.protocols.ss7.map.api.MAPDialog;
import org.mobicents.protocols.ss7.map.api.MAPException;
import org.mobicents.protocols.ss7.map.api.MAPOperationCode;
import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
import org.mobicents.protocols.ss7.map.api.MAPServiceListener;
import org.mobicents.protocols.ss7.map.api.dialog.ServingCheckData;
import org.mobicents.protocols.ss7.map.api.dialog.ServingCheckResult;
import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
import org.mobicents.protocols.ss7.map.api.service.pdpContextActivation.MAPDialogPdpContextActivation;
import org.mobicents.protocols.ss7.map.api.service.pdpContextActivation.MAPServicePdpContextActivation;
import org.mobicents.protocols.ss7.map.api.service.pdpContextActivation.MAPServicePdpContextActivationListener;
import org.mobicents.protocols.ss7.map.dialog.ServingCheckDataImpl;
import org.mobicents.protocols.ss7.sccp.parameter.SccpAddress;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.Dialog;
import org.mobicents.protocols.ss7.tcap.asn.ApplicationContextName;
import org.mobicents.protocols.ss7.tcap.asn.TcapFactory;
import org.mobicents.protocols.ss7.tcap.asn.comp.ComponentType;
import org.mobicents.protocols.ss7.tcap.asn.comp.Invoke;
import org.mobicents.protocols.ss7.tcap.asn.comp.OperationCode;
import org.mobicents.protocols.ss7.tcap.asn.comp.Parameter;
/**
*
* @author sergey vetyutnev
*
*/
public class MAPServicePdpContextActivationImpl extends MAPServiceBaseImpl implements MAPServicePdpContextActivation {
protected Logger loger = Logger.getLogger(MAPServicePdpContextActivationImpl.class);
public MAPServicePdpContextActivationImpl(MAPProviderImpl mapProviderImpl) {
super(mapProviderImpl);
}
/*
* Creating a new outgoing MAP PdpContextActivation dialog and adding it to the MAPProvider.dialog collection
*/
public MAPDialogPdpContextActivation createNewDialog(MAPApplicationContext appCntx, SccpAddress origAddress, AddressString origReference,
SccpAddress destAddress, AddressString destReference) throws MAPException {
return this.createNewDialog(appCntx, origAddress, origReference, destAddress, destReference, null);
}
public MAPDialogPdpContextActivation createNewDialog(MAPApplicationContext appCntx, SccpAddress origAddress, AddressString origReference,
SccpAddress destAddress, AddressString destReference, Long localTrId) throws MAPException {
// We cannot create a dialog if the service is not activated
if (!this.isActivated())
throw new MAPException(
"Cannot create MAPDialogPdpContextActivation because MAPServicePdpContextActivation is not activated");
Dialog tcapDialog = this.createNewTCAPDialog(origAddress, destAddress, localTrId);
MAPDialogPdpContextActivationImpl dialog = new MAPDialogPdpContextActivationImpl(appCntx, tcapDialog,
this.mapProviderImpl, this, origReference, destReference);
this.putMAPDialogIntoCollection(dialog);
return dialog;
}
protected MAPDialogImpl createNewDialogIncoming(MAPApplicationContext appCntx, Dialog tcapDialog) {
return new MAPDialogPdpContextActivationImpl(appCntx, tcapDialog, this.mapProviderImpl, this, null, null);
}
public void addMAPServiceListener(MAPServicePdpContextActivationListener mapServiceListener) {
super.addMAPServiceListener(mapServiceListener);
}
public void removeMAPServiceListener(MAPServicePdpContextActivationListener mapServiceListener) {
super.removeMAPServiceListener(mapServiceListener);
}
public ServingCheckData isServingService(MAPApplicationContext dialogApplicationContext) {
MAPApplicationContextName ctx = dialogApplicationContext.getApplicationContextName();
int vers = dialogApplicationContext.getApplicationContextVersion().getVersion();
switch (ctx) {
case gprsLocationInfoRetrievalContext:
if (vers >= 3 && vers <= 4) {
return new ServingCheckDataImpl(ServingCheckResult.AC_Serving);
} else if (vers > 4) {
long[] altOid = dialogApplicationContext.getOID();
altOid[7] = 2;
ApplicationContextName alt = TcapFactory.createApplicationContextName(altOid);
return new ServingCheckDataImpl(ServingCheckResult.AC_VersionIncorrect, alt);
} else {
return new ServingCheckDataImpl(ServingCheckResult.AC_VersionIncorrect);
}
}
return new ServingCheckDataImpl(ServingCheckResult.AC_NotServing);
}
public void processComponent(ComponentType compType, OperationCode oc, Parameter parameter, MAPDialog mapDialog,
Long invokeId, Long linkedId, Invoke linkedInvoke) throws MAPParsingComponentException {
// if an application-context-name different from version 1 is
// received in a syntactically correct TC-
// BEGIN indication primitive but is not acceptable from a load
// control point of view, the MAP PM
// shall ignore this dialogue request. The MAP-user is not informed.
// if (compType == ComponentType.Invoke && this.mapProviderImpl.isCongested()) {
// // TODO: we need to care of conjection control
// }
MAPDialogPdpContextActivationImpl mapDialogPdpContextActivationImpl = (MAPDialogPdpContextActivationImpl) mapDialog;
Long ocValue = oc.getLocalOperationCode();
if (ocValue == null)
new MAPParsingComponentException("", MAPParsingComponentExceptionReason.UnrecognizedOperation);
MAPApplicationContextName acn = mapDialog.getApplicationContext().getApplicationContextName();
int vers = mapDialog.getApplicationContext().getApplicationContextVersion().getVersion();
int ocValueInt = (int) (long) ocValue;
switch (ocValueInt) {
case MAPOperationCode.sendRoutingInfoForGprs:
if (acn == MAPApplicationContextName.gprsLocationInfoRetrievalContext
|| acn == MAPApplicationContextName.shortMsgMTRelayContext) {
if (compType == ComponentType.Invoke)
this.sendRoutingInfoForGprsRequest(parameter, mapDialogPdpContextActivationImpl, invokeId);
else
this.sendRoutingInfoForGprsResponse(parameter, mapDialogPdpContextActivationImpl, invokeId,
compType == ComponentType.ReturnResult);
}
break;
default:
throw new MAPParsingComponentException("MAPServicePdpContextActivation: unknown incoming operation code: "
+ ocValueInt, MAPParsingComponentExceptionReason.UnrecognizedOperation);
}
}
private void sendRoutingInfoForGprsRequest(Parameter parameter, MAPDialogPdpContextActivationImpl mapDialogImpl, Long invokeId)
throws MAPParsingComponentException {
if (parameter == null)
throw new MAPParsingComponentException(
"Error while decoding sendRoutingInfoForGprsRequest: Parameter is mandatory but not found",
MAPParsingComponentExceptionReason.MistypedParameter);
if (parameter.getTag() != Tag.SEQUENCE || parameter.getTagClass() != Tag.CLASS_UNIVERSAL || parameter.isPrimitive())
throw new MAPParsingComponentException(
"Error while decoding sendRoutingInfoForGprsRequest: Bad tag or tagClass or parameter is primitive, received tag="
+ parameter.getTag(), MAPParsingComponentExceptionReason.MistypedParameter);
byte[] buf = parameter.getData();
AsnInputStream ais = new AsnInputStream(buf);
SendRoutingInfoForGprsRequestImpl ind = new SendRoutingInfoForGprsRequestImpl();
ind.decodeData(ais, buf.length);
ind.setInvokeId(invokeId);
ind.setMAPDialog(mapDialogImpl);
for (MAPServiceListener serLis : this.serviceListeners) {
try {
serLis.onMAPMessage(ind);
((MAPServicePdpContextActivationListener) serLis).onSendRoutingInfoForGprsRequest(ind);
} catch (Exception e) {
loger.error("Error processing onSendRoutingInfoForGprsRequest: " + e.getMessage(), e);
}
}
}
private void sendRoutingInfoForGprsResponse(Parameter parameter, MAPDialogPdpContextActivationImpl mapDialogImpl,
Long invokeId, boolean returnResultNotLast) throws MAPParsingComponentException {
SendRoutingInfoForGprsResponseImpl ind = new SendRoutingInfoForGprsResponseImpl();
if (parameter == null)
throw new MAPParsingComponentException(
"Error while decoding sendRoutingInfoForGprsResponse: Parameter is mandatory but not found",
MAPParsingComponentExceptionReason.MistypedParameter);
if (parameter.getTag() != Tag.SEQUENCE || parameter.getTagClass() != Tag.CLASS_UNIVERSAL || parameter.isPrimitive())
throw new MAPParsingComponentException(
"Error while decoding sendRoutingInfoForGprsResponse: Bad tag or tagClass or parameter is primitive, received tag="
+ parameter.getTag(), MAPParsingComponentExceptionReason.MistypedParameter);
byte[] buf = parameter.getData();
AsnInputStream ais = new AsnInputStream(buf);
ind.decodeData(ais, buf.length);
ind.setInvokeId(invokeId);
ind.setMAPDialog(mapDialogImpl);
ind.setReturnResultNotLast(returnResultNotLast);
for (MAPServiceListener serLis : this.serviceListeners) {
try {
serLis.onMAPMessage(ind);
((MAPServicePdpContextActivationListener) serLis).onSendRoutingInfoForGprsResponse(ind);
} catch (Exception e) {
loger.error("Error processing onSendRoutingInfoForGprsResponse: " + e.getMessage(), e);
}
}
}
}