Merge "DataLake seed code"
[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.List;
24 import java.util.Optional;
25
26 import org.onap.datalake.feeder.domain.Topic;
27 import org.onap.datalake.feeder.repository.TopicRepository;
28 import org.onap.datalake.feeder.service.DmaapService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.MediaType;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.web.bind.annotation.GetMapping;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.PostMapping;
37 import org.springframework.web.bind.annotation.PutMapping;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.ResponseBody;
40 import org.springframework.web.bind.annotation.RestController;
41
42 /**
43  * This controller manages all the topic settings. Topic "_DL_DEFAULT_" acts as
44  * the default. For example, if a topic's enabled=null, _DL_DEFAULT_.enabled is
45  * used for that topic. All the settings are saved in Couchbase. topic
46  * "_DL_DEFAULT_" is populated at setup by a DB script.
47  * 
48  * @author Guobiao Mo
49  *
50  */
51
52 @RestController
53 @RequestMapping(value = "/topics", produces = { MediaType.APPLICATION_JSON_VALUE })
54 public class TopicController {
55
56         private final Logger log = LoggerFactory.getLogger(this.getClass());
57
58         @Autowired
59         private DmaapService dmaapService;
60
61         @Autowired
62         private TopicRepository topicRepository;
63
64         //list all topics in DMaaP
65         @GetMapping("/dmaap/")
66         @ResponseBody
67         public List<String> listDmaapTopics() throws IOException {
68                 return dmaapService.getTopics();
69         }
70
71         //list all topics 
72         @GetMapping("/")
73         @ResponseBody
74         public Iterable<Topic> list() throws IOException {
75                 Iterable<Topic> ret = topicRepository.findAll();
76                 return ret;
77         }
78
79         //Read a topic
80         @GetMapping("/{name}")
81         @ResponseBody
82         public Topic getTopic(@PathVariable("name") String topicName) throws IOException {
83                 //Topic topic = topicRepository.findFirstById(topicName);       
84                 Optional<Topic> topic = topicRepository.findById(topicName);
85                 if (topic.isPresent()) {
86                         return topic.get();
87                 } else {
88                         return null;
89                 }
90         }
91
92         //Update Topic
93         @PutMapping("/")
94         @ResponseBody
95         public Topic updateTopic(Topic topic, BindingResult result) throws IOException {
96
97                 if (result.hasErrors()) {
98                         log.error(result.toString());
99                         
100                         return null;//TODO return binding error
101                 }
102
103                 Topic oldTopic = getTopic(topic.getId());
104                 if (oldTopic == null) {
105                         return null;//TODO return not found error
106                 } else {
107                         topicRepository.save(topic);
108                         return topic;
109                 }
110         }
111
112         //create a new Topic  
113         @PostMapping("/")
114         @ResponseBody
115         public Topic createTopic(Topic topic, BindingResult result) throws IOException {
116
117                 if (result.hasErrors()) {
118                         log.error(result.toString());
119                         return null;
120                 }
121
122                 Topic oldTopic = getTopic(topic.getId());
123                 if (oldTopic != null) {
124                         return null;//TODO return 'already exists' error
125                 } else {
126                         topicRepository.save(topic);
127                         return topic;
128                 }
129         }
130
131 }