2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.fe.impl;
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;
32 public class HealthCheckService {
34 private static final Logger healthLogger = Logger.getLogger("asdc.fe.healthcheck");
35 private final HealthCheckScheduledTask task;
37 * This executor will execute the health check task.
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;
44 public HealthCheckService(ServletContext context) {
45 this.context = context;
46 this.task = new HealthCheckScheduledTask(this);
49 public Configuration getConfig() {
50 return ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
53 public void start(int interval) {
54 this.healthCheckExecutor.scheduleAtFixedRate(getTask(), 0, interval, TimeUnit.SECONDS);
58 * To be used by the HealthCheckServlet
62 public Response getFeHealth() {
63 return this.buildResponse(lastHealthStatus);
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();
71 public HealthStatus getLastHealthStatus() {
72 return lastHealthStatus;
75 void setLastHealthStatus(HealthStatus lastHealthStatus) {
76 this.lastHealthStatus = lastHealthStatus;
79 public HealthCheckScheduledTask getTask() {
84 static class HealthStatus {
87 private int statusCode;
89 public HealthStatus(int code, String body) {
91 this.statusCode = code;
94 public int getStatusCode() {
98 public void setStatusCode(int statusCode) {
99 this.statusCode = statusCode;
102 public String getBody() {
106 public void setBody(String body) {