Modify TopicConfig
[dcaegen2/services.git] / components / datalake-handler / feeder / src / main / java / org / onap / datalake / feeder / controller / TopicController.java
index bf9e417..f08a994 100644 (file)
@@ -20,6 +20,7 @@
 package org.onap.datalake.feeder.controller;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
@@ -27,6 +28,9 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.onap.datalake.feeder.domain.Db;
 import org.onap.datalake.feeder.domain.Topic;
+import org.onap.datalake.feeder.controller.domain.PostReturnBody;
+import org.onap.datalake.feeder.dto.TopicConfig;
+import org.onap.datalake.feeder.repository.DbRepository;
 import org.onap.datalake.feeder.repository.TopicRepository;
 import org.onap.datalake.feeder.service.DbService;
 import org.onap.datalake.feeder.service.DmaapService;
@@ -49,14 +53,15 @@ import org.springframework.web.bind.annotation.RestController;
 import io.swagger.annotations.ApiOperation;
 
 /**
- * This controller manages topic settings. 
+ * This controller manages topic settings.
  * 
- * Topic "_DL_DEFAULT_" acts as the default. For example, if a topic's enabled=null, _DL_DEFAULT_.enabled is used for that topic. 
- * All the settings are saved in database. 
- * topic "_DL_DEFAULT_" is populated at setup by a DB script.
+ * Topic "_DL_DEFAULT_" acts as the default. For example, if a topic's
+ * enabled=null, _DL_DEFAULT_.enabled is used for that topic. All the settings
+ * are saved in database. topic "_DL_DEFAULT_" is populated at setup by a DB
+ * script.
  * 
  * @author Guobiao Mo
- *
+ * @contributor Kate Hsuan @ QCT
  */
 
 @RestController
