2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.datalake.feeder.controller;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
27 import javax.servlet.http.HttpServletResponse;
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 import org.springframework.web.bind.annotation.CrossOrigin;
54 import io.swagger.annotations.ApiOperation;
57 * This controller manages topic settings.
59 * Topic "_DL_DEFAULT_" acts as the default.
60 * If a topic is not present in database, "_DL_DEFAULT_" is used for it.
61 * If a topic is present in database, itself should be complete, and no setting from "_DL_DEFAULT_" is used.
62 * Topic "_DL_DEFAULT_" is populated at setup by a DB script.
65 * @contributor Kate Hsuan @ QCT
68 @CrossOrigin(origins = "*")
70 @RequestMapping(value = "/topics", produces = { MediaType.APPLICATION_JSON_VALUE })//, consumes= {MediaType.APPLICATION_JSON_UTF8_VALUE})
71 public class TopicController {
73 private final Logger log = LoggerFactory.getLogger(this.getClass());
76 private DmaapService dmaapService;
79 private TopicRepository topicRepository;
82 private TopicService topicService;
86 @ApiOperation(value = "List all topic names in DMaaP.")
87 public List<String> listDmaapTopics() {
88 return dmaapService.getTopics();
93 @ApiOperation(value="List all topic names in database")
94 public List<String> list() {
95 Iterable<Topic> ret = topicRepository.findAll();
96 List<String> retString = new ArrayList<>();
99 if(!topicService.istDefaultTopic(item))
100 retString.add(item.getName());
107 @ApiOperation(value="Create a new topic.")
108 public PostReturnBody<TopicConfig> createTopic(@RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
110 if (result.hasErrors()) {
111 sendError(response, 400, "Error parsing Topic: "+result.toString());
114 Topic oldTopic = topicService.getTopic(topicConfig.getName());
115 if (oldTopic != null) {
116 sendError(response, 400, "Topic already exists "+topicConfig.getName());
119 Topic wTopic = topicService.fillTopicConfiguration(topicConfig);
120 if(wTopic.getTtl() == 0)
122 topicRepository.save(wTopic);
123 return mkPostReturnBody(200, wTopic);
127 @GetMapping("/{topicName}")
129 @ApiOperation(value="Get a topic's settings.")
130 public TopicConfig getTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException {
131 Topic topic = topicService.getTopic(topicName);
133 sendError(response, 404, "Topic not found");
136 return topic.getTopicConfig();
139 //This is not a partial update: old topic is wiped out, and new topic is created based on the input json.
140 //One exception is that old DBs are kept
141 @PutMapping("/{topicName}")
143 @ApiOperation(value="Update a topic.")
144 public PostReturnBody<TopicConfig> updateTopic(@PathVariable("topicName") String topicName, @RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
146 if (result.hasErrors()) {
147 sendError(response, 400, "Error parsing Topic: "+result.toString());
151 if(!topicName.equals(topicConfig.getName()))
153 sendError(response, 400, "Topic name mismatch" + topicName + topicConfig.getName());
157 Topic oldTopic = topicService.getTopic(topicConfig.getName());
158 if (oldTopic == null) {
159 sendError(response, 404, "Topic not found "+topicConfig.getName());
162 topicService.fillTopicConfiguration(topicConfig, oldTopic);
163 topicRepository.save(oldTopic);
164 return mkPostReturnBody(200, oldTopic);
168 @DeleteMapping("/{topicName}")
170 @ApiOperation(value="Update a topic.")
171 public void deleteTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException
173 Topic oldTopic = topicService.getTopic(topicName);
174 if (oldTopic == null) {
175 sendError(response, 404, "Topic not found "+topicName);
177 Set<Db> dbRelation = oldTopic.getDbs();
179 topicRepository.save(oldTopic);
180 topicRepository.delete(oldTopic);
181 response.setStatus(204);
185 private PostReturnBody<TopicConfig> mkPostReturnBody(int statusCode, Topic topic)
187 PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
188 retBody.setStatusCode(statusCode);
189 retBody.setReturnBody(topic.getTopicConfig());
194 private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
196 response.sendError(sc, msg);