diff options
author | Chenfei Gao <cgao@research.att.com> | 2019-04-04 21:47:01 -0400 |
---|---|---|
committer | Pamela Dragosh <pdragosh@research.att.com> | 2019-04-05 13:13:59 -0400 |
commit | 74d69a2a262f78d76abfec4dcee19421a5cd6ec9 (patch) | |
tree | 0950fcc1bd89775550fc651050a2d04e072790d2 /main/src | |
parent | e156fb8bf79ee361793af0aef4db5228c4c16f20 (diff) |
Incorporate filters in provider functions
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>
Diffstat (limited to 'main/src')
10 files changed, 398 insertions, 65 deletions
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 38736e85..dbf519eb 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 @@ -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) { 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 60d1b1f2..bdfe4306 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 @@ -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; } 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 2f0b127a..e46b4c62 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 @@ -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; } 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 8ce9a499..d7f152ea 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 @@ -29,10 +29,15 @@ 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.PfModelException;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
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.ToscaPolicyFilter;
+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;
/**
@@ -52,7 +57,6 @@ public class PolicyProvider { ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
- modelsProvider.init();
}
/**
@@ -70,13 +74,20 @@ public class PolicyProvider { public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion,
String policyId, String policyVersion) throws PfModelException {
- validatePathParam(policyTypeId, policyTypeVersion);
+ validatePolicyTypeExist(policyTypeId, policyTypeVersion);
+
ToscaServiceTemplate serviceTemplate;
- if (policyId == null) {
- serviceTemplate = modelsProvider.getPolicies4PolicyType(policyTypeId, policyTypeVersion);
+ if (policyId == null || policyVersion == null) {
+ ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()
+ .name(policyId).version(policyVersion)
+ .type(policyTypeId).typeVersion(policyTypeVersion).build();
+ serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);
} else {
serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);
}
+
+ validatePolicyTypeMatch(policyTypeId, policyTypeVersion, serviceTemplate);
+
close();
return serviceTemplate;
}
@@ -95,8 +106,15 @@ public class PolicyProvider { public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion,
String policyId) throws PfModelException {
- validatePathParam(policyTypeId, policyTypeVersion);
- ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicies(policyId);
+ validatePolicyTypeExist(policyTypeId, policyTypeVersion);
+
+ ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()
+ .name(policyId).version(ToscaPolicyFilter.LATEST_VERSION)
+ .type(policyTypeId).typeVersion(policyTypeVersion).build();
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);
+
+ validatePolicyTypeMatch(policyTypeId, policyTypeVersion, serviceTemplate);
+
close();
return serviceTemplate;
}
@@ -115,10 +133,19 @@ public class PolicyProvider { public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(
String policyTypeId, String policyTypeVersion, String policyId) throws PfModelException {
- validatePathParam(policyTypeId, policyTypeVersion);
- Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = modelsProvider.getDeployedPolicyList(policyId);
+ validatePolicyTypeExist(policyTypeId, policyTypeVersion);
+
+ ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier();
+ policyIdentifier.setName(policyId);
+ PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()
+ .policyType(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion))
+ .policy(policyIdentifier).build();
+ List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
+
+ //TODO: I don't know how to get policies matching policyId that are deployed in those PDP groups
+
close();
- return deployedPolicies;
+ return null;
}
/**
@@ -135,9 +162,11 @@ public class PolicyProvider { public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion,
ToscaServiceTemplate body) throws PfModelException {
- validatePathParam(policyTypeId, policyTypeVersion);
+ validatePolicyTypeExist(policyTypeId, policyTypeVersion);
validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);
+
ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicies(body);
+
close();
return serviceTemplate;
}
@@ -157,23 +186,29 @@ public class PolicyProvider { public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion,
String policyId, String policyVersion) throws PfModelException {
- validatePathParam(policyTypeId, policyTypeVersion);
- ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);
+ validatePolicyTypeExist(policyTypeId, policyTypeVersion);
+
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);
+
+ validatePolicyTypeMatch(policyTypeId, policyTypeVersion, serviceTemplate);
+ validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion);
+
+ ToscaServiceTemplate deletedServiceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);
+
close();
- return serviceTemplate;
+ return deletedServiceTemplate;
}
/**
- * Checks the validation of policy type info passed in as path param.
+ * Validates whether policy type exists.
*
* @param policyTypeId the ID of policy type
* @param policyTypeVersion the version of policy type
*
* @throws PfModelException the PfModel parsing exception
*/
- private void validatePathParam(String policyTypeId, String policyTypeVersion) throws PfModelException {
+ private void validatePolicyTypeExist(String policyTypeId, String policyTypeVersion) throws PfModelException {
- // Check policy type existence
try {
modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);
} catch (Exception e) {
@@ -182,16 +217,18 @@ public class PolicyProvider { }
/**
- * Validates the match between policy type specified in path param and the one specified in type of policy.
+ * Validates the match between policy type specified in path and the one specified in type of policy.
*
- * @param body the ToscaServiceTemplate to create
+ * @param policyTypeId the ID of policy type
+ * @param policyTypeVersion the version of policy type
+ * @param serviceTemplate the ToscaServiceTemplate to validate
*
* @throws PfModelException the PfModel parsing exception
*/
- private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
- throws PfModelException {
+ private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion,
+ ToscaServiceTemplate serviceTemplate) throws PfModelException {
- List<Map<String, ToscaPolicy>> policies = body.getToscaTopologyTemplate().getPolicies();
+ List<Map<String, ToscaPolicy>> policies = serviceTemplate.getToscaTopologyTemplate().getPolicies();
for (Map<String, ToscaPolicy> policy : policies) {
if (policy.size() != 1) {
throw new PfModelException(Response.Status.BAD_REQUEST,
@@ -199,13 +236,36 @@ public class PolicyProvider { }
ToscaPolicy policyContent = policy.values().iterator().next();
if (!policyTypeId.equalsIgnoreCase(policyContent.getType())
- || !policyTypeVersion.equalsIgnoreCase(policyContent.getVersion())) {
- throw new PfModelException(Response.Status.BAD_REQUEST, "policy type info does not match");
+ || !policyTypeVersion.equalsIgnoreCase(policyContent.getTypeVersion())) {
+ throw new PfModelException(Response.Status.BAD_REQUEST, "policy type does not match");
}
}
}
/**
+ * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
+ *
+ * @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
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion,
+ String policyId, String policyVersion) throws PfModelException {
+
+ PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()
+ .policyType(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion))
+ .policy(new ToscaPolicyIdentifier(policyId, policyVersion)).build();
+
+ List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
+ if (!pdpGroups.isEmpty()) {
+ throw new PfModelException(Response.Status.CONFLICT, "the policy has been deployed in pdp group");
+ }
+ }
+
+ /**
* Closes the connection to database.
*
* @throws PfModelException the PfModel parsing exception
@@ -218,4 +278,4 @@ public class PolicyProvider { Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);
}
}
-}
\ No newline at end of file +}
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java index 04d8a725..8fab2bdb 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java @@ -22,13 +22,18 @@ package org.onap.policy.api.main.rest.provider;
+import java.util.List;
import javax.ws.rs.core.Response;
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.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
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.ToscaPolicyTypeFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
/**
@@ -48,7 +53,6 @@ public class PolicyTypeProvider { ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
- modelsProvider.init();
}
/**
@@ -64,7 +68,15 @@ public class PolicyTypeProvider { public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)
throws PfModelException {
- ToscaServiceTemplate serviceTemplate = modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);
+ ToscaServiceTemplate serviceTemplate;
+ if (policyTypeId == null || policyTypeVersion == null) {
+ ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()
+ .name(policyTypeId).version(policyTypeVersion).build();
+ serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
+ } else {
+ serviceTemplate = modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);
+ }
+
close();
return serviceTemplate;
}
@@ -80,7 +92,10 @@ public class PolicyTypeProvider { */
public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {
- ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicyTypes(policyTypeId);
+ ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()
+ .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
+
close();
return serviceTemplate;
}
@@ -96,6 +111,7 @@ public class PolicyTypeProvider { public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {
ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicyTypes(body);
+
close();
return serviceTemplate;
}
@@ -113,12 +129,38 @@ public class PolicyTypeProvider { public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)
throws PfModelException {
+ validateDeleteEligibility(policyTypeId, policyTypeVersion);
+
ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);
+
close();
return serviceTemplate;
}
/**
+ * Validates whether specified policy type can be deleted based on the rule that
+ * policy type parameterized by at least one policies cannot be deleted.
+ *
+ * @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
+ *
+ * @throws PfModelException the PfModel parsing exception
+ */
+ private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {
+
+ PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()
+ .policyType(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion)).build();
+
+ List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
+ if (!pdpGroups.isEmpty()) {
+ throw new PfModelException(Response.Status.CONFLICT,
+ "the policy type is parameterized by at least one policies that have been deployed in pdp group");
+ }
+ }
+
+ /**
* Closes the connection to database.
*
* @throws PfModelException the PfModel parsing exception
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 4cd98849..dc61f3de 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 @@ -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); } /** 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 8f4f9aed..08f7fecd 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 @@ -20,7 +20,62 @@ * ============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() { + + } +} 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 de12fca0..86124c74 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 @@ -20,7 +20,62 @@ * ============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() { + + } +} 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 11d7e404..eeb4dbae 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 @@ -22,4 +22,71 @@ 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() { + + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java index d60dc2e5..4c3f23b3 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java @@ -22,4 +22,65 @@ 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() { + + } +} |