c34befcc66db2fb5ef9535e6d1819a9ec0ba7b1c
[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 /**
46  * This controller manages the big data storage settings. All the settings are saved in database. 
47  * 
48  * @author Guobiao Mo
49  *
50  */
51
52 @RestController
53 @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE })
54 public class DbController {
55
56         private final Logger log = LoggerFactory.getLogger(this.getClass());
57
58         @Autowired
59         private DbRepository dbRepository;
60         
61         @Autowired
62         private DbService dbService;
63
64         //list all dbs 
65         @GetMapping("/")
66         @ResponseBody
67         public Iterable<Db> list() throws IOException {
68                 Iterable<Db> ret = dbRepository.findAll();
69                 return ret;
70         }
71
72         //Read a db
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}")
76         @ResponseBody
77         public Db getDb(@PathVariable("name") String dbName) throws IOException {
78                 Db db = dbService.getDb(dbName);
79                 return db;
80         }
81
82         //Read topics in a DB 
83         @GetMapping("/{name}/topics")
84         @ResponseBody
85         public Set<Topic> getDbTopics(@PathVariable("name") String dbName) throws IOException {
86                 Db db = dbService.getDb(dbName);
87                 Set<Topic> topics = db.getTopics();
88                 return topics;
89         }
90
91         //Update Db
92         @PutMapping("/")
93         @ResponseBody
94         public Db updateDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
95
96                 if (result.hasErrors()) {
97                         sendError(response, 400, "Error parsing DB: "+result.toString());
98                         return null; 
99                 }
100
101                 Db oldDb = getDb(db.getName());
102                 if (oldDb == null) {
103                         sendError(response, 404, "Db not found: "+db.getName());
104                         return null; 
105                 } else {
106                         dbRepository.save(db);                  
107                         return db;
108                 }
109         }
110
111         //create a new Db  
112         @PostMapping("/")
113         @ResponseBody
114         public Db createDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException {
115
116                 if (result.hasErrors()) {
117                         sendError(response, 400, "Error parsing DB: "+result.toString());
118                         return null;
119                 }
120
121                 Db oldDb = getDb(db.getName());
122                 if (oldDb != null) {
123                         sendError(response, 400, "Db already exists: "+db.getName());
124                         return null;
125                 } else {
126                         dbRepository.save(db);                  
127                         return db;
128                 }
129         }
130
131         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
132                 log.info(msg);
133                 response.sendError(sc, msg);            
134         }
135 }