Search in sources :

Example 1 with UserSessionInfo

use of org.mobicents.charging.server.data.UserSessionInfo in project charging-server by RestComm.

the class DiameterChargingServerSbb method onCreditControlRequest.

public void onCreditControlRequest(RoCreditControlRequest ccr, ActivityContextInterface aci) {
    String serviceContextId = "Some-Service-Context-Id";
    String sessionId = ccr.getSessionId();
    UserSessionInfo sessionInfo = getSessionInfo();
    if (sessionInfo == null) {
        sessionInfo = new UserSessionInfo();
        sessionInfo.setSessionStartTime(System.currentTimeMillis());
    }
    sessionInfo.setCcr(ccr);
    sessionInfo.setSessionId(sessionId);
    setSessionInfo(sessionInfo);
    String reqType = ccr.getCcRequestType().toString();
    long reqNumber = ccr.getCcRequestNumber();
    sidString = "SID<" + limitString(sessionId, 9, 9, "..") + "/" + reqType.substring(0, 3) + "#" + reqNumber + ">";
    if (tracer.isInfoEnabled()) {
        tracer.info("[<<] " + sidString + " Received Credit-Control-Request [" + reqType + "]");
        if (tracer.isFineEnabled()) {
            tracer.fine(ccr.toString());
        }
    }
    // Some common ops. may be moved to proper places to avoid unnecessary ops
    RoServerSessionActivity ccServerActivity = (RoServerSessionActivity) aci.getActivity();
    SubscriptionIdType endUserType = null;
    String endUserId = null;
    // Get the Subscription-Id and it's Type .. for now we only care for first, still we log all
    SubscriptionIdAvp[] subscriptionIds = ccr.getSubscriptionIds();
    RoCreditControlAnswer cca = null;
    if (subscriptionIds != null && subscriptionIds.length > 0) {
        endUserType = subscriptionIds[0].getSubscriptionIdType();
        endUserId = subscriptionIds[0].getSubscriptionIdData();
        if (tracer.isFineEnabled()) {
            String subsIdsStr = "";
            for (SubscriptionIdAvp subscriptionId : subscriptionIds) {
                subsIdsStr += subscriptionId.getSubscriptionIdType() + "=" + subscriptionId.getSubscriptionIdData() + " ";
            }
            tracer.fine("[--] " + sidString + " Received CCR has Subcription-Id(s): " + subsIdsStr.substring(0, subsIdsStr.length() - 1));
        }
    } else {
        tracer.severe("[xx] " + sidString + " Subscription-Id AVP missing in CCR. Rejecting CCR.");
        cca = createCCA(ccServerActivity, ccr, null, DiameterResultCode.DIAMETER_MISSING_AVP);
        sendCCA(cca, aci, true);
        return;
    }
    if (endUserId == null) {
        tracer.severe("[xx] " + sidString + " Subscription-Id AVP is present but could not read it's data. Rejecting CCR.");
        cca = createCCA(ccServerActivity, ccr, null, DiameterResultCode.DIAMETER_MISSING_AVP);
        sendCCA(cca, aci, true);
        return;
    }
    // Retrieve child SBBs
    try {
        accountBalanceManagement = getAccountManager();
        ratingEngineManagement = getRatingEngineManager();
        cdrGenerator = getCDRGenerator();
    } catch (Exception e) {
        // TODO: By configuration it should be possible to proceed
        tracer.severe("[xx] " + sidString + " Unable to retrieve Account & Balance Management or Rating Child SBB. Unable to continue.", e);
        cca = createCCA(ccServerActivity, ccr, new ArrayList<CreditControlInfo>(), DiameterResultCode.DIAMETER_UNABLE_TO_COMPLY);
        sendCCA(cca, aci, true);
    }
    switch(ccr.getCcRequestType()) {
        // INITIAL_REQUEST 1
        case INITIAL_REQUEST:
        // UPDATE_REQUEST 2
        case UPDATE_REQUEST:
            try {
                // retrieve service information from AVPs
                serviceContextId = ccr.getServiceContextId();
                if (serviceContextId == null) {
                    tracer.severe("[xx] " + sidString + " Service-Context-Id AVP missing in CCR. Rejecting CCR.");
                    // TODO: include missing avp - its a "SHOULD"
                    cca = createCCA(ccServerActivity, ccr, null, DiameterResultCode.DIAMETER_MISSING_AVP);
                    sendCCA(cca, aci, true);
                } else {
                    if (serviceContextId.equals("")) {
                        tracer.severe("[xx] " + sidString + " Service-Context-Id AVP is empty in CCR. Rejecting CCR.");
                        cca = createCCA(ccServerActivity, ccr, null, DiameterResultCode.DIAMETER_INVALID_AVP_VALUE);
                        sendCCA(cca, aci, true);
                    }
                }
                // TODO: For Ro, support Service-Information AVP
                List<CreditControlInfo> reservations = new ArrayList<CreditControlInfo>();
                long resultCode = DiameterResultCode.DIAMETER_SUCCESS;
                MultipleServicesCreditControlAvp[] multipleServicesCreditControlAvps = ccr.getMultipleServicesCreditControls();
                if (multipleServicesCreditControlAvps != null && tracer.isFineEnabled()) {
                    tracer.fine("[--] " + sidString + " Received CCR has Multiple-Services-Credit-Control AVP with length = " + multipleServicesCreditControlAvps.length);
                }
                // If there's no MSCC AVP, we'll create one, just to go inside the for and have it processed..
                if (multipleServicesCreditControlAvps.length == 0) {
                    MultipleServicesCreditControlAvp fakeMSCC = avpFactory.createMultipleServicesCreditControl();
                    fakeMSCC.setServiceIdentifier(0);
                    RequestedServiceUnitAvp rsu = avpFactory.createRequestedServiceUnit();
                    rsu.setCreditControlTotalOctets(0);
                    fakeMSCC.setRequestedServiceUnit(rsu);
                    multipleServicesCreditControlAvps = new MultipleServicesCreditControlAvp[] { fakeMSCC };
                }
                // single rating group.
                for (MultipleServicesCreditControlAvp mscc : multipleServicesCreditControlAvps) {
                    // The Service-Identifier and the Rating-Group AVPs are used to associate the granted units to a
                    // given service or rating group.  If both the Service-Identifier and the Rating-Group AVPs are
                    // included, the target of the service units is always the service(s) indicated by the value of the
                    // Service-Identifier AVP(s).  If only the Rating-Group-Id AVP is present, the Multiple-Services-
                    // -Credit-Control AVP relates to all the services that belong to the specified rating group.
                    long ratingGroup = mscc.getRatingGroup();
                    long[] serviceIds = mscc.getServiceIdentifiers();
                    // The Requested-Service-Unit AVP MAY contain the amount of requested service units [...]. It MUST
                    // be present in the initial interrogation and within the intermediate interrogations in which new
                    // quota is requested.  If the credit-control client does not include the Requested-Service-Unit AVP
                    // in a request command, because for instance, it has determined that the end-user terminated the
                    // service, the server MUST debit the used amount from the user's account but MUST NOT return a new
                    // quota in the corresponding answer.
                    RequestedServiceUnitAvp rsu = mscc.getRequestedServiceUnit();
                    ArrayList<CreditControlUnit> ccUnits = getRequestedUnits(ccr, rsu, serviceIds);
                    // if its UPDATE, lets first update data
                    if (ccr.getCcRequestType() == CcRequestType.UPDATE_REQUEST) {
                        // update used units for each CC-Type.
                        UsedServiceUnitAvp[] usedUnitsAvps = mscc.getUsedServiceUnits();
                        sessionInfo = getSessionInfo();
                        CreditControlInfo reservedInfo = sessionInfo.getReservations().get(sessionInfo.getReservations().size() - 1);
                        ArrayList<CreditControlUnit> usedCCUnits = collectUsedUnits(usedUnitsAvps, reservedInfo.getCcUnits());
                        // Merge Requested with Used/Reserved CC Units into a single CCUnits
                        ccUnits.addAll(usedCCUnits);
                        // Call ABMF with this Credit Control Info
                        CreditControlInfo ccInfo = buildCCInfo(ccr, endUserId, endUserType, ccUnits);
                        accountBalanceManagement.updateRequest(ccInfo);
                    } else {
                        // Initial Request
                        // Call ABMF with this Credit Control Info
                        CreditControlInfo ccInfo = buildCCInfo(ccr, endUserId, endUserType, ccUnits);
                        accountBalanceManagement.initialRequest(ccInfo);
                    }
                    // Store Credit Control Info in CMP
                    sessionInfo = getSessionInfo();
                    sessionInfo.setCcr(ccr);
                    sessionInfo.setServiceIds(serviceIds);
                    sessionInfo.setEndUserId(endUserId);
                    sessionInfo.setEndUserType(endUserType);
                    setSessionInfo(sessionInfo);
                    // we'll continue @ resumeOnCreditControlRequest(..)
                    return;
                }
                if (reservations.size() > 0) {
                    cca = createCCA(ccServerActivity, ccr, reservations, resultCode);
                } else {
                    cca = createCCA(ccServerActivity, ccr, null, DiameterResultCode.DIAMETER_MISSING_AVP);
                }
                sendCCA(cca, aci, false);
            } catch (Exception e) {
                tracer.severe("[xx] " + sidString + " Failure processing Credit-Control-Request [" + (ccr.getCcRequestType() == CcRequestType.INITIAL_REQUEST ? "INITIAL" : "UPDATE") + "]", e);
            }
            break;
        // TERMINATION_REQUEST 3
        case TERMINATION_REQUEST:
            try {
                if (tracer.isInfoEnabled()) {
                    tracer.info("[>>] " + sidString + " '" + endUserId + "' requested service termination for '" + serviceContextId + "'.");
                }
                for (MultipleServicesCreditControlAvp mscc : ccr.getMultipleServicesCreditControls()) {
                    UsedServiceUnitAvp[] usedUnitsAvps = mscc.getUsedServiceUnits();
                    sessionInfo = getSessionInfo();
                    CreditControlInfo reservedInfo = sessionInfo.getReservations().get(sessionInfo.getReservations().size() - 1);
                    ArrayList<CreditControlUnit> ccUnits = collectUsedUnits(usedUnitsAvps, reservedInfo.getCcUnits());
                    // Call ABMF with this Credit Control Info
                    CreditControlInfo ccInfo = buildCCInfo(ccr, endUserId, endUserType, ccUnits);
                    accountBalanceManagement.terminateRequest(ccInfo);
                    // we'll continue @ resumeOnCreditControlRequest(..)
                    return;
                }
                // 8.7.  Cost-Information AVP
                // 
                // The Cost-Information AVP (AVP Code 423) is of type Grouped, and it is
                // used to return the cost information of a service, which the credit-
                // control client can transfer transparently to the end user.  The
                // included Unit-Value AVP contains the cost estimate (always type of
                // money) of the service, in the case of price enquiry, or the
                // accumulated cost estimation, in the case of credit-control session.
                // 
                // The Currency-Code specifies in which currency the cost was given.
                // The Cost-Unit specifies the unit when the service cost is a cost per
                // unit (e.g., cost for the service is $1 per minute).
                // 
                // When the Requested-Action AVP with value PRICE_ENQUIRY is included in
                // the Credit-Control-Request command, the Cost-Information AVP sent in
                // the succeeding Credit-Control-Answer command contains the cost
                // estimation of the requested service, without any reservation being
                // made.
                // 
                // The Cost-Information AVP included in the Credit-Control-Answer
                // command with the CC-Request-Type set to UPDATE_REQUEST contains the
                // accumulated cost estimation for the session, without taking any
                // credit reservation into account.
                // 
                // The Cost-Information AVP included in the Credit-Control-Answer
                // command with the CC-Request-Type set to EVENT_REQUEST or
                // TERMINATION_REQUEST contains the estimated total cost for the
                // requested service.
                // 
                // It is defined as follows (per the grouped-avp-def of
                // RFC 3588 [DIAMBASE]):
                // 
                // Cost-Information ::= < AVP Header: 423 >
                // { Unit-Value }
                // { Currency-Code }
                // [ Cost-Unit ]
                // 7.2.133 Remaining-Balance AVP
                // 
                // The Remaining-Balance AVP (AVPcode 2021) is of type Grouped and
                // provides information about the remaining account balance of the
                // subscriber.
                // 
                // It has the following ABNF grammar:
                // Remaining-Balance :: =  < AVP Header: 2021 >
                // { Unit-Value }
                // { Currency-Code }
                // We use no money notion ... maybe later.
                // AvpSet costInformation = ccaAvps.addGroupedAvp(423);
                // Answer with DIAMETER_SUCCESS, since "4) The default action for failed operations should be to terminate the data session"
                // its terminated, we cant do much here...
                cca = createCCA(ccServerActivity, ccr, null, DiameterResultCode.DIAMETER_SUCCESS);
                sendCCA(cca, aci, true);
            } catch (Exception e) {
                tracer.severe("[xx] " + sidString + " Failure processing Credit-Control-Request [TERMINATION]", e);
            }
            break;
        // EVENT_REQUEST 4
        case EVENT_REQUEST:
            try {
                RequestedActionType reqAction = ccr.getRequestedAction();
                if (tracer.isInfoEnabled()) {
                    tracer.info("[<<] " + sidString + " Received Credit-Control-Request [EVENT] with Requested-Action [" + reqAction + "]");
                    if (tracer.isFineEnabled()) {
                        tracer.fine(ccr.toString());
                    }
                }
                if (reqAction == null) {
                    tracer.severe("[xx] " + sidString + " Unable to retrieve Requested-Action AVP. Replying with MISSING_AVP.");
                    createCCA(ccServerActivity, ccr, new ArrayList<CreditControlInfo>(), DiameterResultCode.DIAMETER_MISSING_AVP);
                    sendCCA(cca, aci, true);
                } else if (reqAction == RequestedActionType.DIRECT_DEBITING) {
                    for (MultipleServicesCreditControlAvp mscc : ccr.getMultipleServicesCreditControls()) {
                        RequestedServiceUnitAvp rsu = mscc.getRequestedServiceUnit();
                        long[] serviceIds = mscc.getServiceIdentifiers();
                        ArrayList<CreditControlUnit> ccUnits = getRequestedUnits(ccr, rsu, serviceIds);
                        // Call ABMF with this Credit Control Info
                        CreditControlInfo ccInfo = buildCCInfo(ccr, endUserId, endUserType, ccUnits);
                        accountBalanceManagement.eventRequest(ccInfo);
                        // Store Credit Control Info in CMP
                        sessionInfo = getSessionInfo();
                        sessionInfo.setCcr(ccr);
                        sessionInfo.setEndUserId(endUserId);
                        // sessionInfo.getReservations().add(ccInfo);
                        setSessionInfo(sessionInfo);
                        if (tracer.isInfoEnabled()) {
                            tracer.info(sessionInfo.toString());
                        }
                        // we'll continue @ resumeOnCreditControlRequest(..)
                        return;
                    }
                } else {
                    tracer.severe("[xx] " + sidString + " Unsupported Requested-Action AVP (" + reqAction + "). Replying with DIAMETER_UNABLE_TO_COMPLY.");
                    createCCA(ccServerActivity, ccr, new ArrayList<CreditControlInfo>(), DiameterResultCode.DIAMETER_UNABLE_TO_COMPLY);
                    sendCCA(cca, aci, true);
                }
            } catch (Exception e) {
                tracer.severe("[xx] " + sidString + " Failure processing Credit-Control-Request [EVENT]", e);
            }
            break;
        default:
            tracer.warning("[xx] " + sidString + " Unknown request type found!");
            break;
    }
}
Also used : MultipleServicesCreditControlAvp(net.java.slee.resource.diameter.cca.events.avp.MultipleServicesCreditControlAvp) RoServerSessionActivity(net.java.slee.resource.diameter.ro.RoServerSessionActivity) ArrayList(java.util.ArrayList) RequestedActionType(net.java.slee.resource.diameter.cca.events.avp.RequestedActionType) SubscriptionIdAvp(net.java.slee.resource.diameter.cca.events.avp.SubscriptionIdAvp) CreateException(javax.slee.CreateException) IOException(java.io.IOException) SLEEException(javax.slee.SLEEException) UsedServiceUnitAvp(net.java.slee.resource.diameter.cca.events.avp.UsedServiceUnitAvp) UserSessionInfo(org.mobicents.charging.server.data.UserSessionInfo) CreditControlUnit(org.mobicents.charging.server.account.CreditControlUnit) CreditControlInfo(org.mobicents.charging.server.account.CreditControlInfo) SubscriptionIdType(net.java.slee.resource.diameter.cca.events.avp.SubscriptionIdType) RoCreditControlAnswer(net.java.slee.resource.diameter.ro.events.RoCreditControlAnswer) RequestedServiceUnitAvp(net.java.slee.resource.diameter.cca.events.avp.RequestedServiceUnitAvp)

