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.DesignType;
 
  29 import org.onap.datalake.feeder.domain.Topic;
 
  30 import org.onap.datalake.feeder.repository.DbRepository;
 
  31 import org.onap.datalake.feeder.dto.DbConfig;
 
  32 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
 
  33 import org.onap.datalake.feeder.repository.DesignTypeRepository;
 
  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());
 
  58         private static final String DB_NOT_FOUND = "Db not found: ";
 
  61         private DbRepository dbRepository;
 
  64         private DesignTypeRepository designTypeRepository;
 
  69         @ApiOperation(value="Gat all databases name")
 
  70         public List<String> list() {
 
  71                 Iterable<Db> ret = dbRepository.findAll();
 
  72                 List<String> retString = new ArrayList<>();
 
  75                         log.info(db.getName());
 
  76                         retString.add(db.getName());
 
  82         @GetMapping("/idAndName/{id}")
 
  84         @ApiOperation(value="Get all databases id and name by designTypeId")
 
  85         public Map<Integer, String> listIdAndName(@PathVariable String id) {
 
  86                 Optional<DesignType> designType  = designTypeRepository.findById(id);
 
  87                 Map<Integer, String> map = new HashMap<>();
 
  88                 if (designType.isPresent()) {
 
  89                         Set<Db> dbs = designType.get().getDbType().getDbs();
 
  91                                 map.put(item.getId(), item.getName());
 
 100         @ApiOperation(value="Create a new database.")
 
 101         public PostReturnBody<DbConfig> createDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
 
 102                 if (result.hasErrors()) {
 
 103                         sendError(response, 400, "Malformed format of Post body: " + result.toString());
 
 107 /*              Db oldDb = dbService.getDb(dbConfig.getName());
 
 109                         sendError(response, 400, "Db already exists: " + dbConfig.getName());
 
 113                         newdb.setName(dbConfig.getName());
 
 114                         newdb.setHost(dbConfig.getHost());
 
 115                         newdb.setPort(dbConfig.getPort());
 
 116                         newdb.setEnabled(dbConfig.isEnabled());
 
 117                         newdb.setLogin(dbConfig.getLogin());
 
 118                         newdb.setPass(dbConfig.getPassword());
 
 119                         newdb.setEncrypt(dbConfig.isEncrypt());
 
 121                         if(!dbConfig.getName().equals("Elecsticsearch") || dbConfig.getName().equals("Druid"))
 
 123                                 newdb.setDatabase(new String(dbConfig.getDatabase()));
 
 125                         dbRepository.save(newdb);
 
 127                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
 
 128                         retMsg = new DbConfig();
 
 129                         composeRetMessagefromDbConfig(newdb, retMsg);
 
 130                         retBody.setReturnBody(retMsg);
 
 131                         retBody.setStatusCode(200);
 
 137         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics 
 
 138         //need to the the following method to retrieve the topic list 
 
 139         @GetMapping("/{dbName}")
 
 141         @ApiOperation(value="Get a database's details.")
 
 142         public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
 
 143                 Db db = dbRepository.findByName(dbName);
 
 145                         sendError(response, 404, DB_NOT_FOUND + dbName);
 
 152         //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
 
 153         //need to the the following method to retrieve the topic list
 
 154         @DeleteMapping("/{dbName}")
 
 156         @ApiOperation(value="Delete a database.")
 
 157         public void deleteDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
 
 159                 Db delDb = dbRepository.findByName(dbName);
 
 161                         sendError(response, 404, DB_NOT_FOUND + dbName);
 
 164                 Set<Topic> topicRelation = delDb.getTopics();
 
 165                 topicRelation.clear();
 
 166                 dbRepository.save(delDb);
 
 167                 dbRepository.delete(delDb);
 
 168                 response.setStatus(204);
 
 171         //Read topics in a DB
 
 172         @GetMapping("/{dbName}/topics")
 
 174         @ApiOperation(value="Get a database's all topics.")
 
 175         public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
 
 178                         Db db = dbRepository.findByName(dbName);
 
 179                         topics = db.getTopics();
 
 180                 } catch(Exception ex) {
 
 181                         sendError(response, 404, "DB: " + dbName + " or Topics not found");
 
 182                         return Collections.emptySet();
 
 191         @ApiOperation(value="Update a database.")
 
 192         public PostReturnBody<DbConfig> updateDb(@RequestBody DbConfig dbConfig, BindingResult result, HttpServletResponse response) throws IOException {
 
 194                 if (result.hasErrors()) {
 
 195                         sendError(response, 400, "Error parsing DB: " + result.toString());
 
 199                 Db oldDb = dbRepository.findById(dbConfig.getId()).get();
 
 201                         sendError(response, 404, DB_NOT_FOUND + dbConfig.getName());
 
 204                         oldDb.setHost(dbConfig.getHost());
 
 205                         oldDb.setPort(dbConfig.getPort());
 
 206                         oldDb.setEnabled(dbConfig.isEnabled());
 
 207                         oldDb.setLogin(dbConfig.getLogin());
 
 208                         oldDb.setPass(dbConfig.getPassword());
 
 209                         oldDb.setEncrypt(dbConfig.isEncrypt());
 
 210                         if (!oldDb.getName().equals("Elecsticsearch") || !oldDb.getName().equals("Druid")) {
 
 211                                 oldDb.setDatabase(dbConfig.getDatabase());
 
 214                         dbRepository.save(oldDb);
 
 216                         PostReturnBody<DbConfig> retBody = new PostReturnBody<>();
 
 217                         retMsg = new DbConfig();
 
 218                         composeRetMessagefromDbConfig(oldDb, retMsg);
 
 219                         retBody.setReturnBody(retMsg);
 
 220                         retBody.setStatusCode(200);
 
 227         @PostMapping("/verify")
 
 229         @ApiOperation(value="Database connection verification")
 
 230         public PostReturnBody<DbConfig> verifyDbConnection(@RequestBody DbConfig dbConfig, HttpServletResponse response) throws IOException {
 
 236                 response.setStatus(501);
 
 240         private void composeRetMessagefromDbConfig(Db db, DbConfig dbConfigMsg)
 
 242                 dbConfigMsg.setName(db.getName());
 
 243                 dbConfigMsg.setHost(db.getHost());
 
 244                 dbConfigMsg.setEnabled(db.isEnabled());
 
 245                 dbConfigMsg.setPort(db.getPort());
 
 246                 dbConfigMsg.setLogin(db.getLogin());
 
 247                 dbConfigMsg.setDatabase(db.getDatabase());
 
 252         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
 
 254                 response.sendError(sc, msg);