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;
46 * This controller manages the big data storage settings. All the settings are saved in database.
53 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
54 public class DbController {
56 private final Logger log = LoggerFactory.getLogger(this.getClass());
59 private DbRepository dbRepository;
62 private DbService dbService;
67 public Iterable<Db> list() throws IOException {
68 Iterable<Db> ret = dbRepository.findAll();
73 //the topics are missing in the return, since in we use @JsonBackReference on Db's topics
74 //need to the the following method to retrieve the topic list
75 @GetMapping("/{name}")
77 public Db getDb(@PathVariable("name") String dbName) throws IOException {
78 Db db = dbService.getDb(dbName);
83 @GetMapping("/{name}/topics")
85 public Set<Topic> getDbTopics(@PathVariable("name") String dbName) throws IOException {
86 Db db = dbService.getDb(dbName);
87 Set<Topic> topics = db.getTopics();
94 public Db updateDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
96 if (result.hasErrors()) {
97 sendError(response, 400, "Error parsing DB: "+result.toString());
101 Db oldDb = getDb(db.getName());
103 sendError(response, 404, "Db not found: "+db.getName());
106 dbRepository.save(db);
114 public Db createDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
116 if (result.hasErrors()) {
117 sendError(response, 400, "Error parsing DB: "+result.toString());
121 Db oldDb = getDb(db.getName());
123 sendError(response, 400, "Db already exists: "+db.getName());
126 dbRepository.save(db);
131 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
133 response.sendError(sc, msg);