2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.policy.pdp.rest.api.services;
22 import java.util.UUID;
24 import javax.json.JsonException;
26 import org.json.JSONObject;
27 import org.openecomp.policy.api.MetricsRequestParameters;
28 import org.openecomp.policy.api.MetricsResponse;
29 import org.openecomp.policy.api.PolicyException;
30 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
32 import org.openecomp.policy.std.StdMetricsResponse;
33 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
34 import org.springframework.http.HttpStatus;
36 public class GetMetricsService {
37 private static final Logger LOGGER = FlexLogger
38 .getLogger(GetMetricsService.class.getName());
40 private MetricsResponse response = null;
41 private HttpStatus status = HttpStatus.BAD_REQUEST;
42 private MetricsRequestParameters metricsParameters = null;
44 public GetMetricsService(String requestID) {
45 UUID requestUUID = null;
46 if (requestID != null && !requestID.isEmpty()) {
48 requestUUID = UUID.fromString(requestID);
49 } catch (IllegalArgumentException e) {
50 requestUUID = UUID.randomUUID();
51 LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
54 requestUUID = UUID.randomUUID();
55 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
57 metricsParameters = new MetricsRequestParameters();
58 this.metricsParameters.setRequestID(requestUUID);
63 } catch (PolicyException e) {
64 StdMetricsResponse metricsResponse = new StdMetricsResponse();
66 .setResponseMessage(XACMLErrorConstants.ERROR_DATA_ISSUE
68 this.response = metricsResponse;
69 status = HttpStatus.BAD_REQUEST;
73 private void specialCheck() {
74 if (response != null && (response.getResponseMessage() != null
75 && response.getResponseMessage().contains("PE300"))) {
76 status = HttpStatus.BAD_REQUEST;
80 private void run() throws PolicyException {
83 status = HttpStatus.OK;
84 response = processResult();
85 } catch (Exception e) {
86 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
87 status = HttpStatus.BAD_REQUEST;
88 throw new PolicyException(e);
92 private MetricsResponse processResult() throws PolicyException {
93 StdMetricsResponse metricsResponse = new StdMetricsResponse();
94 PAPServices papServices = new PAPServices();
95 String result = (String) papServices.callPAP(null, new String[] {
96 "operation=get", "apiflag=getMetrics" },
97 metricsParameters.getRequestID(), "metrics");
99 JSONObject json = null;
100 String message = null;
101 if (result != null) {
102 if (result.length() > 81 && result.contains("{")) {
104 String responseMessage = result.substring(0, 82);
105 String jsonString = result.substring(result.indexOf('{'),
107 json = new JSONObject(jsonString);
109 int papCount = (int) json.get("papCount");
110 int pdpCount = (int) json.get("pdpCount");
112 metricsResponse.setResponseCode(papServices
114 metricsResponse.setResponseMessage(responseMessage);
115 metricsResponse.setPapMetrics(papCount);
116 metricsResponse.setPdpMetrics(pdpCount);
118 } catch (JsonException | IllegalStateException e) {
119 String jsonString = null;
121 jsonString = json.toString();
123 message = XACMLErrorConstants.ERROR_DATA_ISSUE
124 + " improper JSON object : " + jsonString;
125 LOGGER.error(message + e);
126 metricsResponse.setResponseMessage(message);
127 metricsResponse.setResponseCode(400);
128 return metricsResponse;
132 metricsResponse.setResponseCode(400);
133 metricsResponse.setResponseMessage(message);
134 return metricsResponse;
138 message = XACMLErrorConstants.ERROR_SYSTEM_ERROR
139 + "There was an issue with connecting to the PAP, "
140 + "review the logs for further debugging.";
141 metricsResponse.setResponseCode(500);
142 metricsResponse.setResponseMessage(message);
143 return metricsResponse;
146 return metricsResponse;
149 public MetricsResponse getResult() {
153 public HttpStatus getResponseCode() {