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