0869fde7925a647ffe6b74cc37ec73173924df2c
[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.List;
24 import java.util.Set;
25
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.onap.datalake.feeder.domain.Db;
29 import org.onap.datalake.feeder.domain.Topic;
30 import org.onap.datalake.feeder.repository.TopicRepository;
31 import org.onap.datalake.feeder.service.DbService;
32 import org.onap.datalake.feeder.service.DmaapService;
33 import org.onap.datalake.feeder.service.TopicService;
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.DeleteMapping;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.ResponseBody;
47 import org.springframework.web.bind.annotation.RestController;
48
49 import io.swagger.annotations.ApiOperation;
50
51 /**
52  * This controller manages topic settings.
53  * 
54  * Topic "_DL_DEFAULT_" acts as the default. For example, if a topic's
55  * enabled=null, _DL_DEFAULT_.enabled is used for that topic. All the settings
56  * are saved in database. topic "_DL_DEFAULT_" is populated at setup by a DB
57  * script.
58  * 
59  * @author Guobiao Mo
60  *
61  */
62
63 @RestController
64 @RequestMapping(value = "/topics", produces = { MediaType.APPLICATION_JSON_VALUE }) //, consumes= {MediaType.APPLICATION_JSON_UTF8_VALUE})
65 public class TopicController {
66
67         private final Logger log = LoggerFactory.getLogger(this.getClass());
68
69         @Autowired
70         private DmaapService dmaapService;
71
72         @Autowired
73         private TopicRepository topicRepository;
74
75         @Autowired
76         private TopicService topicService;
77
78         @Autowired
79         private DbService dbService;
80
81         @GetMapping("/dmaap/")
82         @ResponseBody
83         @ApiOperation(value = "List all topic names in DMaaP.")
84         public List<String> listDmaapTopics() throws IOException {
85                 return dmaapService.getTopics();
86         }
87
88         @GetMapping("/")
89         @ResponseBody
90         @ApiOperation(value = "List all topics' settings.")
91         public Iterable<Topic> list() throws IOException {
92                 Iterable<Topic> ret = topicRepository.findAll();
93                 return ret;
94         }
95
96         @GetMapping("/{topicName}")
97         @ResponseBody
98         @ApiOperation(value = "Get a topic's settings.")
99         public Topic getTopic(@PathVariable("topicName") String topicName) throws IOException {
100                 Topic topic = topicService.getTopic(topicName);
101                 return topic;
102         }
103
104         @GetMapping("/{topicName}/dbs")
105         @ResponseBody
106         @ApiOperation(value = "Get all DBs in a topic.")
107         public Set<Db> getTopicDbs(@PathVariable("topicName") String topicName) throws IOException {
108                 Topic topic = topicService.getTopic(topicName);
109                 Set<Db> dbs = topic.getDbs();
110                 return dbs;
111         }
112
113         //This is not a partial update: old topic is wiped out, and new topic is created based on the input json. 
114         //One exception is that old DBs are kept
115         @PutMapping("/")
116         @ResponseBody
117         @ApiOperation(value = "Update a topic.")
118         public Topic updateTopic(@RequestBody Topic topic, BindingResult result, HttpServletResponse response) throws IOException {
119
120                 if (result.hasErrors()) {
121                         sendError(response, 400, "Error parsing Topic: " + result.toString());
122                         return null;
123                 }
124
125                 Topic oldTopic = getTopic(topic.getName());
126                 if (oldTopic == null) {
127                         sendError(response, 404, "Topic not found " + topic.getName());
128                         return null;
129                 } else {
130                         if (!topicService.istDefaultTopic(topic)) {
131                                 Topic defaultTopic = topicService.getDefaultTopic();
132                                 topic.setDefaultTopic(defaultTopic);
133                         }
134
135                         topic.setDbs(oldTopic.getDbs());
136                         topicRepository.save(topic);
137                         return topic;
138                 }
139         }
140
141         @PostMapping("/")
142         @ResponseBody
143         @ApiOperation(value = "Create a new topic.")
144         public Topic createTopic(@RequestBody Topic topic, BindingResult result, HttpServletResponse response) throws IOException {
145
146                 if (result.hasErrors()) {
147                         sendError(response, 400, "Error parsing Topic: " + result.toString());
148                         return null;
149                 }
150
151                 Topic oldTopic = getTopic(topic.getName());
152                 if (oldTopic != null) {
153                         sendError(response, 400, "Topic already exists " + topic.getName());
154                         return null;
155                 } else {
156                         if (!topicService.istDefaultTopic(topic)) {
157                                 Topic defaultTopic = topicService.getDefaultTopic();
158                                 topic.setDefaultTopic(defaultTopic);
159                         }
160
161                         topicRepository.save(topic);
162                         return topic;
163                 }
164         }
165
166         @DeleteMapping("/{topicName}/db/{dbName}")
167         @ResponseBody
168         @ApiOperation(value = "Delete a DB from a topic.")
169         public Set<Db> deleteDb(@PathVariable("topicName") String topicName, @PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
170                 Topic topic = topicService.getTopic(topicName);
171                 Set<Db> dbs = topic.getDbs();
172                 dbs.remove(new Db(dbName));
173
174                 topicRepository.save(topic);
175                 return topic.getDbs();
176         }
177
178         @PutMapping("/{topicName}/db/{dbName}")
179         @ResponseBody
180         @ApiOperation(value = "Add a DB to a topic.")
181         public Set<Db> addDb(@PathVariable("topicName") String topicName, @PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
182                 Topic topic = topicService.getTopic(topicName);
183                 Set<Db> dbs = topic.getDbs();
184
185                 Db db = dbService.getDb(dbName);
186                 dbs.add(db);
187
188                 topicRepository.save(topic);
189                 return topic.getDbs();
190         }
191
192         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
193                 log.info(msg);
194                 response.sendError(sc, msg);
195         }
196 }