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 Logger LOGGER = FlexLogger
38 .getLogger(GetDictionaryService.class.getName());
40 private MetricsResponse response = null;
41 private HttpStatus status = HttpStatus.BAD_REQUEST;
42 private String message = null;
43 private MetricsRequestParameters metricsParameters = null;
45 public GetMetricsService(String requestID) {
46 UUID requestUUID = null;
47 if (requestID != null && !requestID.isEmpty()) {
49 requestUUID = UUID.fromString(requestID);
50 } catch (IllegalArgumentException e) {
51 requestUUID = UUID.randomUUID();
52 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
55 requestUUID = UUID.randomUUID();
56 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
58 metricsParameters = new MetricsRequestParameters();
59 this.metricsParameters.setRequestID(requestUUID);
64 } catch (PolicyException e) {
65 StdMetricsResponse metricsResponse = new StdMetricsResponse();
67 .setResponseMessage(XACMLErrorConstants.ERROR_DATA_ISSUE
69 this.response = metricsResponse;
70 status = HttpStatus.BAD_REQUEST;
74 private void specialCheck() {
75 if (response != null) {
76 if (response.getResponseMessage() != null
77 && response.getResponseMessage().contains("PE300")) {
78 status = HttpStatus.BAD_REQUEST;
83 private void run() throws PolicyException {
86 status = HttpStatus.OK;
87 response = processResult();
88 } catch (Exception e) {
89 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
90 status = HttpStatus.BAD_REQUEST;
91 throw new PolicyException(e);
95 private MetricsResponse processResult() throws PolicyException {
96 StdMetricsResponse metricsResponse = new StdMetricsResponse();
97 PAPServices papServices = new PAPServices();
98 String result = (String) papServices.callPAP(null, new String[] {
99 "operation=get", "apiflag=getMetrics" },
100 metricsParameters.getRequestID(), "metrics");
102 JSONObject json = null;
103 if (result != null) {
104 if (result.length() > 81 && result.contains("{")) {
106 String responseMessage = result.substring(0, 82);
107 String jsonString = result.substring(result.indexOf("{"),
109 json = new JSONObject(jsonString);
111 int papCount = (int) json.get("papCount");
112 int pdpCount = (int) json.get("pdpCount");
114 metricsResponse.setResponseCode(papServices
116 metricsResponse.setResponseMessage(responseMessage);
117 metricsResponse.setPapMetrics(papCount);
118 metricsResponse.setPdpMetrics(pdpCount);
120 } catch (JsonException | IllegalStateException e) {
121 message = XACMLErrorConstants.ERROR_DATA_ISSUE
122 + " improper JSON object : " + json != null ? json.toString() : "JSON is null";
123 LOGGER.error(message);
124 metricsResponse.setResponseMessage(message);
125 metricsResponse.setResponseCode(400);
126 return metricsResponse;
130 metricsResponse.setResponseCode(400);
131 metricsResponse.setResponseMessage(message);
132 return metricsResponse;
136 message = XACMLErrorConstants.ERROR_SYSTEM_ERROR
137 + "There was an issue with connecting to the PAP, "
138 + "review the logs for further debugging.";
139 metricsResponse.setResponseCode(500);
140 metricsResponse.setResponseMessage(message);
141 return metricsResponse;
144 return metricsResponse;
147 public MetricsResponse getResult() {
151 public HttpStatus getResponseCode() {