7a49016681a4a7a5d2ea77b68c03276d7bf20dc9
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyComponentsHealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020-2021 Bell Canada. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import java.net.HttpURLConnection;
26 import java.util.AbstractMap;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.concurrent.Callable;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
36 import java.util.concurrent.Future;
37 import java.util.regex.Pattern;
38 import java.util.stream.Collectors;
39 import javax.annotation.PostConstruct;
40 import javax.annotation.PreDestroy;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.Response.Status;
43 import org.apache.commons.lang3.tuple.Pair;
44 import org.onap.policy.common.endpoints.http.client.HttpClient;
45 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
46 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
47 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
48 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
49 import org.onap.policy.common.endpoints.report.HealthCheckReport;
50 import org.onap.policy.common.utils.services.Registry;
51 import org.onap.policy.models.base.PfModelException;
52 import org.onap.policy.models.base.PfModelRuntimeException;
53 import org.onap.policy.models.pdp.concepts.Pdp;
54 import org.onap.policy.models.pdp.concepts.PdpGroup;
55 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
56 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
57 import org.onap.policy.models.provider.PolicyModelsProvider;
58 import org.onap.policy.pap.main.PapConstants;
59 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
60 import org.onap.policy.pap.main.parameters.PapParameterGroup;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.beans.factory.annotation.Value;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.stereotype.Service;
67
68 /**
69  * Provider for PAP to fetch health status of all Policy components, including PAP, API, Distribution, and PDPs.
70  *
71  * @author Yehui Wang (yehui.wang@est.tech)
72  */
73 @Service
74 public class PolicyComponentsHealthCheckProvider {
75
76     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyComponentsHealthCheckProvider.class);
77     private static final String HEALTH_STATUS = "healthy";
78     private static final Pattern IP_REPLACEMENT_PATTERN = Pattern.compile("//(\\S+):");
79     private static final String POLICY_PAP_HEALTHCHECK_URI = "/policy/pap/v1/healthcheck";
80     private static List<HttpClient> clients = new ArrayList<>();
81     private ExecutorService clientHealthCheckExecutorService;
82
83     @Autowired
84     private PapParameterGroup papParameterGroup;
85
86     @Value("${server.ssl.enabled:false}")
87     private boolean isHttps;
88
89     @Value("${server.port}")
90     private int port;
91
92     /**
93      * This method is used to initialize clients and executor.
94      */
95     @PostConstruct
96     public void initializeClientHealthCheckExecutorService() throws HttpClientConfigException {
97         HttpClientFactory clientFactory = HttpClientFactoryInstance.getClientFactory();
98         for (RestClientParameters params : papParameterGroup.getHealthCheckRestClientParameters()) {
99             params.setManaged(false);
100             clients.add(clientFactory.build(params));
101         }
102         clientHealthCheckExecutorService = Executors.newFixedThreadPool(clients.isEmpty() ? 1 : clients.size());
103     }
104
105     /**
106      * This method clears clients {@link List} and clientHealthCheckExecutorService {@link ExecutorService}.
107      */
108     @PreDestroy
109     public void cleanup() {
110         clients.clear();
111         clientHealthCheckExecutorService.shutdown();
112     }
113
114     /**
115      * Returns health status of all Policy components.
116      *
117      * @return a pair containing the status and the response
118      */
119     public Pair<HttpStatus, Map<String, Object>> fetchPolicyComponentsHealthStatus() {
120         boolean isHealthy;
121         Map<String, Object> result;
122
123         // Check remote components
124         List<Callable<Entry<String, Object>>> tasks = new ArrayList<>(clients.size());
125
126         for (HttpClient client : clients) {
127             tasks.add(() -> new AbstractMap.SimpleEntry<>(client.getName(), fetchPolicyComponentHealthStatus(client)));
128         }
129
130         try {
131             List<Future<Entry<String, Object>>> futures = clientHealthCheckExecutorService.invokeAll(tasks);
132             result = futures.stream().map(entryFuture -> {
133                 try {
134                     return entryFuture.get();
135                 } catch (ExecutionException e) {
136                     throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check Failed ", e);
137                 } catch (InterruptedException e) {
138                     Thread.currentThread().interrupt();
139                     throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", e);
140                 }
141             }).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
142             //true when all the clients health status is true
143             isHealthy = result.values().stream().allMatch(o -> ((HealthCheckReport) o).isHealthy());
144         } catch (InterruptedException exp) {
145             Thread.currentThread().interrupt();
146             throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", exp);
147         }
148
149         // Check PAP itself
150         HealthCheckReport papReport = new HealthCheckProvider().performHealthCheck();
151         papReport
152             .setUrl(isHttps ? "https://" : "http://" + papReport.getUrl() + ":" + port + POLICY_PAP_HEALTHCHECK_URI);
153         if (!papReport.isHealthy()) {
154             isHealthy = false;
155         }
156         result.put(PapConstants.POLICY_PAP, papReport);
157
158         // Check PDPs, read status from DB
159         try {
160             List<PdpGroup> groups = fetchPdpGroups();
161             Map<String, List<Pdp>> pdpListWithType = fetchPdpsHealthStatus(groups);
162             if (isHealthy && (!verifyNumberOfPdps(groups) || pdpListWithType.values().stream().flatMap(List::stream)
163                             .anyMatch(pdp -> !PdpHealthStatus.HEALTHY.equals(pdp.getHealthy())))) {
164                 isHealthy = false;
165             }
166             result.put(PapConstants.POLICY_PDPS, pdpListWithType);
167         } catch (final PfModelException exp) {
168             result.put(PapConstants.POLICY_PDPS, exp.getErrorResponse());
169             isHealthy = false;
170         }
171
172         result.put(HEALTH_STATUS, isHealthy);
173         LOGGER.debug("Policy Components HealthCheck Response - {}", result);
174         return Pair.of(HttpStatus.OK, result);
175     }
176
177     private Map<String, List<Pdp>> fetchPdpsHealthStatus(List<PdpGroup> groups) {
178         Map<String, List<Pdp>> pdpListWithType = new HashMap<>();
179         for (final PdpGroup group : groups) {
180             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
181                 List<Pdp> pdpList = new ArrayList<>(subGroup.getPdpInstances());
182                 pdpListWithType.computeIfAbsent(subGroup.getPdpType(), k -> new ArrayList<>()).addAll(pdpList);
183             }
184         }
185         return pdpListWithType;
186     }
187
188     private boolean verifyNumberOfPdps(List<PdpGroup> groups) {
189         var flag = true;
190         for (final PdpGroup group : groups) {
191             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
192                 if (subGroup.getCurrentInstanceCount() < subGroup.getDesiredInstanceCount()) {
193                     flag = false;
194                     break;
195                 }
196             }
197         }
198         return flag;
199     }
200
201     private List<PdpGroup> fetchPdpGroups() throws PfModelException {
202         List<PdpGroup> groups = new ArrayList<>();
203         final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
204                         Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
205         try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
206             groups = databaseProvider.getPdpGroups(null);
207         }
208         return groups;
209     }
210
211     private HealthCheckReport fetchPolicyComponentHealthStatus(HttpClient httpClient) {
212         HealthCheckReport clientReport;
213         try {
214             Response resp = httpClient.get();
215             if (httpClient.getName().equalsIgnoreCase("dmaap")) {
216                 clientReport = verifyDmaapClient(httpClient, resp);
217             } else {
218                 clientReport = replaceIpWithHostname(resp.readEntity(HealthCheckReport.class), httpClient.getBaseUrl());
219             }
220
221             // A health report is read successfully when HTTP status is not OK, it is also
222             // not healthy
223             // even in the report it says healthy.
224             if (resp.getStatus() != HttpURLConnection.HTTP_OK) {
225                 clientReport.setHealthy(false);
226             }
227         } catch (RuntimeException e) {
228             LOGGER.warn("{} connection error", httpClient.getName());
229             clientReport = createHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(),
230                             HttpURLConnection.HTTP_INTERNAL_ERROR, false, e.getMessage());
231         }
232         return clientReport;
233     }
234
235     private HealthCheckReport createHealthCheckReport(String name, String url, int code, boolean status,
236                     String message) {
237         var report = new HealthCheckReport();
238         report.setName(name);
239         report.setUrl(url);
240         report.setHealthy(status);
241         report.setCode(code);
242         report.setMessage(message);
243         return report;
244     }
245
246     private HealthCheckReport replaceIpWithHostname(HealthCheckReport report, String baseUrl) {
247         var matcher = IP_REPLACEMENT_PATTERN.matcher(baseUrl);
248         if (matcher.find()) {
249             var ip = matcher.group(1);
250             report.setUrl(baseUrl.replace(ip, report.getUrl()));
251         }
252         return report;
253     }
254
255     private HealthCheckReport verifyDmaapClient(HttpClient httpClient, Response resp) {
256         DmaapGetTopicResponse dmaapResponse = resp.readEntity(DmaapGetTopicResponse.class);
257         var topicVerificationStatus = (dmaapResponse.getTopics() != null
258                         && dmaapResponse.getTopics().contains(PapConstants.TOPIC_POLICY_PDP_PAP));
259         String message = (topicVerificationStatus ? "PAP to DMaaP connection check is successfull"
260                         : "PAP to DMaaP connection check failed");
261         int code = (topicVerificationStatus ? resp.getStatus() : 503);
262         return createHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(), code,
263                         topicVerificationStatus, message);
264     }
265
266 }