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