package org.onap.datalake.feeder.controller;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.List;
import java.util.Set;
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;
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.
+ * If a topic is not present in database, "_DL_DEFAULT_" is used for it.
+ * If a topic is present in database, itself should be complete, and no setting from "_DL_DEFAULT_" is used.
+ * Topic "_DL_DEFAULT_" is populated at setup by a DB script.
*
* @author Guobiao Mo
- *
+ * @contributor Kate Hsuan @ QCT
*/
@RestController
@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 topic names 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 {
+ Topic wTopic = topicService.fillTopicConfiguration(topicConfig);
+ if(wTopic.getTtl() == 0)
+ wTopic.setTtl(3650);
+ topicRepository.save(wTopic);
+ return mkPostReturnBody(200, wTopic);
+ }
}
- @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;
+ topicService.fillTopicConfiguration(topicConfig, oldTopic);
+ topicRepository.save(oldTopic);
+ return mkPostReturnBody(200, oldTopic);
}
}
- @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 PostReturnBody<TopicConfig> mkPostReturnBody(int statusCode, Topic topic)
+ {
+ PostReturnBody<TopicConfig> retBody = new PostReturnBody<>();
+ retBody.setStatusCode(statusCode);
+ retBody.setReturnBody(topic.getTopicConfig());
+
+ return retBody;
}
private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {