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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations