Search in sources :

Example 6 with GroupedAvp

use of net.java.slee.resource.diameter.base.events.avp.GroupedAvp in project jain-slee.diameter by RestComm.

the class ShClientMessageFactoryImpl method addAvp.

protected void addAvp(DiameterAvp avp, AvpSet set) {
    if (avp instanceof GroupedAvp) {
        AvpSet avpSet = set.addGroupedAvp(avp.getCode(), avp.getVendorId(), avp.getMandatoryRule() == 1, avp.getProtectedRule() == 1);
        DiameterAvp[] groupedAVPs = ((GroupedAvp) avp).getExtensionAvps();
        for (DiameterAvp avpFromGroup : groupedAVPs) {
            addAvp(avpFromGroup, avpSet);
        }
    } else if (avp != null)
        set.addAvp(avp.getCode(), avp.byteArrayValue(), avp.getVendorId(), avp.getMandatoryRule() == 1, avp.getProtectedRule() == 1);
}
Also used : GroupedAvp(net.java.slee.resource.diameter.base.events.avp.GroupedAvp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) AvpSet(org.jdiameter.api.AvpSet)

Example 7 with GroupedAvp

use of net.java.slee.resource.diameter.base.events.avp.GroupedAvp 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]");
}
Also used : ArrayList(java.util.ArrayList) DiameterMessage(net.java.slee.resource.diameter.base.events.DiameterMessage) AccountingServerSessionActivity(net.java.slee.resource.diameter.base.AccountingServerSessionActivity) AccountingAnswer(net.java.slee.resource.diameter.base.events.AccountingAnswer) GroupedAvp(net.java.slee.resource.diameter.base.events.avp.GroupedAvp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp)

Example 8 with GroupedAvp

use of net.java.slee.resource.diameter.base.events.avp.GroupedAvp in project jain-slee.diameter by RestComm.

the class RoMessageFactoryImpl method addAvp.

protected void addAvp(DiameterAvp avp, AvpSet set) {
    // proper function?
    if (avp instanceof GroupedAvp) {
        AvpSet avpSet = set.addGroupedAvp(avp.getCode(), avp.getVendorId(), avp.getMandatoryRule() == 1, avp.getProtectedRule() == 1);
        DiameterAvp[] groupedAVPs = ((GroupedAvp) avp).getExtensionAvps();
        for (DiameterAvp avpFromGroup : groupedAVPs) {
            addAvp(avpFromGroup, avpSet);
        }
    } else if (avp != null) {
        set.addAvp(avp.getCode(), avp.byteArrayValue(), avp.getVendorId(), avp.getMandatoryRule() == 1, avp.getProtectedRule() == 1);
    }
}
Also used : GroupedAvp(net.java.slee.resource.diameter.base.events.avp.GroupedAvp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) AvpSet(org.jdiameter.api.AvpSet)

Example 9 with GroupedAvp

use of net.java.slee.resource.diameter.base.events.avp.GroupedAvp in project jain-slee.diameter by RestComm.

the class RxMessageFactoryImpl method addAvp.

protected void addAvp(DiameterAvp avp, AvpSet set) {
    if (avp instanceof GroupedAvp) {
        AvpSet avpSet = set.addGroupedAvp(avp.getCode(), avp.getVendorId(), avp.getMandatoryRule() != DiameterAvp.FLAG_RULE_MUSTNOT, avp.getProtectedRule() == DiameterAvp.FLAG_RULE_MUST);
        DiameterAvp[] groupedAVPs = ((GroupedAvp) avp).getExtensionAvps();
        for (DiameterAvp avpFromGroup : groupedAVPs) {
            addAvp(avpFromGroup, avpSet);
        }
    } else if (avp != null) {
        set.addAvp(avp.getCode(), avp.byteArrayValue(), avp.getVendorId(), avp.getMandatoryRule() != DiameterAvp.FLAG_RULE_MUSTNOT, avp.getProtectedRule() == DiameterAvp.FLAG_RULE_MUST);
    }
}
Also used : GroupedAvp(net.java.slee.resource.diameter.base.events.avp.GroupedAvp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) AvpSet(org.jdiameter.api.AvpSet)

Example 10 with GroupedAvp

use of net.java.slee.resource.diameter.base.events.avp.GroupedAvp in project jain-slee.diameter by RestComm.

the class CreditControlMessageFactoryImpl method addAvp.

protected void addAvp(DiameterAvp avp, AvpSet set) {
    // proper function?
    if (avp instanceof GroupedAvp) {
        AvpSet avpSet = set.addGroupedAvp(avp.getCode(), avp.getVendorId(), avp.getMandatoryRule() == 1, avp.getProtectedRule() == 1);
        DiameterAvp[] groupedAVPs = ((GroupedAvp) avp).getExtensionAvps();
        for (DiameterAvp avpFromGroup : groupedAVPs) {
            addAvp(avpFromGroup, avpSet);
        }
    } else if (avp != null) {
        set.addAvp(avp.getCode(), avp.byteArrayValue(), avp.getVendorId(), avp.getMandatoryRule() == 1, avp.getProtectedRule() == 1);
    }
}
Also used : GroupedAvp(net.java.slee.resource.diameter.base.events.avp.GroupedAvp) DiameterAvp(net.java.slee.resource.diameter.base.events.avp.DiameterAvp) AvpSet(org.jdiameter.api.AvpSet)

Aggregations

DiameterAvp (net.java.slee.resource.diameter.base.events.avp.DiameterAvp)14 GroupedAvp (net.java.slee.resource.diameter.base.events.avp.GroupedAvp)14 AvpSet (org.jdiameter.api.AvpSet)9 ApplicationId (org.jdiameter.api.ApplicationId)2 ArrayList (java.util.ArrayList)1 AccountingServerSessionActivity (net.java.slee.resource.diameter.base.AccountingServerSessionActivity)1 AccountingAnswer (net.java.slee.resource.diameter.base.events.AccountingAnswer)1 DiameterMessage (net.java.slee.resource.diameter.base.events.DiameterMessage)1