6203cdf86dd1d167e12802e447294b6272591c8a
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsHealthcheckService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  */
22
23 package org.onap.clamp.clds.service;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27
28 import java.util.Date;
29
30 import org.onap.clamp.clds.model.CldsHealthCheck;
31 import org.onap.clamp.clds.util.LoggingUtils;
32 import org.onap.clamp.clds.util.OnapLogConstants;
33 import org.onap.clamp.loop.LoopController;
34 import org.slf4j.event.Level;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * Service to retrieve the Health Check of the clds application.
41  *
42  */
43 @Component
44 public class CldsHealthcheckService {
45
46     @Autowired
47     private LoopController loopController;
48
49     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsHealthcheckService.class);
50
51     /**
52      * REST service that retrieves clds healthcheck information.
53      *
54      * @return CldsHealthCheck class containing healthcheck info
55      */
56     public CldsHealthCheck gethealthcheck() {
57         CldsHealthCheck cldsHealthCheck = new CldsHealthCheck();
58         Date startTime = new Date();
59         LoggingUtils util = new LoggingUtils(logger);
60         LoggingUtils.setRequestContext("CldsService: GET healthcheck", "Clamp-Health-Check");
61         LoggingUtils.setTimeContext(startTime, new Date());
62         try {
63             loopController.getLoopNames();
64             cldsHealthCheck.setHealthCheckComponent("CLDS-APP");
65             cldsHealthCheck.setHealthCheckStatus("UP");
66             cldsHealthCheck.setDescription("OK");
67             LoggingUtils.setResponseContext("0", "Get healthcheck success", this.getClass().getName());
68             util.exiting("200", "Healthcheck success", Level.INFO, OnapLogConstants.ResponseStatus.COMPLETED);
69         } catch (Exception e) {
70             logger.error("CLAMP application Heath check failed", e);
71             LoggingUtils.setResponseContext("999", "Get healthcheck failed", this.getClass().getName());
72             cldsHealthCheck.setHealthCheckComponent("CLDS-APP");
73             cldsHealthCheck.setHealthCheckStatus("DOWN");
74             cldsHealthCheck.setDescription("NOT-OK");
75             util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), "Healthcheck failed", Level.INFO,
76                     OnapLogConstants.ResponseStatus.ERROR);
77         }
78         return cldsHealthCheck;
79     }
80 }