Incorporate filters in provider functions 97/84297/3
authorChenfei Gao <cgao@research.att.com>
Fri, 5 Apr 2019 01:47:01 +0000 (21:47 -0400)
committerPamela Dragosh <pdragosh@research.att.com>
Fri, 5 Apr 2019 17:13:59 +0000 (13:13 -0400)
Use filters to GET wildcard/latest/deployed policies
and policy types.

Junit tests for providers are coming in next patch/review.

Issue-ID: POLICY-1441
Change-Id: Iccd02f395d612ba95728d1bc5808ad16c6f386fe
Signed-off-by: Chenfei Gao <cgao@research.att.com>
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
main/pom.xml
main/src/main/java/org/onap/policy/api/main/rest/LegacyApiRestController.java
main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyGuardPolicyProvider.java
main/src/main/java/org/onap/policy/api/main/rest/provider/LegacyOperationalPolicyProvider.java
main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java
main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java
main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java
main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java

index 381d39f..01cd533 100644 (file)
             <groupId>org.mariadb.jdbc</groupId>
             <artifactId>mariadb-java-client</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
index 38736e8..dbf519e 100644 (file)
@@ -58,8 +58,8 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
  */
 @Path("/policy/api/v1")
 @Api(value = "Legacy Policy Design API")
-@Produces({"application/json; vnd.onap.guard", "application/json; vnd.onap.operational"})
-@Consumes({"application/json; vnd.onap.guard", "application/json; vnd.onap.operational"})
+@Produces("application/json")
+@Consumes("application/json")
 public class LegacyApiRestController {
 
     /**
@@ -69,7 +69,6 @@ public class LegacyApiRestController {
      */
     @GET
     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies")
-    @Produces("application/json; vnd.onap.guard")
     @ApiOperation(value = "Retrieve all versions of guard policies",
             notes = "Returns a list of all versions of guard policies",
             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
@@ -108,7 +107,7 @@ public class LegacyApiRestController {
 
         try {
             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
-                    .fetchGuardPolicies(null, null);
+                    .fetchGuardPolicy(null, null);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policies).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -127,7 +126,6 @@ public class LegacyApiRestController {
      */
     @GET
     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/{policyId}")
-    @Produces("application/json; vnd.onap.guard")
     @ApiOperation(value = "Retrieve all versions of a particular guard policy",
             notes = "Returns a list of all versions of the specified guard policy",
             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
@@ -168,7 +166,7 @@ public class LegacyApiRestController {
 
         try {
             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
-                    .fetchGuardPolicies(policyId, null);
+                    .fetchGuardPolicy(policyId, null);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policies).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -188,7 +186,6 @@ public class LegacyApiRestController {
      */
     @GET
     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
-    @Produces("application/json; vnd.onap.guard")
     @ApiOperation(value = "Retrieve one version of a particular guard policy",
             notes = "Returns a particular version of a specified guard policy",
             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
@@ -230,7 +227,7 @@ public class LegacyApiRestController {
 
         try {
             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
-                    .fetchGuardPolicies(policyId, policyVersion);
+                    .fetchGuardPolicy(policyId, policyVersion);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policies).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -249,8 +246,6 @@ public class LegacyApiRestController {
      */
     @POST
     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies")
-    @Consumes("application/json; vnd.onap.guard")
-    @Produces("application/json; vnd.onap.guard")
     @ApiOperation(value = "Create a new guard policy",
             notes = "Client should provide entity body of the new guard policy",
             authorizations = @Authorization(value = "basicAuth"),
@@ -310,7 +305,6 @@ public class LegacyApiRestController {
      */
     @DELETE
     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
-    @Produces("application/json; vnd.onap.guard")
     @ApiOperation(value = "Delete a particular version of a guard policy",
             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
             authorizations = @Authorization(value = "basicAuth"),
@@ -353,7 +347,7 @@ public class LegacyApiRestController {
 
         try {
             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
-                    .deleteGuardPolicies(policyId, policyVersion);
+                    .deleteGuardPolicy(policyId, policyVersion);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policies).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -370,7 +364,6 @@ public class LegacyApiRestController {
      */
     @GET
     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies")
-    @Produces("application/json; vnd.onap.operational")
     @ApiOperation(value = "Retrieve all versions of operational policies",
             notes = "Returns a list of all versions of operational policies",
             response = LegacyOperationalPolicy.class,
@@ -409,7 +402,7 @@ public class LegacyApiRestController {
 
         try {
             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
-                    .fetchOperationalPolicies(null, null);
+                    .fetchOperationalPolicy(null, null);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policy).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -428,7 +421,6 @@ public class LegacyApiRestController {
      */
     @GET
     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies/{policyId}")
-    @Produces("application/json; vnd.onap.operational")
     @ApiOperation(value = "Retrieve all versions of a particular operational policy",
             notes = "Returns a list of all versions of the specified operational policy",
             response = LegacyOperationalPolicy.class,
@@ -469,7 +461,7 @@ public class LegacyApiRestController {
 
         try {
             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
-                    .fetchOperationalPolicies(policyId, null);
+                    .fetchOperationalPolicy(policyId, null);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policy).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -490,7 +482,6 @@ public class LegacyApiRestController {
     @GET
     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/"
          + "policies/{policyId}/versions/{policyVersion}")
-    @Produces("application/json; vnd.onap.operational")
     @ApiOperation(value = "Retrieve one version of a particular operational policy",
             notes = "Returns a particular version of a specified operational policy",
             response = LegacyOperationalPolicy.class,
@@ -532,7 +523,7 @@ public class LegacyApiRestController {
 
         try {
             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
-                    .fetchOperationalPolicies(policyId, policyVersion);
+                    .fetchOperationalPolicy(policyId, policyVersion);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policy).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
@@ -551,8 +542,6 @@ public class LegacyApiRestController {
      */
     @POST
     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies")
-    @Consumes("application/json; vnd.onap.operational")
-    @Produces("application/json; vnd.onap.operational")
     @ApiOperation(value = "Create a new operational policy",
             notes = "Client should provide entity body of the new operational policy",
             authorizations = @Authorization(value = "basicAuth"),
@@ -614,7 +603,6 @@ public class LegacyApiRestController {
     @DELETE
     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/"
          + "policies/{policyId}/versions/{policyVersion}")
-    @Produces("application/json; vnd.onap.operational")
     @ApiOperation(value = "Delete a particular version of a specified operational policy",
             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
             authorizations = @Authorization(value = "basicAuth"),
@@ -657,7 +645,7 @@ public class LegacyApiRestController {
 
         try {
             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
-                    .deleteOperationalPolicies(policyId, policyVersion);
+                    .deleteOperationalPolicy(policyId, policyVersion);
             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
                     .entity(policy).build();
         } catch (PfModelException | PfModelRuntimeException pfme) {
index 60d1b1f..bdfe430 100644 (file)
@@ -50,7 +50,6 @@ public class LegacyGuardPolicyProvider {
         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
-        modelsProvider.init();
     }
 
     /**
@@ -61,10 +60,11 @@ public class LegacyGuardPolicyProvider {
      *
      * @return the map of LegacyGuardPolicyOutput objects
      */
-    public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicies(String policyId, String policyVersion)
+    public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.getGuardPolicy(policyId);
+
         close();
         return guardPolicies;
     }
@@ -77,7 +77,9 @@ public class LegacyGuardPolicyProvider {
      * @return the map of LegacyGuardPolicyOutput objectst
      */
     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
+
         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.createGuardPolicy(body);
+
         close();
         return guardPolicies;
     }
@@ -90,10 +92,11 @@ public class LegacyGuardPolicyProvider {
      *
      * @return the map of LegacyGuardPolicyOutput objects
      */
-    public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicies(String policyId, String policyVersion)
+    public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.deleteGuardPolicy(policyId);
+
         close();
         return guardPolicies;
     }
index 2f0b127..e46b4c6 100644 (file)
@@ -48,7 +48,6 @@ public class LegacyOperationalPolicyProvider {
         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
-        modelsProvider.init();
     }
 
     /**
@@ -59,10 +58,11 @@ public class LegacyOperationalPolicyProvider {
      *
      * @return the LegacyOperationalPolicy object
      */
-    public LegacyOperationalPolicy fetchOperationalPolicies(String policyId, String policyVersion)
+    public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
         LegacyOperationalPolicy operationalPolicy = modelsProvider.getOperationalPolicy(policyId);
+
         close();
         return operationalPolicy;
     }
@@ -77,6 +77,7 @@ public class LegacyOperationalPolicyProvider {
     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
 
         LegacyOperationalPolicy operationalPolicy = modelsProvider.createOperationalPolicy(body);
+
         close();
         return operationalPolicy;
     }
@@ -89,10 +90,11 @@ public class LegacyOperationalPolicyProvider {
      *
      * @return the LegacyOperationalPolicy object
      */
-    public LegacyOperationalPolicy deleteOperationalPolicies(String policyId, String policyVersion)
+    public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
         LegacyOperationalPolicy operationalPolicy = modelsProvider.deleteOperationalPolicy(policyId);
+
         close();
         return operationalPolicy;
     }
index 8ce9a49..d7f152e 100644 (file)
@@ -29,10 +29,15 @@ import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
 import org.onap.policy.common.parameters.ParameterService;\r
 import org.onap.policy.models.base.PfModelException;\r
+import org.onap.policy.models.pdp.concepts.PdpGroup;\r
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;\r
 import org.onap.policy.models.provider.PolicyModelsProvider;\r
 import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
 \r
 /**\r
@@ -52,7 +57,6 @@ public class PolicyProvider {
         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
-        modelsProvider.init();\r
     }\r
 \r
     /**\r
@@ -70,13 +74,20 @@ public class PolicyProvider {
     public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion,\r
             String policyId, String policyVersion) throws PfModelException {\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
+        validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
+\r
         ToscaServiceTemplate serviceTemplate;\r
-        if (policyId == null) {\r
-            serviceTemplate = modelsProvider.getPolicies4PolicyType(policyTypeId, policyTypeVersion);\r
+        if (policyId == null || policyVersion == null) {\r
+            ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
+                    .name(policyId).version(policyVersion)\r
+                    .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
+            serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);\r
         } else {\r
             serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);\r
         }\r
+\r
+        validatePolicyTypeMatch(policyTypeId, policyTypeVersion, serviceTemplate);\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
@@ -95,8 +106,15 @@ public class PolicyProvider {
     public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion,\r
             String policyId) throws PfModelException {\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicies(policyId);\r
+        validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
+\r
+        ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
+                .name(policyId).version(ToscaPolicyFilter.LATEST_VERSION)\r
+                .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
+        ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);\r
+\r
+        validatePolicyTypeMatch(policyTypeId, policyTypeVersion, serviceTemplate);\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
@@ -115,10 +133,19 @@ public class PolicyProvider {
     public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(\r
             String policyTypeId, String policyTypeVersion, String policyId) throws PfModelException {\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = modelsProvider.getDeployedPolicyList(policyId);\r
+        validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
+\r
+        ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier();\r
+        policyIdentifier.setName(policyId);\r
+        PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()\r
+                .policyType(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion))\r
+                .policy(policyIdentifier).build();\r
+        List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
+\r
+        //TODO: I don't know how to get policies matching policyId that are deployed in those PDP groups\r
+\r
         close();\r
-        return deployedPolicies;\r
+        return null;\r
     }\r
 \r
     /**\r
@@ -135,9 +162,11 @@ public class PolicyProvider {
     public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion,\r
                                              ToscaServiceTemplate body) throws PfModelException {\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
+        validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
         validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);\r
+\r
         ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicies(body);\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
@@ -157,23 +186,29 @@ public class PolicyProvider {
     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion,\r
                                  String policyId, String policyVersion) throws PfModelException {\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
+        validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
+\r
+        ToscaServiceTemplate serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);\r
+\r
+        validatePolicyTypeMatch(policyTypeId, policyTypeVersion, serviceTemplate);\r
+        validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
+\r
+        ToscaServiceTemplate deletedServiceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
+\r
         close();\r
-        return serviceTemplate;\r
+        return deletedServiceTemplate;\r
     }\r
 \r
     /**\r
-     * Checks the validation of policy type info passed in as path param.\r
+     * Validates whether policy type exists.\r
      *\r
      * @param policyTypeId the ID of policy type\r
      * @param policyTypeVersion the version of policy type\r
      *\r
      * @throws PfModelException the PfModel parsing exception\r
      */\r
-    private void validatePathParam(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
+    private void validatePolicyTypeExist(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
 \r
-        // Check policy type existence\r
         try {\r
             modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);\r
         } catch (Exception e) {\r
@@ -182,16 +217,18 @@ public class PolicyProvider {
     }\r
 \r
     /**\r
-     * Validates the match between policy type specified in path param and the one specified in type of policy.\r
+     * Validates the match between policy type specified in path and the one specified in type of policy.\r
      *\r
-     * @param body the ToscaServiceTemplate to create\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param serviceTemplate the ToscaServiceTemplate to validate\r
      *\r
      * @throws PfModelException the PfModel parsing exception\r
      */\r
-    private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
-            throws PfModelException {\r
+    private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion,\r
+            ToscaServiceTemplate serviceTemplate) throws PfModelException {\r
 \r
-        List<Map<String, ToscaPolicy>> policies = body.getToscaTopologyTemplate().getPolicies();\r
+        List<Map<String, ToscaPolicy>> policies = serviceTemplate.getToscaTopologyTemplate().getPolicies();\r
         for (Map<String, ToscaPolicy> policy : policies) {\r
             if (policy.size() != 1) {\r
                 throw new PfModelException(Response.Status.BAD_REQUEST,\r
@@ -199,12 +236,35 @@ public class PolicyProvider {
             }\r
             ToscaPolicy policyContent = policy.values().iterator().next();\r
             if (!policyTypeId.equalsIgnoreCase(policyContent.getType())\r
-                    || !policyTypeVersion.equalsIgnoreCase(policyContent.getVersion())) {\r
-                throw new PfModelException(Response.Status.BAD_REQUEST, "policy type info does not match");\r
+                    || !policyTypeVersion.equalsIgnoreCase(policyContent.getTypeVersion())) {\r
+                throw new PfModelException(Response.Status.BAD_REQUEST, "policy type does not match");\r
             }\r
         }\r
     }\r
 \r
+    /**\r
+     * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of policy\r
+     * @param policyVersion the version of policy\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion,\r
+            String policyId, String policyVersion) throws PfModelException {\r
+\r
+        PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()\r
+                .policyType(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion))\r
+                .policy(new ToscaPolicyIdentifier(policyId, policyVersion)).build();\r
+\r
+        List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
+        if (!pdpGroups.isEmpty()) {\r
+            throw new PfModelException(Response.Status.CONFLICT, "the policy has been deployed in pdp group");\r
+        }\r
+    }\r
+\r
     /**\r
      * Closes the connection to database.\r
      *\r
@@ -218,4 +278,4 @@ public class PolicyProvider {
                     Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);\r
         }\r
     }\r
-}
\ No newline at end of file
+}\r
index 04d8a72..8fab2bd 100644 (file)
 \r
 package org.onap.policy.api.main.rest.provider;\r
 \r
+import java.util.List;\r
 import javax.ws.rs.core.Response;\r
 import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
 import org.onap.policy.common.parameters.ParameterService;\r
 import org.onap.policy.models.base.PfModelException;\r
+import org.onap.policy.models.pdp.concepts.PdpGroup;\r
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;\r
 import org.onap.policy.models.provider.PolicyModelsProvider;\r
 import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;\r
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
 \r
 /**\r
@@ -48,7 +53,6 @@ public class PolicyTypeProvider {
         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
-        modelsProvider.init();\r
     }\r
 \r
     /**\r
@@ -64,7 +68,15 @@ public class PolicyTypeProvider {
     public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
             throws PfModelException {\r
 \r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);\r
+        ToscaServiceTemplate serviceTemplate;\r
+        if (policyTypeId == null || policyTypeVersion == null) {\r
+            ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
+                    .name(policyTypeId).version(policyTypeVersion).build();\r
+            serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
+        } else {\r
+            serviceTemplate = modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);\r
+        }\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
@@ -80,7 +92,10 @@ public class PolicyTypeProvider {
      */\r
     public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {\r
 \r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicyTypes(policyTypeId);\r
+        ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
+                .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();\r
+        ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
@@ -96,6 +111,7 @@ public class PolicyTypeProvider {
     public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
 \r
         ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicyTypes(body);\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
@@ -113,11 +129,37 @@ public class PolicyTypeProvider {
     public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
             throws PfModelException {\r
 \r
+        validateDeleteEligibility(policyTypeId, policyTypeVersion);\r
+\r
         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
+\r
         close();\r
         return serviceTemplate;\r
     }\r
 \r
+    /**\r
+     * Validates whether specified policy type can be deleted based on the rule that\r
+     * policy type parameterized by at least one policies cannot be deleted.\r
+     *\r
+     * @param policyTypeId the ID of policy type\r
+     * @param policyTypeVersion the version of policy type\r
+     * @param policyId the ID of policy\r
+     * @param policyVersion the version of policy\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
+\r
+        PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()\r
+                .policyType(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion)).build();\r
+\r
+        List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
+        if (!pdpGroups.isEmpty()) {\r
+            throw new PfModelException(Response.Status.CONFLICT,\r
+                    "the policy type is parameterized by at least one policies that have been deployed in pdp group");\r
+        }\r
+    }\r
+\r
     /**\r
      * Closes the connection to database.\r
      *\r
index 4cd9884..dc61f3d 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- * ONAP Policy API 
- * ================================================================================ 
+ * ONAP Policy API
+ * ================================================================================
  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
@@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory;
  * and forwarding handlers.
  */
 public class ApiActivator {
-    
+
     private static final Logger LOGGER = LoggerFactory.getLogger(ApiActivator.class);
 
     private final ApiParameterGroup apiParameterGroup;
@@ -112,7 +112,7 @@ public class ApiActivator {
      * @param apiParameterGroup the api parameter group
      */
     public void registerToParameterService(final ApiParameterGroup apiParameterGroup) {
-        ParameterService.register(apiParameterGroup);
+        ParameterService.register(apiParameterGroup, true);
     }
 
     /**
index 8f4f9ae..08f7fec 100644 (file)
  * ============LICENSE_END=========================================================
  */
 
-
 package org.onap.policy.api.main.rest.provider;
 
-public class TestLegacyGuardPolicyProvider {}
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.Base64;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class performs unit test of {@link LegacyGuardPolicyProvider}
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ */
+public class TestLegacyGuardPolicyProvider {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyProvider.class);
+
+    private LegacyGuardPolicyProvider guardPolicyProvider;
+
+    /**
+     * Initialize parameters.
+     */
+    @Before
+    public void setupParameters() throws PfModelException {
+
+        PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
+        parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
+        parameters.setDatabaseUser("policy");
+        parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
+        parameters.setPersistenceUnit("ToscaConceptTest");
+        ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
+        ParameterService.register(paramGroup, true);
+        guardPolicyProvider = new LegacyGuardPolicyProvider();
+    }
+
+    @Test
+    public void testFetchGuardPolicy() {
+
+    }
+
+    @Test
+    public void testCreateGuardPolicy() {
+
+    }
+
+    @Test
+    public void testDeleteGuardPolicy() {
+
+    }
+}
index de12fca..86124c7 100644 (file)
  * ============LICENSE_END=========================================================
  */
 
-
 package org.onap.policy.api.main.rest.provider;
 
-public class TestLegacyOperationalPolicyProvider {}
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.Base64;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class performs unit test of {@link LegacyOperationalPolicyProvider}
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ */
+public class TestLegacyOperationalPolicyProvider {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyProvider.class);
+
+    private LegacyOperationalPolicyProvider operationalPolicyProvider;
+
+    /**
+     * Initialize parameters.
+     */
+    @Before
+    public void setupParameters() throws PfModelException {
+
+        PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
+        parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
+        parameters.setDatabaseUser("policy");
+        parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
+        parameters.setPersistenceUnit("ToscaConceptTest");
+        ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
+        ParameterService.register(paramGroup, true);
+        operationalPolicyProvider = new LegacyOperationalPolicyProvider();
+    }
+
+    @Test
+    public void testFetchOperationalPolicy() {
+
+    }
+
+    @Test
+    public void testCreateOperationalPolicy() {
+
+    }
+
+    @Test
+    public void testDeleteOperationalPolicy() {
+
+    }
+}
index 11d7e40..eeb4dba 100644 (file)
 
 package org.onap.policy.api.main.rest.provider;
 
-public class TestPolicyProvider {}
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.Base64;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class performs unit test of {@link PolicyProvider}
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ */
+public class TestPolicyProvider {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyProvider.class);
+
+    private PolicyProvider policyProvider;
+
+    /**
+     * Initialize parameters.
+     */
+    @Before
+    public void setupParameters() throws PfModelException {
+
+        PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
+        parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
+        parameters.setDatabaseUser("policy");
+        parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
+        parameters.setPersistenceUnit("ToscaConceptTest");
+        ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
+        ParameterService.register(paramGroup, true);
+        policyProvider = new PolicyProvider();
+        System.out.println("xxxx");
+    }
+
+    @Test
+    public void testFetchPolicies() {
+
+    }
+
+    @Test
+    public void testFetchLatestPolicies() {
+
+    }
+
+    @Test
+    public void testFetchDeployedPolicies() {
+
+    }
+
+    @Test
+    public void testCreatePolicy() {
+
+    }
+
+    @Test
+    public void testDeletePolicy() {
+
+    }
+}
index d60dc2e..4c3f23b 100644 (file)
 
 package org.onap.policy.api.main.rest.provider;
 
-public class TestPolicyTypeProvider {}
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.util.Base64;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class performs unit test of {@link PolicyTypeProvider}
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ */
+public class TestPolicyTypeProvider {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(TestPolicyTypeProvider.class);
+
+    private PolicyTypeProvider policyTypeProvider;
+
+    /**
+     * Initialize parameters.
+     */
+    @Before
+    public void setupParameters() throws PfModelException {
+
+        PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
+        parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
+        parameters.setDatabaseUser("policy");
+        parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
+        parameters.setPersistenceUnit("ToscaConceptTest");
+        ApiParameterGroup paramGroup = new ApiParameterGroup("ApiGroup", null, parameters);
+        ParameterService.register(paramGroup, true);
+        policyTypeProvider = new PolicyTypeProvider();
+    }
+
+    @Test
+    public void testFetchPolicyTypes() {
+
+    }
+
+    @Test
+    public void testFetchLatestPolicyTypes() {
+
+    }
+
+    @Test
+    public void testCreatePolicyType() {
+
+    }
+
+    @Test
+    public void testDeletePolicyType() {
+
+    }
+}