Search in sources :

Example 1 with RatingInfo

use of org.mobicents.charging.server.ratingengine.RatingInfo in project charging-server by RestComm.

the class DiameterChargingServerSbb method getRateForService.

// --------- Call to decentralized rating engine ---------------------
@SuppressWarnings({ "rawtypes", "unchecked" })
private double getRateForService(RoCreditControlRequest ccr, long serviceId, long unitTypeId, long requestedUnits) {
    // Let's make some variables available to be sent to the rating engine
    HashMap params = new HashMap();
    params.put("ChargingServerHost", ccr.getDestinationHost());
    params.put("SessionId", ccr.getSessionId());
    params.put("RequestType", ccr.getCcRequestType().toString());
    SubscriptionIdAvp[] subscriptionIds = ccr.getSubscriptionIds();
    boolean hasSubscriptionIds = (subscriptionIds != null && subscriptionIds.length > 0);
    params.put("SubscriptionIdType", hasSubscriptionIds ? subscriptionIds[0].getSubscriptionIdType().getValue() : -1);
    params.put("SubscriptionIdData", hasSubscriptionIds ? subscriptionIds[0].getSubscriptionIdData() : null);
    // params.put("UnitId", getUnitId((int)serviceId));
    params.put("UnitTypeId", unitTypeId);
    params.put("UnitValue", requestedUnits);
    params.put("ServiceId", serviceId);
    params.put("BeginTime", ccr.getEventTimestamp().getTime());
    params.put("ActualTime", System.currentTimeMillis());
    // TODO: Extract DestinationId AVP from the CCR if available.
    params.put("DestinationIdType", "?");
    params.put("DestinationIdData", "?");
    RatingInfo ratingInfo = ratingEngineManagement.getRateForService(params);
    // Retrieve the rating information [and optionally the unit type] from ratingInfo.
    int responseCode = ratingInfo.getResponseCode();
    double rate = 1.0;
    if (responseCode == 0) {
        // Rate obtained successfully from Rating Engine, let's use that.
        rate = ratingInfo.getRate();
    } else {
        // TODO: if rate was not found or error occurred while determining rate, what to do? Block traffic (certain types of traffic? for certain profiles? Allow for Free?)
        tracer.warning("[xx] " + sidString + " Unexpected response code '" + responseCode + "' received from Rating Engine.");
    }
    // allow traffic for free :(
    return rate;
}
Also used : HashMap(java.util.HashMap) RatingInfo(org.mobicents.charging.server.ratingengine.RatingInfo) SubscriptionIdAvp(net.java.slee.resource.diameter.cca.events.avp.SubscriptionIdAvp)

Example 2 with RatingInfo

use of org.mobicents.charging.server.ratingengine.RatingInfo in project charging-server by RestComm.

the class HTTPClientSbb method onResponseEvent.

