use of net.java.slee.resource.diameter.base.events.DiameterMessage in project jain-slee.diameter by RestComm.
the class DiameterS6aResourceAdaptor method fireEvent.
/*
* (non-Javadoc)
* @see org.mobicents.slee.resource.diameter.base.handlers.BaseSessionCreationListener#fireEvent(java.lang.String, org.jdiameter.api.Request, org.jdiameter.api.Answer)
*/
public void fireEvent(String sessionId, Message message) {
DiameterMessage event = (DiameterMessage) createEvent(message);
FireableEventType eventId = eventIdCache.getEventId(eventLookup, message);
this.fireEvent(event, getActivityHandle(sessionId), eventId, null, true, message.isRequest());
}
use of net.java.slee.resource.diameter.base.events.DiameterMessage in project jain-slee.diameter by RestComm.
the class S6aMessageFactoryImpl method createS6aMessage.
/**
* Creates a S6a Message with specified command-code and avps filled. If a header is present an answer will be created, if not
* it will generate a request.
*
* @param diameterHeader
* @param avps
* @param _commandCode
* @param appId
* @return
* @throws InternalException
*/
DiameterMessage createS6aMessage(DiameterHeader diameterHeader, DiameterAvp[] avps, int _commandCode, ApplicationId appId) throws InternalException {
boolean creatingRequest = diameterHeader == null;
Message msg = null;
if (!creatingRequest) {
Message raw = createMessage(diameterHeader, avps, 0, appId);
raw.setProxiable(diameterHeader.isProxiable());
raw.setRequest(false);
// just in case. answers never have T flag set
raw.setReTransmitted(false);
// FIXME ? raw.getAvps().removeAvp(Avp.AUTH_APPLICATION_ID);
msg = raw;
} else {
Message raw = createMessage(diameterHeader, avps, _commandCode, appId);
raw.setProxiable(true);
raw.setRequest(true);
msg = raw;
}
int commandCode = creatingRequest ? _commandCode : diameterHeader.getCommandCode();
DiameterMessage diamMessage = null;
switch(commandCode) {
case UpdateLocationRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new UpdateLocationRequestImpl(msg) : new UpdateLocationAnswerImpl(msg);
break;
case AuthenticationInformationRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new AuthenticationInformationRequestImpl(msg) : new AuthenticationInformationAnswerImpl(msg);
break;
case CancelLocationRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new CancelLocationRequestImpl(msg) : new CancelLocationAnswerImpl(msg);
break;
case InsertSubscriberDataRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new InsertSubscriberDataRequestImpl(msg) : new InsertSubscriberDataAnswerImpl(msg);
break;
case DeleteSubscriberDataRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new DeleteSubscriberDataRequestImpl(msg) : new DeleteSubscriberDataAnswerImpl(msg);
break;
case PurgeUERequest.COMMAND_CODE:
diamMessage = creatingRequest ? new PurgeUERequestImpl(msg) : new PurgeUEAnswerImpl(msg);
break;
case ResetRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new ResetRequestImpl(msg) : new ResetAnswerImpl(msg);
break;
case NotifyRequest.COMMAND_CODE:
diamMessage = creatingRequest ? new NotifyRequestImpl(msg) : new NotifyAnswerImpl(msg);
break;
default:
diamMessage = new ExtensionDiameterMessageImpl(msg);
}
if (!diamMessage.hasSessionId() && session != null) {
diamMessage.setSessionId(session.getSessionId());
}
return diamMessage;
}
use of net.java.slee.resource.diameter.base.events.DiameterMessage in project jain-slee.diameter by RestComm.
the class DiameterShClientResourceAdaptor method fireEvent.
/*
* (non-Javadoc)
* @see org.mobicents.slee.resource.diameter.base.handlers.BaseSessionCreationListener#fireEvent(java.lang.String, org.jdiameter.api.Request, org.jdiameter.api.Answer)
*/
public void fireEvent(String sessionId, Message message) {
DiameterMessage event = (DiameterMessage) createEvent(message);
FireableEventType eventId = eventIdCache.getEventId(eventLookup, message);
this.fireEvent(event, getActivityHandle(sessionId), eventId, null, true, message.isRequest());
}
use of net.java.slee.resource.diameter.base.events.DiameterMessage in project jain-slee.diameter by RestComm.
the class ShServerActivityAnswerCreationTest method testShServerActivityAnswerCreation.
@Test
public void testShServerActivityAnswerCreation() throws Exception {
ServerShSession session = new ShServerSessionImpl(new ShServerSessionDataLocalImpl(), new IShMessageFactoryImpl(), (ISessionFactory) stack.getSessionFactory(), new ServerShSessionListenerImpl());
DiameterMessageFactoryImpl msgFactory = new DiameterMessageFactoryImpl(session.getSessions().get(0), stack, null, null);
ShClientMessageFactoryImpl factory = new ShClientMessageFactoryImpl(session.getSessions().get(0), stack);
UserDataRequest udr = factory.createUserDataRequest();
net.java.slee.resource.diameter.sh.events.ProfileUpdateRequest pur = factory.createProfileUpdateRequest();
net.java.slee.resource.diameter.sh.events.SubscribeNotificationsRequest snr = factory.createSubscribeNotificationsRequest();
ArrayList<DiameterMessage> list = new ArrayList<DiameterMessage>();
list.add(udr);
list.add(pur);
list.add(snr);
ShServerActivityImpl activity = new ShServerActivityImpl(new ShServerMessageFactoryImpl(msgFactory, session.getSessions().get(0), stack, diameterShAvpFactory), diameterShAvpFactory, session, null, null);
DiameterActivityAnswerCreationHelper.testAnswerCreation(activity, "stateMessages", list);
}
use of net.java.slee.resource.diameter.base.events.DiameterMessage in project jain-slee.diameter by RestComm.
the class DiameterTestsSbb method onAccountingRequest.
public void onAccountingRequest(net.java.slee.resource.diameter.base.events.AccountingRequest acr, ActivityContextInterface aci) {
long start = System.currentTimeMillis();
logger.info("Accounting-Request received. [" + acr + "]");
boolean actAsProxy = false;
try {
// Are we gonna act as a proxy?
if (actAsProxy) {
// In here we act as a "proxy". Just for testing we take the original message,
// replace the Origin/Destination Host/Realm AVPs and send it to the emulator.
boolean hasDestinationHost = false;
boolean hasDestinationRealm = false;
List<DiameterAvp> avps = new ArrayList<DiameterAvp>();
for (DiameterAvp avp : acr.getAvps()) {
switch(avp.getCode()) {
case Avp.ORIGIN_HOST:
avps.add(avpFactory.createAvp(Avp.ORIGIN_HOST, "aaa://127.0.0.1:3868".getBytes()));
break;
case Avp.ORIGIN_REALM:
avps.add(avpFactory.createAvp(Avp.ORIGIN_REALM, "mobicents.org".getBytes()));
break;
case Avp.DESTINATION_HOST:
avps.add(avpFactory.createAvp(Avp.DESTINATION_HOST, "aaa://127.0.0.1:13868".getBytes()));
hasDestinationHost = true;
break;
case Avp.DESTINATION_REALM:
avps.add(avpFactory.createAvp(Avp.DESTINATION_REALM, "mobicents.org".getBytes()));
hasDestinationRealm = true;
break;
default:
avps.add(avp);
}
}
if (!hasDestinationHost)
avps.add(avpFactory.createAvp(Avp.DESTINATION_HOST, "127.0.0.1".getBytes()));
if (!hasDestinationRealm)
avps.add(avpFactory.createAvp(Avp.DESTINATION_REALM, "mobicents.org".getBytes()));
logger.info("AVPs ==> " + avps);
DiameterAvp[] avpArray = new DiameterAvp[avps.size()];
avpArray = avps.toArray(avpArray);
logger.info("Creating Custom Message...");
DiameterMessage ms = messageFactory.createAccountingRequest(avpArray);
logger.info("Created Custom Message[" + ms + "]");
logger.info("Sending Custom Message...");
provider.createActivity().sendMessage(ms);
logger.info("Sent Custom Message[" + ms + "]");
} else {
// In here we act as a server...
int subscriptionIdType = -1;
String subscriptionIdData = "";
int unitType = -1;
long valueDigits = -1;
int exponent = 0;
int requestedAction = -1;
int serviceParameterType;
int serviceParameterValue;
int serviceParameterInfo;
DiameterAvp subscriptionIdAvp = null;
DiameterAvp requestedActionAvp = null;
if (aci.getActivity() instanceof AccountingServerSessionActivity) {
for (DiameterAvp avp : acr.getAvps()) {
switch(avp.getCode()) {
case SUBSCRIPTION_ID:
{
// This should contain a SUBSCRIPTION_ID_TYPE and a SUBSCRIPTION_ID_DATA
if (avp instanceof GroupedAvp) {
GroupedAvp gAvp = (GroupedAvp) avp;
for (DiameterAvp subAvp : gAvp.getExtensionAvps()) {
switch(subAvp.getCode()) {
case SUBSCRIPTION_ID_TYPE:
subscriptionIdType = subAvp.intValue();
break;
case SUBSCRIPTION_ID_DATA:
subscriptionIdData = subAvp.stringValue();
break;
}
}
}
}
break;
case REQUESTED_SERVICE_UNIT:
{
// This should contain a UNIT_TYPE and a UNIT_VALUE
if (avp instanceof GroupedAvp) {
GroupedAvp gAvp = (GroupedAvp) avp;
for (DiameterAvp subAvp : gAvp.getExtensionAvps()) {
switch(subAvp.getCode()) {
case UNIT_TYPE:
unitType = subAvp.intValue();
break;
case UNIT_VALUE:
{
// This should contain a VALUE_DIGITS
if (subAvp instanceof GroupedAvp) {
GroupedAvp gSubAvp = (GroupedAvp) subAvp;
for (DiameterAvp subSubAvp : gSubAvp.getExtensionAvps()) {
switch(subSubAvp.getCode()) {
case VALUE_DIGITS:
valueDigits = subSubAvp.longValue();
break;
case EXPONENT:
exponent = subSubAvp.intValue();
break;
}
}
}
break;
}
}
}
}
}
break;
case REQUESTED_ACTION:
requestedAction = avp.intValue();
requestedActionAvp = avp;
break;
case SERVICE_PARAMETER_TYPE:
// We can discard this...
case SERVICE_PARAMETER_VALUE:
// We can discard this...
case SERVICE_PARAMETER_INFO:
// We can discard this...
default:
}
}
logger.info("Subscription-Id-Type: " + subscriptionIdType);
logger.info("Subscription-Id-Data: " + subscriptionIdData);
logger.info("Unit-Type: " + unitType);
logger.info("Value-Digits: " + valueDigits);
logger.info("Exponent: " + exponent);
logger.info("Requested-Action: " + requestedAction);
AccountingServerSessionActivity assa = (AccountingServerSessionActivity) aci.getActivity();
// Aditional AVPs container
List<DiameterAvp> avps = new ArrayList<DiameterAvp>();
// By default, let's consider it's OK and create answer with 2001
AccountingAnswer ans = assa.createAccountingAnswer(acr);
ans.setResultCode(ResultCode.SUCCESS);
double chargingValue = valueDigits * Math.pow(10, exponent);
if (subscriptionIdType == 0 || subscriptionIdType == 1) {
DiameterUser user = null;
if ((user = users.get(subscriptionIdData)) == null) {
// Not a valid user. Reject it with DIAMETER_END_USER_NOT_FOUND.
ans = assa.createAccountingAnswer(acr);
ans.setResultCode(5241);
// Subscription ID
DiameterAvp subscriptionIdTypeAvp = avpFactory.createAvp(193, 555, subscriptionIdType);
DiameterAvp subscriptionIdDataAvp = avpFactory.createAvp(193, 554, subscriptionIdData);
avps.add(avpFactory.createAvp(193, 553, new DiameterAvp[] { subscriptionIdTypeAvp, subscriptionIdDataAvp }));
} else if (requestedAction == 0 && user.balance < chargingValue) {
logger.info("Received Direct Debit Request:");
logger.info("User ID " + subscriptionIdData + " (" + user.name + ")");
logger.info("Current Balance: " + user.balance);
logger.info("Charging Value: " + chargingValue);
// Not able to provide the service. not enough balance.
ans = assa.createAccountingAnswer(acr);
ans.setResultCode(4241);
// Subscription ID
DiameterAvp subscriptionIdTypeAvp = avpFactory.createAvp(193, 555, subscriptionIdType);
DiameterAvp subscriptionIdDataAvp = avpFactory.createAvp(193, 554, subscriptionIdData);
avps.add(avpFactory.createAvp(193, 553, new DiameterAvp[] { subscriptionIdTypeAvp, subscriptionIdDataAvp }));
} else {
boolean isError = false;
// Refund Account?
if (requestedAction == 1) {
logger.info("Received Refund Account Request:");
logger.info("User ID " + subscriptionIdData + " (" + user.name + ")");
logger.info("Old Balance: " + user.balance);
user.balance += chargingValue;
logger.info("New Balance: " + user.balance);
} else if (requestedAction == 0) {
logger.info("Received Direct Debit Request:");
logger.info("User ID " + subscriptionIdData + " (" + user.name + ")");
logger.info("Old Balance: " + user.balance);
user.balance -= chargingValue;
logger.info("New Balance: " + user.balance);
} else {
logger.warn("Unknown requested action (" + requestedAction + ")");
DiameterAvp failedAvp = avpFactory.createAvp(0, 279, new DiameterAvp[] { requestedActionAvp });
ans = assa.createAccountingAnswer(acr);
ans.setResultCode(ResultCode.INVALID_AVP_VALUE);
avps.add(failedAvp);
isError = true;
}
if (!isError) {
// Subscription ID
DiameterAvp subscriptionIdTypeAvp = avpFactory.createAvp(193, 555, subscriptionIdType);
DiameterAvp subscriptionIdDataAvp = avpFactory.createAvp(193, 554, subscriptionIdData);
avps.add(avpFactory.createAvp(193, 553, new DiameterAvp[] { subscriptionIdTypeAvp, subscriptionIdDataAvp }));
// Granted Service Unit
DiameterAvp unitTypeAvp = avpFactory.createAvp(193, 611, unitType);
DiameterAvp valueDigitsAvp = avpFactory.createAvp(193, 617, valueDigits);
DiameterAvp unitValueAvp = avpFactory.createAvp(193, 612, new DiameterAvp[] { valueDigitsAvp });
avps.add(avpFactory.createAvp(193, 602, new DiameterAvp[] { unitTypeAvp, unitValueAvp }));
// Cost Information
DiameterAvp costAvp = avpFactory.createAvp(193, 603, chargingValue);
DiameterAvp currencyCodeAvp = avpFactory.createAvp(193, 544, 978);
avps.add(avpFactory.createAvp(193, 604, new DiameterAvp[] { costAvp, currencyCodeAvp }));
}
}
}
DiameterAvp[] avpArray = new DiameterAvp[avps.size()];
avpArray = avps.toArray(avpArray);
ans.setExtensionAvps(avpArray);
logger.info("Sending Accounting-Answer [" + ans + "]");
assa.sendAccountingAnswer(ans);
logger.info("Accounting-Answer sent.");
}
}
} catch (Exception e) {
logger.error("", e);
}
long end = System.currentTimeMillis();
logger.info("Accounting-Request proccessed. [" + (end - start) + "ms]");
}
Aggregations