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.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;
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;
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.
47 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";
63 * Model for JSON response with health-check results.
65 public class HealthStatus {
67 public int statusCode;
69 // Additional detail in case of error, empty in case of success.
70 public String message;
74 public HealthStatus(int code, String msg) {
75 this.statusCode = code;
79 public HealthStatus(int code, String date, String msg) {
80 this.statusCode = code;
85 public int getStatusCode() {
89 public void setStatusCode(int code) {
90 this.statusCode = code;
93 public String getMessage() {
97 public void setMessage(String msg) {
101 public String getDate() {
105 public void setDate(String date) {
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);
120 * Obtain the HealthCheck Status from the System.Properties file.
121 * Used by IDNS for redundancy
123 * @return ResponseEntity The response entity
124 * @throws IOException Signals that an I/O exception has occurred.
126 @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
127 public HealthStatus gethealthCheckStatusforIDNS() {
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");
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);
140 HealthStatus healthStatus = null;
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) {
148 LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
149 healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());
155 * Obtain the HealthCheck Status from the System.Properties file.
157 * @return ResponseEntity The response entity
158 * @throws IOException Signals that an I/O exception has occurred.
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) {
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");
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);
177 HealthStatus healthStatus = null;
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);
184 int count = getProfileCount(driver, URL, username, password);
186 LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count);
187 healthStatus = new HealthStatus(200, dateFormat.format(new Date()), "health check succeeded");
188 } catch (Exception ex) {
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());
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);