From 4d2e296cb512550448d7b0114ffdbdc6f3174b19 Mon Sep 17 00:00:00 2001 From: "Kumar, Amaresh (ak583p)" Date: Thu, 10 May 2018 14:35:25 +0530 Subject: initial commit for CDT proxy Service Initial commit for CDT proxy server to fix CORS Issues. Issue-ID: APPC-885 Change-Id: Idf2688dbca3fd6c25636544147ed5b23a5a4ed7f Signed-off-by: Kumar, Amaresh (ak583p) --- .gitignore | 2 + CdtProxyService/pom.xml | 97 ++++++++++++ .../org/onap/appc/cdt/service/MainApplication.java | 79 ++++++++++ .../org/onap/appc/cdt/service/SwaggerConfig.java | 60 ++++++++ .../appc/cdt/service/controller/CdtController.java | 169 +++++++++++++++++++++ .../appc/cdt/service/domain/DesignRequest.java | 58 +++++++ .../appc/cdt/service/domain/GetDesignRequest.java | 41 +++++ .../org/onap/appc/cdt/service/domain/Input.java | 42 +++++ .../org/onap/appc/cdt/service/domain/Payload.java | 68 +++++++++ .../onap/appc/cdt/service/exceptions/ApiError.java | 65 ++++++++ .../service/exceptions/GlobalExceptionHandler.java | 74 +++++++++ .../exceptions/ResourceNotFoundException.java | 51 +++++++ .../src/main/resources/application.properties | 17 +++ pom.xml | 5 +- 14 files changed, 827 insertions(+), 1 deletion(-) create mode 100644 CdtProxyService/pom.xml create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/MainApplication.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/SwaggerConfig.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/controller/CdtController.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/DesignRequest.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/GetDesignRequest.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Input.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Payload.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ApiError.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/GlobalExceptionHandler.java create mode 100644 CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ResourceNotFoundException.java create mode 100644 CdtProxyService/src/main/resources/application.properties diff --git a/.gitignore b/.gitignore index f862a53..115ef63 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ target/ .settings/ coverage/ npm-debug.log +/CdtProxyService/CdtProxyService.iml +/CdtProxyService/.idea diff --git a/CdtProxyService/pom.xml b/CdtProxyService/pom.xml new file mode 100644 index 0000000..801ad21 --- /dev/null +++ b/CdtProxyService/pom.xml @@ -0,0 +1,97 @@ + + + + + 4.0.0 + org.onap.appc.cdt + cdt-proxy-service + CdtProxyService + 1.6.0-SNAPSHOT + jar + + 1.8 + UTF-8 + org.onap.appc.cdt.service.MainApplication + + + + org.springframework.boot + spring-boot-starter-parent + 1.4.5.RELEASE + + + + + org.springframework.boot + spring-boot-starter + + + + + + + org.springframework.boot + spring-boot-starter-web + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-annotations + + + io.springfox + springfox-swagger2 + 2.7.0 + compile + + + io.springfox + springfox-swagger-ui + 2.7.0 + compile + + + org.apache.httpcomponents + httpclient + 4.5 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/MainApplication.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/MainApplication.java new file mode 100644 index 0000000..c160519 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/MainApplication.java @@ -0,0 +1,79 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ + +@EnableAutoConfiguration +@Configuration +@ComponentScan +public class MainApplication { + + public static void main(String args[]) { + SpringApplication.run(MainApplication.class, args); + } + + @Bean + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { + MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + jsonConverter.setObjectMapper(objectMapper); + return jsonConverter; + } + + @Bean + public StringHttpMessageConverter stringHttpMessageConverter() { + StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); + stringHttpMessageConverter.setWriteAcceptCharset(true); + return stringHttpMessageConverter; + } + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurerAdapter() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE"); + } + }; + } +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/SwaggerConfig.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/SwaggerConfig.java new file mode 100644 index 0000000..f9630a6 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/SwaggerConfig.java @@ -0,0 +1,60 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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; +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(getApiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("org.onap.appc.cdt.service.controller")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo getApiInfo() { + + return new ApiInfoBuilder() + .title("VNF Lifecycle Tetsing Api Doc") + .description("Developer API Reference guide for devlopong VNF lifecylye testing") + .version("1.0.0") + .license("Apache 2.0") + .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0") + .contact("kamaresh@in.ibm.com") + .build(); + } +} \ No newline at end of file diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/controller/CdtController.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/controller/CdtController.java new file mode 100644 index 0000000..78a94f6 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/controller/CdtController.java @@ -0,0 +1,169 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; + +import java.net.UnknownHostException; +import java.util.Base64; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ + +@RestController +@RequestMapping("/cdtService") +@CrossOrigin(origins = "*", allowedHeaders = "*") +@Api(value = "cdtService", description = "Backend service to eliminate CORS issue for CDT Tool") +public class CdtController { + + private static final Logger logger = LoggerFactory.getLogger(CdtController.class); + private RestTemplate restTemplate = new RestTemplate(); + private String urlAddress; + + @Value("${restConf.backend.hostname}") + private String restConfHostname; + + @Value("${restConf.backend.port}") + private String restConfPort; + + @Value("${restConf.username}") + private String restConfUsername; + + @Value("${restConf.password}") + private String restConfPassword; + + @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "The resource not found") + }) + @RequestMapping(value = "", method = RequestMethod.GET) + @CrossOrigin(origins = "*", allowedHeaders = "*") + public String DefaultEndpoint() { + return "CDT Proxy Service is up and running."; + + } + + @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "The resource not found") + }) + @RequestMapping(value = "/getDesigns", method = RequestMethod.POST) + @CrossOrigin(origins = "*", allowedHeaders = "*") + public String getDesigns(@RequestBody String getDesignsRequest) throws UnknownHostException { + HttpEntity entity = getStringHttpEntity(getDesignsRequest); + HttpClient httpClient = HttpClientBuilder.create().build(); + ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); + restTemplate.setRequestFactory(factory); + String getDesignsResponse = restTemplate.postForObject(getUrl("getDesigns"), entity, String.class); + return getDesignsResponse; + } + + @ApiOperation(value = "Test VNF", response = CdtController.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "The resource not found") + }) + @RequestMapping(value = "/testVnf", method = RequestMethod.POST) + @CrossOrigin(origins = "*", allowedHeaders = "*") + public String testVnf(@RequestParam String urlAction, @RequestBody String testVnf) throws UnknownHostException { + HttpEntity entity = getStringHttpEntity(testVnf); + String testVnfResponse = restTemplate.postForObject(getUrl("testVnf")+urlAction, entity, String.class); + return testVnfResponse; + } + + @ApiOperation(value = "Check status of submitted Test", response = CdtController.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "The resource not found") + }) + @RequestMapping(value = "/checkTestStatus", method = RequestMethod.POST) + @CrossOrigin(origins = "*", allowedHeaders = "*") + public String checkTestStatus(@RequestBody String checkTestStatusRequest) throws UnknownHostException { + HttpEntity entity = getStringHttpEntity(checkTestStatusRequest); + String checkTestStatusResponse = restTemplate.postForObject(getUrl("checkTestStatus"), entity, String.class); + return checkTestStatusResponse; + } + + @ApiOperation(value = "Validate a template which is being uploaded", response = CdtController.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "The resource not found") + }) + @RequestMapping(value = "/validateTemplate", method = RequestMethod.POST) + @CrossOrigin(origins = "*", allowedHeaders = "*") + public String validateTemplate(@RequestBody String validateTemplateRequest) throws UnknownHostException { + HttpEntity entity = getStringHttpEntity(validateTemplateRequest); + String validateTemplateResponse = restTemplate.postForObject(getUrl("validateTemplate"), entity, String.class); + return validateTemplateResponse; + } + + private HttpEntity getStringHttpEntity(@RequestBody String getDesignsRequest) { + HttpHeaders headers = new HttpHeaders(); + headers.setAccessControlAllowCredentials(true); + headers.setContentType(MediaType.APPLICATION_JSON); + String planCredentials = restConfUsername + ":" + restConfPassword; + String base64Credentails = Base64.getEncoder().encodeToString(planCredentials.getBytes()); + headers.set("Authorization", "Basic " + base64Credentails); + return new HttpEntity(getDesignsRequest, headers); + } + + private String getUrl(String ApiName) throws UnknownHostException { + + switch (ApiName) { + case "getDesigns": + urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:dbservice"; + break; + case "testVnf": + urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:"; + break; + case "checkTestStatus": + urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:action-status"; + break; + case "validateTemplate": + urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:validator"; + break; + default: + logger.error("URI not resolved because Api call not supported "); + } + logger.info("Calling Endpoint..... " + urlAddress); + return urlAddress; + } +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/DesignRequest.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/DesignRequest.java new file mode 100644 index 0000000..e3e5fb8 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/DesignRequest.java @@ -0,0 +1,58 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.domain; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +public class DesignRequest { + private String requestId; + private String action; + private Payload payload; + + public DesignRequest() { + } + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + public Payload getPayload() { + return payload; + } + + public void setPayload(Payload payload) { + this.payload = payload; + } +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/GetDesignRequest.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/GetDesignRequest.java new file mode 100644 index 0000000..4e57a28 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/GetDesignRequest.java @@ -0,0 +1,41 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.domain; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +public class GetDesignRequest { + private Input input; + + public GetDesignRequest() { + + } + + public Input getInput() { + return input; + } + + public void setInput(Input input) { + this.input = input; + } +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Input.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Input.java new file mode 100644 index 0000000..1be4453 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Input.java @@ -0,0 +1,42 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.domain; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +public class Input { + private DesignRequest designRequest; + + public Input() { + + } + + public DesignRequest getDesignRequest() { + return designRequest; + } + + public void setDesignRequest(DesignRequest designRequest) { + this.designRequest = designRequest; + } +} + diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Payload.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Payload.java new file mode 100644 index 0000000..4e85fc0 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/domain/Payload.java @@ -0,0 +1,68 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.domain; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +public class Payload { + + private String userId; + private String vnfType; + private String artifactType = "APPC-CONFIG"; + private String artifactName; + + public Payload() { + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getVnfType() { + return vnfType; + } + + public void setVnfType(String vnfType) { + this.vnfType = vnfType; + } + + public String getArtifactType() { + return artifactType; + } + + public void setArtifactType(String artifactType) { + this.artifactType = artifactType; + } + + public String getArtifactName() { + return artifactName; + } + + public void setArtifactName(String artifactName) { + this.artifactName = artifactName; + } +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ApiError.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ApiError.java new file mode 100644 index 0000000..07fadcc --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ApiError.java @@ -0,0 +1,65 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.exceptions; + +import org.springframework.http.HttpStatus; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +public class ApiError { + + private HttpStatus Status; + private String message; + private String detailedMessage; + + + public ApiError(HttpStatus status, String detailedMessage, String message) { + Status = status; + this.detailedMessage = detailedMessage; + this.message = message; + } + + public HttpStatus getStatus() { + return Status; + } + + public void setStatus(HttpStatus status) { + Status = status; + } + + public String getDetailedMessage() { + return detailedMessage; + } + + public void setDetailedMessage(String detailedMessage) { + this.detailedMessage = detailedMessage; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/GlobalExceptionHandler.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/GlobalExceptionHandler.java new file mode 100644 index 0000000..3995860 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/GlobalExceptionHandler.java @@ -0,0 +1,74 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.exceptions; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.context.request.WebRequest; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.net.UnknownHostException; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +@ControllerAdvice +public class GlobalExceptionHandler { + private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + @ExceptionHandler(UnknownHostException.class) + public String handleSQLException(HttpServletRequest request, Exception ex) { + logger.info("UnknownHostException Occured:: URL=" + request.getRequestURL()); + return "Host not found"; + } + + @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "IOException occured") + @ExceptionHandler(IOException.class) + public void handleIOException() { + logger.error("IOException handler executed"); + } + + @ResponseStatus(value = HttpStatus.GATEWAY_TIMEOUT, reason = "Cannot access restconf URl") + @ExceptionHandler(ResourceAccessException.class) + public void handleResourceAccessException() { + logger.error("IOException handler executed"); + } + + @ExceptionHandler(value = {ResourceNotFoundException.class}) + protected ResponseEntity handleResourceNotFoundExceptionException(ResourceNotFoundException ex, WebRequest request) { + StringBuilder builder = new StringBuilder(); + builder.append(ex.getMessage()); + ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, + "Resource Doesnt Exists.", builder.substring(0, builder.length())); + return new ResponseEntity(apiError, new HttpHeaders(), apiError.getStatus()); + } + +} diff --git a/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ResourceNotFoundException.java b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ResourceNotFoundException.java new file mode 100644 index 0000000..7ef5b62 --- /dev/null +++ b/CdtProxyService/src/main/java/org/onap/appc/cdt/service/exceptions/ResourceNotFoundException.java @@ -0,0 +1,51 @@ +/* +============LICENSE_START========================================== +=================================================================== +Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. +=================================================================== + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the License); +you may not use this software 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.appc.cdt.service.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Created by Amaresh Kumar on 09/May/2018. + */ +@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource Not Found") +public class ResourceNotFoundException extends Exception { + + private static final long serialVersionUID = 1L; + + private String errCode; + private String errMsg; + + /** + * Constructs a new runtime exception with the specified detail message. + * The cause is not initialized, and may subsequently be initialized by a + * call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for + * later retrieval by the {@link #getMessage()} method. + */ + public ResourceNotFoundException(String message, String errCode, String errMsg) { + super(message); + this.errCode = errCode; + this.errMsg = errMsg; + } +} diff --git a/CdtProxyService/src/main/resources/application.properties b/CdtProxyService/src/main/resources/application.properties new file mode 100644 index 0000000..1d9a438 --- /dev/null +++ b/CdtProxyService/src/main/resources/application.properties @@ -0,0 +1,17 @@ +#Created by Amaresh Kumar on 09/May/2018. +#=====Application level properties START====================== +server.port=9090 +spring.application.name=CdtProxyService +logging.level.root=DEBUG +Djavax.net.debug=ssl; +#=====Application level properties END====================== +#=========RestConf Backend properties START================== +restConf.backend.hostname=10.12.5.49 +restConf.backend.port=8282 +restConf.username=admin +restConf.password=admin +#=========RestConf Backend properties END================== + +#====Allowed origins====================== + + diff --git a/pom.xml b/pom.xml index d53701b..35ac8b1 100644 --- a/pom.xml +++ b/pom.xml @@ -202,8 +202,11 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property. - + + + CdtProxyService + -- cgit 1.2.3-korg