Parallel execution of Client Health check
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import java.net.HttpURLConnection;
24 import java.util.AbstractMap;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.concurrent.Callable;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.Future;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40 import org.apache.commons.lang3.tuple.Pair;
41 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
42 import org.onap.policy.common.endpoints.http.client.HttpClient;
43 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
44 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
45 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
46 import org.onap.policy.common.endpoints.report.HealthCheckReport;
47 import org.onap.policy.common.parameters.ParameterService;
48 import org.onap.policy.common.utils.services.Registry;
49 import org.onap.policy.models.base.PfModelException;
50 import org.onap.policy.models.base.PfModelRuntimeException;
51 import org.onap.policy.models.pdp.concepts.Pdp;
52 import org.onap.policy.models.pdp.concepts.PdpGroup;
53 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
54 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
55 import org.onap.policy.models.provider.PolicyModelsProvider;
56 import org.onap.policy.pap.main.PapConstants;
57 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
58 import org.onap.policy.pap.main.parameters.PapParameterGroup;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * Provider for PAP to fetch health status of all Policy components, including PAP, API, Distribution, and PDPs.
64  *
65  * @author Yehui Wang (yehui.wang@est.tech)
66  */
67 public class PolicyComponentsHealthCheckProvider {
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyComponentsHealthCheckProvider.class);
70     private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
71     private static final String HEALTH_STATUS = "healthy";
72     private static final Pattern IP_REPLACEMENT_PATTERN = Pattern.compile("//(\\S+):");
73     private static final String POLICY_PAP_HEALTHCHECK_URI = "/policy/pap/v1/healthcheck";
74     private static List<HttpClient> clients = new ArrayList<>();
75     private static ExecutorService clientHealthCheckExecutorService;
76
77     private PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
78
79     /**
80      * This method is used to initialize clients and executor.
81      * @param papParameterGroup
82      * @{link PapParameterGroup} contains the Pap Parameters set during startup
83      * @param clientFactory
84      * @{link HttpClientFactory} contains the client details
85      */
86     public static void initializeClientHealthCheckExecutorService(PapParameterGroup papParameterGroup,
87         HttpClientFactory clientFactory) throws HttpClientConfigException {
88         for (BusTopicParams params : papParameterGroup.getHealthCheckRestClientParameters()) {
89             params.setManaged(false);
90             clients.add(clientFactory.build(params));
91         }
92         clientHealthCheckExecutorService = Executors.newFixedThreadPool(clients.isEmpty() ? 1 : clients.size());
93     }
94
95     /**
96      * Returns health status of all Policy components.
97      *
98      * @return a pair containing the status and the response
99      */
100     public Pair<Status, Map<String, Object>> fetchPolicyComponentsHealthStatus() {
101         boolean isHealthy = true;
102         Map<String, Object> result = new HashMap<>();
103
104         // Check remote components
105         List<Callable<Entry<String, Object>>> tasks = new ArrayList<>();
106
107         for (HttpClient client : clients) {
108             tasks.add(() -> new AbstractMap.SimpleEntry<>(client.getName(), fetchPolicyComponentHealthStatus(client)));
109         }
110
111         try {
112             List<Future<Entry<String, Object>>> futures = clientHealthCheckExecutorService.invokeAll(tasks);
113             result = futures.stream().map(entryFuture -> {
114                 try {
115                     return entryFuture.get();
116                 } catch (ExecutionException e) {
117                     throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check Failed ", e);
118                 } catch (InterruptedException e) {
119                     Thread.currentThread().interrupt();
120                     throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", e);
121                 }
122             }).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
123             //true when all the clients health status is true
124             isHealthy = result.values().stream().allMatch(o -> ((HealthCheckReport) o).isHealthy());
125         } catch (InterruptedException exp) {
126             Thread.currentThread().interrupt();
127             throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", exp);
128         }
129
130         // Check PAP itself
131         HealthCheckReport papReport = new HealthCheckProvider().performHealthCheck();
132         RestServerParameters restServerParameters = papParameterGroup.getRestServerParameters();
133         papReport.setUrl(
134             (restServerParameters.isHttps() ? "https://" : "http://") + papReport.getUrl() + ":" + restServerParameters
135                 .getPort() + POLICY_PAP_HEALTHCHECK_URI);
136         if (!papReport.isHealthy()) {
137             isHealthy = false;
138         }
139         result.put(PapConstants.POLICY_PAP, papReport);
140
141         // Check PDPs, read status from DB
142         try {
143             Map<String, List<Pdp>> pdpListWithType = fetchPdpsHealthStatus();
144             if (isHealthy && (pdpListWithType.isEmpty() || pdpListWithType.values().stream().flatMap(List::stream)
145                 .anyMatch(pdp -> !PdpHealthStatus.HEALTHY.equals(pdp.getHealthy())))) {
146                 isHealthy = false;
147             }
148             result.put(PapConstants.POLICY_PDPS, pdpListWithType);
149         } catch (final PfModelException exp) {
150             result.put(PapConstants.POLICY_PDPS, exp.getErrorResponse());
151             isHealthy = false;
152         }
153
154         result.put(HEALTH_STATUS, isHealthy);
155         LOGGER.debug("Policy Components HealthCheck Response - {}", result);
156         return Pair.of(Status.OK, result);
157     }
158
159     private Map<String, List<Pdp>> fetchPdpsHealthStatus() throws PfModelException {
160         Map<String, List<Pdp>> pdpListWithType = new HashMap<>();
161         final PolicyModelsProviderFactoryWrapper modelProviderWrapper = Registry
162             .get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
163         try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
164             final List<PdpGroup> groups = databaseProvider.getPdpGroups(null);
165             for (final PdpGroup group : groups) {
166                 for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
167                     List<Pdp> pdpList = new ArrayList<>(subGroup.getPdpInstances());
168                     pdpListWithType.computeIfAbsent(subGroup.getPdpType(), k -> new ArrayList<>()).addAll(pdpList);
169                 }
170             }
171         }
172         return pdpListWithType;
173     }
174
175     private HealthCheckReport fetchPolicyComponentHealthStatus(HttpClient httpClient) {
176         HealthCheckReport clientReport;
177         try {
178             Response resp = httpClient.get();
179             clientReport = replaceIpWithHostname(resp.readEntity(HealthCheckReport.class), httpClient.getBaseUrl());
180
181             // A health report is read successfully when HTTP status is not OK, it is also not healthy
182             // even in the report it says healthy.
183             if (resp.getStatus() != HttpURLConnection.HTTP_OK) {
184                 clientReport.setHealthy(false);
185             }
186         } catch (RuntimeException e) {
187             LOGGER.warn("{} connection error", httpClient.getName());
188             clientReport = createUnHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(),
189                 HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage());
190         }
191         return clientReport;
192     }
193
194     private HealthCheckReport createUnHealthCheckReport(String name, String url, int code, String message) {
195         HealthCheckReport report = new HealthCheckReport();
196         report.setName(name);
197         report.setUrl(url);
198         report.setHealthy(false);
199         report.setCode(code);
200         report.setMessage(message);
201         return report;
202     }
203
204     private HealthCheckReport replaceIpWithHostname(HealthCheckReport report, String baseUrl) {
205         Matcher matcher = IP_REPLACEMENT_PATTERN.matcher(baseUrl);
206         String ip = "";
207         if (matcher.find()) {
208             ip = matcher.group(1);
209             report.setUrl(baseUrl.replace(ip, report.getUrl()));
210         }
211         return report;
212     }
213
214     /**
215      * This method clears clients {@link List} and clientHealthCheckExecutorService {@link ExecutorService}.
216      */
217     public static void cleanup() {
218         clients.clear();
219         clientHealthCheckExecutorService.shutdown();
220     }
221 }