From 422412ec8f21017aff1641f133c2258f52f7d706 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Sat, 6 Apr 2019 05:19:43 +0000 Subject: Fix bugs on filters Filters were not being invoked from providers. Filter for getting latest version was filtering out everything Filter on PDP state was not implemented. Issue-ID: POLICY-1095 Change-Id: If43ce48a57b010e05f75db8cfa80e63f2719ace1 Signed-off-by: liamfallon --- .../authorative/concepts/ToscaPolicyFilter.java | 2 +- .../concepts/ToscaPolicyTypeFilter.java | 2 +- .../provider/AuthorativeToscaProvider.java | 43 +++++++++++++++++--- .../tosca/simple/concepts/JpaToscaEntrySchema.java | 10 +++-- .../tosca/simple/provider/SimpleToscaProvider.java | 47 ---------------------- 5 files changed, 46 insertions(+), 58 deletions(-) (limited to 'models-tosca/src/main/java/org') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java index d29f303f2..b4d1b3ee3 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java @@ -58,7 +58,7 @@ public class ToscaPolicyFilter implements PfObjectFilter { // @formatter:off List returnList = originalList.stream() .filter(p -> filterOnRegexp(p.getName(), name)) - .filter(p -> filterOnRegexp(p.getVersion(), version)) + .filter(p -> version.equals(LATEST_VERSION) || filterOnRegexp(p.getVersion(), version)) .filter(p -> filterOnRegexp(p.getType(), type)) .filter(p -> filterOnRegexp(p.getTypeVersion(), typeVersion)) .collect(Collectors.toList()); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java index 097fb6139..041179513 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java @@ -52,7 +52,7 @@ public class ToscaPolicyTypeFilter implements PfObjectFilter { // @formatter:off List returnList = originalList.stream() .filter(p -> filterOnRegexp(p.getName(), name)) - .filter(p -> filterOnRegexp(p.getVersion(), version)) + .filter(p -> version.equals(LATEST_VERSION) || filterOnRegexp(p.getVersion(), version)) .collect(Collectors.toList()); // @formatter:off diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index a843711ee..4bf014644 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.authorative.provider; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -30,6 +31,7 @@ import lombok.NonNull; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; 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.ToscaPolicyType; @@ -85,7 +87,16 @@ public class AuthorativeToscaProvider { */ public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao, @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { - return new SimpleToscaProvider().getFilteredPolicyTypes(dao, filter).toAuthorative(); + + ToscaServiceTemplate serviceTemplate = + new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative(); + + List filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes()); + filteredPolicyTypes = filter.filter(filteredPolicyTypes); + + serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes)); + + return serviceTemplate; } /** @@ -99,8 +110,7 @@ public class AuthorativeToscaProvider { public List getFilteredPolicyTypeList(@NonNull final PfDao dao, @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { - return (asConceptList( - new SimpleToscaProvider().getFilteredPolicyTypes(dao, filter).toAuthorative().getPolicyTypes())); + return filter.filter(getPolicyTypeList(dao, null, null)); } /** @@ -190,7 +200,14 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) throws PfModelException { - return new SimpleToscaProvider().getFilteredPolicies(dao, filter).toAuthorative(); + ToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, null, null).toAuthorative(); + + List filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies()); + filteredPolicies = filter.filter(filteredPolicies); + + serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies)); + + return serviceTemplate; } /** @@ -204,8 +221,7 @@ public class AuthorativeToscaProvider { public List getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) throws PfModelException { - return asConceptList(new SimpleToscaProvider().getFilteredPolicies(dao, filter).toAuthorative() - .getToscaTopologyTemplate().getPolicies()); + return filter.filter(getPolicyList(dao, null, null)); } /** @@ -273,4 +289,19 @@ public class AuthorativeToscaProvider { return returnList; } + + /** + * Return the contents of a list of concepts as a list of maps of concepts. + * + * @param comceptList the concept list + * @return the concept map + */ + private List> asConceptMap(List conceptList) { + Map conceptMap = new LinkedHashMap<>(); + for (T concept : conceptList) { + conceptMap.put(concept.getName(), concept); + } + + return Collections.singletonList(conceptMap); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java index 1177368b2..881d87c4b 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java @@ -59,8 +59,12 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaEntrySchema; @NoArgsConstructor public class JpaToscaEntrySchema implements PfAuthorative, Serializable, Comparable { + private static final long serialVersionUID = 3645882081163287058L; + // Recurring string constants + private static final String ENTRY_SCHEMA = "EntrySchema"; + @Column private PfConceptKey type; @@ -157,12 +161,12 @@ public class JpaToscaEntrySchema PfValidationResult result = resultIn; if (type == null || type.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION), + result.addValidationMessage(new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), this.getClass(), ValidationResult.INVALID, "entry schema type may not be null")); } if (description != null && description.trim().length() == 0) { - result.addValidationMessage(new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION), + result.addValidationMessage(new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank")); } @@ -171,7 +175,7 @@ public class JpaToscaEntrySchema for (JpaToscaConstraint constraint : constraints) { if (constraint == null) { result.addValidationMessage( - new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION), + new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), this.getClass(), ValidationResult.INVALID, "property constraint may not be null ")); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index 6c588a50c..a207c4267 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -33,8 +33,6 @@ import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; @@ -81,28 +79,6 @@ public class SimpleToscaProvider { } } - /** - * Get filtered policy types. - * - * @param dao the DAO to use to access the database - * @param filter the filter for the policy types to get - * @return the policy types found - * @throws PfModelException on errors getting policy types - */ - public JpaToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao, - @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { - - // Create the structure of the TOSCA service template to contain the policy type - JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); - - List jpaPolicyTypeList = dao.getAll(JpaToscaPolicyType.class); - // TODO: The actual filtering - - serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); - return serviceTemplate; - } - /** * Create policy types. * @@ -212,29 +188,6 @@ public class SimpleToscaProvider { } } - /** - * Get filtered policies. - * - * @param dao the DAO to use to access the database - * @param filter the filter for the policies to get - * @return the policies found - * @throws PfModelException on errors getting policies - */ - public JpaToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, - @NonNull final ToscaPolicyFilter filter) throws PfModelException { - - // Create the structure of the TOSCA service template to contain the policy type - JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); - serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - - List jpaPolicyList = dao.getAll(JpaToscaPolicy.class); - // TODO: Do the actual filtering - - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); - return serviceTemplate; - } - /** * Create policies. * -- cgit 1.2.3-korg