2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 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=========================================================
20 package org.onap.datalake.feeder.controller;
22 import java.io.IOException;
25 import javax.servlet.http.HttpServletResponse;
27 import org.onap.datalake.feeder.domain.Db;
28 import org.onap.datalake.feeder.domain.DbType;
29 import org.onap.datalake.feeder.domain.DesignType;
30 import org.onap.datalake.feeder.domain.Topic;
31 import org.onap.datalake.feeder.repository.DbRepository;
32 import org.onap.datalake.feeder.dto.DbConfig;
33 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
34 import org.onap.datalake.feeder.repository.DbTypeRepository;
35 import org.onap.datalake.feeder.repository.DesignTypeRepository;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.validation.BindingResult;
41 import org.springframework.web.bind.annotation.*;
43 import io.swagger.annotations.ApiOperation;
46 * This controller manages the big data storage settings. All the settings are
54 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
56 //@Api(value = "db", consumes = "application/json", produces = "application/json")
57 public class DbController {
59 private final Logger log = LoggerFactory.getLogger(this.getClass());
60 private static final String DB_NOT_FOUND = "Db not found: ";
63 private DbRepository dbRepository;
66 private DbTypeRepository dbTypeRepository;
69 private DesignTypeRepository designTypeRepository;
74 @ApiOperation(value="Get all databases name")
75 public List<String> list() {
76 Iterable<Db> ret = dbRepository.findAll();
77 List<String> retString = new ArrayList<>();
80 log.info(db.getName());
81 retString.add(db.getName());
89 @ApiOperation(value="Get all databases by encrypt")
90 public List<DbConfig> dblistByEncrypt(@RequestParam boolean encrypt) throws IOException {
91 Iterable<Db> ret = dbRepository.findByEncrypt(encrypt);
92 List<DbConfig> retDbConfig = new ArrayList<>();
94 retDbConfig.add(db.getDbConfig());
99 @GetMapping("/idAndName/{id}")
101 @ApiOperation(value="Get all databases id and name by designTypeId")
102 public Map<Integer, String> listIdAndName(@PathVariable String id) {
103 Optional<DesignType> designType = designTypeRepository.findById(id);
104 Map<Integer, String> map = new HashMap<>();
105 if (designType.isPresent()) {
106 Set<Db> dbs = designType.get().getDbType().getDbs();
107 for (Db item : dbs) {
108 map.put(item.getId(), item.getName());
117 @ApiOperation(value="Create a new database.")
118 public PostReturnBody<DbConfig> createDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
119 if (result.hasErrors()) {
120 sendError(response, 400, "Malformed format of Post body: " + result.toString());
124 /* Db oldDb = dbService.getDb(dbConfig.getName());
126 sendError(response, 400, "Db already exists: " + dbConfig.getName());
130 newdb.setName(dbConfig.getName());
131 newdb.setHost(dbConfig.getHost());
132 newdb.setPort(dbConfig.getPort());
133 newdb.setEnabled(dbConfig.isEnabled());
134 newdb.setLogin(dbConfig.getLogin());
135 newdb.setPass(dbConfig.getPass());
136 newdb.setEncrypt(dbConfig.isEncrypt());
137 if (dbConfig.getDbTypeId().isEmpty()) {
138 sendError(response, 400, "Malformed format of Post body: " + result.toString());
140 Optional<DbType> dbType = dbTypeRepository.findById(dbConfig.getDbTypeId());
141 if (dbType.isPresent()) {
142 newdb.setDbType(dbType.get());
146 if(!dbConfig.getName().equals("Elecsticsearch") || dbConfig.getName().equals("Druid"))
148 newdb.setDatabase(new String(dbConfig.getDatabase()));
150 dbRepository.save(newdb);
151 log.info("Db save ....... name: " + dbConfig.getName());
153 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
154 retMsg = new DbConfig();
155 composeRetMessagefromDbConfig(newdb, retMsg);
156 retBody.setReturnBody(retMsg);
157 retBody.setStatusCode(200);
163 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
164 //need to the the following method to retrieve the topic list
165 @GetMapping("/{dbName}")
167 @ApiOperation(value="Get a database's details.")
168 public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
169 Db db = dbRepository.findByName(dbName);
171 sendError(response, 404, DB_NOT_FOUND + dbName);
178 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
179 //need to the the following method to retrieve the topic list
180 @DeleteMapping("/{id}")
182 @ApiOperation(value="Delete a database.")
183 public void deleteDb(@PathVariable("id") int id, HttpServletResponse response) throws IOException {
185 Optional<Db> delDb = dbRepository.findById(id);
186 if (!delDb.isPresent()) {
187 sendError(response, 404, "Db not found: " + id);
190 Set<Topic> topicRelation = delDb.get().getTopics();
191 topicRelation.clear();
192 dbRepository.delete(delDb.get());
193 response.setStatus(204);
197 //Read topics in a DB
198 @GetMapping("/{dbName}/topics")
200 @ApiOperation(value="Get a database's all topics.")
201 public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
204 Db db = dbRepository.findByName(dbName);
205 topics = db.getTopics();
206 } catch(Exception ex) {
207 sendError(response, 404, "DB: " + dbName + " or Topics not found");
208 return Collections.emptySet();
217 @ApiOperation(value="Update a database.")
218 public PostReturnBody<DbConfig> updateDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
220 if (result.hasErrors()) {
221 sendError(response, 400, "Error parsing DB: " + result.toString());
225 Db oldDb = dbRepository.findById(dbConfig.getId()).get();
227 sendError(response, 404, DB_NOT_FOUND + dbConfig.getName());
231 oldDb.setEnabled(dbConfig.isEnabled());
232 oldDb.setName(dbConfig.getName());
233 oldDb.setHost(dbConfig.getHost());
234 oldDb.setPort(dbConfig.getPort());
235 oldDb.setLogin(dbConfig.getLogin());
236 oldDb.setPass(dbConfig.getPass());
237 oldDb.setEncrypt(dbConfig.isEncrypt());
238 if (dbConfig.getDbTypeId().isEmpty()) {
239 sendError(response, 400, "Malformed format of Post body: " + result.toString());
241 Optional<DbType> dbType = dbTypeRepository.findById(dbConfig.getDbTypeId());
242 if (dbType.isPresent()) {
243 oldDb.setDbType(dbType.get());
246 if (!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid")) {
247 oldDb.setDatabase(dbConfig.getDatabase());
250 dbRepository.save(oldDb);
252 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
253 retMsg = new DbConfig();
254 composeRetMessagefromDbConfig(oldDb, retMsg);
255 retBody.setReturnBody(retMsg);
256 retBody.setStatusCode(200);
263 @PostMapping("/verify")
265 @ApiOperation(value="Database connection verification")
266 public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
272 response.setStatus(501);
276 private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
278 dbConfigMsg.setId(db.getId());
279 dbConfigMsg.setName(db.getName());
280 dbConfigMsg.setHost(db.getHost());
281 dbConfigMsg.setEnabled(db.isEnabled());
282 dbConfigMsg.setPort(db.getPort());
283 dbConfigMsg.setLogin(db.getLogin());
284 dbConfigMsg.setDatabase(db.getDatabase());
285 dbConfigMsg.setDbTypeId(db.getDbType().getId());
286 dbConfigMsg.setPass(db.getPass());
290 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
292 response.sendError(sc, msg);