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 io.swagger.annotations.*;
28 import org.onap.datalake.feeder.domain.Db;
29 import org.onap.datalake.feeder.domain.Topic;
30 import org.onap.datalake.feeder.repository.DbRepository;
31 import org.onap.datalake.feeder.repository.TopicRepository;
32 import org.onap.datalake.feeder.service.DbService;
33 import org.onap.datalake.feeder.controller.domain.DbConfig;
34 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.MediaType;
39 import org.springframework.validation.BindingResult;
40 import org.springframework.web.bind.annotation.*;
42 import io.swagger.annotations.Api;
43 import io.swagger.annotations.ApiOperation;
44 import io.swagger.annotations.ApiResponse;
45 import io.swagger.annotations.ApiResponses;
48 * This controller manages the big data storage settings. All the settings are
56 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
58 //@Api(value = "db", consumes = "application/json", produces = "application/json")
59 public class DbController {
61 private final Logger log = LoggerFactory.getLogger(this.getClass());
64 private DbRepository dbRepository;
67 private TopicRepository topicRepository;
70 private DbService dbService;
75 @ApiOperation(value="Gat all databases name")
76 //public Iterable<Db> list() throws IOException {
77 public List<String> list() throws IOException {
78 Iterable<Db> ret = dbRepository.findAll();
79 List<String> retString = new ArrayList<>();
82 log.info(db.getName());
83 retString.add(db.getName());
92 @ApiOperation(value="Create a new database.")
93 public PostReturnBody<DbConfig> createDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
94 if (result.hasErrors()) {
95 sendError(response, 400, "Malformed format of Post body: " + result.toString());
99 Db oldDb = dbService.getDb(dbConfig.getName());
101 sendError(response, 400, "Db already exists: " + dbConfig.getName());
105 newdb.setName(dbConfig.getName());
106 newdb.setHost(dbConfig.getHost());
107 newdb.setPort(dbConfig.getPort());
108 newdb.setLogin(dbConfig.getLogin());
109 newdb.setPass(dbConfig.getPassword());
110 newdb.setEncrypt(false);
112 if(dbConfig.getName().equals("Elecsticsearch") || dbConfig.getName().equals("Druid"))
114 newdb.setDatabase(new String(dbConfig.getDatabase()));
116 dbRepository.save(newdb);
118 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
119 retMsg = new DbConfig();
120 composeRetMessagefromDbConfig(newdb, retMsg);
121 retBody.setReturnBody(retMsg);
122 retBody.setStatusCode(200);
128 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
129 //need to the the following method to retrieve the topic list
130 @GetMapping("/{dbName}")
132 @ApiOperation(value="Get a database's details.")
133 public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
134 /*Db db = dbService.getDb(dbName);
136 sendError(response, 404, "Db not found: " + dbName);
138 Db db = dbRepository.findByName(dbName);
140 sendError(response, 404, "Db not found: " + dbName);
147 @PutMapping("/{dbName}")
149 @ApiOperation(value="Update a database.")
150 public PostReturnBody<DbConfig> updateDb(@PathVariable("dbName") String dbName, @RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
152 if (result.hasErrors()) {
153 sendError(response, 400, "Error parsing DB: " + result.toString());
157 Db oldDb = dbService.getDb(dbConfig.getName());
159 sendError(response, 404, "Db not found: " + dbConfig.getName());
162 oldDb.setName(dbConfig.getName());
163 oldDb.setHost(dbConfig.getHost());
164 oldDb.setPort(dbConfig.getPort());
165 oldDb.setLogin(dbConfig.getLogin());
166 oldDb.setPass(dbConfig.getPassword());
167 oldDb.setEncrypt(false);
169 if(oldDb.getName().equals("Elecsticsearch") || oldDb.getName().equals("Druid"))
171 oldDb.setDatabase(dbConfig.getDatabase());
173 dbRepository.save(oldDb);
175 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
176 retMsg = new DbConfig();
177 composeRetMessagefromDbConfig(oldDb, retMsg);
178 retBody.setReturnBody(retMsg);
179 retBody.setStatusCode(200);
185 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
186 //need to the the following method to retrieve the topic list
187 @DeleteMapping("/{dbName}")
189 @ApiOperation(value="Delete a database.")
190 public void deleteDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
192 Db delDb = dbRepository.findByName(dbName);
194 sendError(response, 404, "Db not found: " + dbName);
196 Set<Topic> topicRelation = delDb.getTopics();
197 topicRelation.clear();
198 dbRepository.save(delDb);
199 dbRepository.delete(delDb);
200 response.setStatus(204);
203 //Read topics in a DB
204 @GetMapping("/{dbName}/topics")
206 @ApiOperation(value="Get a database's all topics.")
207 public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
208 //Db db = dbService.getDb(dbName);
211 Db db = dbRepository.findByName(dbName);
212 topics = db.getTopics();
215 sendError(response, 404, "DB: " + dbName + " or Topics not found");
223 @PostMapping("/verify")
225 @ApiOperation(value="Database connection verification")
226 public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
232 response.setStatus(501);
236 private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
238 dbConfigMsg.setName(db.getName());
239 dbConfigMsg.setHost(db.getHost());
240 dbConfigMsg.setPort(db.getPort());
243 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
245 response.sendError(sc, msg);