org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / 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.controller;
22
23 import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController;
24 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.openecomp.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                  * @throws InterruptedException the interrupted exception
120                  */     
121                 @RequestMapping(value="/healthCheck",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)   
122                 public HealthStatus gethealthCheckStatusforIDNS() throws IOException, InterruptedException {
123
124                         String driver = SystemProperties.getProperty("db.driver");
125                         String URL = SystemProperties.getProperty("db.connectionURL");
126                         String username = SystemProperties.getProperty("db.userName");
127                         String password = SystemProperties.getProperty("db.password");
128                         
129                         LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);
130                         LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);
131                         LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username);
132                         LOGGER.debug(EELFLoggerDelegate.debugLogger,"password::" + password);
133                         
134                         
135                         HealthStatus healthStatus = null;   
136                         try {
137                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
138                                 int count=getProfileCount(driver,URL,username,password);
139                                 LOGGER.debug(EELFLoggerDelegate.debugLogger,"count:::"+count);
140                                 healthStatus = new HealthStatus(200, "health check succeeded");
141                         } catch (Exception ex) {
142                         
143                                 LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
144                                 healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());
145                         }
146                         return healthStatus;
147                 }
148                 
149                 /**
150                  * Obtain the  HealthCheck Status from the System.Properties file.
151                  *
152                  * @return ResponseEntity The response entity
153                  * @throws IOException Signals that an I/O exception has occurred.
154                  * @throws InterruptedException the interrupted exception
155                  * Project :
156                  */     
157                 @RequestMapping(value="rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)      
158                 public HealthStatus getHealthCheck(
159                                 @PathVariable("User-Agent") String UserAgent,
160                                 @PathVariable("X-ECOMP-RequestID") String ECOMPRequestID) throws IOException, InterruptedException {
161
162                         String driver = SystemProperties.getProperty("db.driver");
163                         String URL = SystemProperties.getProperty("db.connectionURL");
164                         String username = SystemProperties.getProperty("db.userName");
165                         String password = SystemProperties.getProperty("db.password");
166                         
167                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);
168                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);
169                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username);
170                                 LOGGER.debug(EELFLoggerDelegate.debugLogger,"password::" + password);
171                                 
172                         
173                         HealthStatus healthStatus = null;   
174                         try {
175                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
176                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "User-Agent" + UserAgent);
177                                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "X-ECOMP-RequestID" + ECOMPRequestID);
178
179                                 
180                                 int count=getProfileCount(driver,URL,username,password);
181                                 
182                                 LOGGER.debug(EELFLoggerDelegate.debugLogger,"count:::"+count);
183                                 healthStatus = new HealthStatus(200,dateFormat.format(new Date()) ,"health check succeeded");
184                         } catch (Exception ex) {
185                         
186                                 LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
187                                 healthStatus = new HealthStatus(500,dateFormat.format(new Date()),"health check failed: " + ex.toString());
188                         }
189                         return healthStatus;
190                 }
191 }
192