Merge "Add new endpoint with commit info"
[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.http.MediaType;
29 import org.springframework.web.bind.annotation.PathVariable;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.RestController;
33
34 import java.io.IOException;
35 import java.text.DateFormat;
36 import java.text.SimpleDateFormat;
37 import java.util.Date;
38 import java.util.Properties;
39
40 /**
41  * Controller for user profile view. The view is restricted to authenticated
42  * users. The view name resolves to page user_profile.jsp which uses Angular.
43  */
44
45 @RestController
46 @RequestMapping("/")
47 public class HealthCheckController extends UnRestrictedBaseController {
48
49
50     /**
51      * The logger.
52      */
53     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(HealthCheckController.class);
54
55     /**
56      * The Constant dateFormat.
57      */
58     final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
59
60     private static final String HEALTH_CHECK_PATH = "/healthCheck";
61
62     /**
63      * Model for JSON response with health-check results.
64      */
65     public class HealthStatus {
66         // Either 200 or 500
67         public int statusCode;
68
69         // Additional detail in case of error, empty in case of success.
70         public String message;
71
72         public String date;
73
74         public HealthStatus(int code, String msg) {
75             this.statusCode = code;
76             this.message = msg;
77         }
78
79         public HealthStatus(int code, String date, String msg) {
80             this.statusCode = code;
81             this.message = msg;
82             this.date = date;
83         }
84
85         public int getStatusCode() {
86             return statusCode;
87         }
88
89         public void setStatusCode(int code) {
90             this.statusCode = code;
91         }
92
93         public String getMessage() {
94             return message;
95         }
96
97         public void setMessage(String msg) {
98             this.message = msg;
99         }
100
101         public String getDate() {
102             return date;
103         }
104
105         public void setDate(String date) {
106             this.date = date;
107         }
108
109     }
110
111     @SuppressWarnings("unchecked")
112     public int getProfileCount(String driver, String URL, String username, String password) {
113         FnAppDoaImpl doa = new FnAppDoaImpl();
114         int count = doa.getProfileCount(driver, URL, username, password);
115         return count;
116     }
117
118
119     /**
120      * Obtain the HealthCheck Status from the System.Properties file.
121      * Used by IDNS for redundancy
122      *
123      * @return ResponseEntity The response entity
124      * @throws IOException Signals that an I/O exception has occurred.
125      */
126     @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
127     public HealthStatus gethealthCheckStatusforIDNS() {
128
129         String driver = SystemProperties.getProperty("db.driver");
130         String URL = SystemProperties.getProperty("db.connectionURL");
131         String username = SystemProperties.getProperty("db.userName");
132         String password = SystemProperties.getProperty("db.password");
133
134         LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);
135         LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);
136         LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username);
137         LOGGER.debug(EELFLoggerDelegate.debugLogger, "password::" + password);
138
139
140         HealthStatus healthStatus = null;
141         try {
142             LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
143             int count = getProfileCount(driver, URL, username, password);
144             LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count);
145             healthStatus = new HealthStatus(200, "health check succeeded");
146         } catch (Exception ex) {
147
148             LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
149             healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());
150         }
151         return healthStatus;
152     }
153
154     /**
155      * Obtain the  HealthCheck Status from the System.Properties file.
156      *
157      * @return ResponseEntity The response entity
158      * @throws IOException Signals that an I/O exception has occurred.
159      *                     Project :
160      */
161     @RequestMapping(value = "rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
162     public HealthStatus getHealthCheck(
163             @PathVariable("User-Agent") String UserAgent,
164             @PathVariable("X-ECOMP-RequestID") String ECOMPRequestID) {
165
166         String driver = SystemProperties.getProperty("db.driver");
167         String URL = SystemProperties.getProperty("db.connectionURL");
168         String username = SystemProperties.getProperty("db.userName");
169         String password = SystemProperties.getProperty("db.password");
170
171         LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);
172         LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);
173         LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username);
174         LOGGER.debug(EELFLoggerDelegate.debugLogger, "password::" + password);
175
176
177         HealthStatus healthStatus = null;
178         try {
179             LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
180             LOGGER.debug(EELFLoggerDelegate.debugLogger, "User-Agent" + UserAgent);
181             LOGGER.debug(EELFLoggerDelegate.debugLogger, "X-ECOMP-RequestID" + ECOMPRequestID);
182
183
184             int count = getProfileCount(driver, URL, username, password);
185
186             LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count);
187             healthStatus = new HealthStatus(200, dateFormat.format(new Date()), "health check succeeded");
188         } catch (Exception ex) {
189
190             LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
191             healthStatus = new HealthStatus(500, dateFormat.format(new Date()), "health check failed: " + ex.toString());
192         }
193         return healthStatus;
194     }
195
196     @RequestMapping(value = "/version", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
197     public GitRepositoryState getCommitInfo() throws IOException {
198         Properties properties = new Properties();
199         properties.load(getClass().getClassLoader().getResourceAsStream("git.properties"));
200         return new GitRepositoryState(properties);
201     }
202 }
203