DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / controller / health / HealthController.java
1 package org.onap.sdc.dcae.composition.controller.health;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
7 import org.onap.sdc.common.onaplog.OnapLoggerError;
8 import org.onap.sdc.common.onaplog.Enums.LogLevel;
9 import org.onap.sdc.dcae.composition.restmodels.health.ComponentsInfo;
10 import org.onap.sdc.dcae.composition.restmodels.health.HealthResponse;
11 import org.onap.sdc.dcae.composition.CompositionEngine;
12 import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.http.ResponseEntity;
17 import org.springframework.web.bind.annotation.CrossOrigin;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RequestMethod;
20 import org.springframework.web.bind.annotation.RestController;
21
22 import com.google.gson.Gson;
23
24 @RestController
25 @EnableAutoConfiguration
26 @CrossOrigin
27 public class HealthController {
28         private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
29         private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
30         Gson gson = new Gson();
31         
32         @Autowired
33         ToscaLabHealthState toscaLabHealthState;
34         
35         @RequestMapping(value = "/healthCheck", method = RequestMethod.GET)
36         public ResponseEntity<String> healthCheck() {
37                 HttpStatus httpSts = HttpStatus.OK;
38                 try{
39                         HealthResponse healthResponse = new HealthResponse();
40                         healthResponse.setHealthCheckComponent(DcaeBeConstants.Health.APP_NAME);
41                         healthResponse.setHealthCheckStatus(DcaeBeConstants.Health.UP);
42                         healthResponse.setSdcVersion(CompositionEngine.getDcaeVersion());
43                         healthResponse.setDescription(DcaeBeConstants.Health.OK);
44                         
45                         List<ComponentsInfo> componentsInfoList = new ArrayList<ComponentsInfo>();
46                         ComponentsInfo componentsInfo = new ComponentsInfo();
47                         componentsInfo.setHealthCheckComponent(DcaeBeConstants.Health.BE);
48                         componentsInfo.setHealthCheckStatus(DcaeBeConstants.Health.UP);
49                         componentsInfo.setVersion(CompositionEngine.getDcaeVersion());
50                         componentsInfo.setDescription(DcaeBeConstants.Health.OK);
51                         componentsInfoList.add(componentsInfo);
52                         
53                         ComponentsInfo toscaLab = new ComponentsInfo();
54                         ComponentsInfo toscaLabHealthRes = toscaLabHealthState.getToscaLabHealthResponse();
55                         if(toscaLabHealthRes.getHealthCheckStatus().equals(DcaeBeConstants.Health.DOWN)){
56                                 healthResponse.setHealthCheckStatus(DcaeBeConstants.Health.DOWN);
57                                 healthResponse.setDescription(toscaLabHealthRes.getHealthCheckComponent()+" is down");
58                                 httpSts = HttpStatus.INTERNAL_SERVER_ERROR;
59                         }
60                         toscaLab.setHealthCheckComponent(toscaLabHealthRes.getHealthCheckComponent());
61                         toscaLab.setHealthCheckStatus(toscaLabHealthRes.getHealthCheckStatus());
62                         toscaLab.setVersion(toscaLabHealthRes.getVersion());
63                         toscaLab.setDescription(toscaLabHealthRes.getDescription());
64                         componentsInfoList.add(toscaLab);
65                         
66                         healthResponse.setComponentsInfo(componentsInfoList);
67                         String json = gson.toJson(healthResponse, HealthResponse.class);
68                         
69                         return new ResponseEntity<String>(json, httpSts);
70                 }
71                 catch(Exception e){
72                         errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error occured while performing HealthCheck: {}", e.getLocalizedMessage());
73                         return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
74                 }
75         }
76         
77 }