aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/GetMetricsService.java
blob: a0a477cdfc34a566e853050fab072f4a0a36fd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PDP-REST
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.policy.pdp.rest.api.services;

import java.util.UUID;

import javax.json.JsonException;

import org.json.JSONObject;
import org.onap.policy.api.MetricsRequestParameters;
import org.onap.policy.api.MetricsResponse;
import org.onap.policy.api.PolicyException;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.std.StdMetricsResponse;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.springframework.http.HttpStatus;

public class GetMetricsService {
	private static final Logger LOGGER = FlexLogger
			.getLogger(GetMetricsService.class.getName());

	private MetricsResponse response = null;
	private HttpStatus status = HttpStatus.BAD_REQUEST;
	private MetricsRequestParameters metricsParameters = null;

	public GetMetricsService(String requestID) {
		UUID requestUUID = null;
		if (requestID != null && !requestID.isEmpty()) {
			try {
				requestUUID = UUID.fromString(requestID);
			} catch (IllegalArgumentException e) {
				requestUUID = UUID.randomUUID();
				LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
			}
		} else {
			requestUUID = UUID.randomUUID();
			LOGGER.info("Generated Random UUID: " + requestUUID.toString());
		}
		metricsParameters = new MetricsRequestParameters();
		this.metricsParameters.setRequestID(requestUUID);

		try {
			run();
			specialCheck();
		} catch (PolicyException e) {
			StdMetricsResponse metricsResponse = new StdMetricsResponse();
			metricsResponse
					.setResponseMessage(XACMLErrorConstants.ERROR_DATA_ISSUE
							+ e);
			this.response = metricsResponse;
			status = HttpStatus.BAD_REQUEST;
		}
	}

	private void specialCheck() {
		if (response != null && (response.getResponseMessage() != null
					&& response.getResponseMessage().contains("PE300"))) {
				status = HttpStatus.BAD_REQUEST;
		}
	}

	private void run() throws PolicyException {
		// Get Result.
		try {
			status = HttpStatus.OK;
			response = processResult();
		} catch (Exception e) {
			LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
			status = HttpStatus.BAD_REQUEST;
			throw new PolicyException(e);
		}
	}

	private MetricsResponse processResult() throws PolicyException {
		StdMetricsResponse metricsResponse = new StdMetricsResponse();
		PAPServices papServices = new PAPServices();
		String result = (String) papServices.callPAP(null, new String[] {
				"operation=get", "apiflag=getMetrics" },
				metricsParameters.getRequestID(), "metrics");

		JSONObject json = null;
		String message = null;
		if (result != null) {
			if (result.length() > 81 && result.contains("{")) {
				try {
					String responseMessage = result.substring(0, 82);
					String jsonString = result.substring(result.indexOf('{'),
							result.length());
					json = new JSONObject(jsonString);

					int papCount = (int) json.get("papCount");
					int pdpCount = (int) json.get("pdpCount");

					metricsResponse.setResponseCode(papServices
							.getResponseCode());
					metricsResponse.setResponseMessage(responseMessage);
					metricsResponse.setPapMetrics(papCount);
					metricsResponse.setPdpMetrics(pdpCount);

				} catch (JsonException | IllegalStateException e) {
					String jsonString = null;
					if(json != null){
						jsonString = json.toString();
					}
					message = XACMLErrorConstants.ERROR_DATA_ISSUE
							+ " improper JSON object : " + jsonString;
					LOGGER.error(message + e);
					metricsResponse.setResponseMessage(message);
					metricsResponse.setResponseCode(400);
					return metricsResponse;
				}
			} else {
				message = result;
				metricsResponse.setResponseCode(400);
				metricsResponse.setResponseMessage(message);
				return metricsResponse;
			}

		} else {
			message = XACMLErrorConstants.ERROR_SYSTEM_ERROR
					+ "There was an issue with connecting to the PAP, "
					+ "review the logs for further debugging.";
			metricsResponse.setResponseCode(500);
			metricsResponse.setResponseMessage(message);
			return metricsResponse;
		}

		return metricsResponse;
	}

	public MetricsResponse getResult() {
		return response;
	}

	public HttpStatus getResponseCode() {
		return status;
	}

}