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