diff options
Diffstat (limited to 'mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web')
8 files changed, 757 insertions, 3 deletions
diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java index 1a2f5f9..b8aeca6 100644 --- a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java @@ -20,6 +20,9 @@ package org.onap.dcaegen2.platform.mod.web.controller; +import com.fasterxml.jackson.core.JsonParseException; +import com.google.gson.Gson; +import lombok.extern.slf4j.Slf4j; import org.onap.dcaegen2.platform.mod.model.exceptions.MissingRequestBodyException; import org.onap.dcaegen2.platform.mod.model.exceptions.OperationNotAllowedException; import org.onap.dcaegen2.platform.mod.model.exceptions.ResourceConflictException; @@ -27,12 +30,10 @@ import org.onap.dcaegen2.platform.mod.model.exceptions.basemicroservice.BaseMicr import org.onap.dcaegen2.platform.mod.model.exceptions.common.UserNotPassedException; import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.DeploymentArtifactNotFound; import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceNotFoundException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelNotFoundException; import org.onap.dcaegen2.platform.mod.model.exceptions.specification.SpecificationInvalid; import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse; import org.onap.dcaegen2.platform.mod.model.restapi.GenericErrorResponse; -import com.fasterxml.jackson.core.JsonParseException; -import com.google.gson.Gson; -import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.FieldError; @@ -54,6 +55,9 @@ import java.util.Map; @Slf4j public class AppExceptionHandler { + /** + * Exception Handler for Invalid Component Spec + */ @ExceptionHandler(value = {WebClientResponseException.class}) public ResponseEntity<ErrorResponse> handleCompSpecInvalidException (WebClientResponseException ex, WebRequest request) { @@ -61,6 +65,9 @@ public class AppExceptionHandler { (new ErrorResponse(ex.getResponseBodyAsString()), HttpStatus.INTERNAL_SERVER_ERROR); } + /** + * Exception Handler for Json Parsing + */ @ExceptionHandler(value = {JsonParseException.class}) public ResponseEntity<GenericErrorResponse> handleJsonParsedException (JsonParseException ex, WebRequest request) { @@ -72,6 +79,9 @@ public class AppExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Invalid Specification + */ @ExceptionHandler public ResponseEntity<GenericErrorResponse> specificationInvalid(SpecificationInvalid ex) { Map<String, Object> errorResponse = new Gson().fromJson(ex.getMessage(), Map.class); @@ -82,6 +92,9 @@ public class AppExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Missing requested Body + */ @ExceptionHandler public ResponseEntity<GenericErrorResponse> missingRequestBodyException(MissingRequestBodyException ex){ GenericErrorResponse response = new GenericErrorResponse(); @@ -91,17 +104,35 @@ public class AppExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for missing User + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveUserNotPassedException(UserNotPassedException ex){ return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for MsInstance Not Found + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveMsInstanceNotFoundException(MsInstanceNotFoundException ex) { log.error(ex.getMessage()); return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Policy Model Not Found + */ + @ExceptionHandler + public ResponseEntity<ErrorResponse> resolvePolicyModelNotFoundException(PolicyModelNotFoundException ex) { + log.error(ex.getMessage()); + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); + } + + /** + * Exception Handler for Policy Model Not Found + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveDeploymentArtifactNotFound(DeploymentArtifactNotFound ex) { log.error(ex.getMessage()); @@ -109,6 +140,9 @@ public class AppExceptionHandler { HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Operation Not Allowed + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveOperationNotAllowed(OperationNotAllowedException ex) { log.error(ex.getMessage()); @@ -116,17 +150,26 @@ public class AppExceptionHandler { HttpStatus.CONFLICT); } + /** + * Exception Handler for Resource Conflict + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveResourceConflict(ResourceConflictException ex) { return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.CONFLICT); } + /** + * Exception Handler for Resource Not Found + */ @ExceptionHandler(value = {BaseMicroserviceNotFoundException.class}) public ResponseEntity<ErrorResponse> resolveResourceNotFoundExcetions(RuntimeException ex) { log.error(ex.getMessage(), ex); return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.CONFLICT); } + /** + * Exception Handler for Bean Validation + */ @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GenericErrorResponse> resolveBeanValidationException(MethodArgumentNotValidException ex){ log.error(ex.getMessage()); diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelController.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelController.java new file mode 100644 index 0000000..44ad88f --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelController.java @@ -0,0 +1,114 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.common.UserNotPassedException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelConflictException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; +import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.Valid; +import javax.validation.constraints.NotBlank; +import java.util.List; + +/** + * Controller class to manage Policy Model's REST endpoints + */ +@CrossOrigin +@RestController +@Api(tags = "Policy Model", description = "APIs to manage Policy Model Rest endpoints") +@RequestMapping("/api/policy-model") +@Slf4j +public class PolicyModelController { + + @Autowired + PolicyModelService policyModelService; + + /** + * Controller class to manage Policy Model's GET all policy model's + */ + @GetMapping + @ApiOperation("Get all Policy Models") + public List<PolicyModel> getAll() { + return policyModelService.getAll(); + } + + /** + * Controller class to manage Policy Model's GET by policy model id + */ + @GetMapping("/{modelId}") + @ApiOperation("Get specific Policy model") + public PolicyModel getPolicyModelById(@PathVariable String modelId) { + log.info(modelId); + return policyModelService.getPolicyModelById(modelId); + } + + /** + * Controller class to manage Policy Model's POST + */ + @PostMapping + @ApiOperation("Create a Policy Model") + @ResponseStatus(HttpStatus.CREATED) + public PolicyModel createPolicyModel(@RequestBody @Valid PolicyModelCreateRequest request, @RequestParam @NotBlank String user) { + return policyModelService.createPolicyModel(request,user); + } + + /** + * Controller class to manage Policy Model's PATCH by policy model id + */ + @PatchMapping("/{modelId}") + @ApiOperation("Patch a Policy Model") + @ResponseStatus(HttpStatus.OK) + public PolicyModel patchPolicyModel(@RequestBody @Valid PolicyModelUpdateRequest request, @PathVariable String modelId, @RequestParam @NotBlank String user) { + return policyModelService.updatePolicyModel(request, modelId,user); + } + + /** + * Controller class to manage Policy Model's conflict exception + */ + @ExceptionHandler + public ResponseEntity<ErrorResponse> resolvePolicyModelConflict(PolicyModelConflictException ex) { + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.CONFLICT); + } + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelDistributionController.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelDistributionController.java new file mode 100644 index 0000000..3a3571c --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelDistributionController.java @@ -0,0 +1,82 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse; +import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelDistributionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller class to manage Policy Model's REST endpoints + */ +@CrossOrigin +@RestController +@Api(tags = "Policy Model Distribution", description = "APIs to manage Policy Model Distribution Rest endpoints") +@RequestMapping("/api/policy-type") +@Slf4j +public class PolicyModelDistributionController { + + @Autowired + PolicyModelDistributionService policyModelDistributionService; + + /** + * Controller class to manage Policy Model Distribution's GET by policy model id + */ + @GetMapping("/{modelId}") + @ApiOperation("Get the status of specific Policy model Distribution") + public ResponseEntity getPolicyModelDistributionById(@RequestParam("env") String env,@PathVariable String modelId) { + log.info(modelId); + return policyModelDistributionService.getPolicyModelDistributionById(env,modelId); + } + + /** + * Controller class to manage Policy Model Distribution's POST by policy model id + */ + @PostMapping("/{modelId}") + @ApiOperation("Distribute a specific Policy Model") + public ResponseEntity distributePolicyModelById(@RequestParam("env") String env, @PathVariable String modelId) { + return policyModelDistributionService.distributePolicyModel(env,modelId); + } + + /** + * Controller class to manage Policy Model Distribution Environment Not Found exception + */ + @ExceptionHandler + public ResponseEntity<ErrorResponse> resolvePolicyModelDistributionEnvNotFound(PolicyModelDistributionEnvNotFoundException ex){ + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); + } + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionService.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionService.java new file mode 100644 index 0000000..8847a63 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionService.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.service.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelDistributionRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; +import org.springframework.http.ResponseEntity; + +import java.io.IOException; +import java.util.List; + +/** + * An interface to access Policy Model Services + */ + +public interface PolicyModelDistributionService { + + ResponseEntity getPolicyModelDistributionById(String env, String modelId); + + ResponseEntity distributePolicyModel(String env, String modelId); + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionServiceImpl.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionServiceImpl.java new file mode 100644 index 0000000..d0646d2 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionServiceImpl.java @@ -0,0 +1,183 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.service.policymodel; + +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelStatus; +import org.onap.dcaegen2.platform.mod.util.PolicyModelUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Base64Utils; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; +import reactor.netty.http.client.HttpClient; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Policy Model Service implementation + */ + +@Service +@Setter +@Slf4j +public class PolicyModelDistributionServiceImpl implements PolicyModelDistributionService { + + @Autowired + PolicyModelServiceImpl policyModelServiceImpl; + + @Autowired + PolicyModelGateway policyModelGateway; + + @Autowired + PolicyModelUtils policyModelUtils; + + /** + * Gets the Policy Model Distribution by Policy Model ID + * @param env + * @param modelId + * @return + */ + @Override + public ResponseEntity getPolicyModelDistributionById(String env,String modelId) { + + String responseBody = null; + HttpStatus httpStatus; + String url = policyModelUtils.getPolicyEngineURL(env); + PolicyModel policyModel = policyModelServiceImpl.getPolicyModelById(modelId); + + try{ + WebClient webClient = policyModelUtils.getWebClient(env); + WebClient.RequestHeadersSpec<?> requestHeadersSpec = webClient.method(HttpMethod.GET).uri(url + "/" + policyModel.getName()); + httpStatus = requestHeadersSpec.exchange().map(response -> response.statusCode()).block(); + if (httpStatus.is2xxSuccessful()) { + responseBody = requestHeadersSpec.retrieve().bodyToMono(String.class).block(); + } else if(httpStatus.is4xxClientError()){ + if(httpStatus.value() == 401) { + responseBody = "Authentication Error"; + } else if(httpStatus.value() == 403) { + responseBody = "Authorization Error"; + } else if(httpStatus.value() == 404) { + responseBody = "Resource Not Found"; + } + } else if(httpStatus.is5xxServerError()){ + responseBody = "Internal Server Error"; + } + else { + responseBody = "Problem in getting the Policy Model."; + } + }catch(Exception ex){ + log.error("Problem in getting the Policy Model."); + log.error("error: ", ex); + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + responseBody = "Problem in getting the Policy Model."; + + } + + return ResponseEntity.status(httpStatus).body(responseBody); + + } + + /** + * Distributes a Policy Model + * @param env + * @param modelId + * @return + */ + @Override + @Transactional + public ResponseEntity distributePolicyModel(String env, String modelId) { + + String responseBody = null; + HttpStatus httpStatus; + String url = policyModelUtils.getPolicyEngineURL(env); + PolicyModel policyModel = policyModelServiceImpl.getPolicyModelById(modelId); + String content = policyModel.getContent(); + DistributionInfo distributionInfo = DistributionInfo.builder().url(env).build(); + + try{ + WebClient webClient = policyModelUtils.getWebClient(env); + WebClient.RequestHeadersSpec<?> requestHeadersSpec = webClient.method(HttpMethod.POST).uri(url).bodyValue(content.getBytes()); + httpStatus = requestHeadersSpec.exchange().map(response -> response.statusCode()).block(); + if (httpStatus.is2xxSuccessful()) { + distributionInfo.setStatus(PolicyModelStatus.SUCCESS); + responseBody = requestHeadersSpec.retrieve().bodyToMono(String.class).block(); + } else if(httpStatus.is4xxClientError()){ + distributionInfo.setStatus(PolicyModelStatus.FAILED); + if(httpStatus.value() == 400) { + responseBody = "Invalid Body"; + } else if(httpStatus.value() == 401) { + responseBody = "Authentication Error"; + } else if(httpStatus.value() == 403) { + responseBody = "Authorization Error"; + } else if(httpStatus.value() == 406) { + responseBody = "Not Acceptable Version"; + } else if(httpStatus.value() == 415) { + responseBody = "UnSupported Media Type"; + } + } else if(httpStatus.is5xxServerError()){ + responseBody = "Internal Server Error"; + distributionInfo.setStatus(PolicyModelStatus.FAILED); + } + else { + distributionInfo.setStatus(PolicyModelStatus.FAILED); + responseBody = "Problem in Distributing the Policy Model."; + } + }catch(Exception ex){ + log.error("Problem in Distributing the Policy Model."); + log.error("error: ", ex); + distributionInfo.setStatus(PolicyModelStatus.FAILED); + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + responseBody = "Problem in Distributing the Policy Model."; + + } + List<DistributionInfo> distributionInfos = new ArrayList<>(); + distributionInfos.add(distributionInfo); + policyModel.setDistributionInfo(distributionInfos); + + policyModelGateway.save(policyModel); + return ResponseEntity.status(httpStatus).body(responseBody); + + } + + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelGateway.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelGateway.java new file mode 100644 index 0000000..bda3998 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelGateway.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.service.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; + +import java.util.List; +import java.util.Optional; + +/** + * An interface to interact with Policy Model persistence + */ +public interface PolicyModelGateway { + + List<PolicyModel> findAll(); + + Optional<PolicyModel> findById(String id); + + PolicyModel save(PolicyModel newPolicyModel) ; + + Optional<PolicyModel> findByNameAndVersion(String name,String version); + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelService.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelService.java new file mode 100644 index 0000000..61abf60 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelService.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.service.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; + +import java.util.List; + +/** + * An interface to access Policy Model Services + */ + +public interface PolicyModelService { + + List<PolicyModel> getAll(); + + PolicyModel getPolicyModelById(String modelId); + + PolicyModel createPolicyModel(PolicyModelCreateRequest request, String user); + + PolicyModel updatePolicyModel(PolicyModelUpdateRequest request, String modelId, String user); + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelServiceImpl.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelServiceImpl.java new file mode 100644 index 0000000..58fc447 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelServiceImpl.java @@ -0,0 +1,203 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.dcaegen2.platform.mod.web.service.policymodel; + +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelConflictException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv; +import org.onap.dcaegen2.platform.mod.model.restapi.MetadataRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Date; +import java.util.List; + +/** + * Policy Model Service implementation + */ + +@Service +@Setter +@Slf4j +public class PolicyModelServiceImpl implements PolicyModelService { + + @Value("${dev.server}") + private String devServer; + + @Value("${dev.port}") + private String devServerPort; + + @Value("${dev.user}") + private String devServerUser; + + @Value("${dev.password}") + private String devServerPassword; + + @Autowired + private PolicyModelGateway policyModelGateway; + + /** + * Lists all Policy Models + * + * @return + */ + @Override + public List<PolicyModel> getAll() { + return policyModelGateway.findAll(); + } + + /** + * List a Policy Model by policyModel ID + * + * @param id + * @return + */ + @Override + public PolicyModel getPolicyModelById(String id) { + return policyModelGateway.findById(id).orElseThrow(() -> new PolicyModelNotFoundException(String.format("Policy Model with id %s not found", id))); + } + + /** + * creates a Policy Model + * + * @param request + * @return + */ + @Override + @Transactional + public PolicyModel createPolicyModel(PolicyModelCreateRequest request, String user) { + checkIfPMNameAndVersionAlreadyExists(request.getName(), request.getVersion()); + PolicyModel policyModel = new PolicyModel(); + policyModel.setName(request.getName()); + policyModel.setContent(request.getContent()); + policyModel.setVersion(request.getVersion()); + policyModel.setOwner(request.getOwner()); + policyModel.setMetadata(getMetadataFields(request.getMetadata(), user)); + return policyModelGateway.save(policyModel); + } + + /** + * Update a Policy Model + * + * @param request + * @param modelId + * @return + */ + @Override + public PolicyModel updatePolicyModel(PolicyModelUpdateRequest request, String modelId, String user) { + PolicyModel policyModel = getPolicyModelById(modelId); + updateFields(request, policyModel,user); + return policyModelGateway.save(policyModel); + + } + + + /** + * Cerifies if a Policy Model Exist + * + * @param pmName + * @param pmVersion + * @return + */ + private void checkIfPMNameAndVersionAlreadyExists(String pmName, String pmVersion) { + if (policyModelGateway.findByNameAndVersion(pmName, pmVersion).isPresent()) + throw new PolicyModelConflictException(ErrorMessages.POLICYMODEL_NAME_VERSION_CONFLICT_MESSAGE); + } + + /** + * creates a Policy Model Metadata + * + * @param metadata + * @param user + * @return + */ + private MetadataRequest getMetadataFields(MetadataRequest metadata, String user) { + MetadataRequest metadataFields = MetadataRequest.builder().build(); + metadataFields.setCreatedBy(user); + metadataFields.setCreatedOn(new Date()); + if(metadata != null) { + if (metadata.getNotes() != null) + metadataFields.setNotes(metadata.getNotes()); + if (metadata.getLabels() != null) + metadataFields.setLabels(metadata.getLabels()); + } + return metadataFields; + } + + + /** + * Updates a Policy Model + * + * @param request + * @param policyModel + * @param user + * @return + */ + private void updateFields(PolicyModelUpdateRequest request, PolicyModel policyModel, String user) { + String name = request.getName(); + String version = request.getVersion(); + if (!(name.equalsIgnoreCase(policyModel.getName()) && version.equalsIgnoreCase(policyModel.getVersion()))) { + checkIfPMNameAndVersionAlreadyExists(name, version); + policyModel.setName(name); + policyModel.setVersion(version); + } + if (request.getOwner() != null) { + policyModel.setOwner(request.getOwner()); + } + + if (request.getContent() != null) { + policyModel.setContent(request.getContent()); + } + if (request.getMetadata() != null) { + updateMetadata(request,user); + policyModel.setMetadata(request.getMetadata()); + } + } + + + /** + * Updates a Policy Model Metadata + * + * @param request + * @param user + * @return + */ + private void updateMetadata(PolicyModelUpdateRequest request, String user) { + MetadataRequest metadataRequest = request.getMetadata(); + metadataRequest.setUpdatedBy(user); + metadataRequest.setUpdatedOn(new Date()); + } + + +} |