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.Topic;
29 import org.onap.datalake.feeder.repository.DbRepository;
30 import org.onap.datalake.feeder.repository.TopicRepository;
31 import org.onap.datalake.feeder.service.DbService;
32 import org.onap.datalake.feeder.dto.DbConfig;
33 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.validation.BindingResult;
39 import org.springframework.web.bind.annotation.*;
41 import io.swagger.annotations.ApiOperation;
44 * This controller manages the big data storage settings. All the settings are
52 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
54 //@Api(value = "db", consumes = "application/json", produces = "application/json")
55 public class DbController {
57 private final Logger log = LoggerFactory.getLogger(this.getClass());
60 private DbRepository dbRepository;
63 private TopicRepository topicRepository;
66 private DbService dbService;
71 @ApiOperation(value="Gat all databases name")
72 //public Iterable<Db> list() throws IOException {
73 public List<String> list() throws IOException {
74 Iterable<Db> ret = dbRepository.findAll();
75 List<String> retString = new ArrayList<>();
78 log.info(db.getName());
79 retString.add(db.getName());
88 @ApiOperation(value="Create a new database.")
89 public PostReturnBody<DbConfig> createDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
90 if (result.hasErrors()) {
91 sendError(response, 400, "Malformed format of Post body: " + result.toString());
95 Db oldDb = dbService.getDb(dbConfig.getName());
97 sendError(response, 400, "Db already exists: " + dbConfig.getName());
101 newdb.setName(dbConfig.getName());
102 newdb.setHost(dbConfig.getHost());
103 newdb.setPort(dbConfig.getPort());
104 newdb.setEnabled(dbConfig.isEnabled());
105 newdb.setLogin(dbConfig.getLogin());
106 newdb.setPass(dbConfig.getPassword());
107 newdb.setEncrypt(dbConfig.isEncrypt());
109 if(!dbConfig.getName().equals("Elecsticsearch") || !dbConfig.getName().equals("Druid"))
111 newdb.setDatabase(new String(dbConfig.getDatabase()));
113 dbRepository.save(newdb);
115 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
116 retMsg = new DbConfig();
117 composeRetMessagefromDbConfig(newdb, retMsg);
118 retBody.setReturnBody(retMsg);
119 retBody.setStatusCode(200);
125 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
126 //need to the the following method to retrieve the topic list
127 @GetMapping("/{dbName}")
129 @ApiOperation(value="Get a database's details.")
130 public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
131 /*Db db = dbService.getDb(dbName);
133 sendError(response, 404, "Db not found: " + dbName);
135 Db db = dbRepository.findByName(dbName);
137 sendError(response, 404, "Db not found: " + dbName);
144 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
145 //need to the the following method to retrieve the topic list
146 @DeleteMapping("/{dbName}")
148 @ApiOperation(value="Delete a database.")
149 public void deleteDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
151 Db delDb = dbRepository.findByName(dbName);
153 sendError(response, 404, "Db not found: " + dbName);
156 Set<Topic> topicRelation = delDb.getTopics();
157 topicRelation.clear();
158 dbRepository.save(delDb);
159 dbRepository.delete(delDb);
160 response.setStatus(204);
163 //Read topics in a DB
164 @GetMapping("/{dbName}/topics")
166 @ApiOperation(value="Get a database's all topics.")
167 public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
168 //Db db = dbService.getDb(dbName);
171 Db db = dbRepository.findByName(dbName);
172 topics = db.getTopics();
175 sendError(response, 404, "DB: " + dbName + " or Topics not found");
186 @ApiOperation(value="Update a database.")
187 public PostReturnBody<DbConfig> updateDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
189 if (result.hasErrors()) {
190 sendError(response, 400, "Error parsing DB: " + result.toString());
194 Db oldDb = dbService.getDb(dbConfig.getName());
196 sendError(response, 404, "Db not found: " + dbConfig.getName());
199 oldDb.setHost(dbConfig.getHost());
200 oldDb.setPort(dbConfig.getPort());
201 oldDb.setEnabled(dbConfig.isEnabled());
202 oldDb.setLogin(dbConfig.getLogin());
203 oldDb.setPass(dbConfig.getPassword());
204 oldDb.setEncrypt(dbConfig.isEncrypt());
205 if (!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid")) {
206 oldDb.setDatabase(dbConfig.getDatabase());
209 dbRepository.save(oldDb);
211 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
212 retMsg = new DbConfig();
213 composeRetMessagefromDbConfig(oldDb, retMsg);
214 retBody.setReturnBody(retMsg);
215 retBody.setStatusCode(200);
222 @PostMapping("/verify")
224 @ApiOperation(value="Database connection verification")
225 public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
231 response.setStatus(501);
235 private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
237 dbConfigMsg.setName(db.getName());
238 dbConfigMsg.setHost(db.getHost());
239 dbConfigMsg.setEnabled(db.isEnabled());
240 dbConfigMsg.setPort(db.getPort());
241 dbConfigMsg.setLogin(db.getLogin());
242 dbConfigMsg.setDatabase(db.getDatabase());
247 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
249 response.sendError(sc, msg);