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.service.DbService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.MediaType;
35 import org.springframework.validation.BindingResult;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PathVariable;
38 import org.springframework.web.bind.annotation.PostMapping;
39 import org.springframework.web.bind.annotation.PutMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.ResponseBody;
43 import org.springframework.web.bind.annotation.RestController;
45 import io.swagger.annotations.Api;
46 import io.swagger.annotations.ApiOperation;
47 import io.swagger.annotations.ApiResponse;
48 import io.swagger.annotations.ApiResponses;
51 * This controller manages the big data storage settings. All the settings are
59 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
60 //@Api(value = "db", consumes = "application/json", produces = "application/json")
61 public class DbController {
63 private final Logger log = LoggerFactory.getLogger(this.getClass());
66 private DbRepository dbRepository;
69 private DbService dbService;
74 @ApiOperation(value="Get all databases' details.")
75 public Iterable<Db> list() throws IOException {
76 Iterable<Db> ret = dbRepository.findAll();
81 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
82 //need to the the following method to retrieve the topic list
83 @GetMapping("/{dbName}")
85 @ApiOperation(value="Get a database's details.")
86 public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
87 Db db = dbService.getDb(dbName);
89 sendError(response, 404, "Db not found: " + dbName);
95 @GetMapping("/{dbName}/topics")
97 @ApiOperation(value="Get a database's all topics.")
98 public Set<Topic> getDbTopics(@PathVariable("dbName") String dbName) throws IOException {
99 Db db = dbService.getDb(dbName);
100 Set<Topic> topics = db.getTopics();
107 @ApiOperation(value="Update a database.")
108 public Db updateDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
110 if (result.hasErrors()) {
111 sendError(response, 400, "Error parsing DB: " + result.toString());
115 Db oldDb = dbService.getDb(db.getName());
117 sendError(response, 404, "Db not found: " + db.getName());
120 dbRepository.save(db);
127 @ApiOperation(value="Create a new database.")
128 public Db createDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
130 if (result.hasErrors()) {
131 sendError(response, 400, "Error parsing DB: " + result.toString());
135 Db oldDb = dbService.getDb(db.getName());
137 sendError(response, 400, "Db already exists: " + db.getName());
140 dbRepository.save(db);
145 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
147 response.sendError(sc, msg);