use of net.java.slee.resource.diameter.base.events.AccountingAnswer in project jain-slee.diameter by RestComm.
the class BaseFactoriesTest method isAnswerACA.
@Test
public void isAnswerACA() throws Exception {
AccountingAnswer aca = messageFactory.createAccountingAnswer(messageFactory.createAccountingRequest());
assertFalse("Request Flag in Accounting-Answer is set.", aca.getHeader().isRequest());
}
use of net.java.slee.resource.diameter.base.events.AccountingAnswer in project jain-slee.diameter by RestComm.
the class BaseFactoriesTest method hasTFlagSetACA.
@Test
public void hasTFlagSetACA() throws Exception {
AccountingRequest acr = messageFactory.createAccountingRequest();
((DiameterMessageImpl) acr).getGenericData().setReTransmitted(true);
assertTrue("The 'T' flag should be set in Accounting-Request", acr.getHeader().isPotentiallyRetransmitted());
AccountingAnswer aca = messageFactory.createAccountingAnswer(acr);
assertFalse("The 'T' flag should not be set in Accounting-Answer", aca.getHeader().isPotentiallyRetransmitted());
}
use of net.java.slee.resource.diameter.base.events.AccountingAnswer 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]");
}
use of net.java.slee.resource.diameter.base.events.AccountingAnswer in project jain-slee.diameter by RestComm.
the class BaseFactoriesTest method hasDestinationHostACA.
@Test
public void hasDestinationHostACA() throws Exception {
AccountingAnswer aca = messageFactory.createAccountingAnswer(messageFactory.createAccountingRequest());
assertNull("The Destination-Host and Destination-Realm AVPs MUST NOT be present in the answer message. [RFC3588/6.2]", aca.getDestinationHost());
}
use of net.java.slee.resource.diameter.base.events.AccountingAnswer in project jain-slee.diameter by RestComm.
the class BaseFactoriesTest method hasDestinationRealmACA.
@Test
public void hasDestinationRealmACA() throws Exception {
AccountingAnswer aca = messageFactory.createAccountingAnswer(messageFactory.createAccountingRequest());
assertNull("The Destination-Host and Destination-Realm AVPs MUST NOT be present in the answer message. [RFC3588/6.2]", aca.getDestinationRealm());
}
Aggregations