[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / GetMetricsService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.openecomp.policy.pdp.rest.api.services;
21
22 import java.util.UUID;
23
24 import javax.json.JsonException;
25
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;
35
36 public class GetMetricsService {
37         private static final Logger LOGGER = FlexLogger
38                         .getLogger(GetMetricsService.class.getName());
39
40         private MetricsResponse response = null;
41         private HttpStatus status = HttpStatus.BAD_REQUEST;
42         private MetricsRequestParameters metricsParameters = null;
43
44         public GetMetricsService(String requestID) {
45                 UUID requestUUID = null;
46                 if (requestID != null && !requestID.isEmpty()) {
47                         try {
48                                 requestUUID = UUID.fromString(requestID);
49                         } catch (IllegalArgumentException e) {
50                                 requestUUID = UUID.randomUUID();
51                                 LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
52                         }
53                 } else {
54                         requestUUID = UUID.randomUUID();
55                         LOGGER.info("Generated Random UUID: " + requestUUID.toString());
56                 }
57                 metricsParameters = new MetricsRequestParameters();
58                 this.metricsParameters.setRequestID(requestUUID);
59
60                 try {
61                         run();
62                         specialCheck();
63                 } catch (PolicyException e) {
64                         StdMetricsResponse metricsResponse = new StdMetricsResponse();
65                         metricsResponse
66                                         .setResponseMessage(XACMLErrorConstants.ERROR_DATA_ISSUE
67                                                         + e);
68                         this.response = metricsResponse;
69                         status = HttpStatus.BAD_REQUEST;
70                 }
71         }
72
73         private void specialCheck() {
74                 if (response != null && (response.getResponseMessage() != null
75                                         && response.getResponseMessage().contains("PE300"))) {
76                                 status = HttpStatus.BAD_REQUEST;
77                 }
78         }
79
80         private void run() throws PolicyException {
81                 // Get Result.
82                 try {
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);
89                 }
90         }
91
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");
98
99                 JSONObject json = null;
100                 String message = null;
101                 if (result != null) {
102                         if (result.length() > 81 && result.contains("{")) {
103                                 try {
104                                         String responseMessage = result.substring(0, 82);
105                                         String jsonString = result.substring(result.indexOf('{'),
106                                                         result.length());
107                                         json = new JSONObject(jsonString);
108
109                                         int papCount = (int) json.get("papCount");
110                                         int pdpCount = (int) json.get("pdpCount");
111
112                                         metricsResponse.setResponseCode(papServices
113                                                         .getResponseCode());
114                                         metricsResponse.setResponseMessage(responseMessage);
115                                         metricsResponse.setPapMetrics(papCount);
116                                         metricsResponse.setPdpMetrics(pdpCount);
117
118                                 } catch (JsonException | IllegalStateException e) {
119                                         String jsonString = null;
120                                         if(json != null){
121                                                 jsonString = json.toString();
122                                         }
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;
129                                 }
130                         } else {
131                                 message = result;
132                                 metricsResponse.setResponseCode(400);
133                                 metricsResponse.setResponseMessage(message);
134                                 return metricsResponse;
135                         }
136
137                 } else {
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;
144                 }
145
146                 return metricsResponse;
147         }
148
149         public MetricsResponse getResult() {
150                 return response;
151         }
152
153         public HttpStatus getResponseCode() {
154                 return status;
155         }
156
157 }