Release version 1.13.7
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / impl / HealthCheckService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.fe.impl;
21
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
25 import javax.servlet.ServletContext;
26 import javax.ws.rs.core.Response;
27 import org.openecomp.sdc.common.api.Constants;
28 import org.openecomp.sdc.common.log.wrappers.Logger;
29 import org.openecomp.sdc.fe.config.Configuration;
30 import org.openecomp.sdc.fe.config.ConfigurationManager;
31
32 public class HealthCheckService {
33
34     private static final Logger healthLogger = Logger.getLogger("asdc.fe.healthcheck");
35     private final HealthCheckScheduledTask task;
36     /**
37      * This executor will execute the health check task.
38      */
39     private ScheduledExecutorService healthCheckExecutor = Executors
40         .newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "FE-Health-Check-Thread"));
41     private HealthStatus lastHealthStatus = new HealthStatus(500, "{}");
42     private ServletContext context;
43
44     public HealthCheckService(ServletContext context) {
45         this.context = context;
46         this.task = new HealthCheckScheduledTask(this);
47     }
48
49     public Configuration getConfig() {
50         return ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
51     }
52
53     public void start(int interval) {
54         this.healthCheckExecutor.scheduleAtFixedRate(getTask(), 0, interval, TimeUnit.SECONDS);
55     }
56
57     /**
58      * To be used by the HealthCheckServlet
59      *
60      * @return
61      */
62     public Response getFeHealth() {
63         return this.buildResponse(lastHealthStatus);
64     }
65
66     private Response buildResponse(HealthStatus healthStatus) {
67         healthLogger.trace("FE and BE health check status: {}", healthStatus.getBody());
68         return Response.status(healthStatus.getStatusCode()).entity(healthStatus.getBody()).build();
69     }
70
71     public HealthStatus getLastHealthStatus() {
72         return lastHealthStatus;
73     }
74
75     void setLastHealthStatus(HealthStatus lastHealthStatus) {
76         this.lastHealthStatus = lastHealthStatus;
77     }
78
79     public HealthCheckScheduledTask getTask() {
80         return task;
81     }
82
83     //immutable
84     static class HealthStatus {
85
86         private String body;
87         private int statusCode;
88
89         public HealthStatus(int code, String body) {
90             this.body = body;
91             this.statusCode = code;
92         }
93
94         public int getStatusCode() {
95             return statusCode;
96         }
97
98         public void setStatusCode(int statusCode) {
99             this.statusCode = statusCode;
100         }
101
102         public String getBody() {
103             return body;
104         }
105
106         public void setBody(String body) {
107             this.body = body;
108         }
109     }
110 }