From: Chenfei Gao Date: Fri, 2 Aug 2019 20:41:19 +0000 (-0400) Subject: Add get deployed endpoints for legacy policies X-Git-Tag: 2.1.2~10^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=5b4fbae502f73a4ad58135a58ef348b704cc6ad3;p=policy%2Fapi.git Add get deployed endpoints for legacy policies Issue-ID: POLICY-1810 Change-Id: I47046fbbd7d319b58cabf57ff470ec0d5246c88a Signed-off-by: Chenfei Gao --- 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 index 30b004b6..8c3d55ff 100644 --- 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 @@ -803,7 +803,7 @@ public class ApiRestController extends CommonRestController { } /** - * Retrieves deployed versions of a particular policy in pdp groups. + * Retrieves deployed versions of a particular policy in PDP groups. * * @param policyTypeId the ID of specified policy type * @param policyTypeVersion the version of specified policy type diff --git a/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java index 54155a6f..89ca042b 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java @@ -31,6 +31,7 @@ import io.swagger.annotations.Authorization; import io.swagger.annotations.Extension; import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.ResponseHeader; +import java.util.List; import java.util.Map; import java.util.UUID; import javax.ws.rs.Consumes; @@ -42,6 +43,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.api.main.rest.provider.LegacyGuardPolicyProvider; import org.onap.policy.api.main.rest.provider.LegacyOperationalPolicyProvider; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; @@ -185,6 +187,65 @@ public class LegacyApiRestController extends CommonRestController { } } + /** + * Retrieves deployed versions of a particular guard policy in PDP groups. + * + * @param policyId the ID of specified policy + * + * @return the Response object containing the results of the API operation + */ + @GET + @Path("/policytypes/onap.policies.controlloop.Guard/versions/1.0.0/" + + "policies/{policyId}/versions/deployed") + @ApiOperation(value = "Retrieve deployed versions of a particular guard policy in pdp groups", + notes = "Returns deployed versions of a specified guard policy in pdp groups", + response = LegacyGuardPolicyOutput.class, responseContainer = "Map", + 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 = { "Legacy Guard 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 getDeployedVersionsOfGuardPolicy( + @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId, + @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { + + try (LegacyGuardPolicyProvider guardPolicyProvider = new LegacyGuardPolicyProvider()) { + Map, Map> deployedGuardPolicies = + guardPolicyProvider.fetchDeployedGuardPolicies(policyId); + return makeOkResponse(requestId, deployedGuardPolicies); + } catch (PfModelException | PfModelRuntimeException pfme) { + LOGGER.error("GET /policytypes/onap.policies.controlloop.Guard/versions/1.0.0/" + + "policies/{}/versions/deployed", policyId, pfme); + return makeErrorResponse(requestId, pfme); + } + } + /** * Creates a new guard policy. * @@ -424,6 +485,65 @@ public class LegacyApiRestController extends CommonRestController { } } + /** + * Retrieves deployed versions of a particular operational policy in PDP groups. + * + * @param policyId the ID of specified policy + * + * @return the Response object containing the results of the API operation + */ + @GET + @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/" + + "policies/{policyId}/versions/deployed") + @ApiOperation(value = "Retrieve deployed versions of a particular operational policy in pdp groups", + notes = "Returns deployed versions of a specified operational policy in pdp groups", + response = LegacyOperationalPolicy.class, responseContainer = "List", + 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 = { "Legacy Operational 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 getDeployedVersionsOfOperationalPolicy( + @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId, + @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) { + + try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) { + Map, List> deployedOperationalPolicies = + operationalPolicyProvider.fetchDeployedOperationalPolicies(policyId); + return makeOkResponse(requestId, deployedOperationalPolicies); + } catch (PfModelException | PfModelRuntimeException pfme) { + LOGGER.error("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/" + + "policies/{}/versions/deployed", policyId, pfme); + return makeErrorResponse(requestId, pfme); + } + } + /** * Creates a new operational policy. * diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/CommonModelProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/CommonModelProvider.java index 6e9b52b9..f3634b3c 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/CommonModelProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/CommonModelProvider.java @@ -23,17 +23,27 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; import javax.ws.rs.core.Response; + +import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -159,4 +169,176 @@ public class CommonModelProvider implements AutoCloseable { return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " cannot be deleted as it is parameterized by policies " + parameterizedPolicies; } -} + + /** + * Collects all deployed versions of specified policy in all pdp groups. + * + * @param policyId the ID of policy + * @param policyType the concept key of policy type + * @param getter the custom generic getter Bifunction + * @param consumer the BiConsumer + * @param data the data structure storing retrieved deployed policies + * + * @return a map between pdp group and deployed versions of specified policy in that group + * + * @throws PfModelException the PfModel parsing exception + */ + protected Map, T> collectDeployedPolicies(String policyId, PfConceptKey policyType, + BiFunctionWithEx getter, BiConsumer consumer, T data) throws PfModelException { + + List pdpGroups = getPolicyTypeFilteredPdpGroups(policyType); + hasActivePdpGroup(pdpGroups, policyType, policyId); + return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data); + } + + @FunctionalInterface + protected interface BiFunctionWithEx { + public R apply(T value1, U value2) throws PfModelException; + } + + /** + * Checks if the list of pdp groups is empty. + * If so, throws exception saying specified policy deployment is not found in all existing pdp groups. + * + * @param pdpGroups the list of pdp groups to check against + * @param policyType the concept key of policy type + * @param policyId the ID of policy + * + * @throws PfModelException the PfModel parsing exception + */ + private void hasActivePdpGroup(List pdpGroups, PfConceptKey policyType, String policyId) + throws PfModelException { + + if (pdpGroups.isEmpty()) { + throw new PfModelException(Response.Status.NOT_FOUND, + constructDeploymentNotFoundMessage(policyType, policyId)); + } + } + + /** + * Retrieves all pdp groups supporting specified policy type. + * + * @param policyTypeId the ID of policy type + * @param policyTypeVersion the version of policy type + * + * @return a list of pdp groups supporting specified policy type + * + * @throws PfModelException the PfModel parsing exception + */ + private List getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) + throws PfModelException { + + List policyTypes = new ArrayList<>(); + policyTypes.add(new ToscaPolicyTypeIdentifier(policyType.getName(), policyType.getVersion())); + PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes) + .groupState(PdpState.ACTIVE).pdpState(PdpState.ACTIVE).build(); + return modelsProvider.getFilteredPdpGroups(pdpGroupFilter); + } + + /** + * Constructs the map of deployed pdp groups and deployed policies. + * + * @param pdpGroups the list of pdp groups that contain the specified policy + * @param policyId the ID of policy + * @param policyType the concept key of policy type + * @param getter the custom generic getter BiFunction + * @param consumer the BiConsumer + * @param data the data structure storing retrieved deployed policies + * + * @return the constructed map of pdp groups and deployed policies + * + * @throws PfModelException the PfModel parsing exception + */ + private Map, T> constructDeployedPolicyMap(List pdpGroups, String policyId, + PfConceptKey policyType, BiFunctionWithEx getter, BiConsumer consumer, T data) + throws PfModelException { + + Map, T> deployedPolicyMap = new HashMap<>(); + for (PdpGroup pdpGroup : pdpGroups) { + List policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType); + T deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data); + deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies); + } + return deployedPolicyMap; + } + + /** + * Extracts policy identifiers matching specified policy ID from specified pdp group. + * + * @param policyId the ID of policy to match + * @param pdpGroup the target pdp group to search + * @param policyType the concept key of policy type + * + * @return the list of policy identifiers + * + * @throws PfModelException the PfModel parsing exception + */ + private List extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup, + PfConceptKey policyType) throws PfModelException { + + List policyIdentifiers = new ArrayList<>(); + for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { + for (ToscaPolicyIdentifier policyIdentifier : pdpSubGroup.getPolicies()) { + if (policyId.equalsIgnoreCase(policyIdentifier.getName())) { + policyIdentifiers.add(policyIdentifier); + } + } + } + if (policyIdentifiers.isEmpty()) { + throw new PfModelException(Response.Status.NOT_FOUND, + constructDeploymentNotFoundMessage(policyType, policyId)); + } + return policyIdentifiers; + } + + /** + * Retrieves deployed policies in a generic way. + * + * @param policyIdentifiers the identifiers of the policies to return + * @param policyType the concept key of current policy type + * @param getter the method reference of getting deployed policies + * @param consumer the method reference of consuming the returned policies + * @param data the data structure of deployed policies to return + * + * @return the generic type of policy data structure to return + * + * @throws PfModelException the PfModel parsing exception + */ + private T getDeployedPolicies(List policyIdentifiers, PfConceptKey policyType, + BiFunctionWithEx getter, BiConsumer consumer, T data) throws PfModelException { + + for (ToscaPolicyIdentifier policyIdentifier : policyIdentifiers) { + R result = getter.apply(policyIdentifier.getName(), + getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType)); + consumer.accept(data, result); + } + return data; + } + + /** + * Trims the version for legacy policies. + * + * @param fullVersion the full version format with major, minor, patch + * @param policyType the concept key of policy type + * + * @return the trimmed version + */ + private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) { + return (policyType.getName().contains("guard") + || policyType.getName().contains("Operational")) ? fullVersion.split("\\.")[0] : fullVersion; + } + + /** + * Constructs returned message for not found policy deployment. + * + * @param policyType the concept key of policy type + * @param policyId the ID of policy + * + * @return constructed message + */ + private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) { + + return "could not find policy with ID " + policyId + " and type " + + policyType.getName() + ":" + policyType.getVersion() + " deployed in any pdp group"; + } +} \ No newline at end of file diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java index d0032b03..978a8c0a 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java @@ -23,11 +23,15 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - +import java.util.Map.Entry; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -44,7 +48,16 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer"; private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; - + private static final Map GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>(); + + static { + GUARD_POLICY_TYPE_MAP.put("guard.frequency.", + new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0")); + GUARD_POLICY_TYPE_MAP.put("guard.minmax.", + new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); + GUARD_POLICY_TYPE_MAP.put("guard.blacklist.", + new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); + } /** * Default constructor. @@ -70,6 +83,22 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { return modelsProvider.getGuardPolicy(policyId, policyVersion); } + /** + * Retrieves a list of deployed guard policies in each pdp group. + * + * @param policyId the ID of the policy + * + * @return a list of deployed policies in each pdp group + * + * @throws PfModelException the PfModel parsing exception + */ + public Map, Map> fetchDeployedGuardPolicies(String policyId) + throws PfModelException { + + return collectDeployedPolicies( + policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy, Map::putAll, new HashMap<>()); + } + /** * Creates a new guard policy. * @@ -121,4 +150,23 @@ public class LegacyGuardPolicyProvider extends CommonModelProvider { constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups)); } } + + /** + * Retrieves guard policy type given guard policy ID. + * + * @param policyId the ID of guard policy + * + * @return the concept key of guard policy type + * + * @throws PfModelException the PfModel parsing exception + */ + private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException { + + for (Entry guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) { + if (policyId.startsWith(guardPolicyTypeEntry.getKey())) { + return guardPolicyTypeEntry.getValue(); + } + } + throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId); + } } \ No newline at end of file diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java index ac8fa269..8319de41 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java @@ -24,9 +24,11 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; import java.util.List; - +import java.util.Map; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -42,7 +44,8 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer"; private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; - + private static final PfConceptKey LEGACY_OPERATIONAL_TYPE = + new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0"); /** * Default constructor. @@ -68,6 +71,22 @@ public class LegacyOperationalPolicyProvider extends CommonModelProvider { return modelsProvider.getOperationalPolicy(policyId, policyVersion); } + /** + * Retrieves a list of deployed operational policies in each pdp group. + * + * @param policyId the ID of the policy + * + * @return a list of deployed policies in each pdp group + * + * @throws PfModelException the PfModel parsing exception + */ + public Map, List> fetchDeployedOperationalPolicies(String policyId) + throws PfModelException { + + return collectDeployedPolicies( + policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy, List::add, new ArrayList<>()); + } + /** * Creates a new operational policy. * diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java index a4440daf..66fd7f0e 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java @@ -23,16 +23,14 @@ package org.onap.policy.api.main.rest.provider; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; -import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; @@ -122,25 +120,8 @@ public class PolicyProvider extends CommonModelProvider { public Map, List> fetchDeployedPolicies( String policyTypeId, String policyTypeVersion, String policyId) throws PfModelException { - List policyTypes = new ArrayList<>(); - policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion)); - PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes) - .groupState(PdpState.ACTIVE).pdpState(PdpState.ACTIVE).build(); - List pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter); - - if (pdpGroups.isEmpty()) { - throw new PfModelException(Response.Status.NOT_FOUND, - constructDeploymentNotFoundMessage(policyTypeId, policyTypeVersion, policyId)); - } - - Map, List> deployedPolicyMap = - constructDeployedPolicyMap(pdpGroups, policyId); - if (deployedPolicyMap.isEmpty()) { - throw new PfModelException(Response.Status.NOT_FOUND, - constructDeploymentNotFoundMessage(policyTypeId, policyTypeVersion, policyId)); - } - - return deployedPolicyMap; + return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion), + modelsProvider::getPolicyList, List::addAll, new ArrayList<>()); } /** @@ -264,55 +245,6 @@ public class PolicyProvider extends CommonModelProvider { } } - /** - * Constructs the map of deployed pdp groups and deployed policies. - * - * @param pdpGroups the list of pdp groups that contain the specified policy - * @param policyId the ID of policy - * - * @return the constructed map of pdp groups and deployed policies - * - * @throws PfModelException the PfModel parsing exception - */ - private Map, List> constructDeployedPolicyMap( - List pdpGroups, String policyId) throws PfModelException { - - Map, List> deployedPolicyMap = new HashMap<>(); - for (PdpGroup pdpGroup : pdpGroups) { - List policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup); - List deployedPolicies = getDeployedPolicies(policyIdentifiers); - if (!deployedPolicies.isEmpty()) { - deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies); - } - } - return deployedPolicyMap; - } - - private List extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup) { - List policyIdentifiers = new ArrayList<>(); - for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { - for (ToscaPolicyIdentifier policyIdentifier : pdpSubGroup.getPolicies()) { - if (policyId.equalsIgnoreCase(policyIdentifier.getName())) { - policyIdentifiers.add(policyIdentifier); - } - } - } - return policyIdentifiers; - } - - private List getDeployedPolicies(List policyIdentifiers) - throws PfModelException { - - List deployedPolicies = new ArrayList<>(); - if (!policyIdentifiers.isEmpty()) { - for (ToscaPolicyIdentifier policyIdentifier : policyIdentifiers) { - deployedPolicies.addAll( - modelsProvider.getPolicyList(policyIdentifier.getName(), policyIdentifier.getVersion())); - } - } - return deployedPolicies; - } - /** * Constructs returned message for not found resource. * @@ -329,21 +261,4 @@ public class PolicyProvider extends CommonModelProvider { return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":" + policyTypeVersion + " does not exist"; } - - /** - * Constructs returned message for not found policy deployment. - * - * @param policyTypeId the ID of policy type - * @param policyTypeVersion the version of policy type - * @param policyId the ID of policy - * @param policyVersion the version of policy - * - * @return constructed message - */ - private String constructDeploymentNotFoundMessage(String policyTypeId, String policyTypeVersion, - String policyId) { - - return "could not find policy with ID " + policyId + " and type " - + policyTypeId + ":" + policyTypeVersion + " deployed in any pdp group"; - } } diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java index 9708d5ff..41e4e43a 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java @@ -146,4 +146,4 @@ public class ApiActivator { public static void setAlive(final boolean status) { alive = status; } -} +} \ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 665616b5..442f9dba 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -105,12 +105,17 @@ public class TestApiRestServer { + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/1.0.0"; private static final String POLICYTYPES_TCA_POLICIES_VCPE_LATEST = "policytypes/" + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/latest"; + private static final String POLICYTYPES_TCA_POLICIES_VCPE_DEPLOYED = "policytypes/" + + "onap.policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0/policies/onap.restart.tca/versions/deployed"; private static final String GUARD_POLICIES = "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies"; private static final String GUARD_POLICIES_VDNS_FL_LATEST = "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.frequency.scaleout" + "/versions/latest"; + private static final String GUARD_POLICIES_VDNS_FL_DEPLOYED = + "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.frequency.scaleout" + + "/versions/deployed"; private static final String GUARD_POLICIES_VDNS_MINMAX_LATEST = "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/guard.minmax.scaleout" + "/versions/latest"; @@ -122,33 +127,23 @@ public class TestApiRestServer { private static final String OPS_POLICIES = "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies"; private static final String OPS_POLICIES_VCPE_LATEST = - "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.restart" + "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.restart" + "/versions/latest"; + private static final String OPS_POLICIES_VCPE_DEPLOYED = + "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.restart" + + "/versions/deployed"; private static final String OPS_POLICIES_VDNS_LATEST = - "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.scaleout" + "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.scaleout" + "/versions/latest"; private static final String OPS_POLICIES_VFIREWALL_LATEST = - "policytypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.modifyconfig" + "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.modifyconfig" + "/versions/latest"; private static final String OPS_POLICIES_VCPE_VERSION = "policytypes/" - + "onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.restart/versions/1"; + + "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.restart/versions/1"; private static final String OPS_POLICIES_VDNS_VERSION = "policytypes/" - + "onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.scaleout/versions/1"; + + "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.scaleout/versions/1"; private static final String OPS_POLICIES_VFIREWALL_VERSION = "policytypes/" - + "onap.policies.controlloop.Guard/versions/1.0.0/policies/operational.modifyconfig/versions/1"; - - private static final String GET_DEPLOYED_VERSION_OF_POLICY = - "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app/versions/" - + "1.0.0/policies/onap.restart.tca/versions/deployed"; - private static final String GET_LATEST_VERSION_OF_OPERATIONAL_POLICY = - "policytypes/onap.policies.controlloop.Operational/versions/" - + "1.0.0/policies/operational.scaleout/versions/latest"; - private static final String GET_SPECIFIC_VERSION_OF_OPERATIONAL_POLICY = - "policytypes/onap.policies.controlloop.Operational/versions/" - + "1.0.0/policies/operational.scaleout/versions/3"; - private static final String DEL_SPECIFIC_VERSION_OF_OPERATIONAL_POLICY = - "policytypes/onap.policies.controlloop.Operational/versions/1.0.0/" - + "policies/operational.scaleout/versions/1"; + + "onap.policies.controlloop.Operational/versions/1.0.0/policies/operational.modifyconfig/versions/1"; private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; private static final CommonTestData COMMON_TEST_DATA = new CommonTestData(); @@ -567,6 +562,15 @@ public class TestApiRestServer { }).doesNotThrowAnyException(); } + @Test + public void testGetDeployedVersionsOfGuardPolicy() { + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = readResource(GUARD_POLICIES_VDNS_FL_DEPLOYED, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + }).doesNotThrowAnyException(); + } + @Test public void testDeleteOperationalPolicy() { @@ -638,7 +642,7 @@ public class TestApiRestServer { public void testGetDeployedVersionsOfPolicy() { assertThatCode(() -> { main = startApiService(true); - Response rawResponse = readResource(GET_DEPLOYED_VERSION_OF_POLICY, true); + Response rawResponse = readResource(POLICYTYPES_TCA_POLICIES_VCPE_DEPLOYED, true); assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); }).doesNotThrowAnyException(); } @@ -647,7 +651,7 @@ public class TestApiRestServer { public void testGetLatestVersionOfOperationalPolicy() { assertThatCode(() -> { main = startApiService(true); - Response rawResponse = readResource(GET_LATEST_VERSION_OF_OPERATIONAL_POLICY, true); + Response rawResponse = readResource(OPS_POLICIES_VDNS_LATEST, true); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); }).doesNotThrowAnyException(); } @@ -656,16 +660,25 @@ public class TestApiRestServer { public void testGetSpecificVersionOfOperationalPolicy() { assertThatCode(() -> { main = startApiService(true); - Response rawResponse = readResource(GET_SPECIFIC_VERSION_OF_OPERATIONAL_POLICY, true); + Response rawResponse = readResource(OPS_POLICIES_VDNS_VERSION, true); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); }).doesNotThrowAnyException(); } + @Test + public void testGetDeployedVersionsOfOperationalPolicy() { + assertThatCode(() -> { + main = startApiService(true); + Response rawResponse = readResource(OPS_POLICIES_VCPE_DEPLOYED, true); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawResponse.getStatus()); + }).doesNotThrowAnyException(); + } + @Test public void testDeleteSpecificVersionOfOperationalPolicy() { assertThatCode(() -> { main = startApiService(true); - Response rawResponse = deleteResource(DEL_SPECIFIC_VERSION_OF_OPERATIONAL_POLICY, true); + Response rawResponse = deleteResource(OPS_POLICIES_VDNS_VERSION, true); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawResponse.getStatus()); }).doesNotThrowAnyException(); } diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java index 6091e1d2..1f0077db 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java @@ -77,7 +77,12 @@ public class TestLegacyGuardPolicyProvider { private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.json"; private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"; + private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.guard.FrequencyLimiter"; + private static final String POLICY_TYPE_VERSION = "1.0.0"; private static final String POLICY_ID = "guard.frequency.scaleout:1.0.0"; + private static final String POLICY_NAME = "guard.frequency.scaleout"; + private static final String POLICY_VERSION = "1"; + private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; /** * Initializes parameters. @@ -171,6 +176,102 @@ public class TestLegacyGuardPolicyProvider { }).doesNotThrowAnyException(); } + @Test + public void testFetchDeployedGuardPolicies() { + + assertThatThrownBy(() -> { + guardPolicyProvider.fetchDeployedGuardPolicies("dummy"); + }).hasMessage("No policy type defined for dummy"); + + try (PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) { + assertEquals(0, databaseProvider.getPdpGroups("name").size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("group"); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier( + POLICY_TYPE_NAME, POLICY_TYPE_VERSION)); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + // Create Pdp Groups + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups("group").size()); + + // Create Policy Type + assertThatCode(() -> { + String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE); + ToscaServiceTemplate policyTypeServiceTemplate = + standardCoder.decode(policyTypeString, ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + }).doesNotThrowAnyException(); + + // Create Policy + assertThatCode(() -> { + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyGuardPolicyInput policyToCreate = + standardCoder.decode(policyString, LegacyGuardPolicyInput.class); + Map policyCreated = guardPolicyProvider + .createGuardPolicy(policyToCreate); + assertFalse(policyCreated.isEmpty()); + }).doesNotThrowAnyException(); + + // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true + assertThatThrownBy( + () -> { + guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME); + }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + + POLICY_TYPE_ID + " deployed in any pdp group"); + + + // Update pdpSubGroup + pdpSubGroup.setPolicies(new ArrayList<>()); + pdpSubGroup.getPolicies().add( + new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX)); + assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getPolicies().size()); + + // Test fetchDeployedPolicies + assertThatCode( + () -> { + guardPolicyProvider.fetchDeployedGuardPolicies(POLICY_NAME); + }).doesNotThrowAnyException(); + + // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty()) + assertThatThrownBy( + () -> { + guardPolicyProvider.deleteGuardPolicy( + POLICY_NAME, POLICY_VERSION); + }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION + + " cannot be deleted as it is deployed in pdp groups"); + } + catch (Exception exc) { + fail("Test should not throw an exception"); + } + } + @Test public void testCreateGuardPolicy() { diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java index 2915f129..2479223e 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java @@ -25,6 +25,7 @@ package org.onap.policy.api.main.rest.provider; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -72,6 +73,11 @@ public class TestLegacyOperationalPolicyProvider { private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.json"; private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0"; private static final String POLICY_ID = "operational.restart:1.0.0"; + private static final String POLICY_NAME = "operational.restart"; + private static final String POLICY_VERSION = "1"; + private static final String POLICY_TYPE_NAME = "onap.policies.controlloop.Operational"; + private static final String POLICY_TYPE_VERSION = "1.0.0"; + private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; /** * Initializes parameters. @@ -162,6 +168,104 @@ public class TestLegacyOperationalPolicyProvider { }).doesNotThrowAnyException(); } + @Test + public void testFetchDeployedOperationalPolicies() { + + assertThatThrownBy(() -> { + operationalPolicyProvider.fetchDeployedOperationalPolicies("dummy"); + }).hasMessage("could not find policy with ID dummy and type " + POLICY_TYPE_ID + " deployed in any pdp group"); + + try (PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) { + assertEquals(0, databaseProvider.getPdpGroups("name").size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("group"); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier( + POLICY_TYPE_NAME, POLICY_TYPE_VERSION)); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + // Create Pdp Groups + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups("group").size()); + + // Create Policy Type + assertThatCode(() -> { + String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE); + ToscaServiceTemplate policyTypeServiceTemplate = + standardCoder.decode(policyTypeString, ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + }).doesNotThrowAnyException(); + + // Create Policy + assertThatCode(() -> { + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + LegacyOperationalPolicy policyToCreate = + standardCoder.decode(policyString, LegacyOperationalPolicy.class); + LegacyOperationalPolicy policyCreated = operationalPolicyProvider + .createOperationalPolicy(policyToCreate); + assertEquals("operational.restart", policyCreated.getPolicyId()); + assertEquals("1", policyCreated.getPolicyVersion()); + assertFalse(policyCreated.getContent() == null); + }).doesNotThrowAnyException(); + + // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true + assertThatThrownBy( + () -> { + operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME); + }).hasMessage("could not find policy with ID " + POLICY_NAME + " and type " + + POLICY_TYPE_ID + " deployed in any pdp group"); + + + // Update pdpSubGroup + pdpSubGroup.setPolicies(new ArrayList<>()); + pdpSubGroup.getPolicies().add( + new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION + LEGACY_MINOR_PATCH_SUFFIX)); + assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getPolicies().size()); + + // Test fetchDeployedPolicies + assertThatCode( + () -> { + operationalPolicyProvider.fetchDeployedOperationalPolicies(POLICY_NAME); + }).doesNotThrowAnyException(); + + // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty()) + assertThatThrownBy( + () -> { + operationalPolicyProvider.deleteOperationalPolicy( + POLICY_NAME, POLICY_VERSION); + }).hasMessageContaining("policy with ID " + POLICY_NAME + ":" + POLICY_VERSION + + " cannot be deleted as it is deployed in pdp groups"); + } + catch (Exception exc) { + fail("Test should not throw an exception"); + } + } + @Test public void testCreateOperationalPolicy() { diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java index 3954106a..ea886370 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java @@ -141,8 +141,8 @@ public class TestPolicyProvider { //Basic Exception Throw assertThatThrownBy(() -> { - policyProvider.fetchDeployedPolicies("dummy", "dummy", "dummy"); - }).hasMessage("could not find policy with ID dummy and type dummy:dummy deployed in any pdp group"); + policyProvider.fetchDeployedPolicies("dummy", "1.0.0", "dummy"); + }).hasMessage("could not find policy with ID dummy and type dummy:1.0.0 deployed in any pdp group"); try (PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {