2 * ============LICENSE_START=======================================================
3 * ONAP : Data Extraction Service
4 * ================================================================================
5 * Copyright 2020 China Mobile
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.datalake.des.controller;
23 import java.io.IOException;
24 import java.sql.Connection;
25 import java.sql.DriverManager;
26 import java.sql.ResultSet;
27 import java.sql.ResultSetMetaData;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Properties;
36 import javax.servlet.http.HttpServletResponse;
38 import org.apache.commons.text.StringSubstitutor;
39 import org.onap.datalake.des.domain.DataExposure;
40 import org.onap.datalake.des.dto.DataExposureConfig;
41 import org.onap.datalake.des.repository.DataExposureRepository;
42 import org.onap.datalake.des.service.DataExposureService;
43 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.MediaType;
48 import org.springframework.validation.BindingResult;
49 import org.springframework.web.bind.annotation.DeleteMapping;
50 import org.springframework.web.bind.annotation.GetMapping;
51 import org.springframework.web.bind.annotation.PathVariable;
52 import org.springframework.web.bind.annotation.PostMapping;
53 import org.springframework.web.bind.annotation.PutMapping;
54 import org.springframework.web.bind.annotation.RequestBody;
55 import org.springframework.web.bind.annotation.RequestMapping;
56 import org.springframework.web.bind.annotation.ResponseBody;
57 import org.springframework.web.bind.annotation.RestController;
59 import io.swagger.annotations.Api;
60 import io.swagger.annotations.ApiOperation;
69 @RequestMapping(value = "/exposure", produces = { MediaType.APPLICATION_JSON_VALUE })
70 @Api(value = "/exposure", consumes = "application/json", produces = "application/json")
71 public class DataExposureController {
73 private final Logger log = LoggerFactory.getLogger(this.getClass());
75 private DataExposureService dataExposureService;
77 private DataExposureRepository dataExposureRepository;
82 * @param serviceId serviceId
83 * @param requestMap requestMap
84 * @param bindingResult bindingResult
85 * @param response response
86 * @return message that application is started
88 * @throws SQLException
91 @PostMapping("/{serviceId}")
93 @ApiOperation(value = "Datalake Data Exposure Service.")
94 public HashMap<String, Object> serve(@PathVariable String serviceId, @RequestBody Map<String, String> requestMap,
95 BindingResult bindingResult, HttpServletResponse response) throws IOException, SQLException {
96 log.info("Going to start Datalake Data Exposure Service ... requestMap=" + requestMap);
97 HashMap<String, Object> ret = new HashMap<>();
98 ret.put("request", requestMap);
99 DataExposure dataExposure = dataExposureService.getDataExposure(serviceId);
100 String sqlTemplate = dataExposure.getSqlTemplate();
101 StringSubstitutor sub = new StringSubstitutor(requestMap);
102 String query = sub.replace(sqlTemplate);
103 log.info("Going to start Datalake Data Exposure Service ... query=" + query);
104 // https://prestodb.io/docs/current/installation/jdbc.html
105 String url = String.format("jdbc:presto://dl-presto:8080/%s/%s", dataExposure.getDb().getHost(),
106 dataExposure.getDb().getDatabase());
107 Properties properties = new Properties();
108 properties.setProperty("user", "test");
109 // properties.setProperty("password", "secret");
110 // properties.setProperty("SSL", "true");
111 Connection connection = DriverManager.getConnection(url, properties);
112 Statement stmt = connection.createStatement();
113 ResultSet rs = stmt.executeQuery(query);
114 ResultSetMetaData meta = rs.getMetaData();
115 int columnCount = meta.getColumnCount();
116 ArrayList<HashMap<String, Object>> result = new ArrayList<>();
119 HashMap<String, Object> entry = new HashMap<>();
120 for (int i = 1; i <= columnCount; i++) {
121 String label = meta.getColumnLabel(i);
122 Object value = rs.getObject(i);
123 entry.put(label, value);
124 log.info(label + "\t" + value);
129 ret.put("result", result);
130 ret.put("result_count", count);
135 * queryAllDataExposure API.
137 * @return data exposure config list
142 @ApiOperation(value = "Datalake Data Exposure list")
143 public List<DataExposureConfig> queryAllDataExposure() {
144 return dataExposureService.queryAllDataExposure();
151 * @param response HttpServletResponse
152 * @return DataExposureConfig
153 * @throws IOException
158 @ApiOperation(value = "Get Detail of DataExposure")
159 public DataExposureConfig queryAllDataExposure(@PathVariable String id, HttpServletResponse response)
161 log.info("Get Detail of DataExposure Starting.....");
162 DataExposure oldDataExposure = dataExposureService.getDataExposureById(id);
163 if (oldDataExposure == null) {
164 sendError(response, 400, "DataExposure not found, ID: " + id);
167 log.info("ResponseBody......" + oldDataExposure.getDataExposureConfig());
168 return oldDataExposure.getDataExposureConfig();
176 * @param response HttpServletResponse
177 * @throws IOException
180 @DeleteMapping("/{id}")
182 @ApiOperation(value = "delete a dataExposure.")
183 public void deleteKafka(@PathVariable String id, HttpServletResponse response) throws IOException {
184 DataExposure oldDataExposure = dataExposureService.getDataExposureById(id);
185 if (oldDataExposure == null) {
186 sendError(response, 400, "DataExposure not found, ID: " + id);
188 dataExposureRepository.delete(oldDataExposure);
189 response.setStatus(204);
194 * Create a DataExposure.
196 * @param dataExposureConfig dataExposureConfig
197 * @param result BindingResult
198 * @param response HttpServletResponse
199 * @return DataExposureConfig
200 * @throws IOException
205 @ApiOperation(value = "Create a DataExposure.")
206 public PostReturnBody<DataExposureConfig> createDataExposure(@RequestBody DataExposureConfig dataExposureConfig,
207 BindingResult result, HttpServletResponse response) throws IOException {
208 if (result.hasErrors()) {
209 sendError(response, 400, "Error parsing DataExposureConfig : " + result.toString());
212 DataExposure oldDataExposure = dataExposureService.getDataExposureById(dataExposureConfig.getId());
213 if (oldDataExposure != null) {
214 sendError(response, 400, "DataExposure is exist " + dataExposureConfig.getId());
217 DataExposure dataExposure = null;
219 dataExposure = dataExposureService.fillDataExposureConfiguration(dataExposureConfig);
220 } catch (Exception e) {
221 log.debug("FillDataExposureConfiguration failed", e.getMessage());
222 sendError(response, 400, "Error FillDataExposureConfiguration: " + e.getMessage());
225 dataExposureRepository.save(dataExposure);
226 log.info("Kafka save successed");
227 return mkPostReturnBody(200, dataExposure);
232 * Update a DataExposure.
234 * @param dataExposureConfig dataExposureConfig
235 * @param result BindingResult
237 * @param response HttpServletResponse
238 * @return DataExposureConfig
239 * @throws IOException
244 @ApiOperation(value = "Update a DataExposure.")
245 public PostReturnBody<DataExposureConfig> updateDataExposure(@RequestBody DataExposureConfig dataExposureConfig,
246 BindingResult result, @PathVariable String id, HttpServletResponse response) throws IOException {
247 if (result.hasErrors()) {
248 sendError(response, 400, "Error parsing DataExposureConfig : " + result.toString());
251 DataExposure oldDataExposure = dataExposureService.getDataExposureById(id);
252 if (oldDataExposure == null) {
253 sendError(response, 400, "DataExposure not found: " + id);
257 dataExposureService.fillDataExposureConfiguration(dataExposureConfig, oldDataExposure);
258 } catch (Exception e) {
259 log.debug("FillDataExposureConfiguration failed", e.getMessage());
260 sendError(response, 400, "Error FillDataExposureConfiguration: " + e.getMessage());
263 dataExposureRepository.save(oldDataExposure);
264 log.info("DataExposure update successed");
265 return mkPostReturnBody(200, oldDataExposure);
269 private PostReturnBody<DataExposureConfig> mkPostReturnBody(int statusCode, DataExposure dataExposure) {
270 PostReturnBody<DataExposureConfig> retBody = new PostReturnBody<>();
271 retBody.setStatusCode(statusCode);
272 retBody.setReturnBody(dataExposure.getDataExposureConfig());
276 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
278 response.sendError(sc, msg);