2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.controllers;
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;
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;
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.
48 public class HealthCheckController extends UnRestrictedBaseController {
53 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(HealthCheckController.class);
56 * The Constant dateFormat.
58 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
60 private static final String HEALTH_CHECK_PATH = "/healthCheck";
61 private static final String GIT_PROPERTIES_FILENAME = "git.properties";
64 * Model for JSON response with health-check results.
66 public class HealthStatus {
68 public int statusCode;
70 // Additional detail in case of error, empty in case of success.
71 public String message;
75 public HealthStatus(int code, String msg) {
76 this.statusCode = code;
80 public HealthStatus(int code, String date, String msg) {
81 this.statusCode = code;
86 public int getStatusCode() {
90 public void setStatusCode(int code) {
91 this.statusCode = code;
94 public String getMessage() {
98 public void setMessage(String msg) {
102 public String getDate() {
106 public void setDate(String date) {
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);
121 * Obtain the HealthCheck Status from the System.Properties file.
122 * Used by IDNS for redundancy
124 * @return ResponseEntity The response entity
125 * @throws IOException Signals that an I/O exception has occurred.
127 @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
128 public HealthStatus gethealthCheckStatusforIDNS() {
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");
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);
141 HealthStatus healthStatus = null;
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) {
149 LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
150 healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());
156 * Obtain the HealthCheck Status from the System.Properties file.
158 * @return ResponseEntity The response entity
159 * @throws IOException Signals that an I/O exception has occurred.
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) {
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");
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);
178 HealthStatus healthStatus = null;
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);
185 int count = getProfileCount(driver, URL, username, password);
187 LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count);
188 healthStatus = new HealthStatus(200, dateFormat.format(new Date()), "health check succeeded");
189 } catch (Exception ex) {
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());
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);