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 * if(!getValidation()){ LOGGER.error(message); throw new
87 * PolicyException(message); }
91 status = HttpStatus.OK;
92 response = processResult();
93 } catch (Exception e) {
94 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
95 status = HttpStatus.BAD_REQUEST;
96 throw new PolicyException(e);
100 private MetricsResponse processResult() throws PolicyException {
101 StdMetricsResponse metricsResponse = new StdMetricsResponse();
102 PAPServices papServices = new PAPServices();
103 String result = (String) papServices.callPAP(null, new String[] {
104 "operation=get", "apiflag=getMetrics" },
105 metricsParameters.getRequestID(), "metrics");
107 JSONObject json = null;
108 if (result != null) {
109 if (result.length() > 81 && result.contains("{")) {
111 String responseMessage = result.substring(0, 82);
112 String jsonString = result.substring(result.indexOf("{"),
114 json = new JSONObject(jsonString);
116 int papCount = (int) json.get("papCount");
117 int pdpCount = (int) json.get("pdpCount");
119 metricsResponse.setResponseCode(papServices
121 metricsResponse.setResponseMessage(responseMessage);
122 metricsResponse.setPapMetrics(papCount);
123 metricsResponse.setPdpMetrics(pdpCount);
125 } catch (JsonException | IllegalStateException e) {
126 message = XACMLErrorConstants.ERROR_DATA_ISSUE
127 + " improper JSON object : " + json.toString();
128 LOGGER.error(message);
129 metricsResponse.setResponseMessage(message);
130 metricsResponse.setResponseCode(400);
131 return metricsResponse;
135 metricsResponse.setResponseCode(400);
136 metricsResponse.setResponseMessage(message);
137 return metricsResponse;
141 message = XACMLErrorConstants.ERROR_SYSTEM_ERROR
142 + "There was an issue with connecting to the PAP, "
143 + "review the logs for further debugging.";
144 metricsResponse.setResponseCode(500);
145 metricsResponse.setResponseMessage(message);
146 return metricsResponse;
149 return metricsResponse;
152 public MetricsResponse getResult() {
156 public HttpStatus getResponseCode() {