e71ba6b01a1bdf4b4eac6012ce4422c0365abb37
[dcaegen2/services.git] /
1 /*
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
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.datalake.des.controller;
22
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;
33 import java.util.Map;
34 import java.util.Properties;
35
36 import javax.servlet.http.HttpServletResponse;
37
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;
58
59 import io.swagger.annotations.Api;
60 import io.swagger.annotations.ApiOperation;
61
62 /**
63  * Data Exposure WS.
64  *
65  * @author Kai Lu
66  *
67  */
68 @RestController
69 @RequestMapping(value = "/exposure", produces = { MediaType.APPLICATION_JSON_VALUE })
70 @Api(value = "/exposure", consumes = "application/json", produces = "application/json")
71 public class DataExposureController {
72
73         private final Logger log = LoggerFactory.getLogger(this.getClass());
74         @Autowired
75         private DataExposureService dataExposureService;
76         @Autowired
77         private DataExposureRepository dataExposureRepository;
78
79     /**
80      * serve API.
81      *
82      * @param serviceId serviceId
83      * @param requestMap requestMap
84      * @param bindingResult bindingResult
85      * @param response response
86          * @return message that application is started
87          * @throws IOException
88          * @throws SQLException
89      *
90      */
91         @PostMapping("/{serviceId}")
92         @ResponseBody
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().getPrestoCatalog(),
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<>();
117                 int count = 0;
118                 while (rs.next()) {
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);
125                         }
126                         result.add(entry);
127                         count++;
128                 }
129                 ret.put("result", result);
130                 ret.put("result_count", count);
131                 return ret;
132         }
133
134     /**
135      * queryAllDataExposure API.
136      *
137          * @return data exposure config list
138      *
139      */
140         @GetMapping("")
141         @ResponseBody
142         @ApiOperation(value = "Datalake Data Exposure list")
143         public List<DataExposureConfig> queryAllDataExposure() {
144                 return dataExposureService.queryAllDataExposure();
145         }
146
147     /**
148      * query API.
149      *
150      * @param id id
151      * @param response HttpServletResponse
152          * @return DataExposureConfig
153          * @throws IOException
154      *
155      */
156         @GetMapping("/{id}")
157         @ResponseBody
158         @ApiOperation(value = "Get Detail of DataExposure")
159         public DataExposureConfig queryAllDataExposure(@PathVariable String id, HttpServletResponse response)
160                         throws IOException {
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);
165                         return null;
166                 } else {
167                         log.info("ResponseBody......" + oldDataExposure.getDataExposureConfig());
168                         return oldDataExposure.getDataExposureConfig();
169                 }
170         }
171
172     /**
173      * delete Kfaka API.
174      *
175      * @param id id
176      * @param response HttpServletResponse
177          * @throws IOException
178      *
179      */
180         @DeleteMapping("/{id}")
181         @ResponseBody
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);
187                 } else {
188                         dataExposureRepository.delete(oldDataExposure);
189                         response.setStatus(204);
190                 }
191         }
192
193     /**
194      * Create a DataExposure.
195      *
196      * @param dataExposureConfig dataExposureConfig
197      * @param result BindingResult
198      * @param response HttpServletResponse
199          * @return DataExposureConfig
200          * @throws IOException
201      *
202      */
203         @PostMapping("")
204         @ResponseBody
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());
210                         return null;
211                 }
212                 DataExposure oldDataExposure = dataExposureService.getDataExposureById(dataExposureConfig.getId());
213                 if (oldDataExposure != null) {
214                         sendError(response, 400, "DataExposure is exist " + dataExposureConfig.getId());
215                         return null;
216                 } else {
217                         DataExposure dataExposure = null;
218                         try {
219                                 dataExposure = dataExposureService.fillDataExposureConfiguration(dataExposureConfig);
220                         } catch (Exception e) {
221                                 log.debug("FillDataExposureConfiguration failed", e.getMessage());
222                                 sendError(response, 400, "Error FillDataExposureConfiguration: " + e.getMessage());
223                                 return null;
224                         }
225                         dataExposureRepository.save(dataExposure);
226                         log.info("Kafka save successed");
227                         return mkPostReturnBody(200, dataExposure);
228                 }
229         }
230
231     /**
232      * Update a DataExposure.
233      *
234      * @param dataExposureConfig dataExposureConfig
235      * @param result BindingResult
236      * @param id id
237      * @param response HttpServletResponse
238          * @return DataExposureConfig
239          * @throws IOException
240      *
241      */
242         @PutMapping("/{id}")
243         @ResponseBody
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());
249                         return null;
250                 }
251                 DataExposure oldDataExposure = dataExposureService.getDataExposureById(id);
252                 if (oldDataExposure == null) {
253                         sendError(response, 400, "DataExposure not found: " + id);
254                         return null;
255                 } else {
256                         try {
257                                 dataExposureService.fillDataExposureConfiguration(dataExposureConfig, oldDataExposure);
258                         } catch (Exception e) {
259                                 log.debug("FillDataExposureConfiguration failed", e.getMessage());
260                                 sendError(response, 400, "Error FillDataExposureConfiguration: " + e.getMessage());
261                                 return null;
262                         }
263                         dataExposureRepository.save(oldDataExposure);
264                         log.info("DataExposure update successed");
265                         return mkPostReturnBody(200, oldDataExposure);
266                 }
267         }
268
269         private PostReturnBody<DataExposureConfig> mkPostReturnBody(int statusCode, DataExposure dataExposure) {
270                 PostReturnBody<DataExposureConfig> retBody = new PostReturnBody<>();
271                 retBody.setStatusCode(statusCode);
272                 retBody.setReturnBody(dataExposure.getDataExposureConfig());
273                 return retBody;
274         }
275
276         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
277                 log.info(msg);
278                 response.sendError(sc, msg);
279         }
280 }