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