Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / controller / HealthCheckController.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.vid.controller;\r
22 \r
23 import java.io.IOException;\r
24 import java.sql.Connection;\r
25 import java.sql.DriverManager;\r
26 import java.sql.SQLException;\r
27 import java.text.DateFormat;\r
28 import java.text.SimpleDateFormat;\r
29 import java.util.Date;\r
30 \r
31 import org.springframework.http.MediaType;\r
32 import org.springframework.web.bind.annotation.PathVariable;\r
33 import org.springframework.web.bind.annotation.RequestMapping;\r
34 import org.springframework.web.bind.annotation.RequestMethod;\r
35 import org.springframework.web.bind.annotation.RestController;\r
36 \r
37 import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController;\r
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
39 import org.openecomp.portalsdk.core.util.SystemProperties;\r
40 import org.openecomp.vid.dao.FnAppDoaImpl;\r
41 \r
42 /**\r
43  * Controller for user profile view. The view is restricted to authenticated\r
44  * users. The view name resolves to page user_profile.jsp which uses Angular.\r
45  */\r
46 \r
47 @RestController\r
48 @RequestMapping("/")\r
49 public class HealthCheckController extends UnRestrictedBaseController {\r
50 \r
51 \r
52         /** The logger. */\r
53                 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);\r
54                 \r
55                 /** The Constant dateFormat. */\r
56                 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
57                 \r
58            private static final String HEALTH_CHECK_PATH = "/healthCheck";\r
59            \r
60            /**\r
61                  * Model for JSON response with health-check results.\r
62                  */\r
63                 public class HealthStatus {\r
64                         // Either 200 or 500\r
65                         public int statusCode;\r
66                         \r
67                         // Additional detail in case of error, empty in case of success.\r
68                         public String message;\r
69                         \r
70                         public String date;\r
71 \r
72                         public HealthStatus(int code, String msg) {\r
73                                 this.statusCode = code;\r
74                                 this.message = msg;\r
75                         }\r
76                         \r
77                         public HealthStatus(int code,String date, String msg) {\r
78                                 this.statusCode = code;\r
79                                 this.message = msg;\r
80                                 this.date=date;\r
81                         }\r
82 \r
83                         public int getStatusCode() {\r
84                                 return statusCode;\r
85                         }\r
86 \r
87                         public void setStatusCode(int code) {\r
88                                 this.statusCode = code;\r
89                         }\r
90 \r
91                         public String getMessage() {\r
92                                 return message;\r
93                         }\r
94 \r
95                         public void setMessage(String msg) {\r
96                                 this.message = msg;\r
97                         }\r
98                         \r
99                         public String getDate() {\r
100                                 return date;\r
101                         }\r
102 \r
103                         public void setDate(String date) {\r
104                                 this.date = date;\r
105                         }\r
106 \r
107                 }\r
108   \r
109            @SuppressWarnings("unchecked")\r
110                 public int getProfileCount(String driver, String URL, String username, String password) {\r
111                    FnAppDoaImpl doa= new FnAppDoaImpl();\r
112                    int count= doa.getProfileCount(driver,URL,username,password);\r
113                         return count;\r
114                 }\r
115            \r
116            \r
117            \r
118                 /**\r
119                  * Obtain the HealthCheck Status from the System.Properties file.\r
120                  * Used by IDNS for redundancy\r
121                  * @return ResponseEntity The response entity\r
122                  * @throws IOException Signals that an I/O exception has occurred.\r
123                  * @throws InterruptedException the interrupted exception\r
124                  */     \r
125                 @RequestMapping(value="/healthCheck",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)   \r
126                 public HealthStatus gethealthCheckStatusforIDNS() throws IOException, InterruptedException {\r
127 \r
128                         String driver = SystemProperties.getProperty("db.driver");\r
129                         String URL = SystemProperties.getProperty("db.connectionURL");\r
130                         String username = SystemProperties.getProperty("db.userName");\r
131                         String password = SystemProperties.getProperty("db.password");\r
132                         \r
133                         logger.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);\r
134                         logger.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);\r
135                         logger.debug(EELFLoggerDelegate.debugLogger, "username::" + username);\r
136                         logger.debug(EELFLoggerDelegate.debugLogger,"password::" + password);\r
137                         \r
138                         \r
139                         HealthStatus healthStatus = null;   \r
140                         try {\r
141                                 logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check");\r
142                                 int count=getProfileCount(driver,URL,username,password);\r
143                                 logger.debug(EELFLoggerDelegate.debugLogger,"count:::"+count);\r
144                                 healthStatus = new HealthStatus(200, "health check succeeded");\r
145                         } catch (Exception ex) {\r
146                         \r
147                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);\r
148                                 healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());\r
149                         }\r
150                         return healthStatus;\r
151                 }\r
152                 \r
153                 /**\r
154                  * Obtain the  HealthCheck Status from the System.Properties file.\r
155                  *\r
156                  * @return ResponseEntity The response entity\r
157                  * @throws IOException Signals that an I/O exception has occurred.\r
158                  * @throws InterruptedException the interrupted exception\r
159                  * Project :\r
160                  */     \r
161                 @RequestMapping(value="rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)      \r
162                 public HealthStatus getHealthCheck(\r
163                                 @PathVariable("User-Agent") String UserAgent,\r
164                                 @PathVariable("X-ECOMP-RequestID") String ECOMPRequestID) throws IOException, InterruptedException {\r
165 \r
166                         String driver = SystemProperties.getProperty("db.driver");\r
167                         String URL = SystemProperties.getProperty("db.connectionURL");\r
168                         String username = SystemProperties.getProperty("db.userName");\r
169                         String password = SystemProperties.getProperty("db.password");\r
170                         \r
171                                 logger.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);\r
172                                 logger.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);\r
173                                 logger.debug(EELFLoggerDelegate.debugLogger, "username::" + username);\r
174                                 logger.debug(EELFLoggerDelegate.debugLogger,"password::" + password);\r
175                                 \r
176                         \r
177                         HealthStatus healthStatus = null;   \r
178                         try {\r
179                                 logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check");\r
180                                 logger.debug(EELFLoggerDelegate.debugLogger, "User-Agent" + UserAgent);\r
181                                 logger.debug(EELFLoggerDelegate.debugLogger, "X-ECOMP-RequestID" + ECOMPRequestID);\r
182 \r
183                                 \r
184                                 int count=getProfileCount(driver,URL,username,password);\r
185                                 \r
186                                 logger.debug(EELFLoggerDelegate.debugLogger,"count:::"+count);\r
187                                 healthStatus = new HealthStatus(200,dateFormat.format(new Date()) ,"health check succeeded");\r
188                         } catch (Exception ex) {\r
189                         \r
190                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);\r
191                                 healthStatus = new HealthStatus(500,dateFormat.format(new Date()),"health check failed: " + ex.toString());\r
192                         }\r
193                         return healthStatus;\r
194                 }\r
195 }\r
196 \r