From 1995d2e5a0b202d499f59353920a17fc3aa7f4cb Mon Sep 17 00:00:00 2001 From: Guobiao Mo Date: Mon, 8 Apr 2019 16:59:14 -0700 Subject: Integrate Swagger for REST API Issue-ID: DCAEGEN2-1400 Change-Id: Idd68e1f50b6572dcd4c2e17291628810f3813dba Signed-off-by: Guobiao Mo --- components/datalake-handler/feeder/pom.xml | 39 +++++++++----- .../java/org/onap/datalake/feeder/Application.java | 3 ++ .../onap/datalake/feeder/config/SwaggerConfig.java | 60 ++++++++++++++++++++++ .../datalake/feeder/controller/DbController.java | 52 ++++++++++++------- .../feeder/controller/FeederController.java | 5 ++ .../feeder/controller/TopicController.java | 38 +++++++------- .../datalake/feeder/config/SwaggerConfigTest.java | 46 +++++++++++++++++ 7 files changed, 193 insertions(+), 50 deletions(-) create mode 100644 components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/SwaggerConfig.java create mode 100644 components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/SwaggerConfigTest.java diff --git a/components/datalake-handler/feeder/pom.xml b/components/datalake-handler/feeder/pom.xml index 5b47a245..d6b50787 100644 --- a/components/datalake-handler/feeder/pom.xml +++ b/components/datalake-handler/feeder/pom.xml @@ -17,22 +17,21 @@ - + - org.mariadb.jdbc - mariadb-java-client + org.mariadb.jdbc + mariadb-java-client - + org.json json - + org.apache.httpcomponents httpclient - 4.5.6 - + org.apache.kafka @@ -48,12 +47,12 @@ org.springframework.boot spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-data-jpa - - + + + org.springframework.boot + spring-boot-starter-data-jpa + + org.springframework.boot spring-boot-starter-data-couchbase @@ -135,6 +134,20 @@ 6.0.10.Final + + io.springfox + springfox-swagger2 + 2.9.2 + compile + + + + io.springfox + springfox-swagger-ui + 2.9.2 + compile + + diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/Application.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/Application.java index 8f17937b..83f56b1d 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/Application.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/Application.java @@ -26,6 +26,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + /** * Entry point of the DataLake feeder application * @@ -34,6 +36,7 @@ import org.springframework.context.annotation.Bean; */ @SpringBootApplication +@EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/SwaggerConfig.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/SwaggerConfig.java new file mode 100644 index 00000000..dcf00a90 --- /dev/null +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/config/SwaggerConfig.java @@ -0,0 +1,60 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DATALAKE +* ================================================================================ +* Copyright 2019 China Mobile +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.datalake.feeder.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * For Swagger integration + * + * @author Guobiao Mo + * + */ + +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + @Bean + public Docket produceApi() { + return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("org.onap.datalake.feeder")).paths(paths()).build(); + } + + // Describe your apis + private ApiInfo apiInfo() { + return new ApiInfoBuilder().title("DataLake Rest APIs").description("This page lists all the rest apis for DataLake.").version("1.0.0-SNAPSHOT").build(); + } + + // Only select apis that matches the given Predicates. + private Predicate paths() { + // Match all paths except /error + return Predicates.or(PathSelectors.regex("/dbs.*"), PathSelectors.regex("/topics.*"), PathSelectors.regex("/feeder.*")); + } +} \ No newline at end of file diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DbController.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DbController.java index c34befcc..c4288d93 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DbController.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/DbController.java @@ -42,8 +42,14 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + /** - * This controller manages the big data storage settings. All the settings are saved in database. + * This controller manages the big data storage settings. All the settings are + * saved in database. * * @author Guobiao Mo * @@ -51,19 +57,21 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/dbs", produces = { MediaType.APPLICATION_JSON_VALUE }) +//@Api(value = "db", consumes = "application/json", produces = "application/json") public class DbController { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Autowired private DbRepository dbRepository; - + @Autowired private DbService dbService; //list all dbs @GetMapping("/") @ResponseBody + @ApiOperation(value="Get all databases' details.") public Iterable list() throws IOException { Iterable ret = dbRepository.findAll(); return ret; @@ -71,18 +79,23 @@ public class DbController { //Read a db //the topics are missing in the return, since in we use @JsonBackReference on Db's topics - //need to the the following method to retrieve the topic list - @GetMapping("/{name}") + //need to the the following method to retrieve the topic list + @GetMapping("/{dbName}") @ResponseBody - public Db getDb(@PathVariable("name") String dbName) throws IOException { + @ApiOperation(value="Get a database's details.") + public Db getDb(@PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException { Db db = dbService.getDb(dbName); + if (db == null) { + sendError(response, 404, "Db not found: " + dbName); + } return db; } //Read topics in a DB - @GetMapping("/{name}/topics") + @GetMapping("/{dbName}/topics") @ResponseBody - public Set getDbTopics(@PathVariable("name") String dbName) throws IOException { + @ApiOperation(value="Get a database's all topics.") + public Set getDbTopics(@PathVariable("dbName") String dbName) throws IOException { Db db = dbService.getDb(dbName); Set topics = db.getTopics(); return topics; @@ -91,45 +104,46 @@ public class DbController { //Update Db @PutMapping("/") @ResponseBody + @ApiOperation(value="Update a database.") public Db updateDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException { if (result.hasErrors()) { - sendError(response, 400, "Error parsing DB: "+result.toString()); - return null; + sendError(response, 400, "Error parsing DB: " + result.toString()); + return null; } - Db oldDb = getDb(db.getName()); + Db oldDb = dbService.getDb(db.getName()); if (oldDb == null) { - sendError(response, 404, "Db not found: "+db.getName()); - return null; + sendError(response, 404, "Db not found: " + db.getName()); + return null; } else { - dbRepository.save(db); + dbRepository.save(db); return db; } } - //create a new Db @PostMapping("/") @ResponseBody + @ApiOperation(value="Create a new database.") public Db createDb(@RequestBody Db db, BindingResult result, HttpServletResponse response) throws IOException { if (result.hasErrors()) { - sendError(response, 400, "Error parsing DB: "+result.toString()); + sendError(response, 400, "Error parsing DB: " + result.toString()); return null; } - Db oldDb = getDb(db.getName()); + Db oldDb = dbService.getDb(db.getName()); if (oldDb != null) { - sendError(response, 400, "Db already exists: "+db.getName()); + sendError(response, 400, "Db already exists: " + db.getName()); return null; } else { - dbRepository.save(db); + dbRepository.save(db); return db; } } private void sendError(HttpServletResponse response, int sc, String msg) throws IOException { log.info(msg); - response.sendError(sc, msg); + response.sendError(sc, msg); } } diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/FeederController.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/FeederController.java index 2e13e1af..3d296d5f 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/FeederController.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/FeederController.java @@ -30,6 +30,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import io.swagger.annotations.ApiOperation; + /** * This controller controls DL data feeder. * @@ -51,6 +53,7 @@ public class FeederController { * @throws IOException */ @GetMapping("/start") + @ApiOperation(value="Start pulling data.") public String start() throws IOException { log.info("DataLake feeder starting to pull data from DMaaP..."); pullService.start(); @@ -61,6 +64,7 @@ public class FeederController { * @return message that application stop process is triggered */ @GetMapping("/stop") + @ApiOperation(value="Stop pulling data.") public String stop() { pullService.shutdown(); log.info("DataLake feeder is stopped."); @@ -70,6 +74,7 @@ public class FeederController { * @return feeder status */ @GetMapping("/status") + @ApiOperation(value="Retrieve feeder status.") public String status() { String status = "Feeder is running: "+pullService.isRunning(); log.info("senting feeder status ...");//TODO we can send what topics are monitored, how many messages are sent, etc. diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/TopicController.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/TopicController.java index c4aec14c..bf9e417f 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/TopicController.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/TopicController.java @@ -46,6 +46,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import io.swagger.annotations.ApiOperation; + /** * This controller manages topic settings. * @@ -75,43 +77,43 @@ public class TopicController { @Autowired private DbService dbService; - //list all topics in DMaaP @GetMapping("/dmaap/") @ResponseBody + @ApiOperation(value="List all topics in DMaaP.") public List listDmaapTopics() throws IOException { return dmaapService.getTopics(); } - //list all topics @GetMapping("/") @ResponseBody + @ApiOperation(value="List all topics' details.") public Iterable list() throws IOException { Iterable ret = topicRepository.findAll(); return ret; } - //Read a topic - @GetMapping("/{topicname}") + @GetMapping("/{topicName}") @ResponseBody - public Topic getTopic(@PathVariable("topicname") String topicName) throws IOException { + @ApiOperation(value="Get a topic's details.") + public Topic getTopic(@PathVariable("topicName") String topicName) throws IOException { Topic topic = topicService.getTopic(topicName); return topic; } - //Read DBs in a topic - @GetMapping("/{topicname}/dbs") + @GetMapping("/{topicName}/dbs") @ResponseBody - public Set getTopicDbs(@PathVariable("topicname") String topicName) throws IOException { + @ApiOperation(value="Get all DBs in a topic.") + public Set getTopicDbs(@PathVariable("topicName") String topicName) throws IOException { Topic topic = topicService.getTopic(topicName); Set dbs = topic.getDbs(); return dbs; } - //Update Topic - //This is not a partial update: old topic is wiped out, and new topic is created base 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("/") @ResponseBody + @ApiOperation(value="Update a topic.") public Topic updateTopic(@RequestBody Topic topic, BindingResult result, HttpServletResponse response) throws IOException { if (result.hasErrors()) { @@ -134,10 +136,10 @@ public class TopicController { return topic; } } - - //create a new Topic + @PostMapping("/") @ResponseBody + @ApiOperation(value="Create a new topic.") public Topic createTopic(@RequestBody Topic topic, BindingResult result, HttpServletResponse response) throws IOException { if (result.hasErrors()) { @@ -160,10 +162,10 @@ public class TopicController { } } - //delete a db from the topic - @DeleteMapping("/{topicname}/db/{dbname}") + @DeleteMapping("/{topicName}/db/{dbName}") @ResponseBody - public Set deleteDb(@PathVariable("topicname") String topicName, @PathVariable("dbname") String dbName, HttpServletResponse response) throws IOException { + @ApiOperation(value="Delete a DB from a topic.") + public Set deleteDb(@PathVariable("topicName") String topicName, @PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException { Topic topic = topicService.getTopic(topicName); Set dbs = topic.getDbs(); dbs.remove(new Db(dbName)); @@ -172,10 +174,10 @@ public class TopicController { return topic.getDbs(); } - //add a db to the topic - @PutMapping("/{topicname}/db/{dbname}") + @PutMapping("/{topicName}/db/{dbName}") @ResponseBody - public Set addDb(@PathVariable("topicname") String topicName, @PathVariable("dbname") String dbName, HttpServletResponse response) throws IOException { + @ApiOperation(value="Add a DB to a topic.") + public Set addDb(@PathVariable("topicName") String topicName, @PathVariable("dbName") String dbName, HttpServletResponse response) throws IOException { Topic topic = topicService.getTopic(topicName); Set dbs = topic.getDbs(); diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/SwaggerConfigTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/SwaggerConfigTest.java new file mode 100644 index 00000000..4293c698 --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/SwaggerConfigTest.java @@ -0,0 +1,46 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DATALAKE +* ================================================================================ +* Copyright 2019 China Mobile +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.datalake.feeder.config; + +import static org.junit.Assert.fail; + +import org.junit.Test; + +/** + * Test Swagger integration + * + * @author Guobiao Mo + * + */ + +public class SwaggerConfigTest { + + @Test + public void test() { + try { + SwaggerConfig config = new SwaggerConfig(); + config.produceApi(); + } catch (Exception e) { + fail("failed to read configure Swagger."); + } + } + +} \ No newline at end of file -- cgit 1.2.3-korg