// Event handler methods
public void onResponseEvent(ResponseEvent event, ActivityContextInterface aci) {
    HttpResponse response = event.getHttpResponse();
    if (tracer.isInfoEnabled()) {
        tracer.info("[<<] Received HTTP Response. Status Code = " + response.getStatusLine().getStatusCode());
        if (tracer.isFineEnabled()) {
            try {
                tracer.fine("[<<] Received HTTP Response. Response Body = [" + EntityUtils.toString(response.getEntity()) + "]");
            } catch (Exception e) {
                tracer.severe("[xx] Failed reading response body", e);
            }
        }
    }
    // end http activity
    ((HttpClientActivity) aci.getActivity()).endActivity();
    // call back parent
    HashMap params = (HashMap) event.getRequestApplicationData();
    RatingInfo ratInfo = buildRatingInfo(response, params);
    final DiameterChargingServer parent = (DiameterChargingServer) sbbContext.getSbbLocalObject().getParent();
    parent.getRateForServiceResult(ratInfo);
}
Also used : HashMap(java.util.HashMap) RatingInfo(org.mobicents.charging.server.ratingengine.RatingInfo) HttpResponse(org.apache.http.HttpResponse) HttpClientActivity(net.java.client.slee.resource.http.HttpClientActivity) StartActivityException(javax.slee.resource.StartActivityException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DiameterChargingServer(org.mobicents.charging.server.DiameterChargingServer)

Example 3 with RatingInfo

use of org.mobicents.charging.server.ratingengine.RatingInfo in project charging-server by RestComm.

the class HTTPClientSbb method buildRatingInfo.

private RatingInfo buildRatingInfo(HttpResponse response, HashMap params) {
    String responseBody = "";
    String diameterSessionId = (String) params.get("SessionId");
    try {
        responseBody = EntityUtils.toString(response.getEntity());
    } catch (Exception e) {
        tracer.severe("[xx] Failed reading HTTP Rating Engine response body.", e);
        return new RatingInfo(-1, diameterSessionId);
    }
    // tracer.info("Response Body = " + responseBody);
    // The response body is an XML payload. Let's parse it using DOM.
    int responseCode = -1;
    String sessionId = diameterSessionId;
    long actualTime = 0;
    long currentTime = 0;
    double rate = 0.0D;
    String rateDescription = "";
    String ratePromo = "";
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(responseBody));
        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("response");
        Element element = (Element) nodes.item(0);
        responseCode = Integer.parseInt(getCharacterDataFromElement((Element) element.getElementsByTagName("responseCode").item(0)));
        sessionId = getCharacterDataFromElement((Element) element.getElementsByTagName("sessionId").item(0));
        if (!diameterSessionId.equals(sessionId)) {
            tracer.warning("SessionID Mismatch! Something is wrong with the response from the Rating Engine. Expected '" + diameterSessionId + "', received '" + sessionId + "'");
        }
        actualTime = Long.parseLong(getCharacterDataFromElement((Element) element.getElementsByTagName("actualTime").item(0)));
        currentTime = Long.parseLong(getCharacterDataFromElement((Element) element.getElementsByTagName("currentTime").item(0)));
        rate = Double.parseDouble(getCharacterDataFromElement((Element) element.getElementsByTagName("rate").item(0)));
        rateDescription = getCharacterDataFromElement((Element) element.getElementsByTagName("rateDescription").item(0));
        ratePromo = getCharacterDataFromElement((Element) element.getElementsByTagName("ratePromo").item(0));
        tracer.info("responseCode=" + responseCode + "; " + "sessionId=" + sessionId + "; " + "actualTime=" + actualTime + "; " + "currentTime=" + currentTime + "; " + "rate=" + rate + "; " + "rateDescription=" + rateDescription + "; " + "ratePromo=" + ratePromo);
    } catch (Exception e) {
        tracer.warning("[xx] Malformed response from Rating Engine for request:\n" + params + "\n\nResponse Received was:" + responseBody, e);
        return new RatingInfo(-1, diameterSessionId);
    }
    return new RatingInfo(responseCode, sessionId, actualTime, currentTime, rate, rateDescription, ratePromo);
}
Also used : InputSource(org.xml.sax.InputSource) RatingInfo(org.mobicents.charging.server.ratingengine.RatingInfo) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) StartActivityException(javax.slee.resource.StartActivityException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with RatingInfo

use of org.mobicents.charging.server.ratingengine.RatingInfo in project charging-server by RestComm.

the class HTTPClientSbb method getRateForServiceSync.

public RatingInfo getRateForServiceSync(HashMap params) {
    String sessionIdFromRequest = params.get("SessionId").toString();
    HttpClient client = raSbbInterface.getHttpClient();
    long bmStart = System.currentTimeMillis();
    HttpPost httpPost = buildHTTPRequest(params);
    // Synchronous call
    HttpResponse response = null;
    try {
        tracer.info("[>>] Sending HTTP Request to Rating Client in synchronous mode.");
        response = client.execute(httpPost);
    } catch (IOException e) {
        tracer.severe("[xx] Failed to send HTTP Request to Rating Engine.");
        return new RatingInfo(-1, sessionIdFromRequest);
    }
    tracer.info("[%%] Response from Rating Engine took " + (System.currentTimeMillis() - bmStart) + " milliseconds.");
    return buildRatingInfo(response, params);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RatingInfo(org.mobicents.charging.server.ratingengine.RatingInfo) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 5 with RatingInfo

use of org.mobicents.charging.server.ratingengine.RatingInfo in project charging-server by RestComm.

the class HTTPClientSbb method getRateForServiceAsync.

public RatingInfo getRateForServiceAsync(HashMap params) {
    String sessionIdFromRequest = params.get("SessionId").toString();
    HttpClientActivity clientActivity = null;
    try {
        clientActivity = raSbbInterface.createHttpClientActivity(true, null);
    } catch (StartActivityException e) {
        tracer.severe("[xx] Failed creating HTTP Client Activity to send HTTP Request to Rating Engine.");
        return new RatingInfo(-1, sessionIdFromRequest);
    }
    ActivityContextInterface clientAci = httpClientAci.getActivityContextInterface(clientActivity);
    clientAci.attach(sbbContext.getSbbLocalObject());
    params.put("startTime", System.currentTimeMillis());
    HttpPost httpPost = buildHTTPRequest(params);
    // Asynchronous call
    clientActivity.execute(httpPost, params);
    tracer.info("[>>] Sent HTTP Request to Rating Client in asynchronous mode.");
    return null;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RatingInfo(org.mobicents.charging.server.ratingengine.RatingInfo) ActivityContextInterface(javax.slee.ActivityContextInterface) HttpClientActivity(net.java.client.slee.resource.http.HttpClientActivity) StartActivityException(javax.slee.resource.StartActivityException)

Aggregations

RatingInfo (org.mobicents.charging.server.ratingengine.RatingInfo)6 IOException (java.io.IOException)3 StartActivityException (javax.slee.resource.StartActivityException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 NamingException (javax.naming.NamingException)2 HttpClientActivity (net.java.client.slee.resource.http.HttpClientActivity)2 HttpResponse (org.apache.http.HttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringReader (java.io.StringReader)1 ActivityContextInterface (javax.slee.ActivityContextInterface)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 SubscriptionIdAvp (net.java.slee.resource.diameter.cca.events.avp.SubscriptionIdAvp)1 HttpClient (org.apache.http.client.HttpClient)1 DiameterChargingServer (org.mobicents.charging.server.DiameterChargingServer)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1 InputSource (org.xml.sax.InputSource)1