7583684a8502c1ef9bad58fd9a27e276c38317ae
[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.controller.domain.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. For example, if a topic's
59  * enabled=null, _DL_DEFAULT_.enabled is used for that topic. All the settings
60  * are saved in database. topic "_DL_DEFAULT_" is populated at setup by a DB
61  * 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 DbRepository dbRepository;
81
82         @Autowired
83         private TopicService topicService;
84
85         @Autowired
86         private DbService dbService;
87
88         @GetMapping("/dmaap/")
89         @ResponseBody
90         @ApiOperation(value = "List all topic names in DMaaP.")
91         public List<String> listDmaapTopics() throws IOException {
92                 return dmaapService.getTopics();
93         }
94
95         @GetMapping("")
96         @ResponseBody
97         @ApiOperation(value="List all topics")
98         public List<String> list() throws IOException {
99                 Iterable<Topic> ret = topicRepository.findAll();
100                 List<String> retString = new ArrayList<>();
101                 for(Topic item : ret)
102                 {
103                         if(!topicService.istDefaultTopic(item))
104                                 retString.add(item.getName());
105                 }
106                 return retString;
107         }
108
109         @PostMapping("")
110         @ResponseBody
111         @ApiOperation(value="Create a new topic.")
112         public PostReturnBody<TopicConfig> createTopic(@RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
113
114                 if (result.hasErrors()) {
115                         sendError(response, 400, "Error parsing Topic: "+result.toString());
116                         return null;
117                 }
118                 Topic oldTopic = topicService.getTopic(topicConfig.getName());
119                 if (oldTopic != null) {
120                         sendError(response, 400, "Topic already exists "+topicConfig.getName());
121                         return null;
122                 } else {
123                         PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
124                         Topic wTopic = topicService.fillTopicConfiguration(topicConfig);
125                         if(wTopic.getTtl() == 0)
126                                 wTopic.setTtl(3650);
127                         topicRepository.save(wTopic);
128                         mkPostReturnBody(retBody, 200, wTopic);
129                         return retBody;
130                 }
131         }
132
133         @GetMapping("/{topicName}")
134         @ResponseBody
135         @ApiOperation(value="Get a topic's settings.")
136         public TopicConfig getTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException {
137                 Topic topic = topicService.getTopic(topicName);
138                 if(topic == null) {
139                         sendError(response, 404, "Topic not found");
140                 }
141                 TopicConfig tConfig = new TopicConfig();
142                 mkReturnMessage(topic, tConfig);
143                 return tConfig;
144         }
145
146         //This is not a partial update: old topic is wiped out, and new topic is created based on the input json.
147         //One exception is that old DBs are kept
148         @PutMapping("/{topicName}")
149         @ResponseBody
150         @ApiOperation(value="Update a topic.")
151         public PostReturnBody<TopicConfig> updateTopic(@PathVariable("topicName") String topicName, @RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
152
153                 if (result.hasErrors()) {
154                         sendError(response, 400, "Error parsing Topic: "+result.toString());
155                         return null;
156                 }
157
158                 if(!topicName.equals(topicConfig.getName()))
159                 {
160                         sendError(response, 400, "Topic name mismatch" + topicName + topicConfig.getName());
161                         return null;
162                 }
163
164                 Topic oldTopic = topicService.getTopic(topicConfig.getName());
165                 if (oldTopic == null) {
166                         sendError(response, 404, "Topic not found "+topicConfig.getName());
167                         return null;
168                 } else {
169                         PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
170                         topicService.fillTopicConfiguration(topicConfig, oldTopic);
171                         topicRepository.save(oldTopic);
172                         mkPostReturnBody(retBody, 200, oldTopic);
173                         return retBody;
174                 }
175         }
176
177     private void mkReturnMessage(Topic topic, TopicConfig tConfig)
178         {
179                 tConfig.setName(topic.getName());
180                 tConfig.setEnable(topic.getEnabled());
181                 if(topic.getDataFormat() != null)
182                         tConfig.setData_format(topic.getDataFormat().toString());
183                 tConfig.setSave_raw(topic.getSaveRaw());
184                 tConfig.setCorrelated_clearred_message((topic.getCorrelateClearedMessage() == null) ? topic.getCorrelateClearedMessage() : false);
185                 tConfig.setMessage_id_path(topic.getMessageIdPath());
186                 tConfig.setTtl(topic.getTtl());
187                 Set<Db> topicDb = topic.getDbs();
188                 List<String> dbList = new ArrayList<>();
189                 for(Db item: topicDb)
190                 {
191                         dbList.add(item.getName());
192                 }
193                 tConfig.setSinkdbs(dbList);
194         }
195
196         private void mkPostReturnBody(PostReturnBody<TopicConfig> retBody, int statusCode, Topic topic)
197         {
198                 TopicConfig retTopic = new TopicConfig();
199         retBody.setStatusCode(statusCode);
200         mkReturnMessage(topic, retTopic);
201         retBody.setReturnBody(retTopic);
202         }
203         
204         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
205                 log.info(msg);
206                 response.sendError(sc, msg);            
207         }
208 }