blob: c401d320802ea9d89dd587ee666150c60f19319e [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
22
23package org.onap.osam.controllers;
24
25import com.fasterxml.jackson.databind.ObjectMapper;
26import org.onap.osam.aai.AaiResponse;
27import org.onap.osam.services.IAaiService;
28import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
29import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30import org.onap.portalsdk.core.util.SystemProperties;
31import org.onap.osam.dao.FnAppDoaImpl;
32import org.onap.osam.model.GitRepositoryState;
33import org.springframework.beans.factory.annotation.Autowired;
34import org.springframework.http.HttpStatus;
35import org.springframework.http.MediaType;
36import org.springframework.http.ResponseEntity;
37import org.springframework.web.bind.annotation.PathVariable;
38import org.springframework.web.bind.annotation.RequestMapping;
39import org.springframework.web.bind.annotation.RequestMethod;
40import org.springframework.web.bind.annotation.RestController;
41
42import javax.servlet.http.HttpServletRequest;
43import java.io.IOException;
44import java.text.DateFormat;
45import java.text.SimpleDateFormat;
46import java.util.Date;
47import java.util.Properties;
48
49@RestController
50@RequestMapping("/")
51public class PodDemoController extends UnRestrictedBaseController {
52
53 @Autowired
54 private IAaiService aaiService;
55
56 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(PodDemoController.class);
57
58 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
59
60 private static final String HEALTH_CHECK_PATH = "/healthCheck";
61 private static final String GIT_PROPERTIES_FILENAME = "git.properties";
62
63 /**
64 * Model for JSON response with health-check results.
65 */
66 public class HealthStatus {
67 // Either 200 or 500
68 public int statusCode;
69
70 // Additional detail in case of error, empty in case of success.
71 public String message;
72
73 public String date;
74
75 public HealthStatus(int code, String msg) {
76 this.statusCode = code;
77 this.message = msg;
78 }
79
80 public HealthStatus(int code, String date, String msg) {
81 this.statusCode = code;
82 this.message = msg;
83 this.date = date;
84 }
85
86 public int getStatusCode() {
87 return statusCode;
88 }
89
90 public void setStatusCode(int code) {
91 this.statusCode = code;
92 }
93
94 public String getMessage() {
95 return message;
96 }
97
98 public void setMessage(String msg) {
99 this.message = msg;
100 }
101
102 public String getDate() {
103 return date;
104 }
105
106 public void setDate(String date) {
107 this.date = date;
108 }
109
110 }
111
112 @SuppressWarnings("unchecked")
113 public int getProfileCount(String driver, String URL, String username, String password) {
114 FnAppDoaImpl doa = new FnAppDoaImpl();
115 int count = doa.getProfileCount(driver, URL, username, password);
116 return count;
117 }
118
119
120 /**
121 * Obtain the HealthCheck Status from the System.Properties file.
122 * Used by IDNS for redundancy
123 *
124 * @return ResponseEntity The response entity
125 * @throws IOException Signals that an I/O exception has occurred.
126 */
127 @RequestMapping(value = "/healthCheck", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
128 public HealthStatus gethealthCheckStatusforIDNS() {
129
130 String driver = SystemProperties.getProperty("db.driver");
131 String URL = SystemProperties.getProperty("db.connectionURL");
132 String username = SystemProperties.getProperty("db.userName");
133 String password = SystemProperties.getProperty("db.password");
134
135 LOGGER.debug(EELFLoggerDelegate.debugLogger, "driver ::" + driver);
136 LOGGER.debug(EELFLoggerDelegate.debugLogger, "URL::" + URL);
137 LOGGER.debug(EELFLoggerDelegate.debugLogger, "username::" + username);
138 LOGGER.debug(EELFLoggerDelegate.debugLogger, "password::" + password);
139
140
141 HealthStatus healthStatus = null;
142 try {
143 LOGGER.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
144 int count = getProfileCount(driver, URL, username, password);
145 LOGGER.debug(EELFLoggerDelegate.debugLogger, "count:::" + count);
146 healthStatus = new HealthStatus(200, "health check succeeded");
147 } catch (Exception ex) {
148
149 LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
150 healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());
151 }
152 return healthStatus;
153 }
154
155 @RequestMapping(value = "/pods/getAndSaveAll", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
156 public ResponseEntity<String> getAndSaveAllPods() {
157
158 return (new ResponseEntity<>("", HttpStatus.OK));
159 }
160
161 @RequestMapping(value = {"/aai_test"}, method = RequestMethod.GET)
162 public ResponseEntity<String> getAicZones(HttpServletRequest request) throws IOException {
163 LOGGER.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== getAicZones controller start");
164 AaiResponse response = aaiService.getAaiZones();
165 return aaiResponseToResponseEntity(response);
166 }
167
168 private ResponseEntity<String> aaiResponseToResponseEntity(AaiResponse aaiResponseData)
169 throws IOException {
170 ResponseEntity<String> responseEntity;
171 ObjectMapper objectMapper = new ObjectMapper();
172 if (aaiResponseData.getHttpCode() == 200) {
173 responseEntity = new ResponseEntity<String>(objectMapper.writeValueAsString(aaiResponseData.getT()), HttpStatus.OK);
174 } else {
175 responseEntity = new ResponseEntity<String>(aaiResponseData.getErrorMessage(), HttpStatus.valueOf(aaiResponseData.getHttpCode()));
176 }
177 return responseEntity;
178 }
179
180 @RequestMapping(value = "/commitInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
181 public GitRepositoryState getCommitInfo() throws IOException {
182 Properties properties = new Properties();
183 properties.load(getClass().getClassLoader().getResourceAsStream(GIT_PROPERTIES_FILENAME));
184 return new GitRepositoryState(properties);
185 }
186}
187