Dependency fix
[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.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.onap.datalake.feeder.domain.Db;
30 import org.onap.datalake.feeder.domain.Topic;
31 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
32 import org.onap.datalake.feeder.dto.TopicConfig;
33 import org.onap.datalake.feeder.repository.DbRepository;
34 import org.onap.datalake.feeder.repository.TopicRepository;
35 import org.onap.datalake.feeder.service.DbService;
36 import org.onap.datalake.feeder.service.DmaapService;
37 import org.onap.datalake.feeder.service.TopicService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.validation.BindingResult;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.PutMapping;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.ResponseBody;
51 import org.springframework.web.bind.annotation.RestController;
52
53 import io.swagger.annotations.ApiOperation;
54
55 /**
56  * This controller manages topic settings.
57  * 
58  * Topic "_DL_DEFAULT_" acts as the default. 
59  * If a topic is not present in database, "_DL_DEFAULT_" is used for it.
60  * If a topic is present in database, itself should be complete, and no setting from "_DL_DEFAULT_" is used.
61  * Topic "_DL_DEFAULT_" is populated at setup by a DB script.
62  * 
63  * @author Guobiao Mo
64  * @contributor Kate Hsuan @ QCT
65  */
66
67 @RestController
68 @RequestMapping(value = "/topics", produces = { MediaType.APPLICATION_JSON_VALUE })//, consumes= {MediaType.APPLICATION_JSON_UTF8_VALUE})
69 public class TopicController {
70
71         private final Logger log = LoggerFactory.getLogger(this.getClass());
72
73         @Autowired
74         private DmaapService dmaapService;
75
76         @Autowired
77         private TopicRepository topicRepository;
78
79         @Autowired
80         private TopicService topicService;
81
82         @GetMapping("/dmaap")
83         @ResponseBody
84         @ApiOperation(value = "List all topic names in DMaaP.")
85         public List<String> listDmaapTopics() {
86                 return dmaapService.getTopics();
87         }
88
89         @GetMapping("")
90         @ResponseBody
91         @ApiOperation(value="List all topic names in database")
92         public List<String> list() {
93                 Iterable<Topic> ret = topicRepository.findAll();
94                 List<String> retString = new ArrayList<>();
95                 for(Topic item : ret)
96                 {
97                         if(!topicService.istDefaultTopic(item))
98                                 retString.add(item.getName());
99                 }
100                 return retString;
101         }
102
103         @PostMapping("")
104         @ResponseBody
105         @ApiOperation(value="Create a new topic.")
106         public PostReturnBody<TopicConfig> createTopic(@RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
107
108                 if (result.hasErrors()) {
109                         sendError(response, 400, "Error parsing Topic: "+result.toString());
110                         return null;
111                 }
112                 Topic oldTopic = topicService.getTopic(topicConfig.getName());
113                 if (oldTopic != null) {
114                         sendError(response, 400, "Topic already exists "+topicConfig.getName());
115                         return null;
116                 } else {
117                         Topic wTopic = topicService.fillTopicConfiguration(topicConfig);
118                         if(wTopic.getTtl() == 0)
119                                 wTopic.setTtl(3650);
120                         topicRepository.save(wTopic); 
121                         return mkPostReturnBody(200, wTopic);
122                 }
123         }
124
125         @GetMapping("/{topicName}")
126         @ResponseBody
127         @ApiOperation(value="Get a topic's settings.")
128         public TopicConfig getTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException {
129                 Topic topic = topicService.getTopic(topicName);
130                 if(topic == null) {
131                         sendError(response, 404, "Topic not found");
132                         return null;
133                 }
134                 return topic.getTopicConfig();
135         }
136
137         //This is not a partial update: old topic is wiped out, and new topic is created based on the input json.
138         //One exception is that old DBs are kept
139         @PutMapping("/{topicName}")
140         @ResponseBody
141         @ApiOperation(value="Update a topic.")
142         public PostReturnBody<TopicConfig> updateTopic(@PathVariable("topicName") String topicName, @RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
143
144                 if (result.hasErrors()) {
145                         sendError(response, 400, "Error parsing Topic: "+result.toString());
146                         return null;
147                 }
148
149                 if(!topicName.equals(topicConfig.getName()))
150                 {
151                         sendError(response, 400, "Topic name mismatch" + topicName + topicConfig.getName());
152                         return null;
153                 }
154
155                 Topic oldTopic = topicService.getTopic(topicConfig.getName());
156                 if (oldTopic == null) {
157                         sendError(response, 404, "Topic not found "+topicConfig.getName());
158                         return null;
159                 } else {
160                         topicService.fillTopicConfiguration(topicConfig, oldTopic);
161                         topicRepository.save(oldTopic);
162                         return mkPostReturnBody(200, oldTopic);
163                 }
164         }
165
166         @DeleteMapping("/{topicName}")
167         @ResponseBody
168         @ApiOperation(value="Update a topic.")
169         public void deleteTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException
170         {
171                 Topic oldTopic = topicService.getTopic(topicName);
172                 if (oldTopic == null) {
173                         sendError(response, 404, "Topic not found "+topicName);
174                 } else {
175                         Set<Db> dbRelation = oldTopic.getDbs();
176                         dbRelation.clear();
177                         topicRepository.save(oldTopic);
178                         topicRepository.delete(oldTopic);
179                         response.setStatus(204);
180                 }
181         }
182
183         private PostReturnBody<TopicConfig> mkPostReturnBody(int statusCode, Topic topic)
184         {
185                 PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
186         retBody.setStatusCode(statusCode);
187         retBody.setReturnBody(topic.getTopicConfig());
188         
189         return retBody;
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 }