14812edfbd6c78140173e0cab85b6fbd3b94f00f
[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 Logger LOGGER = FlexLogger
38                         .getLogger(GetDictionaryService.class.getName());
39
40         private MetricsResponse response = null;
41         private HttpStatus status = HttpStatus.BAD_REQUEST;
42         private String message = null;
43         private MetricsRequestParameters metricsParameters = null;
44
45         public GetMetricsService(String requestID) {
46                 UUID requestUUID = null;
47                 if (requestID != null && !requestID.isEmpty()) {
48                         try {
49                                 requestUUID = UUID.fromString(requestID);
50                         } catch (IllegalArgumentException e) {
51                                 requestUUID = UUID.randomUUID();
52                                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
53                         }
54                 } else {
55                         requestUUID = UUID.randomUUID();
56                         LOGGER.info("Generated Random UUID: " + requestUUID.toString());
57                 }
58                 metricsParameters = new MetricsRequestParameters();
59                 this.metricsParameters.setRequestID(requestUUID);
60
61                 try {
62                         run();
63                         specialCheck();
64                 } catch (PolicyException e) {
65                         StdMetricsResponse metricsResponse = new StdMetricsResponse();
66                         metricsResponse
67                                         .setResponseMessage(XACMLErrorConstants.ERROR_DATA_ISSUE
68                                                         + e);
69                         this.response = metricsResponse;
70                         status = HttpStatus.BAD_REQUEST;
71                 }
72         }
73
74         private void specialCheck() {
75                 if (response != null) {
76                         if (response.getResponseMessage() != null
77                                         && response.getResponseMessage().contains("PE300")) {
78                                 status = HttpStatus.BAD_REQUEST;
79                         }
80                 }
81         }
82
83         private void run() throws PolicyException {
84                 // Check Validation.
85                 /*
86                  * if(!getValidation()){ LOGGER.error(message); throw new
87                  * PolicyException(message); }
88                  */
89                 // Get Result.
90                 try {
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);
97                 }
98         }
99
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");
106
107                 JSONObject json = null;
108                 if (result != null) {
109                         if (result.length() > 81 && result.contains("{")) {
110                                 try {
111                                         String responseMessage = result.substring(0, 82);
112                                         String jsonString = result.substring(result.indexOf("{"),
113                                                         result.length());
114                                         json = new JSONObject(jsonString);
115
116                                         int papCount = (int) json.get("papCount");
117                                         int pdpCount = (int) json.get("pdpCount");
118
119                                         metricsResponse.setResponseCode(papServices
120                                                         .getResponseCode());
121                                         metricsResponse.setResponseMessage(responseMessage);
122                                         metricsResponse.setPapMetrics(papCount);
123                                         metricsResponse.setPdpMetrics(pdpCount);
124
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;
132                                 }
133                         } else {
134                                 message = result;
135                                 metricsResponse.setResponseCode(400);
136                                 metricsResponse.setResponseMessage(message);
137                                 return metricsResponse;
138                         }
139
140                 } else {
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;
147                 }
148
149                 return metricsResponse;
150         }
151
152         public MetricsResponse getResult() {
153                 return response;
154         }
155
156         public HttpStatus getResponseCode() {
157                 return status;
158         }
159
160 }