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.List;
 
  24 import java.util.Optional;
 
  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;
 
  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.
 
  53 @RequestMapping(value = "/topics", produces = { MediaType.APPLICATION_JSON_VALUE })
 
  54 public class TopicController {
 
  56         private final Logger log = LoggerFactory.getLogger(this.getClass());
 
  59         private DmaapService dmaapService;
 
  62         private TopicRepository topicRepository;
 
  64         //list all topics in DMaaP
 
  65         @GetMapping("/dmaap/")
 
  67         public List<String> listDmaapTopics() throws IOException {
 
  68                 return dmaapService.getTopics();
 
  74         public Iterable<Topic> list() throws IOException {
 
  75                 Iterable<Topic> ret = topicRepository.findAll();
 
  80         @GetMapping("/{name}")
 
  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()) {
 
  95         public Topic updateTopic(Topic topic, BindingResult result) throws IOException {
 
  97                 if (result.hasErrors()) {
 
  98                         log.error(result.toString());
 
 100                         return null;//TODO return binding error
 
 103                 Topic oldTopic = getTopic(topic.getId());
 
 104                 if (oldTopic == null) {
 
 105                         return null;//TODO return not found error
 
 107                         topicRepository.save(topic);
 
 115         public Topic createTopic(Topic topic, BindingResult result) throws IOException {
 
 117                 if (result.hasErrors()) {
 
 118                         log.error(result.toString());
 
 122                 Topic oldTopic = getTopic(topic.getId());
 
 123                 if (oldTopic != null) {
 
 124                         return null;//TODO return 'already exists' error
 
 126                         topicRepository.save(topic);