@@ -70,122 +75,102 @@ public class TopicController {
 
        @Autowired
        private TopicRepository topicRepository;
-       
+
        @Autowired
        private TopicService topicService;
 
-       @Autowired
-       private DbService dbService;
-       
        @GetMapping("/dmaap/")
        @ResponseBody
-       @ApiOperation(value="List all topics in DMaaP.")
-       public List<String> listDmaapTopics() throws IOException {
+       @ApiOperation(value = "List all topic names in DMaaP.")
+       public List<String> listDmaapTopics() {
                return dmaapService.getTopics();
        }
 
-       @GetMapping("/")
+       @GetMapping("")
        @ResponseBody
-       @ApiOperation(value="List all topics' details.")
-       public Iterable<Topic> list() throws IOException {
+       @ApiOperation(value="List all topics in database")
+       public List<String> list() {
                Iterable<Topic> ret = topicRepository.findAll();
-               return ret;
+               List<String> retString = new ArrayList<>();
+               for(Topic item : ret)
+               {
+                       if(!topicService.istDefaultTopic(item))
+                               retString.add(item.getName());
+               }
+               return retString;
        }
 
-       @GetMapping("/{topicName}")
+       @PostMapping("")
        @ResponseBody
-       @ApiOperation(value="Get a topic's details.")
-       public Topic getTopic(@PathVariable("topicName") String topicName) throws IOException {
-               Topic topic = topicService.getTopic(topicName);
-               return topic;
+       @ApiOperation(value="Create a new topic.")
+       public PostReturnBody<TopicConfig> createTopic(@RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
+
+               if (result.hasErrors()) {
+                       sendError(response, 400, "Error parsing Topic: "+result.toString());
+                       return null;
+               }
+               Topic oldTopic = topicService.getTopic(topicConfig.getName());
+               if (oldTopic != null) {
+                       sendError(response, 400, "Topic already exists "+topicConfig.getName());
+                       return null;
+               } else {
+                       PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
+                       Topic wTopic = topicService.fillTopicConfiguration(topicConfig);
+                       if(wTopic.getTtl() == 0)
+                               wTopic.setTtl(3650);
+                       topicRepository.save(wTopic);
+                       mkPostReturnBody(retBody, 200, wTopic);
+                       return retBody;
+               }
        }
 
-       @GetMapping("/{topicName}/dbs")
+       @GetMapping("/{topicName}")
        @ResponseBody
-       @ApiOperation(value="Get all DBs in a topic.")
-       public Set<Db> getTopicDbs(@PathVariable("topicName") String topicName) throws IOException {
+       @ApiOperation(value="Get a topic's settings.")
+       public TopicConfig getTopic(@PathVariable("topicName") String topicName, HttpServletResponse response) throws IOException {
                Topic topic = topicService.getTopic(topicName);
-               Set<Db> dbs = topic.getDbs();
-               return dbs;
+               if(topic == null) {
+                       sendError(response, 404, "Topic not found");
+                       return null;
+               }
+               return topic.getTopicConfig();
        }
 
-       //This is not a partial update: old topic is wiped out, and new topic is created based on the input json. 
+       //This is not a partial update: old topic is wiped out, and new topic is created based on the input json.
        //One exception is that old DBs are kept
-       @PutMapping("/")
+       @PutMapping("/{topicName}")
        @ResponseBody
        @ApiOperation(value="Update a topic.")
-       public Topic updateTopic(@RequestBody Topic topic, BindingResult result, HttpServletResponse response) throws IOException {
+       public PostReturnBody<TopicConfig> updateTopic(@PathVariable("topicName") String topicName, @RequestBody TopicConfig topicConfig, BindingResult result, HttpServletResponse response) throws IOException {
 
                if (result.hasErrors()) {
                        sendError(response, 400, "Error parsing Topic: "+result.toString());
-                       return null; 
+                       return null;
                }
 
-               Topic oldTopic = getTopic(topic.getName());
-               if (oldTopic == null) {
-                       sendError(response, 404, "Topic not found "+topic.getName());
-                       return null; 
-               } else {
-                       if(!topic.isDefault()) {
-                               Topic defaultTopic = topicService.getDefaultTopic();
-                               topic.setDefaultTopic(defaultTopic);
-                       }
-                       
-                       topic.setDbs(oldTopic.getDbs());
-                       topicRepository.save(topic);
-                       return topic;
-               }
-       }
-       @PostMapping("/")
-       @ResponseBody
-       @ApiOperation(value="Create a new topic.")
-       public Topic createTopic(@RequestBody Topic topic, BindingResult result, HttpServletResponse response) throws IOException {
-               
-               if (result.hasErrors()) {
-                       sendError(response, 400, "Error parsing Topic: "+result.toString());
+               if(!topicName.equals(topicConfig.getName()))
+               {
+                       sendError(response, 400, "Topic name mismatch" + topicName + topicConfig.getName());
                        return null;
                }
 
-               Topic oldTopic = getTopic(topic.getName());
-               if (oldTopic != null) {
-                       sendError(response, 400, "Topic already exists "+topic.getName());
+               Topic oldTopic = topicService.getTopic(topicConfig.getName());
+               if (oldTopic == null) {
+                       sendError(response, 404, "Topic not found "+topicConfig.getName());
                        return null;
                } else {
-                       if(!topic.isDefault()) {
-                               Topic defaultTopic = topicService.getDefaultTopic();
-                               topic.setDefaultTopic(defaultTopic);
-                       }
-                       
-                       topicRepository.save(topic);
-                       return topic;
+                       PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
+                       topicService.fillTopicConfiguration(topicConfig, oldTopic);
+                       topicRepository.save(oldTopic);
+                       mkPostReturnBody(retBody, 200, oldTopic);
+                       return retBody;
                }
        }
 
-       @DeleteMapping("/{topicName}/db/{dbName}")
-       @ResponseBody
-       @ApiOperation(value="Delete a DB from a topic.")
-       public Set<Db> deleteDb(@PathVariable("topicName") String topicName, @PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
-               Topic topic = topicService.getTopic(topicName);
-               Set<Db> dbs = topic.getDbs();
-               dbs.remove(new Db(dbName));
-                
-               topicRepository.save(topic);
-               return topic.getDbs();           
-       }
-
-       @PutMapping("/{topicName}/db/{dbName}")
-       @ResponseBody
-       @ApiOperation(value="Add a DB to a topic.")
-       public Set<Db> addDb(@PathVariable("topicName") String topicName, @PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException {
-               Topic topic = topicService.getTopic(topicName);
-               Set<Db> dbs = topic.getDbs();           
-
-               Db db = dbService.getDb(dbName);                
-               dbs.add(db);
-                
-               topicRepository.save(topic);
-               return topic.getDbs();           
+       private void mkPostReturnBody(PostReturnBody<TopicConfig> retBody, int statusCode, Topic topic)
+       {
+        retBody.setStatusCode(statusCode);
+        retBody.setReturnBody(topic.getTopicConfig());
        }
        
        private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {