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