X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=main%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fapi%2Fmain%2Frest%2FApiRestController.java;h=9745ec947cdefb62d1b56742758b38f591065941;hb=3673c303b0939692ded673e96024316af326d342;hp=79d8a64af5e4047f1e4107eb9067a4ad58ab48b9;hpb=379075d6195eb100a97024fbd2cabaaf8f8bc17d;p=policy%2Fapi.git diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java old mode 100644 new mode 100755 index 79d8a64a..9745ec94 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -3,8 +3,9 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020-2023 Nordix Foundation. + * Modifications Copyright (C) 2020-2023 Bell Canada. 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. @@ -24,35 +25,14 @@ package org.onap.policy.api.main.rest; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; -import io.swagger.annotations.Authorization; -import io.swagger.annotations.BasicAuthDefinition; -import io.swagger.annotations.Extension; -import io.swagger.annotations.ExtensionProperty; -import io.swagger.annotations.Info; -import io.swagger.annotations.ResponseHeader; -import io.swagger.annotations.SecurityDefinition; -import io.swagger.annotations.SwaggerDefinition; +import jakarta.ws.rs.core.Response.Status; +import java.util.List; import java.util.UUID; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.DefaultValue; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Response; -import org.onap.policy.api.main.rest.provider.HealthCheckProvider; -import org.onap.policy.api.main.rest.provider.PolicyProvider; -import org.onap.policy.api.main.rest.provider.PolicyTypeProvider; -import org.onap.policy.api.main.rest.provider.StatisticsProvider; +import lombok.RequiredArgsConstructor; +import org.onap.policy.api.main.exception.PolicyApiRuntimeException; +import org.onap.policy.api.main.rest.genapi.PolicyDesignApi; +import org.onap.policy.api.main.rest.provider.healthcheck.HealthCheckProvider; +import org.onap.policy.api.main.service.ToscaServiceTemplateService; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.endpoints.utils.NetLoggerUtil; @@ -60,108 +40,32 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; /** * Class to provide REST API services. * * @author Chenfei Gao (cgao@research.att.com) */ -@Path("/policy/api/v1") -@Api(value = "Policy Design API") -@Produces({"application/json", "application/yaml"}) -@Consumes({"application/json", "application/yaml"}) -@SwaggerDefinition( - info = @Info( - description = "Policy Design API is publicly exposed for clients to Create/Read/Update/Delete" - + " policy types, policy type implementation and policies which can be recognized" - + " and executable by incorporated policy engines. It is an" - + " independent component running rest service that takes all policy design API calls" - + " from clients and then assign them to different API working functions. Besides" - + " that, API is also exposed for clients to retrieve healthcheck status of this API" - + " rest service and the statistics report including the counters of API invocation.", - version = "1.0.0", title = "Policy Design", - extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"), - @ExtensionProperty(name = "component", value = "Policy Framework")})}), - schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS}, - securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")})) -public class ApiRestController extends CommonRestController { +@RestController +@RequiredArgsConstructor +@Profile("default") +public class ApiRestController extends CommonRestController implements PolicyDesignApi { - private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestController.class); + private final ToscaServiceTemplateService toscaServiceTemplateService; + private final HealthCheckProvider healthCheckProvider; /** * Retrieves the healthcheck status of the API component. * * @return the Response object containing the results of the API operation */ - @GET - @Path("/healthcheck") - @ApiOperation(value = "Perform a system healthcheck", notes = "Returns healthy status of the Policy API component", - response = HealthCheckReport.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"HealthCheck", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response - getHealthCheck(@HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - updateApiStatisticsCounter(Target.OTHER, Result.SUCCESS, HttpMethod.GET); - return makeOkResponse(requestId, new HealthCheckProvider().performHealthCheck()); - } - - /** - * Retrieves the statistics report of the API component. - * - * @return the Response object containing the results of the API operation - */ - @GET - @Path("/statistics") - @ApiOperation(value = "Retrieve current statistics", - notes = "Returns current statistics including the counters of API invocation", - response = StatisticsReport.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"Statistics", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response - getStatistics(@HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - updateApiStatisticsCounter(Target.OTHER, Result.SUCCESS, HttpMethod.GET); - - return makeOkResponse(requestId, new StatisticsProvider().fetchCurrentStatistics()); + @Override + public ResponseEntity getHealthCheck(UUID requestId) { + final var report = healthCheckProvider.performHealthCheck(); + return makeResponse(requestId, report, report.getCode()); } /** @@ -169,42 +73,14 @@ public class ApiRestController extends CommonRestController { * * @return the Response object containing the results of the API operation */ - @GET - @Path("/policytypes") - @ApiOperation(value = "Retrieve existing policy types", - notes = "Returns a list of existing policy types stored in Policy Framework", - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response - getAllPolicyTypes(@HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET); + @Override + public ResponseEntity getAllPolicyTypes(UUID requestId) { + try { + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes", pfme); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + final var msg = "GET /policytypes"; + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } @@ -212,47 +88,18 @@ public class ApiRestController extends CommonRestController { * Retrieves all versions of a particular policy type. * * @param policyTypeId the ID of specified policy type - * * @return the Response object containing the results of the API operation */ - @GET - @Path("/policytypes/{policyTypeId}") - @ApiOperation(value = "Retrieve all available versions of a policy type", - notes = "Returns a list of all available versions for the specified policy type", - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response getAllVersionsOfPolicyType( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(policyTypeId, null); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET); + @Override + public ResponseEntity getAllVersionsOfPolicyType( + String policyTypeId, + UUID requestId) { + try { + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(policyTypeId, null); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes/{}", policyTypeId, pfme); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policytypes/%s", policyTypeId); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } @@ -260,48 +107,21 @@ public class ApiRestController extends CommonRestController { * Retrieves specified version of a particular policy type. * * @param policyTypeId the ID of specified policy type - * @param versionId the version of specified policy type - * + * @param versionId the version of specified policy type * @return the Response object containing the results of the API operation */ - @GET - @Path("/policytypes/{policyTypeId}/versions/{versionId}") - @ApiOperation(value = "Retrieve one particular version of a policy type", - notes = "Returns a particular version for the specified policy type", response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response getSpecificVersionOfPolicyType( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(policyTypeId, versionId); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET); + @Override + public ResponseEntity getSpecificVersionOfPolicyType( + String policyTypeId, + String versionId, + UUID requestId) { + try { + ToscaServiceTemplate serviceTemplate = + toscaServiceTemplateService.fetchPolicyTypes(policyTypeId, versionId); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes/{}/versions/{}", policyTypeId, versionId, pfme); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policytypes/%s/versions/%s", policyTypeId, versionId); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } @@ -309,46 +129,18 @@ public class ApiRestController extends CommonRestController { * Retrieves latest version of a particular policy type. * * @param policyTypeId the ID of specified policy type - * * @return the Response object containing the results of the API operation */ - @GET - @Path("/policytypes/{policyTypeId}/versions/latest") - @ApiOperation(value = "Retrieve latest version of a policy type", - notes = "Returns latest version for the specified policy type", response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response getLatestVersionOfPolicyType( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchLatestPolicyTypes(policyTypeId); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET); + @Override + public ResponseEntity getLatestVersionOfPolicyType( + String policyTypeId, + UUID requestId) { + try { + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchLatestPolicyTypes(policyTypeId); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes/{}/versions/latest", policyTypeId, pfme); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policytypes/%s/versions/latest", policyTypeId); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } @@ -356,51 +148,27 @@ public class ApiRestController extends CommonRestController { * Creates a new policy type. * * @param body the body of policy type following TOSCA definition - * * @return the Response object containing the results of the API operation */ - @POST - @Path("/policytypes") - @ApiOperation(value = "Create a new policy type", notes = "Client should provide TOSCA body of the new policy type", - authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", }, - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Body"), - @ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 406, message = "Not Acceptable Payload"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response createPolicyType( - @ApiParam(value = "Entity body of policy type", required = true) ToscaServiceTemplate body, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - + @Override + public ResponseEntity createPolicyType( + ToscaServiceTemplate body, + UUID requestId) { if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) { NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body)); } - - try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(body); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.POST); + try { + mutex.acquire(); + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(body); return makeOkResponse(requestId, serviceTemplate); - } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("POST /policytypes", pfme); - updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.POST); - return makeErrorResponse(requestId, pfme); + } catch (PfModelRuntimeException pfme) { + final var msg = "POST /policytypes"; + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -408,551 +176,299 @@ public class ApiRestController extends CommonRestController { * Deletes specified version of a particular policy type. * * @param policyTypeId the ID of specified policy type - * @param versionId the version of specified policy type - * + * @param versionId the version of specified policy type * @return the Response object containing the results of the API operation */ - @DELETE - @Path("/policytypes/{policyTypeId}/versions/{versionId}") - @ApiOperation(value = "Delete one version of a policy type", - notes = "Rule 1: pre-defined policy types cannot be deleted;" - + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted." - + "The parameterizing TOSCA policies must be deleted first;", - authorizations = @Authorization(value = "basicAuth"), tags = {"PolicyType", }, - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response deleteSpecificVersionOfPolicyType( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - try (PolicyTypeProvider policyTypeProvider = new PolicyTypeProvider()) { - ToscaServiceTemplate serviceTemplate = policyTypeProvider.deletePolicyType(policyTypeId, versionId); + @Override + public ResponseEntity deleteSpecificVersionOfPolicyType( + String policyTypeId, + String versionId, + UUID requestId) { + try { + mutex.acquire(); + ToscaServiceTemplate serviceTemplate = + toscaServiceTemplateService.deletePolicyType(policyTypeId, versionId); return makeOkResponse(requestId, serviceTemplate); - } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("DELETE /policytypes/{}/versions/{}", policyTypeId, versionId, pfme); - return makeErrorResponse(requestId, pfme); + } catch (PfModelRuntimeException pfme) { + var msg = String.format("DELETE /policytypes/%s/versions/%s", policyTypeId, versionId); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } /** * Retrieves all versions of a particular policy. * - * @param policyTypeId the ID of specified policy type + * @param policyTypeId the ID of specified policy type * @param policyTypeVersion the version of specified policy type - * * @return the Response object containing the results of the API operation */ - // @formatter:off - @GET - @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies") - @ApiOperation( - value = "Retrieve all versions of a policy created for a particular policy type version", - notes = "Returns a list of all versions of specified policy created for the specified policy type version", - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy,"}, - extensions = { - @Extension(name = "interface info", properties = { - @ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin") - }) - } - ) - @ApiResponses(value = { - @ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error") - }) - public Response getAllPolicies( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type", - required = true) String policyTypeVersion, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, - @QueryParam("mode") @DefaultValue("bare") @ApiParam("Fetch mode for policies, BARE for bare policies (default)," - + " REFERENCED for fully referenced policies") PolicyFetchMode mode - ) { - - try (PolicyProvider policyProvider = new PolicyProvider()) { + @Override + public ResponseEntity getAllPolicies( + String policyTypeId, + String policyTypeVersion, + PolicyFetchMode mode, + UUID requestId) { + try { ToscaServiceTemplate serviceTemplate = - policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, null, null, mode); - updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET); + toscaServiceTemplateService.fetchPolicies(policyTypeId, policyTypeVersion, null, null, mode); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes/{}/versions/{}/policies", policyTypeId, policyTypeVersion, pfme); - updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } - // @formatter:on /** * Retrieves all versions of a particular policy. * - * @param policyTypeId the ID of specified policy type + * @param policyTypeId the ID of specified policy type * @param policyTypeVersion the version of specified policy type - * @param policyId the ID of specified policy - * + * @param policyId the ID of specified policy * @return the Response object containing the results of the API operation */ - // @formatter:off - @GET - @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}") - @ApiOperation(value = "Retrieve all version details of a policy created for a particular policy type version", - notes = "Returns a list of all version details of the specified policy", response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", }, - extensions = { - @Extension(name = "interface info", properties = { - @ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin") - }) - } - ) - @ApiResponses(value = { - @ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error") - }) - public Response getAllVersionsOfPolicy( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type", - required = true) String policyTypeVersion, - @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, - @QueryParam("mode") @DefaultValue("bare") @ApiParam("Fetch mode for policies, BARE for bare policies (default)," - + " REFERENCED for fully referenced policies") PolicyFetchMode mode - ) { - try (PolicyProvider policyProvider = new PolicyProvider()) { + @Override + public ResponseEntity getAllVersionsOfPolicy( + String policyId, + String policyTypeId, + String policyTypeVersion, + PolicyFetchMode mode, + UUID requestId) { + try { ToscaServiceTemplate serviceTemplate = - policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, policyId, null, mode); - updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET); + toscaServiceTemplateService.fetchPolicies(policyTypeId, policyTypeVersion, policyId, null, mode); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("/policytypes/{}/versions/{}/policies/{}", policyTypeId, policyTypeVersion, policyId, pfme); - updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("/policytypes/%s/versions/%s/policies/%s", + policyTypeId, policyTypeVersion, policyId); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } - // @formatter:on /** * Retrieves the specified version of a particular policy. * - * @param policyTypeId the ID of specified policy type + * @param policyTypeId the ID of specified policy type * @param policyTypeVersion the version of specified policy type - * @param policyId the ID of specified policy - * @param policyVersion the version of specified policy - * + * @param policyId the ID of specified policy + * @param policyVersion the version of specified policy * @return the Response object containing the results of the API operation */ - // @formatter:off - @GET - @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}") - @ApiOperation(value = "Retrieve one version of a policy created for a particular policy type version", - notes = "Returns a particular version of specified policy created for the specified policy type version", - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", }, - extensions = { - @Extension(name = "interface info", properties = { - @ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin") - }) - } - ) - @ApiResponses(value = { - @ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error") - }) - public Response getSpecificVersionOfPolicy( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type", - required = true) String policyTypeVersion, - @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId, - @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, - @QueryParam("mode") @DefaultValue("bare") @ApiParam("Fetch mode for policies, BARE for bare policies (default)," - + " REFERENCED for fully referenced policies") PolicyFetchMode mode - ) { - try (PolicyProvider policyProvider = new PolicyProvider()) { - ToscaServiceTemplate serviceTemplate = - policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode); - updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET); + @Override + public ResponseEntity getSpecificVersionOfPolicy( + String policyId, + String policyTypeId, + String policyTypeVersion, + String policyVersion, + PolicyFetchMode mode, + UUID requestId) { + try { + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService + .fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes/{}/versions/{}/policies/{}/versions/{}", policyTypeId, policyTypeVersion, - policyId, policyVersion, pfme); - updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policytypes/%s/versions/%s/policies/%s/versions/%s", + policyTypeId, policyTypeVersion, policyId, policyVersion); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } - // @formatter:on /** * Retrieves the latest version of a particular policy. * - * @param policyTypeId the ID of specified policy type + * @param policyTypeId the ID of specified policy type * @param policyTypeVersion the version of specified policy type - * @param policyId the ID of specified policy - * + * @param policyId the ID of specified policy * @return the Response object containing the results of the API operation */ - @GET - @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/latest") - @ApiOperation(value = "Retrieve the latest version of a particular policy", - notes = "Returns the latest version of specified policy", response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", }, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response getLatestVersionOfPolicy( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type", - required = true) String policyTypeVersion, - @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, - @QueryParam("mode") @ApiParam("Fetch mode for policies, TERSE for bare policies (default), " - + "REFERENCED for fully referenced policies") PolicyFetchMode mode) { - - try (PolicyProvider policyProvider = new PolicyProvider()) { + @Override + public ResponseEntity getLatestVersionOfPolicy( + String policyId, + String policyTypeId, + String policyTypeVersion, + PolicyFetchMode mode, + UUID requestId) { + try { ToscaServiceTemplate serviceTemplate = - policyProvider.fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId, mode); - updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET); + toscaServiceTemplateService.fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId, mode); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("GET /policytypes/{}/versions/{}/policies/{}/versions/latest", policyTypeId, policyTypeVersion, - policyId, pfme); - updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policytypes/%s/versions/%s/policies/%s/versions/latest", + policyTypeId, policyTypeVersion, policyId); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } /** * Creates a new policy for a particular policy type and version. * - * @param policyTypeId the ID of specified policy type + * @param policyTypeId the ID of specified policy type * @param policyTypeVersion the version of specified policy type - * @param body the body of policy following TOSCA definition - * + * @param body the body of policy following TOSCA definition * @return the Response object containing the results of the API operation */ - @POST - @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies") - @ApiOperation(value = "Create a new policy for a policy type version", - notes = "Client should provide TOSCA body of the new policy", - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", }, - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Body"), - @ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 406, message = "Not Acceptable Payload"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response createPolicy( - @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId, - @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type", - required = true) String policyTypeVersion, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, - @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) { - + @Override + public ResponseEntity createPolicy( + String policyTypeId, + String policyTypeVersion, + ToscaServiceTemplate body, + UUID requestId) { if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) { NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", toJson(body)); } - - try (PolicyProvider policyProvider = new PolicyProvider()) { - ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy(policyTypeId, policyTypeVersion, body); - updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.POST); + try { + mutex.acquire(); + ToscaServiceTemplate serviceTemplate = + toscaServiceTemplateService.createPolicy(policyTypeId, policyTypeVersion, body); return makeOkResponse(requestId, serviceTemplate); - } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("POST /policytypes/{}/versions/{}/policies", policyTypeId, policyTypeVersion, pfme); - updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.POST); - return makeErrorResponse(requestId, pfme); + } catch (PfModelRuntimeException pfme) { + var msg = String.format("POST /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } /** - * Creates one or more new policies in one call. - * - * @param body the body of policy following TOSCA definition + * Deletes the specified version of a particular policy. * + * @param policyTypeId the ID of specified policy type + * @param policyTypeVersion the version of specified policy type + * @param policyId the ID of specified policy + * @param policyVersion the version of specified policy * @return the Response object containing the results of the API operation */ - @POST - @Path("/policies") - @ApiOperation(value = "Create one or more new policies", - notes = "Client should provide TOSCA body of the new polic(ies)", - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", }, - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "El Alto")})}) - @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Body"), - @ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 406, message = "Not Acceptable Payload"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response createPolicies( - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId, - @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) { - - if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) { - NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body)); + @Override + public ResponseEntity deleteSpecificVersionOfPolicy( + String policyTypeId, + String policyTypeVersion, + String policyId, + String policyVersion, + UUID requestId) { + try { + mutex.acquire(); + ToscaServiceTemplate serviceTemplate = + toscaServiceTemplateService.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion); + return makeOkResponse(requestId, serviceTemplate); + } catch (PfModelRuntimeException pfme) { + var msg = String.format("DELETE /policytypes/%s/versions/%s/policies/%s/versions/%s", + policyTypeId, policyTypeVersion, policyId, policyVersion); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } + } - try (PolicyProvider policyProvider = new PolicyProvider()) { - ToscaServiceTemplate serviceTemplate = policyProvider.createPolicies(body); - updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.POST); + /** + * Retrieves all the available policies. + * + * @return the Response object containing the results of the API operation + */ + @Override + public ResponseEntity getPolicies( + PolicyFetchMode mode, + UUID requestId) { + try { + ToscaServiceTemplate serviceTemplate = + toscaServiceTemplateService.fetchPolicies(null, null, null, null, mode); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("POST /policies", pfme); - updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.POST); - return makeErrorResponse(requestId, pfme); + final var msg = "GET /policies/ --"; + if (pfme.getErrorResponse().getResponseCode().equals(Status.NOT_FOUND)) { + pfme.getErrorResponse().setErrorMessage(ERROR_MESSAGE_NO_POLICIES_FOUND); + pfme.getErrorResponse().setErrorDetails(List.of(ERROR_MESSAGE_NO_POLICIES_FOUND)); + } + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } /** - * Deletes the specified version of a particular policy. + * Retrieves the specified version of a particular policy. * - * @param policyTypeId the ID of specified policy type - * @param policyTypeVersion the version of specified policy type - * @param policyId the ID of specified policy + * @param policyId the Name of specified policy * @param policyVersion the version of specified policy - * * @return the Response object containing the results of the API operation */ - @DELETE - @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}") - @ApiOperation(value = "Delete a particular version of a policy", - notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted", - authorizations = @Authorization(value = "basicAuth"), tags = {"Policy", }, - response = ToscaServiceTemplate.class, - responseHeaders = { - @ResponseHeader(name = "X-MinorVersion", - description = "Used to request or communicate a MINOR version back from the client" - + " to the server, and from the server back to the client", - response = String.class), - @ResponseHeader(name = "X-PatchVersion", - description = "Used only to communicate a PATCH version in a response for" - + " troubleshooting purposes only, and will not be provided by" + " the client on request", - response = String.class), - @ResponseHeader(name = "X-LatestVersion", description = "Used only to communicate an API's latest version", - response = String.class), - @ResponseHeader(name = "X-ONAP-RequestID", - description = "Used to track REST transactions for logging purpose", response = UUID.class)}, - extensions = { - @Extension(name = "interface info", properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"), - @ExtensionProperty(name = "last-mod-release", value = "Dublin")})}) - @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"), - @ApiResponse(code = 403, message = "Authorization Error"), - @ApiResponse(code = 404, message = "Resource Not Found"), - @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"), - @ApiResponse(code = 500, message = "Internal Server Error")}) - public Response deleteSpecificVersionOfPolicy( - @PathParam("policyTypeId") @ApiParam(value = "PolicyType ID", required = true) String policyTypeId, - @PathParam("policyTypeVersion") @ApiParam(value = "Version of policy type", - required = true) String policyTypeVersion, - @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId, - @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion, - @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { - - try (PolicyProvider policyProvider = new PolicyProvider()) { + @Override + public ResponseEntity getSpecificPolicy( + String policyId, + String policyVersion, + PolicyFetchMode mode, + UUID requestId) { + try { ToscaServiceTemplate serviceTemplate = - policyProvider.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion); + toscaServiceTemplateService.fetchPolicies(null, null, policyId, policyVersion, mode); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelException | PfModelRuntimeException pfme) { - LOGGER.debug("DELETE /policytypes/{}/versions/{}/policies/{}/versions/{}", policyTypeId, policyTypeVersion, - policyId, policyVersion, pfme); - return makeErrorResponse(requestId, pfme); + var msg = String.format("GET /policies/%s/versions/%s", policyId, policyVersion); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); } } - private enum Target { - POLICY, - POLICY_TYPE, - OTHER - } - - private enum Result { - SUCCESS, - FAILURE - } - - private enum HttpMethod { - POST, - GET - } - - private void updateApiStatisticsCounter(Target target, Result result, HttpMethod http) { - - ApiStatisticsManager.updateTotalApiCallCount(); - - switch (target) { - case POLICY: - updatePolicyStats(result, http); - break; - case POLICY_TYPE: - updatePolicyTypeStats(result, http); - break; - default: - ApiStatisticsManager.updateApiCallSuccessCount(); - break; + /** + * Creates one or more new policies in one call. + * + * @param body the body of policy following TOSCA definition + * @return the Response object containing the results of the API operation + */ + @Override + public ResponseEntity createPolicies( + ToscaServiceTemplate body, + UUID requestId) { + if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) { + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body)); } - } - - private void updatePolicyStats(Result result, HttpMethod http) { - if (result == Result.SUCCESS) { - if (http == HttpMethod.GET) { - ApiStatisticsManager.updateApiCallSuccessCount(); - ApiStatisticsManager.updateTotalPolicyGetCount(); - ApiStatisticsManager.updatePolicyGetSuccessCount(); - } else if (http == HttpMethod.POST) { - ApiStatisticsManager.updateApiCallSuccessCount(); - ApiStatisticsManager.updateTotalPolicyPostCount(); - ApiStatisticsManager.updatePolicyPostSuccessCount(); - } - } else { - if (http == HttpMethod.GET) { - ApiStatisticsManager.updateApiCallFailureCount(); - ApiStatisticsManager.updateTotalPolicyGetCount(); - ApiStatisticsManager.updatePolicyGetFailureCount(); - } else { - ApiStatisticsManager.updateApiCallFailureCount(); - ApiStatisticsManager.updateTotalPolicyPostCount(); - ApiStatisticsManager.updatePolicyPostFailureCount(); - } + try { + mutex.acquire(); + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicies(body); + return makeOkResponse(requestId, serviceTemplate); + } catch (PfModelRuntimeException pfme) { + final var msg = "POST /policies"; + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } - private void updatePolicyTypeStats(Result result, HttpMethod http) { - if (result == Result.SUCCESS) { - if (http == HttpMethod.GET) { - ApiStatisticsManager.updateApiCallSuccessCount(); - ApiStatisticsManager.updateTotalPolicyTypeGetCount(); - ApiStatisticsManager.updatePolicyTypeGetSuccessCount(); - } else if (http == HttpMethod.POST) { - ApiStatisticsManager.updateApiCallSuccessCount(); - ApiStatisticsManager.updatePolicyTypePostSuccessCount(); - ApiStatisticsManager.updatePolicyTypePostSuccessCount(); - } - } else { - if (http == HttpMethod.GET) { - ApiStatisticsManager.updateApiCallFailureCount(); - ApiStatisticsManager.updateTotalPolicyTypeGetCount(); - ApiStatisticsManager.updatePolicyTypeGetFailureCount(); - } else { - ApiStatisticsManager.updateApiCallFailureCount(); - ApiStatisticsManager.updateTotalPolicyTypePostCount(); - ApiStatisticsManager.updatePolicyTypePostFailureCount(); - } + /** + * Deletes the specified version of a particular policy. + * + * @param policyId the ID of specified policy + * @param policyVersion the version of specified policy + * @return the Response object containing the results of the API operation + */ + @Override + public ResponseEntity deleteSpecificPolicy( + String policyId, + String policyVersion, + UUID requestId) { + try { + mutex.acquire(); + ToscaServiceTemplate serviceTemplate = + toscaServiceTemplateService.deletePolicy(null, null, policyId, policyVersion); + return makeOkResponse(requestId, serviceTemplate); + } catch (PfModelRuntimeException pfme) { + var msg = String.format("DELETE /policies/%s/versions/%s", policyId, policyVersion); + throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } -} \ No newline at end of file +}