86e832ba7fbba58cf38bec5380b494969c481293
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / HealthCheckController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.controllers;
22
23 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.portalsdk.core.util.SystemProperties;
26 import org.onap.vid.dao.FnAppDoaImpl;
27 import org.onap.vid.model.GitRepositoryState;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.http.MediaType;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RestController;
34
35 import java.io.IOException;
36 import java.text.DateFormat;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 import java.util.Properties;
40
41 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
42 import static org.springframework.http.HttpStatus.OK;
43
44 /**
45  * Controller for user profile view. The view is restricted to authenticated
46  * users. The view name resolves to page user_profile.jsp which uses Angular.
47  */
48
49 @RestController
50 @RequestMapping("/")
51 public class HealthCheckController extends UnRestrictedBaseController {
52
53     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(HealthCheckController.class);
54     private static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
55     private static final String GIT_PROPERTIES_FILENAME = "git.properties";
56     private FnAppDoaImpl fnAppDoaImpl;
57
58     @Autowired
59     public HealthCheckController(FnAppDoaImpl fnAppDoaImpl) {
60         this.fnAppDoaImpl = fnAppDoaImpl;
61     }
62
63     /**
64      * Obtain the HealthCheck Status from the System.Properties file.
65      * Used by IDNS for redundancy
66      *
67      * @return ResponseEntity The response entity
68      */
69     @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
70     public HealthStatus getHealthCheckStatusForIDNS() {
71         return createCorrespondingStatus();
72     }
73
74     /**
75      * Obtain the  HealthCheck Status from the System.Properties file.
76      *
77      * @return ResponseEntity The response entity
78      */
79     @RequestMapping(value = "rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
80     public HealthStatus getHealthCheck(
81             @PathVariable("User-Agent") String UserAgent,
82             @PathVariable("X-ECOMP-RequestID") String ECOMPRequestID) {
83         LOGGER.debug(EELFLoggerDelegate.debugLogger, "User-Agent ", UserAgent);
84         LOGGER.debug(EELFLoggerDelegate.debugLogger, "X-ECOMP-RequestID ", ECOMPRequestID);
85         return createCorrespondingStatus();
86     }
87
88     @RequestMapping(value = "/commitInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
89     public GitRepositoryState getCommitInfo() throws IOException {
90         Properties properties = new Properties();
91         properties.load(getClass().getClassLoader().getResourceAsStream(GIT_PROPERTIES_FILENAME));
92         return new GitRepositoryState(properties);
93     }
94
95     private HealthStatus createCorrespondingStatus() {
96         logData();
97         try {
98             int count = fnAppDoaImpl.getProfileCount(getUrl(), getUsername(), getPassword());
99             LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::", count);
100             return okStatus();
101         } catch (Exception ex) {
102             String errorMsg = ex.getMessage();
103             LOGGER.error(EELFLoggerDelegate.errorLogger, errorMsg);
104             return errorStatus(errorMsg);
105         }
106     }
107
108     private void logData() {
109         LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
110         LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::", getUrl());
111         LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::", getUsername());
112     }
113
114     private HealthStatus okStatus() {
115         return new HealthStatus(OK, dateFormat.format(new Date()), "health check succeeded");
116     }
117
118     private HealthStatus errorStatus(String msg) {
119         return new HealthStatus(INTERNAL_SERVER_ERROR, dateFormat.format(
120                 new Date()), "health check failed: " + msg);
121     }
122
123     private String getUrl() {
124         return SystemProperties.getProperty("db.connectionURL");
125     }
126
127     private String getUsername() {
128         return SystemProperties.getProperty("db.userName");
129     }
130
131     private String getPassword() {
132         return SystemProperties.getProperty("db.password");
133     }
134 }
135