diff options
author | 2019-04-15 10:19:26 -0400 | |
---|---|---|
committer | 2019-04-15 12:39:45 -0400 | |
commit | 7d8509acbf1a6ce1c0dc8bbd87e0a793420cafd4 (patch) | |
tree | 0868855c47b878026f84556cc1a1d965c97806fd /main/src/main | |
parent | d641bd5f3c4f6333e56ea85d95abb3dc124d4f4b (diff) |
Support integer policy-version
The deploy/undeploy APIs used by CLAMP only pass the major number
when specifying a policy-version. Modified the code to handle
policy-versions of the form, major or major.minor.
Change-Id: I3251df162984f287bd1430b8e46da675b4c265ee
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/main')
3 files changed, 46 insertions, 52 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java index cb2d1e34..3d44a0cd 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java @@ -351,7 +351,7 @@ public class PdpGroupDeployProvider extends ProviderBase<PdpGroupDeployResponse> BeanValidationResult result = new BeanValidationResult(subgrp.getPdpType(), subgrp); for (ToscaPolicyIdentifier ident : subgrp.getPolicies()) { - ToscaPolicy policy = data.getPolicy(ident); + ToscaPolicy policy = data.getPolicy(new ToscaPolicyIdentifierOptVersion(ident)); if (!subgrp.getSupportedPolicyTypes().contains(policy.getTypeIdentifier())) { result.addResult(new ObjectValidationResult("policy", ident, ValidationStatus.INVALID, diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java index 5e0fb71a..b7575df3 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java @@ -37,7 +37,6 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pap.main.PapConstants; @@ -155,12 +154,7 @@ public abstract class ProviderBase<R extends SimpleResponse> { private ToscaPolicy getPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy) throws PfModelException { - if (desiredPolicy.isNullVersion()) { - return data.getPolicyMaxVersion(desiredPolicy.getName()); - - } else { - return data.getPolicy(new ToscaPolicyIdentifier(desiredPolicy.getName(), desiredPolicy.getVersion())); - } + return data.getPolicy(desiredPolicy); } /** @@ -276,7 +270,8 @@ public abstract class ProviderBase<R extends SimpleResponse> { update.setDescription(group.getDescription()); update.setPdpGroup(group.getName()); update.setPdpSubgroup(subgroup.getPdpType()); - update.setPolicies(subgroup.getPolicies().stream().map(data::getPolicy).collect(Collectors.toList())); + update.setPolicies(subgroup.getPolicies().stream().map(ToscaPolicyIdentifierOptVersion::new) + .map(data::getPolicy).collect(Collectors.toList())); return update; } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java index 0537c80b..b7aff765 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.models.base.PfModelException; @@ -36,7 +37,8 @@ import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.provider.PolicyModelsProvider; 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.ToscaPolicyFilter.ToscaPolicyFilterBuilder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pap.main.PolicyPapRuntimeException; @@ -44,6 +46,14 @@ import org.onap.policy.pap.main.PolicyPapRuntimeException; * Data used during a single REST call when updating PDP policies. */ public class SessionData { + /** + * If a version string matches this, then it is just a prefix (i.e., major or major.minor). + */ + private static final Pattern VERSION_PREFIX_PAT = Pattern.compile("[^.]+(?:[.][^.]*)?"); + + /** + * DB provider. + */ private final PolicyModelsProvider dao; /** @@ -66,13 +76,7 @@ public class SessionData { /** * Maps a policy's identifier to the policy. */ - private final Map<ToscaPolicyIdentifier, ToscaPolicy> policyCache = new HashMap<>(); - - /** - * Maps a policy name to its latest policy. Every policy appearing within this map has - * a corresponding entry in {@link #policyCache}. - */ - private final Map<String, ToscaPolicy> latestPolicy = new HashMap<>(); + private final Map<ToscaPolicyIdentifierOptVersion, ToscaPolicy> policyCache = new HashMap<>(); /** @@ -88,28 +92,50 @@ public class SessionData { * Gets the policy, referenced by an identifier. Loads it from the cache, if possible. * Otherwise, gets it from the DB. * - * @param ident policy identifier + * @param desiredPolicy policy identifier * @return the specified policy * @throws PolicyPapRuntimeException if an error occurs */ - public ToscaPolicy getPolicy(ToscaPolicyIdentifier ident) { + public ToscaPolicy getPolicy(ToscaPolicyIdentifierOptVersion desiredPolicy) { - return policyCache.computeIfAbsent(ident, key -> { + ToscaPolicy policy = policyCache.computeIfAbsent(desiredPolicy, key -> { try { - List<ToscaPolicy> lst = dao.getPolicyList(ident.getName(), ident.getVersion()); + ToscaPolicyFilterBuilder filterBuilder = ToscaPolicyFilter.builder().name(desiredPolicy.getName()); + + String version = desiredPolicy.getVersion(); + if (version == null) { + // no version specified - get the latest + filterBuilder.version(ToscaPolicyFilter.LATEST_VERSION); + + } else if (VERSION_PREFIX_PAT.matcher(version).matches()) { + // version prefix provided - match the prefix and then pick the latest + filterBuilder.versionPrefix(version + ".").version(ToscaPolicyFilter.LATEST_VERSION); + + } else { + // must be an exact match + filterBuilder.version(version); + } + + + List<ToscaPolicy> lst = dao.getFilteredPolicyList(filterBuilder.build()); if (lst.isEmpty()) { - throw new PolicyPapRuntimeException( - "cannot find policy: " + ident.getName() + " " + ident.getVersion()); + throw new PolicyPapRuntimeException("cannot find policy: " + desiredPolicy.getName() + " " + + desiredPolicy.getVersion()); } return lst.get(0); } catch (PfModelException e) { - throw new PolicyPapRuntimeException("cannot get policy: " + ident.getName() + " " + ident.getVersion(), - e); + throw new PolicyPapRuntimeException( + "cannot get policy: " + desiredPolicy.getName() + " " + desiredPolicy.getVersion(), e); } }); + + // desired version may have only been a prefix - cache with full identifier, too + policyCache.putIfAbsent(new ToscaPolicyIdentifierOptVersion(policy.getIdentifier()), policy); + + return policy; } /** @@ -177,33 +203,6 @@ public class SessionData { } /** - * Gets the policy having the given name and the maximum version. - * - * @param name name of the desired policy - * @return the desired policy, or {@code null} if there is no policy with given name - * @throws PfModelException if an error occurs - */ - public ToscaPolicy getPolicyMaxVersion(String name) throws PfModelException { - ToscaPolicy policy = latestPolicy.get(name); - if (policy != null) { - return policy; - } - - ToscaPolicyFilter filter = - ToscaPolicyFilter.builder().name(name).version(ToscaPolicyFilter.LATEST_VERSION).build(); - List<ToscaPolicy> policies = dao.getFilteredPolicyList(filter); - if (policies.isEmpty()) { - throw new PolicyPapRuntimeException("cannot find policy: " + name); - } - - policy = policies.get(0); - policyCache.putIfAbsent(policy.getIdentifier(), policy); - latestPolicy.put(name, policy); - - return policy; - } - - /** * Creates a group. * * @param newGroup the new group |