package org.onap.policy.api.main.rest.provider;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.core.Response;
+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.tosca.authorative.concepts.ToscaPolicyIdentifier;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
/**
* Class to provide all kinds of legacy guard policy operations.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
-public class LegacyGuardPolicyProvider {
+public class LegacyGuardPolicyProvider extends CommonModelProvider {
- private static final String DELETE_OK = "Successfully deleted";
+ private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
+
+
+ /**
+ * Default constructor.
+ */
+ public LegacyGuardPolicyProvider() throws PfModelException {
+ super();
+ }
/**
* Retrieves a list of guard policies matching specified ID and version.
* @param policyId the ID of policy
* @param policyVersion the version of policy
*
- * @return the ToscaServiceTemplate object
+ * @return the map of LegacyGuardPolicyOutput objects
*/
- public ToscaServiceTemplate fetchGuardPolicies(String policyId, String policyVersion) {
- // placeholder
- return new ToscaServiceTemplate();
+ public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
+ throws PfModelException {
+
+ if (policyVersion != null) {
+ validNumber(policyVersion, INVALID_POLICY_VERSION);
+ }
+ return modelsProvider.getGuardPolicy(policyId, policyVersion);
}
/**
*
* @param body the entity body of policy
*
- * @return the ToscaServiceTemplate object
+ * @return the map of LegacyGuardPolicyOutput objectst
*/
- public ToscaServiceTemplate createGuardPolicy(LegacyGuardPolicy body) {
- // placeholder
- return new ToscaServiceTemplate();
+ public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
+ throws PfModelException {
+
+ return modelsProvider.createGuardPolicy(body);
}
/**
* @param policyId the ID of policy
* @param policyVersion the version of policy
*
- * @return a string message indicating the operation results
+ * @return the map of LegacyGuardPolicyOutput objects
+ */
+ public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
+ throws PfModelException {
+
+ validNumber(policyVersion, INVALID_POLICY_VERSION);
+ validateDeleteEligibility(policyId, policyVersion);
+
+ return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
+ }
+
+ /**
+ * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
+ *
+ * @param policyId the ID of policy
+ * @param policyVersion the version of policy
+ *
+ * @throws PfModelException the PfModel parsing exception
*/
- public String deleteGuardPolicies(String policyId, String policyVersion) {
- // placeholder
- return DELETE_OK;
+ private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
+
+ List<ToscaPolicyIdentifier> policies = new ArrayList<>();
+ policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));
+ PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
+
+ List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
+
+ if (!pdpGroups.isEmpty()) {
+ throw new PfModelException(Response.Status.CONFLICT,
+ constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
+ }
}
}
\ No newline at end of file