* ============LICENSE_START=======================================================
* ONAP Policy API
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.api.main.rest.provider;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Map.Entry;
+
import javax.ws.rs.core.Response;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.common.parameters.ParameterService;
+
+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.provider.PolicyModelsProvider;
-import org.onap.policy.models.provider.PolicyModelsProviderFactory;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-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;
*
* @author Chenfei Gao (cgao@research.att.com)
*/
-public class LegacyGuardPolicyProvider implements AutoCloseable {
+public class LegacyGuardPolicyProvider extends CommonModelProvider {
+
+ private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
- private PolicyModelsProvider modelsProvider;
+ 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.
*/
public LegacyGuardPolicyProvider() throws PfModelException {
-
- ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
- PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
- modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
+ super();
}
/**
public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
throws PfModelException {
- if (policyVersion != null) {
- validateLegacyGuardPolicyVersion(policyVersion);
- }
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<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
+ throws PfModelException {
+
+ return collectDeployedPolicies(policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy,
+ Map::putAll, new HashMap<>());
+ }
+
/**
* Creates a new guard policy.
*
*
* @return the map of LegacyGuardPolicyOutput objectst
*/
- public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
- throws PfModelException {
+ public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
return modelsProvider.createGuardPolicy(body);
}
public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
throws PfModelException {
- validateLegacyGuardPolicyVersion(policyVersion);
- 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.
+ * Retrieves guard policy type given guard policy ID.
*
- * @param policyId the ID of policy
- * @param policyVersion the version of policy
- *
- * @throws PfModelException the PfModel parsing exception
- */
- 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,
- constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));
- }
- }
-
- /**
- * Validates whether the legacy guard policy version is an integer.
+ * @param policyId the ID of guard policy
*
- * @param policyVersion the version of policy
+ * @return the concept key of guard policy type
*
* @throws PfModelException the PfModel parsing exception
*/
- private void validateLegacyGuardPolicyVersion(String policyVersion) throws PfModelException {
-
- try {
- Integer.valueOf(policyVersion);
- } catch (NumberFormatException exc) {
- throw new PfModelException(Response.Status.BAD_REQUEST,
- "legacy policy version is not an integer", exc);
- }
- }
-
- /**
- * Constructs returned message for policy delete rule violation.
- *
- * @param policyId the ID of policy
- * @param policyVersion the version of policy
- * @param pdpGroups the list of pdp groups
- *
- * @return the constructed message
- */
- private String constructDeleteRuleViolationMessage(
- String policyId, String policyVersion, List<PdpGroup> pdpGroups) {
+ private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException {
- List<String> pdpGroupNameVersionList = new ArrayList<>();
- for (PdpGroup pdpGroup : pdpGroups) {
- pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());
+ for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
+ if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
+ return guardPolicyTypeEntry.getValue();
+ }
}
- String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);
- return "policy with ID " + policyId + ":" + policyVersion
- + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;
- }
-
- /**
- * Closes the connection to database.
- *
- * @throws PfModelException the PfModel parsing exception
- */
- @Override
- public void close() throws PfModelException {
-
- modelsProvider.close();
+ throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId);
}
}
\ No newline at end of file