Example 2 with UserSessionInfo

use of org.mobicents.charging.server.data.UserSessionInfo in project charging-server by RestComm.

the class DiameterChargingServerSbb method resumeOnCreditControlRequest.

// private String storedEndUserId;
// private long storedRequestedUnits;
// private long[] storedServiceIds;
// private ArrayList<UnitReservation> storedReservations = new ArrayList<UnitReservation>();
@Override
public void resumeOnCreditControlRequest(CreditControlInfo ccInfo) {
    UserSessionInfo sessionInfo = getSessionInfo();
    RoCreditControlRequest storedCCR = sessionInfo.getCcr();
    if (tracer.isInfoEnabled()) {
        tracer.info("[<<] " + sidString + " Resuming Handling of Credit-Control-Request [" + storedCCR.getCcRequestType().toString() + "]");
    }
    if (tracer.isFineEnabled()) {
        tracer.fine("[<<] \" + sidString + \" " + ccInfo);
    }
    sessionInfo.getReservations().add(ccInfo);
    setSessionInfo(sessionInfo);
    long resultCode = DiameterResultCode.DIAMETER_SUCCESS;
    if (ccInfo.isSuccessful()) {
        if (tracer.isInfoEnabled()) {
            tracer.info("[>>] " + sidString + " '" + sessionInfo.getEndUserId() + "' GRANTED for '" + Arrays.toString(sessionInfo.getServiceIds()) + "'.");
        }
    } else {
        if (tracer.isInfoEnabled()) {
            tracer.info("[>>] " + sidString + " '" + sessionInfo.getEndUserId() + "' DENIED for '" + Arrays.toString(sessionInfo.getServiceIds()) + "'.");
        }
        // If we can't determine error, say UNABLE_TO_COMPLY
        resultCode = ccInfo.getErrorCodeType() != null ? getResultCode(ccInfo.getErrorCodeType()) : DiameterResultCode.DIAMETER_UNABLE_TO_COMPLY;
    }
    try {
        ActivityContextInterface[] acis = this.sbbContextExt.getActivities();
        ActivityContextInterface aci = null;
        RoServerSessionActivity activity = null;
        for (ActivityContextInterface curAci : acis) {
            if (curAci.getActivity() instanceof RoServerSessionActivity) {
                aci = curAci;
                activity = (RoServerSessionActivity) curAci.getActivity();
                break;
            }
        }
        RoCreditControlAnswer cca = sessionInfo.getReservations().size() > 0 ? createCCA(activity, storedCCR, sessionInfo.getReservations(), resultCode) : createCCA(activity, storedCCR, null, DiameterResultCode.DIAMETER_MISSING_AVP);
        sendCCA(cca, aci, storedCCR.getCcRequestType() == CcRequestType.TERMINATION_REQUEST || storedCCR.getCcRequestType() == CcRequestType.EVENT_REQUEST);
        // Output the user session details.
        if (tracer.isInfoEnabled()) {
            tracer.info("[--] " + sidString + " CCA successfully sent.");
        }
        if (tracer.isFineEnabled()) {
            tracer.fine("[--] " + sidString + "Dumping session info...\n" + sessionInfo);
        }
    } catch (Exception e) {
        tracer.severe("[xx] " + sidString + " Unable to send Credit-Control-Answer.", e);
    }
    if (generateCDR && cdrGenerator != null && storedCCR.getCcRequestType() == CcRequestType.TERMINATION_REQUEST) {
        if (tracer.isInfoEnabled()) {
            tracer.info("[><] " + sidString + " Generating CDR.");
        }
        try {
            cdrGenerator.writeCDR(sessionInfo);
        } catch (Exception e) {
            tracer.severe("[xx] " + sidString + " Unable to generate CDR", e);
        }
    }
}
Also used : RoServerSessionActivity(net.java.slee.resource.diameter.ro.RoServerSessionActivity) ActivityContextInterface(javax.slee.ActivityContextInterface) RoCreditControlAnswer(net.java.slee.resource.diameter.ro.events.RoCreditControlAnswer) RoCreditControlRequest(net.java.slee.resource.diameter.ro.events.RoCreditControlRequest) CreateException(javax.slee.CreateException) IOException(java.io.IOException) SLEEException(javax.slee.SLEEException) UserSessionInfo(org.mobicents.charging.server.data.UserSessionInfo)

