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