a017d54b0ab885fbec8c81d9acd182bd7d55a70e
[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 package org.onap.clamp.clds.service;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26
27 import java.util.Date;
28
29 import org.onap.clamp.clds.dao.CldsDao;
30 import org.onap.clamp.clds.model.CldsHealthCheck;
31 import org.onap.clamp.clds.util.LoggingUtils;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Component;
36
37 /**
38  * Service to retrieve the Health Check of the clds application.
39  *
40  */
41 @Component
42 public class CldsHealthcheckService {
43
44     @Autowired
45     private CldsDao cldsDao;
46
47     protected static final EELFLogger logger          = EELFManager.getInstance().getLogger(CldsHealthcheckService.class);
48
49     /**
50      * REST service that retrieves clds healthcheck information.
51      *
52      * @return CldsHealthCheck class containing healthcheck info
53      */
54     public ResponseEntity<CldsHealthCheck> gethealthcheck() {
55         CldsHealthCheck cldsHealthCheck = new CldsHealthCheck();
56         Date startTime = new Date();
57         LoggingUtils.setRequestContext("CldsService: GET healthcheck", "Clamp-Health-Check");
58         LoggingUtils.setTimeContext(startTime, new Date());
59         boolean healthcheckFailed = false;
60         try {
61             cldsDao.doHealthCheck();
62             cldsHealthCheck.setHealthCheckComponent("CLDS-APP");
63             cldsHealthCheck.setHealthCheckStatus("UP");
64             cldsHealthCheck.setDescription("OK");
65             LoggingUtils.setResponseContext("0", "Get healthcheck success", this.getClass().getName());
66         } catch (Exception e) {
67             healthcheckFailed = true;
68             logger.error("CLAMP application Heath check failed", e);
69             LoggingUtils.setResponseContext("999", "Get healthcheck failed", this.getClass().getName());
70             cldsHealthCheck.setHealthCheckComponent("CLDS-APP");
71             cldsHealthCheck.setHealthCheckStatus("DOWN");
72             cldsHealthCheck.setDescription("NOT-OK");
73         }
74         // audit log
75         LoggingUtils.setTimeContext(startTime, new Date());
76         if(healthcheckFailed) {
77             return new ResponseEntity<>(cldsHealthCheck, HttpStatus.INTERNAL_SERVER_ERROR);
78         } else {
79             return new ResponseEntity<>(cldsHealthCheck, HttpStatus.OK);
80         }
81     }
82 }