package org.onap.policy.api.main.rest.provider;
+import java.util.ArrayList;
+import java.util.List;
+
+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.LegacyOperationalPolicy;
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
/**
* Class to provide all kinds of legacy operational policy operations.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
-public class LegacyOperationalPolicyProvider {
+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 String DELETE_OK = "Successfully deleted";
+
+ /**
+ * Default constructor.
+ */
+ public LegacyOperationalPolicyProvider() throws PfModelException {
+ super();
+ }
/**
* Retrieves a list of operational policies matching specified ID and version.
* @param policyId the ID of policy
* @param policyVersion the version of policy
*
- * @return the ToscaServiceTemplate object
+ * @return the LegacyOperationalPolicy object
*/
- public ToscaServiceTemplate fetchOperationalPolicies(String policyId, String policyVersion) {
- // placeholder
- return new ToscaServiceTemplate();
+ public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
+ throws PfModelException {
+
+ if (policyVersion != null) {
+ validNumber(policyVersion, INVALID_POLICY_VERSION);
+ }
+ return modelsProvider.getOperationalPolicy(policyId, policyVersion);
}
/**
*
* @return the LegacyOperationalPolicy object
*/
- public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) {
- // placeholder
- return new LegacyOperationalPolicy();
+ public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
+
+ return modelsProvider.createOperationalPolicy(body);
}
/**
* @param policyId the ID of policy
* @param policyVersion the version of policy
*
- * @return a string message indicating the operation results
+ * @return the LegacyOperationalPolicy object
+ */
+ public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
+ throws PfModelException {
+
+ validNumber(policyVersion, INVALID_POLICY_VERSION);
+ validateDeleteEligibility(policyId, policyVersion);
+
+ return modelsProvider.deleteOperationalPolicy(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 deleteOperationalPolicies(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 + LEGACY_MINOR_PATCH_SUFFIX));
+ 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