diff options
Diffstat (limited to 'cps-nf-proxy-rest')
12 files changed, 587 insertions, 0 deletions
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 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.cps</groupId> + <artifactId>cps-parent</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../cps-parent/pom.xml</relativePath> + </parent> + + <artifactId>cps-nf-proxy-rest</artifactId> + + <properties> + <minimum-coverage>0.44</minimum-coverage> + </properties> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>cps-service</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>cps-ri</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-tomcat</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-jetty</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + <dependency> + <groupId>io.swagger.core.v3</groupId> + <artifactId>swagger-annotations</artifactId> + </dependency> + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </dependency> + <!-- T E S T D E P E N D E N C I E S --> + <dependency> + <groupId>org.codehaus.groovy</groupId> + <artifactId>groovy</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.spockframework</groupId> + <artifactId>spock-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.spockframework</groupId> + <artifactId>spock-spring</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>cglib</groupId> + <artifactId>cglib-nodep</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + <!-- Swagger code generation. --> + <plugin> + <groupId>io.swagger.codegen.v3</groupId> + <artifactId>swagger-codegen-maven-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>generate</goal> + </goals> + <configuration> + <inputSpec>${project.basedir}/docs/openapi/openapi.yml</inputSpec> + <invokerPackage>org.onap.cps.nfproxy.rest.controller</invokerPackage> + <modelPackage>org.onap.cps.nfproxy.rest.model</modelPackage> + <apiPackage>org.onap.cps.nfproxy.rest.api</apiPackage> + <language>spring</language> + <generateSupportingFiles>false</generateSupportingFiles> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> 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<Object> helloWorld() { + return new ResponseEntity<>("Hello World!", HttpStatus.OK); + } + + @Override + public ResponseEntity<Object> 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<Object> handleInternalServerErrorExceptions( + final Exception exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); + } + + @ExceptionHandler({CpsException.class}) + public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); + } + + private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) { + return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception)); + } + + private static ResponseEntity<Object> 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") + } +} |