Aggregations

IOException (java.io.IOException)2 CreateException (javax.slee.CreateException)2 SLEEException (javax.slee.SLEEException)2 RoServerSessionActivity (net.java.slee.resource.diameter.ro.RoServerSessionActivity)2 RoCreditControlAnswer (net.java.slee.resource.diameter.ro.events.RoCreditControlAnswer)2 UserSessionInfo (org.mobicents.charging.server.data.UserSessionInfo)2 ArrayList (java.util.ArrayList)1 ActivityContextInterface (javax.slee.ActivityContextInterface)1 MultipleServicesCreditControlAvp (net.java.slee.resource.diameter.cca.events.avp.MultipleServicesCreditControlAvp)1 RequestedActionType (net.java.slee.resource.diameter.cca.events.avp.RequestedActionType)1 RequestedServiceUnitAvp (net.java.slee.resource.diameter.cca.events.avp.RequestedServiceUnitAvp)1 SubscriptionIdAvp (net.java.slee.resource.diameter.cca.events.avp.SubscriptionIdAvp)1 SubscriptionIdType (net.java.slee.resource.diameter.cca.events.avp.SubscriptionIdType)1 UsedServiceUnitAvp (net.java.slee.resource.diameter.cca.events.avp.UsedServiceUnitAvp)1 RoCreditControlRequest (net.java.slee.resource.diameter.ro.events.RoCreditControlRequest)1 CreditControlInfo (org.mobicents.charging.server.account.CreditControlInfo)1 CreditControlUnit (org.mobicents.charging.server.account.CreditControlUnit)1