Fix upcoming checkstyle issues
[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) 2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020 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.Matcher;
38 import java.util.regex.Pattern;
39 import java.util.stream.Collectors;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
42 import org.apache.commons.lang3.tuple.Pair;
43 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
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.parameters.RestServerParameters;
48 import org.onap.policy.common.endpoints.report.HealthCheckReport;
49 import org.onap.policy.common.parameters.ParameterService;
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
64 /**
65  * Provider for PAP to fetch health status of all Policy components, including PAP, API, Distribution, and PDPs.
66  *
67  * @author Yehui Wang (yehui.wang@est.tech)
68  */
69 public class PolicyComponentsHealthCheckProvider {
70
71     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyComponentsHealthCheckProvider.class);
72     private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
73     private static final String HEALTH_STATUS = "healthy";
74     private static final Pattern IP_REPLACEMENT_PATTERN = Pattern.compile("//(\\S+):");
75     private static final String POLICY_PAP_HEALTHCHECK_URI = "/policy/pap/v1/healthcheck";
76     private static List<HttpClient> clients = new ArrayList<>();
77     private static ExecutorService clientHealthCheckExecutorService;
78
79     private PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
80
81     /**
82      * This method is used to initialize clients and executor.
83      * @param papParameterGroup
84      *     @{link PapParameterGroup} contains the Pap Parameters set during startup
85      * @param clientFactory
86      *     @{link HttpClientFactory} contains the client details
87      */
88     public static void initializeClientHealthCheckExecutorService(PapParameterGroup papParameterGroup,
89         HttpClientFactory clientFactory) throws HttpClientConfigException {
90         for (BusTopicParams params : papParameterGroup.getHealthCheckRestClientParameters()) {
91             params.setManaged(false);
92             clients.add(clientFactory.build(params));
93         }
94         clientHealthCheckExecutorService = Executors.newFixedThreadPool(clients.isEmpty() ? 1 : clients.size());
95     }
96
97     /**
98      * Returns health status of all Policy components.
99      *
100      * @return a pair containing the status and the response
101      */
102     public Pair<Status, Map<String, Object>> fetchPolicyComponentsHealthStatus() {
103         boolean isHealthy;
104         Map<String, Object> result;
105
106         // Check remote components
107         List<Callable<Entry<String, Object>>> tasks = new ArrayList<>(clients.size());
108
109         for (HttpClient client : clients) {
110             tasks.add(() -> new AbstractMap.SimpleEntry<>(client.getName(), fetchPolicyComponentHealthStatus(client)));
111         }
112
113         try {
114             List<Future<Entry<String, Object>>> futures = clientHealthCheckExecutorService.invokeAll(tasks);
115             result = futures.stream().map(entryFuture -> {
116                 try {
117                     return entryFuture.get();
118                 } catch (ExecutionException e) {
119                     throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check Failed ", e);
120                 } catch (InterruptedException e) {
121                     Thread.currentThread().interrupt();
122                     throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", e);
123                 }
124             }).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
125             //true when all the clients health status is true
126             isHealthy = result.values().stream().allMatch(o -> ((HealthCheckReport) o).isHealthy());
127         } catch (InterruptedException exp) {
128             Thread.currentThread().interrupt();
129             throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", exp);
130         }
131
132         // Check PAP itself
133         HealthCheckReport papReport = new HealthCheckProvider().performHealthCheck();
134         RestServerParameters restServerParameters = papParameterGroup.getRestServerParameters();
135         papReport.setUrl(
136             (restServerParameters.isHttps() ? "https://" : "http://") + papReport.getUrl() + ":" + restServerParameters
137                 .getPort() + POLICY_PAP_HEALTHCHECK_URI);
138         if (!papReport.isHealthy()) {
139             isHealthy = false;
140         }
141         result.put(PapConstants.POLICY_PAP, papReport);
142
143         // Check PDPs, read status from DB
144         try {
145             List<PdpGroup> groups = fetchPdpGroups();
146             Map<String, List<Pdp>> pdpListWithType = fetchPdpsHealthStatus(groups);
147             if (isHealthy && (!verifyNumberOfPdps(groups) || pdpListWithType.values().stream().flatMap(List::stream)
148                             .anyMatch(pdp -> !PdpHealthStatus.HEALTHY.equals(pdp.getHealthy())))) {
149                 isHealthy = false;
150             }
151             result.put(PapConstants.POLICY_PDPS, pdpListWithType);
152         } catch (final PfModelException exp) {
153             result.put(PapConstants.POLICY_PDPS, exp.getErrorResponse());
154             isHealthy = false;
155         }
156
157         result.put(HEALTH_STATUS, isHealthy);
158         LOGGER.debug("Policy Components HealthCheck Response - {}", result);
159         return Pair.of(Status.OK, result);
160     }
161
162     private Map<String, List<Pdp>> fetchPdpsHealthStatus(List<PdpGroup> groups) {
163         Map<String, List<Pdp>> pdpListWithType = new HashMap<>();
164         for (final PdpGroup group : groups) {
165             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
166                 List<Pdp> pdpList = new ArrayList<>(subGroup.getPdpInstances());
167                 pdpListWithType.computeIfAbsent(subGroup.getPdpType(), k -> new ArrayList<>()).addAll(pdpList);
168             }
169         }
170         return pdpListWithType;
171     }
172
173     private boolean verifyNumberOfPdps(List<PdpGroup> groups) {
174         boolean flag = true;
175         for (final PdpGroup group : groups) {
176             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
177                 if (subGroup.getCurrentInstanceCount() < subGroup.getDesiredInstanceCount()) {
178                     flag = false;
179                     break;
180                 }
181             }
182         }
183         return flag;
184     }
185
186     private List<PdpGroup> fetchPdpGroups() throws PfModelException {
187         List<PdpGroup> groups = new ArrayList<>();
188         final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
189                         Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
190         try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
191             groups = databaseProvider.getPdpGroups(null);
192         }
193         return groups;
194     }
195
196     private HealthCheckReport fetchPolicyComponentHealthStatus(HttpClient httpClient) {
197         HealthCheckReport clientReport;
198         try {
199             Response resp = httpClient.get();
200             clientReport = replaceIpWithHostname(resp.readEntity(HealthCheckReport.class), httpClient.getBaseUrl());
201
202             // A health report is read successfully when HTTP status is not OK, it is also not healthy
203             // even in the report it says healthy.
204             if (resp.getStatus() != HttpURLConnection.HTTP_OK) {
205                 clientReport.setHealthy(false);
206             }
207         } catch (RuntimeException e) {
208             LOGGER.warn("{} connection error", httpClient.getName());
209             clientReport = createUnHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(),
210                 HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage());
211         }
212         return clientReport;
213     }
214
215     private HealthCheckReport createUnHealthCheckReport(String name, String url, int code, String message) {
216         HealthCheckReport report = new HealthCheckReport();
217         report.setName(name);
218         report.setUrl(url);
219         report.setHealthy(false);
220         report.setCode(code);
221         report.setMessage(message);
222         return report;
223     }
224
225     private HealthCheckReport replaceIpWithHostname(HealthCheckReport report, String baseUrl) {
226         Matcher matcher = IP_REPLACEMENT_PATTERN.matcher(baseUrl);
227         String ip = "";
228         if (matcher.find()) {
229             ip = matcher.group(1);
230             report.setUrl(baseUrl.replace(ip, report.getUrl()));
231         }
232         return report;
233     }
234
235     /**
236      * This method clears clients {@link List} and clientHealthCheckExecutorService {@link ExecutorService}.
237      */
238     public static void cleanup() {
239         clients.clear();
240         clientHealthCheckExecutorService.shutdown();
241     }
242 }