93cec8bbe6ad106ffea5c78aa5e788c237f7e856
[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.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
54 import io.swagger.annotations.ApiOperation;
55
56 /**
57  * This controller manages topic settings.
58  * 
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.
63  * 
64  * @author Guobiao Mo
65  * @contributor Kate Hsuan @ QCT
66  */
67
68 @RestController
69 @RequestMapping(value = "/topics", produces = { MediaType.APPLICATION_JSON_VALUE })//, consumes= {MediaType.APPLICATION_JSON_UTF8_VALUE})
70 public class TopicController {
71
72         private final Logger log = LoggerFactory.getLogger(this.getClass());
73
74         @Autowired
75         private DmaapService dmaapService;
76
77         @Autowired
78         private TopicRepository topicRepository;
79
80         @Autowired
81         private TopicService topicService;
82
83         @GetMapping("/dmaap")
84         @ResponseBody
85         @ApiOperation(value = "List all topic names in DMaaP.")
86         public List<String> listDmaapTopics() {
87                 return dmaapService.getTopics();
88         }
89
90         @GetMapping("")
91         @ResponseBody
92         @ApiOperation(value="List all topic names in database")
93         public List<String> list() {
94                 Iterable<Topic> ret = topicRepository.findAll();
95                 List<String> retString = new ArrayList<>();
96                 for(Topic item : ret)
97                 {
98                         if(!topicService.istDefaultTopic(item))
99                                 retString.add(item.getName());
100                 }
101                 return retString;
102         }
103
104         @PostMapping("")
105         @ResponseBody
106         @ApiOperation(value="Create a new topic.")
107         public PostReturnBody<TopicConfig> createTopic(@RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
108
109                 if (result.hasErrors()) {
110                         sendError(response, 400, "Error parsing Topic: "+result.toString());
111                         return null;
112                 }
113                 Topic oldTopic = topicService.getTopic(topicConfig.getName());
114                 if (oldTopic != null) {
115                         sendError(response, 400, "Topic already exists "+topicConfig.getName());
116                         return null;
117                 } else {
118                         Topic wTopic = topicService.fillTopicConfiguration(topicConfig);
119                         if(wTopic.getTtl() == 0)
120                                 wTopic.setTtl(3650);
121                         topicRepository.save(wTopic); 
122                         return mkPostReturnBody(200, wTopic);
123                 }
124         }
125
126         @GetMapping("/{topicName}")
127         @ResponseBody
128         @ApiOperation(value="Get a topic's settings.")
129         public TopicConfig getTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException {
130                 Topic topic = topicService.getTopic(topicName);
131                 if(topic == null) {
132                         sendError(response, 404, "Topic not found");
133                         return null;
134                 }
135                 return topic.getTopicConfig();
136         }
137
138         //This is not a partial update: old topic is wiped out, and new topic is created based on the input json.
139         //One exception is that old DBs are kept
140         @PutMapping("/{topicName}")
141         @ResponseBody
142         @ApiOperation(value="Update a topic.")
143         public PostReturnBody<TopicConfig> updateTopic(@PathVariable("topicName") String topicName, @RequestBody TopicConfig topicConfig, 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                 if(!topicName.equals(topicConfig.getName()))
151                 {
152                         sendError(response, 400, "Topic name mismatch" + topicName + topicConfig.getName());
153                         return null;
154                 }
155
156                 Topic oldTopic = topicService.getTopic(topicConfig.getName());
157                 if (oldTopic == null) {
158                         sendError(response, 404, "Topic not found "+topicConfig.getName());
159                         return null;
160                 } else {
161                         topicService.fillTopicConfiguration(topicConfig, oldTopic);
162                         topicRepository.save(oldTopic);
163                         return mkPostReturnBody(200, oldTopic);
164                 }
165         }
166
167         @DeleteMapping("/{topicName}")
168         @ResponseBody
169         @ApiOperation(value="Update a topic.")
170         public void deleteTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException
171         {
172                 Topic oldTopic = topicService.getTopic(topicName);
173                 if (oldTopic == null) {
174                         sendError(response, 404, "Topic not found "+topicName);
175                 } else {
176                         Set<Db> dbRelation = oldTopic.getDbs();
177                         dbRelation.clear();
178                         topicRepository.save(oldTopic);
179                         topicRepository.delete(oldTopic);
180                         response.setStatus(204);
181                 }
182         }
183
184         private PostReturnBody<TopicConfig> mkPostReturnBody(int statusCode, Topic topic)
185         {
186                 PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
187         retBody.setStatusCode(statusCode);
188         retBody.setReturnBody(topic.getTopicConfig());
189         
190         return retBody;
191         }
192         
193         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
194                 log.info(msg);
195                 response.sendError(sc, msg);            
196         }
197 }