summaryrefslogtreecommitdiffstats
path: root/cps-nf-proxy-rest/src/main
diff options
context:
space:
mode:
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-01-22 12:53:01 +0200
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-01-26 07:53:27 +0200
commit9f52f60a2e70f2344c671af28e62fe2315ee8e02 (patch)
treee02fff6a25ad49e177522e14ed3cc646d7ffd06f /cps-nf-proxy-rest/src/main
parenta2f384de843c3c3f68d2d371d185c71a900e9810 (diff)
Initial cps-nf-proxy-rest module setup in CPS project
Issue-ID: CPS-171 Change-Id: I8998dc2818b6bc07fc4fe25a2d735b4ab8b4b817 Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-nf-proxy-rest/src/main')
-rw-r--r--cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/Application.java30
-rwxr-xr-xcps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/config/NfProxyConfig.java44
-rw-r--r--cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/controller/NfProxyController.java42
-rw-r--r--cps-nf-proxy-rest/src/main/java/org/onap/cps/nfproxy/rest/exceptions/NfProxyRestExceptionHandler.java78
-rw-r--r--cps-nf-proxy-rest/src/main/resources/application.yml46
-rw-r--r--cps-nf-proxy-rest/src/main/resources/openapi-configuration.json28
6 files changed, 268 insertions, 0 deletions
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"
+ }
+ }
+}