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 tools or dbs")
 
  90         public List<DbConfig> dblistByTool(@RequestParam boolean isDb) {
 
  91                 log.info("Search dbs by tool start......");
 
  92                 Iterable<DbType> dbType  = dbTypeRepository.findByTool(!isDb);
 
  93                 List<DbConfig> retDbConfig = new ArrayList<>();
 
  94                 for (DbType item : dbType) {
 
  95                         for (Db d : item.getDbs()) {
 
  96                                 retDbConfig.add(d.getDbConfig());
 
 102         @GetMapping("/idAndName/{id}")
 
 104         @ApiOperation(value="Get all databases id and name by designTypeId")
 
 105         public Map<Integer, String> listIdAndName(@PathVariable String id) {
 
 106                 Optional<DesignType> designType  = designTypeRepository.findById(id);
 
 107                 Map<Integer, String> map = new HashMap<>();
 
 108                 if (designType.isPresent()) {
 
 109                         Set<Db> dbs = designType.get().getDbType().getDbs();
 
 110                         for (Db item : dbs) {
 
 111                                 map.put(item.getId(), item.getName());
 
 120         @ApiOperation(value="Create a new database.")
 
 121         public PostReturnBody<DbConfig> createDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
 
 122                 if (result.hasErrors()) {
 
 123                         sendError(response, 400, "Malformed format of Post body: " + result.toString());
 
 127 /*              Db oldDb = dbService.getDb(dbConfig.getName());
 
 129                         sendError(response, 400, "Db already exists: " + dbConfig.getName());
 
 133                         newdb.setName(dbConfig.getName());
 
 134                         newdb.setHost(dbConfig.getHost());
 
 135                         newdb.setPort(dbConfig.getPort());
 
 136                         newdb.setEnabled(dbConfig.isEnabled());
 
 137                         newdb.setLogin(dbConfig.getLogin());
 
 138                         newdb.setPass(dbConfig.getPass());
 
 139                         newdb.setEncrypt(dbConfig.isEncrypt());
 
 140                         if (dbConfig.getDbTypeId().isEmpty()) {
 
 141                 sendError(response, 400, "Malformed format of Post body: " + result.toString());
 
 143                 Optional<DbType> dbType = dbTypeRepository.findById(dbConfig.getDbTypeId());
 
 144                 if (dbType.isPresent()) {
 
 145                     newdb.setDbType(dbType.get());
 
 149                         if(!dbConfig.getName().equals("Elecsticsearch") || dbConfig.getName().equals("Druid"))
 
 151                                 newdb.setDatabase(dbConfig.getDatabase());
 
 153                         dbRepository.save(newdb);
 
 154             log.info("Db save ....... name: " + dbConfig.getName());
 
 156                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
 
 157                         retMsg = new DbConfig();
 
 158                         composeRetMessagefromDbConfig(newdb, retMsg);
 
 159                         retBody.setReturnBody(retMsg);
 
 160                         retBody.setStatusCode(200);
 
 166         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics 
 
 167         //need to the the following method to retrieve the topic list 
 
 168         @GetMapping("/{dbName}")
 
 170         @ApiOperation(value="Get a database's details.")
 
 171         public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
 
 172                 Db db = dbRepository.findByName(dbName);
 
 174                         sendError(response, 404, DB_NOT_FOUND + dbName);
 
 181         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
 
 182         //need to the the following method to retrieve the topic list
 
 183         @DeleteMapping("/{id}")
 
 185         @ApiOperation(value="Delete a database.")
 
 186         public void deleteDb(@PathVariable("id") int id, HttpServletResponse response) throws IOException {
 
 188                 Optional<Db> delDb = dbRepository.findById(id);
 
 189                 if (!delDb.isPresent()) {
 
 190                         sendError(response, 404, "Db not found: " + id);
 
 193             Set<Topic> topicRelation = delDb.get().getTopics();
 
 194             topicRelation.clear();
 
 195             dbRepository.delete(delDb.get());
 
 196             response.setStatus(204);
 
 200         //Read topics in a DB
 
 201         @GetMapping("/{dbName}/topics")
 
 203         @ApiOperation(value="Get a database's all topics.")
 
 204         public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
 
 207                         Db db = dbRepository.findByName(dbName);
 
 208                         topics = db.getTopics();
 
 209                 } catch(Exception ex) {
 
 210                         sendError(response, 404, "DB: " + dbName + " or Topics not found");
 
 211                         return Collections.emptySet();
 
 220         @ApiOperation(value="Update a database.")
 
 221         public PostReturnBody<DbConfig> updateDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
 
 223                 if (result.hasErrors()) {
 
 224                         sendError(response, 400, "Error parsing DB: " + result.toString());
 
 228                 Db oldDb = dbRepository.findById(dbConfig.getId()).get();
 
 230                         sendError(response, 404, DB_NOT_FOUND + dbConfig.getName());
 
 234                         oldDb.setEnabled(dbConfig.isEnabled());
 
 235             oldDb.setName(dbConfig.getName());
 
 236             oldDb.setHost(dbConfig.getHost());
 
 237             oldDb.setPort(dbConfig.getPort());
 
 238             oldDb.setLogin(dbConfig.getLogin());
 
 239             oldDb.setPass(dbConfig.getPass());
 
 240             oldDb.setEncrypt(dbConfig.isEncrypt());
 
 241             if (dbConfig.getDbTypeId().isEmpty()) {
 
 242                 sendError(response, 400, "Malformed format of Post body: " + result.toString());
 
 244                 Optional<DbType> dbType = dbTypeRepository.findById(dbConfig.getDbTypeId());
 
 245                 if (dbType.isPresent()) {
 
 246                     oldDb.setDbType(dbType.get());
 
 249                         if (!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid")) {
 
 250                                 oldDb.setDatabase(dbConfig.getDatabase());
 
 253                         dbRepository.save(oldDb);
 
 255                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
 
 256                         retMsg = new DbConfig();
 
 257                         composeRetMessagefromDbConfig(oldDb, retMsg);
 
 258                         retBody.setReturnBody(retMsg);
 
 259                         retBody.setStatusCode(200);
 
 266         @PostMapping("/verify")
 
 268         @ApiOperation(value="Database connection verification")
 
 269         public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
 
 275                 response.setStatus(501);
 
 279         private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
 
 281         dbConfigMsg.setId(db.getId());
 
 282                 dbConfigMsg.setName(db.getName());
 
 283                 dbConfigMsg.setHost(db.getHost());
 
 284                 dbConfigMsg.setEnabled(db.isEnabled());
 
 285                 dbConfigMsg.setPort(db.getPort());
 
 286                 dbConfigMsg.setLogin(db.getLogin());
 
 287                 dbConfigMsg.setDatabase(db.getDatabase());
 
 288         dbConfigMsg.setDbTypeId(db.getDbType().getId());
 
 289         dbConfigMsg.setPass(db.getPass());
 
 293         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
 
 295                 response.sendError(sc, msg);