From 9f52f60a2e70f2344c671af28e62fe2315ee8e02 Mon Sep 17 00:00:00 2001 From: Ruslan Kashapov Date: Fri, 22 Jan 2021 12:53:01 +0200 Subject: Initial cps-nf-proxy-rest module setup in CPS project Issue-ID: CPS-171 Change-Id: I8998dc2818b6bc07fc4fe25a2d735b4ab8b4b817 Signed-off-by: Ruslan Kashapov --- cps-bom/pom.xml | 5 + cps-nf-proxy-rest/docs/openapi/components.yaml | 70 ++++++++++++ cps-nf-proxy-rest/docs/openapi/openapi.yml | 13 +++ cps-nf-proxy-rest/docs/openapi/xnfProxy.yml | 31 ++++++ cps-nf-proxy-rest/pom.xml | 119 +++++++++++++++++++++ .../java/org/onap/cps/nfproxy/Application.java | 30 ++++++ .../org/onap/cps/nfproxy/config/NfProxyConfig.java | 44 ++++++++ .../nfproxy/rest/controller/NfProxyController.java | 42 ++++++++ .../exceptions/NfProxyRestExceptionHandler.java | 78 ++++++++++++++ .../src/main/resources/application.yml | 46 ++++++++ .../src/main/resources/openapi-configuration.json | 28 +++++ .../cps/nfproxy/config/NfProxyConfigSpec.groovy | 32 ++++++ .../rest/controller/NfProxyControllerSpec.groovy | 54 ++++++++++ cps-parent/pom.xml | 2 + pom.xml | 1 + 15 files changed, 595 insertions(+) create mode 100644 cps-nf-proxy-rest/docs/openapi/components.yaml create mode 100755 cps-nf-proxy-rest/docs/openapi/openapi.yml create mode 100644 cps-nf-proxy-rest/docs/openapi/xnfProxy.yml create mode 100755 cps-nf-proxy-rest/pom.xml create mode 100644 cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java create mode 100755 cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java create mode 100644 cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java create mode 100644 cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java create mode 100644 cps-nf-proxy-rest/src/main/resources/application.yml create mode 100644 cps-nf-proxy-rest/src/main/resources/openapi-configuration.json create mode 100644 cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy create mode 100644 cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy diff --git a/cps-bom/pom.xml b/cps-bom/pom.xml index ab904c5593..cc89de7aa8 100644 --- a/cps-bom/pom.xml +++ b/cps-bom/pom.xml @@ -42,6 +42,11 @@ cps-rest ${project.version} + + org.onap.cps + cps-nf-proxy-rest + ${project.version} + org.onap.cps cps-ri diff --git a/cps-nf-proxy-rest/docs/openapi/components.yaml b/cps-nf-proxy-rest/docs/openapi/components.yaml new file mode 100644 index 0000000000..5352199834 --- /dev/null +++ b/cps-nf-proxy-rest/docs/openapi/components.yaml @@ -0,0 +1,70 @@ +components: + schemas: + ErrorMessage: + type: object + title: Error + properties: + status: + type: string + message: + type: string + details: + type: string + MultipartFile: + required: + - file + properties: + multipartFile: + type: string + description: multipartFile + format: binary + + parameters: + + + responses: + NotFound: + description: The specified resource was not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorMessage' + Unauthorized: + description: Unauthorized + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorMessage' + Forbidden: + description: Forbidden + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorMessage' + BadRequest: + description: Bad Request + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorMessage' + Conflict: + description: Conflict + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorMessage' + Ok: + description: OK + content: + application/json: + schema: + type: object + Created: + description: Created + content: + text/plain: + schema: + type: string + NoContent: + description: No Content + content: {} diff --git a/cps-nf-proxy-rest/docs/openapi/openapi.yml b/cps-nf-proxy-rest/docs/openapi/openapi.yml new file mode 100755 index 0000000000..0dbab34eef --- /dev/null +++ b/cps-nf-proxy-rest/docs/openapi/openapi.yml @@ -0,0 +1,13 @@ +openapi: 3.0.1 +info: + title: xNF to CPS Proxy API + description: xNF to CPS Proxy API + version: "1.0" +servers: + - url: //localhost:8088/ +paths: + /v1/hello-world: + $ref: 'xnfProxy.yml#/helloWorld' + /v1/hello-error: + $ref: 'xnfProxy.yml#/helloError' + diff --git a/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml b/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml new file mode 100644 index 0000000000..0bb673ac3d --- /dev/null +++ b/cps-nf-proxy-rest/docs/openapi/xnfProxy.yml @@ -0,0 +1,31 @@ +helloWorld: + get: + tags: + - nf-proxy + summary: rest interface validation + operationId: helloWorld + responses: + 200: + $ref: 'components.yaml#/components/responses/Ok' + 400: + $ref: 'components.yaml#/components/responses/BadRequest' + 401: + $ref: 'components.yaml#/components/responses/Unauthorized' + 403: + $ref: 'components.yaml#/components/responses/Forbidden' + +helloError: + get: + tags: + - nf-proxy + summary: error handler validation + operationId: helloError + responses: + 200: + $ref: 'components.yaml#/components/responses/Ok' + 400: + $ref: 'components.yaml#/components/responses/BadRequest' + 401: + $ref: 'components.yaml#/components/responses/Unauthorized' + 403: + $ref: 'components.yaml#/components/responses/Forbidden' \ No newline at end of file diff --git a/cps-nf-proxy-rest/pom.xml b/cps-nf-proxy-rest/pom.xml new file mode 100755 index 0000000000..23adb93355 --- /dev/null +++ b/cps-nf-proxy-rest/pom.xml @@ -0,0 +1,119 @@ + + 4.0.0 + + org.onap.cps + cps-parent + 0.0.1-SNAPSHOT + ../cps-parent/pom.xml + + + cps-nf-proxy-rest + + + 0.44 + + + + + ${project.groupId} + cps-service + + + ${project.groupId} + cps-ri + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-jetty + + + org.springframework.boot + spring-boot-starter-actuator + + + io.swagger.core.v3 + swagger-annotations + + + io.springfox + springfox-boot-starter + + + org.apache.commons + commons-lang3 + + + + org.codehaus.groovy + groovy + test + + + org.spockframework + spock-core + test + + + org.spockframework + spock-spring + test + + + cglib + cglib-nodep + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + io.swagger.codegen.v3 + swagger-codegen-maven-plugin + + + + generate + + + ${project.basedir}/docs/openapi/openapi.yml + org.onap.cps.nfproxy.rest.controller + org.onap.cps.nfproxy.rest.model + org.onap.cps.nfproxy.rest.api + spring + false + + + + + + + diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java new file mode 100644 index 0000000000..abad806e0b --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.nfproxy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java new file mode 100755 index 0000000000..3bdedc3631 --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java @@ -0,0 +1,44 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.nfproxy.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +@Configuration +public class NfProxyConfig { + + /** + * Swagger-ui configuration. + */ + @Bean + public Docket api() { + return new Docket(DocumentationType.OAS_30).select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + +} \ No newline at end of file diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java new file mode 100644 index 0000000000..8125c5aed7 --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.nfproxy.rest.controller; + +import org.onap.cps.nfproxy.rest.api.NfProxyApi; +import org.onap.cps.spi.exceptions.CpsException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("${rest.api.base-path}") +public class NfProxyController implements NfProxyApi { + + @Override + public ResponseEntity helloWorld() { + return new ResponseEntity<>("Hello World!", HttpStatus.OK); + } + + @Override + public ResponseEntity helloError() { + throw new CpsException("Example error Message", "Example error description"); + } +} diff --git a/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java new file mode 100644 index 0000000000..96a1cb267d --- /dev/null +++ b/cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.nfproxy.rest.exceptions; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.onap.cps.nfproxy.rest.controller.NfProxyController; +import org.onap.cps.nfproxy.rest.model.ErrorMessage; +import org.onap.cps.spi.exceptions.CpsException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * Exception handler with error message return. + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@RestControllerAdvice(assignableTypes = {NfProxyController.class}) +public class NfProxyRestExceptionHandler { + + /** + * Default exception handler. + * + * @param exception the exception to handle + * @return response with response code 500. + */ + @ExceptionHandler + public static ResponseEntity handleInternalServerErrorExceptions( + final Exception exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); + } + + @ExceptionHandler({CpsException.class}) + public static ResponseEntity handleAnyOtherCpsExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); + } + + private static ResponseEntity buildErrorResponse(final HttpStatus status, final Exception exception) { + return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception)); + } + + private static ResponseEntity buildErrorResponse(final HttpStatus status, final String message, + final String details) { + log.error("An error has occurred : {} Status: {} Details: {}", message, status, details); + final ErrorMessage errorMessage = new ErrorMessage(); + errorMessage.setStatus(status.toString()); + errorMessage.setMessage(message); + errorMessage.setDetails(details); + return new ResponseEntity<>(errorMessage, status); + } + + private static String extractDetails(final CpsException exception) { + return exception.getCause() == null + ? exception.getDetails() + : ExceptionUtils.getStackTrace(exception.getCause()); + } +} diff --git a/cps-nf-proxy-rest/src/main/resources/application.yml b/cps-nf-proxy-rest/src/main/resources/application.yml new file mode 100644 index 0000000000..06c0fd1ea3 --- /dev/null +++ b/cps-nf-proxy-rest/src/main/resources/application.yml @@ -0,0 +1,46 @@ +server: + port: 8080 + +rest: + api: + base-path: /cps-nf-proxy/api + +spring: + main: + banner-mode: "off" +# for POC only, later this should move to cpi-ri module + jpa: + ddl-auto: create + open-in-view: false + properties: + hibernate: + enable_lazy_load_no_trans: true + dialect: org.hibernate.dialect.PostgreSQLDialect + + datasource: + url: jdbc:postgresql://${DB_HOST}:5432/cpsdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driverClassName: org.postgresql.Driver + initialization-mode: always + +# Actuator +management: + endpoints: + web: + base-path: /manage + exposure: + include: info,health,loggers + endpoint: + health: + show-details: always + # kubernetes probes: liveness and readiness + probes: + enabled: true + loggers: + enabled: true + +logging: + level: + org: + springframework: INFO diff --git a/cps-nf-proxy-rest/src/main/resources/openapi-configuration.json b/cps-nf-proxy-rest/src/main/resources/openapi-configuration.json new file mode 100644 index 0000000000..efc2f9778a --- /dev/null +++ b/cps-nf-proxy-rest/src/main/resources/openapi-configuration.json @@ -0,0 +1,28 @@ +{ + "resourcePackages": [ + "org.onap.cps.nfproxy.rest.controller" + ], + "prettyPrint": true, + "cacheTTL": 0, + "openAPI": { + "info": { + "title": "ONAP Open API v3 CPS xNF Proxy Spec", + "description": "The API Description may be multiline, and GitHub Flavored Markdown, GFM syntax, can be used for rich text representation.", + "x-logo": { + "url": "logo.png" + }, + "contact": { + "name": "ONAP", + "url": "https://onap.readthedocs.io", + "email": "onap-discuss@lists.onap.org" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0" + }, + "version": "1.2.34", + "x-planned-retirement-date": "202207", + "x-component": "Modeling" + } + } +} diff --git a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy new file mode 100644 index 0000000000..3eb42d7779 --- /dev/null +++ b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/config/NfProxyConfigSpec.groovy @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.cps.nfproxy.config + + +import spock.lang.Specification +import springfox.documentation.spring.web.plugins.Docket + +class NfProxyConfigSpec extends Specification { + def objectUnderTest = new NfProxyConfig() + + def 'xNF Proxy configuration has a Docket API'() { + expect: 'the CPS configuration has a Docket API' + objectUnderTest.api() instanceof Docket + } +} diff --git a/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy new file mode 100644 index 0000000000..d1c3b1648f --- /dev/null +++ b/cps-nf-proxy-rest/src/test/groovy/org/onap/cps/nfproxy/rest/controller/NfProxyControllerSpec.groovy @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.nfproxy.rest.controller + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.http.HttpStatus +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import spock.lang.Specification + +@WebMvcTest +class NfProxyControllerSpec extends Specification { + + @Autowired + MockMvc mvc + + @Value('${rest.api.base-path}') + def basePath + + def 'Hello world method invocation.'(){ + when: 'hello-world request performed' + def response = mvc.perform(MockMvcRequestBuilders.get("$basePath/v1/hello-world")).andReturn().response + then: 'success response returned' + response.status == HttpStatus.OK.value() + response.getContentAsString().contains("Hello World!") + } + + def 'Example error handling.'(){ + when: 'hello-error request performed' + def response = mvc.perform(MockMvcRequestBuilders.get("$basePath/v1/hello-error")).andReturn().response + then: 'error response returned' + response.status == HttpStatus.INTERNAL_SERVER_ERROR.value() + response.getContentAsString().contains("Example error") + } +} diff --git a/cps-parent/pom.xml b/cps-parent/pom.xml index c3b61541f6..b8f8975a67 100755 --- a/cps-parent/pom.xml +++ b/cps-parent/pom.xml @@ -43,6 +43,8 @@ ../cps-service/target/site/jacoco-aggregate/jacoco.xml, ../cps-rest/target/site/jacoco-ut/jacoco.xml, ../cps-rest/target/site/jacoco-aggregate/jacoco.xml, + ../cps-nf-proxy-rest/target/site/jacoco-ut/jacoco.xml, + ../cps-nf-proxy-rest/target/site/jacoco-aggregate/jacoco.xml, diff --git a/pom.xml b/pom.xml index 2ff88c11b8..eaa388cc1c 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,7 @@ cps-parent cps-service cps-rest + cps-nf-proxy-rest cps-ri checkstyle spotbugs -- cgit 1.2.3-korg