c4288d93d6214c8c16f0ba4c142fa6a50a9f9e1e
[dcaegen2/services.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DataLake
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
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20 package org.onap.datalake.feeder.controller;
21
22 import java.io.IOException;
23 import java.util.Set;
24
25 import javax.servlet.http.HttpServletResponse;
26
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;
44
45 import io.swagger.annotations.Api;
46 import io.swagger.annotations.ApiOperation;
47 import io.swagger.annotations.ApiResponse;
48 import io.swagger.annotations.ApiResponses;
49
50 /**
51  * This controller manages the big data storage settings. All the settings are
52  * saved in database.
53  * 
54  * @author Guobiao Mo
55  *
56  */
57
58 @RestController
59 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
60 //@Api(value = "db", consumes = "application/json", produces = "application/json")
61 public class DbController {
62
63         private final Logger log = LoggerFactory.getLogger(this.getClass());
64
65         @Autowired
66         private DbRepository dbRepository;
67
68         @Autowired
69         private DbService dbService;
70
71         //list all dbs 
72         @GetMapping("/")
73         @ResponseBody
74         @ApiOperation(value="Get all databases' details.")
75         public Iterable<Db> list() throws IOException {
76                 Iterable<Db> ret = dbRepository.findAll();
77                 return ret;
78         }
79
80         //Read a db
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}")
84         @ResponseBody
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);
88                 if (db == null) {
89                         sendError(response, 404, "Db not found: " + dbName);
90                 }
91                 return db;
92         }
93
94         //Read topics in a DB 
95         @GetMapping("/{dbName}/topics")
96         @ResponseBody
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();
101                 return topics;
102         }
103
104         //Update Db
105         @PutMapping("/")
106         @ResponseBody
107         @ApiOperation(value="Update a database.")
108         public Db updateDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
109
110                 if (result.hasErrors()) {
111                         sendError(response, 400, "Error parsing DB: " + result.toString());
112                         return null;
113                 }
114
115                 Db oldDb = dbService.getDb(db.getName());
116                 if (oldDb == null) {
117                         sendError(response, 404, "Db not found: " + db.getName());
118                         return null;
119                 } else {
120                         dbRepository.save(db);
121                         return db;
122                 }
123         }
124
125         @PostMapping("/")
126         @ResponseBody
127         @ApiOperation(value="Create a new database.")
128         public Db createDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
129
130                 if (result.hasErrors()) {
131                         sendError(response, 400, "Error parsing DB: " + result.toString());
132                         return null;
133                 }
134
135                 Db oldDb = dbService.getDb(db.getName());
136                 if (oldDb != null) {
137                         sendError(response, 400, "Db already exists: " + db.getName());
138                         return null;
139                 } else {
140                         dbRepository.save(db);
141                         return db;
142                 }
143         }
144
145         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
146                 log.info(msg);
147                 response.sendError(sc, msg);
148         }
149 }