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.setEnabled(dbConfig.isEnabled());
109 newdb.setLogin(dbConfig.getLogin());
110 newdb.setPass(dbConfig.getPassword());
111 newdb.setEncrypt(false);
113 if(!dbConfig.getName().equals("Elecsticsearch") || !dbConfig.getName().equals("Druid"))
115 newdb.setDatabase(new String(dbConfig.getDatabase()));
117 dbRepository.save(newdb);
119 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
120 retMsg = new DbConfig();
121 composeRetMessagefromDbConfig(newdb, retMsg);
122 retBody.setReturnBody(retMsg);
123 retBody.setStatusCode(200);
129 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
130 //need to the the following method to retrieve the topic list
131 @GetMapping("/{dbName}")
133 @ApiOperation(value="Get a database's details.")
134 public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
135 /*Db db = dbService.getDb(dbName);
137 sendError(response, 404, "Db not found: " + dbName);
139 Db db = dbRepository.findByName(dbName);
141 sendError(response, 404, "Db not found: " + dbName);
148 @PutMapping("/{dbName}")
150 @ApiOperation(value="Update a database.")
151 public PostReturnBody<DbConfig> updateDb(@PathVariable("dbName") String dbName, @RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
153 if (result.hasErrors()) {
154 sendError(response, 400, "Error parsing DB: " + result.toString());
158 if(!dbName.equals(dbConfig.getName()))
160 sendError(response, 400, "Mismatch DB name.");
164 Db oldDb = dbService.getDb(dbConfig.getName());
166 sendError(response, 404, "Db not found: " + dbConfig.getName());
169 oldDb.setName(dbConfig.getName());
170 oldDb.setHost(dbConfig.getHost());
171 oldDb.setPort(dbConfig.getPort());
172 oldDb.setEnabled(dbConfig.isEnabled());
173 oldDb.setLogin(dbConfig.getLogin());
174 oldDb.setPass(dbConfig.getPassword());
175 oldDb.setEncrypt(false);
177 if(!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid"))
179 oldDb.setDatabase(dbConfig.getDatabase());
181 dbRepository.save(oldDb);
183 PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
184 retMsg = new DbConfig();
185 composeRetMessagefromDbConfig(oldDb, retMsg);
186 retBody.setReturnBody(retMsg);
187 retBody.setStatusCode(200);
193 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
194 //need to the the following method to retrieve the topic list
195 @DeleteMapping("/{dbName}")
197 @ApiOperation(value="Delete a database.")
198 public void deleteDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
200 Db delDb = dbRepository.findByName(dbName);
202 sendError(response, 404, "Db not found: " + dbName);
205 Set<Topic> topicRelation = delDb.getTopics();
206 topicRelation.clear();
207 dbRepository.save(delDb);
208 dbRepository.delete(delDb);
209 response.setStatus(204);
212 //Read topics in a DB
213 @GetMapping("/{dbName}/topics")
215 @ApiOperation(value="Get a database's all topics.")
216 public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
217 //Db db = dbService.getDb(dbName);
220 Db db = dbRepository.findByName(dbName);
221 topics = db.getTopics();
224 sendError(response, 404, "DB: " + dbName + " or Topics not found");
232 @PostMapping("/verify")
234 @ApiOperation(value="Database connection verification")
235 public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
241 response.setStatus(501);
245 private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
247 dbConfigMsg.setName(db.getName());
248 dbConfigMsg.setHost(db.getHost());
249 dbConfigMsg.setEnabled(db.isEnabled());
250 dbConfigMsg.setPort(db.getPort());
251 dbConfigMsg.setLogin(db.getLogin());
252 dbConfigMsg.setDatabase(db.getDatabase());
257 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
259 response.sendError(sc, msg);