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.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;
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;
41 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
42 import static org.springframework.http.HttpStatus.OK;
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.
51 public class HealthCheckController extends UnRestrictedBaseController {
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;
59 public HealthCheckController(FnAppDoaImpl fnAppDoaImpl) {
60 this.fnAppDoaImpl = fnAppDoaImpl;
64 * Obtain the HealthCheck Status from the System.Properties file.
65 * Used by IDNS for redundancy
67 * @return ResponseEntity The response entity
69 @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
70 public HealthStatus getHealthCheckStatusForIDNS() {
71 return createCorrespondingStatus();
75 * Obtain the HealthCheck Status from the System.Properties file.
77 * @return ResponseEntity The response entity
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();
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);
95 private HealthStatus createCorrespondingStatus() {
98 int count = fnAppDoaImpl.getProfileCount(getUrl(), getUsername(), getPassword());
99 LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::", count);
101 } catch (Exception ex) {
102 String errorMsg = ex.getMessage();
103 LOGGER.error(EELFLoggerDelegate.errorLogger, errorMsg);
104 return errorStatus(errorMsg);
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());
114 private HealthStatus okStatus() {
115 return new HealthStatus(OK, dateFormat.format(new Date()), "health check succeeded");
118 private HealthStatus errorStatus(String msg) {
119 return new HealthStatus(INTERNAL_SERVER_ERROR, dateFormat.format(
120 new Date()), "health check failed: " + msg);
123 private String getUrl() {
124 return SystemProperties.getProperty("db.connectionURL");
127 private String getUsername() {
128 return SystemProperties.getProperty("db.userName");
131 private String getPassword() {
132 return SystemProperties.getProperty("db.password");