From fc297c8a98df155971e2fa485c7724a61b70c69c Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 4 Apr 2019 12:16:12 +0000 Subject: Add filter obejcts for concepts This review: - Fixes the bug in getting policies where the key fields were null - Removes complex version checks from DAO interface - Simplifies provider API by introducing filter objects for searches Issue-ID: POLICY-1095 Change-Id: I5ab7471c03e8b61849e7882ed18541acd627dc39 Signed-off-by: liamfallon --- .../authorative/concepts/ToscaPolicyFilter.java | 30 ++++++ .../concepts/ToscaPolicyTypeFilter.java | 30 ++++++ .../provider/AuthorativeToscaProvider.java | 106 +++++++++++---------- .../tosca/simple/provider/SimpleToscaProvider.java | 105 ++++++++++++++++---- .../simple/provider/SimpleToscaProviderTest.java | 27 +----- 5 files changed, 207 insertions(+), 91 deletions(-) create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java (limited to 'models-tosca') 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 new file mode 100644 index 000000000..496c62677 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +/** + * Filter class for searches for {@link ToscaPolicy} instances. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyFilter { + +} 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 new file mode 100644 index 000000000..a77e1856b --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +/** + * Filter class for searches for {@link ToscaPolicyType} instances. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyTypeFilter { + +} 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 2b6c25e7a..274130a71 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 @@ -21,7 +21,9 @@ package org.onap.policy.models.tosca.authorative.provider; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; import lombok.NonNull; @@ -29,7 +31,9 @@ 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.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; @@ -52,7 +56,7 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - return new SimpleToscaProvider().getPolicyTypes(dao, new PfConceptKey(name, version)).toAuthorative(); + return new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative(); } /** @@ -66,33 +70,37 @@ public class AuthorativeToscaProvider { */ public List getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - return new ArrayList<>(); + + return (asConceptList( + new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes())); } /** - * Get latest policy types. + * Get filtered policy types. * * @param dao the DAO to use to access the database - * @param name the name of the policy type to get, set to null to get all policy types + * @param filter the filter for the policy types to get * @return the policy types found * @throws PfModelException on errors getting policy types */ - public ToscaServiceTemplate getLatestPolicyTypes(@NonNull final PfDao dao, final String name) - throws PfModelException { - return null; + public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao, + @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { + return new SimpleToscaProvider().getFilteredPolicyTypes(dao, filter).toAuthorative(); } /** - * Get latest policy types. + * Get filtered policy types. * * @param dao the DAO to use to access the database - * @param name the name of the policy type to get, set to null to get all policy types + * @param filter the filter for the policy types to get * @return the policy types found * @throws PfModelException on errors getting policy types */ - public List getLatestPolicyTypeList(@NonNull final PfDao dao, final String name) - throws PfModelException { - return new ArrayList<>(); + public List getFilteredPolicyTypeList(@NonNull final PfDao dao, + @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { + + return (asConceptList( + new SimpleToscaProvider().getFilteredPolicyTypes(dao, filter).toAuthorative().getPolicyTypes())); } /** @@ -152,7 +160,7 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version) throws PfModelException { - return new SimpleToscaProvider().getPolicies(dao, new PfConceptKey(name, version)).toAuthorative(); + return new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative(); } /** @@ -166,61 +174,38 @@ public class AuthorativeToscaProvider { */ public List getPolicyList(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - return new ArrayList<>(); - } - /** - * Get policies for a policy type name. - * - * @param dao the DAO to use to access the database - * @param policyTypeName the name of the policy type for which to get policies - * @param policyTypeVersion the version of the policy type, null returns all versions of deployed policies for - * policy types - * @return the policies found - * @throws PfModelException on errors getting policies - */ - public ToscaServiceTemplate getPolicies4PolicyType(@NonNull final PfDao dao, @NonNull final String policyTypeName, - final String policyTypeVersion) throws PfModelException { - return null; + return asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative() + .getToscaTopologyTemplate().getPolicies()); } /** - * Get policies for a policy type name. + * Get filtered policies. * * @param dao the DAO to use to access the database - * @param policyTypeName the name of the policy type for which to get policies - * @param policyTypeVersion the version of the policy type, null returns all versions of deployed policies for - * policy types + * @param filter the filter for the policies to get * @return the policies found * @throws PfModelException on errors getting policies */ - public List getPolicyList4PolicyType(@NonNull final PfDao dao, @NonNull final String policyTypeName, - final String policyTypeVersion) throws PfModelException { - return new ArrayList<>(); - } + public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) + throws PfModelException { - /** - * Get latest policies. - * - * @param dao the DAO to use to access the database - * @param name the name of the policy to get, null to get all policies - * @return the policies found - * @throws PfModelException on errors getting policies - */ - public ToscaServiceTemplate getLatestPolicies(@NonNull final PfDao dao, final String name) throws PfModelException { - return null; + return new SimpleToscaProvider().getFilteredPolicies(dao, filter).toAuthorative(); } /** - * Get latest policies. + * Get filtered policies. * * @param dao the DAO to use to access the database - * @param name the name of the policy to get, null to get all policies + * @param filter the filter for the policies to get * @return the policies found * @throws PfModelException on errors getting policies */ - public List getLatestPolicyList(@NonNull final PfDao dao, final String name) throws PfModelException { - return new ArrayList<>(); + public List getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) + throws PfModelException { + + return asConceptList(new SimpleToscaProvider().getFilteredPolicies(dao, filter).toAuthorative() + .getToscaTopologyTemplate().getPolicies()); } /** @@ -267,4 +252,25 @@ public class AuthorativeToscaProvider { return new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative(); } + + /** + * Return the contents of a list of maps as a plain list. + * + * @param listOfMaps the list of maps + * @return the plain list + */ + private List asConceptList(final List> listOfMaps) { + if (listOfMaps == null) { + return Collections.emptyList(); + } + + List returnList = new ArrayList<>(); + for (Map conceptMap : listOfMaps) { + for (T concept : conceptMap.values()) { + returnList.add(concept); + } + } + + return returnList; + } } 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 e7e81603a..6c588a50c 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 @@ -20,14 +20,21 @@ package org.onap.policy.models.tosca.simple.provider; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + import javax.ws.rs.core.Response; import lombok.NonNull; +import org.onap.policy.models.base.PfConcept; 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; @@ -50,12 +57,12 @@ public class SimpleToscaProvider { * Get policy types. * * @param dao the DAO to use to access the database - * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy - * types. A null key version returns all versions of the policy type name specified in the key. + * @param name the name of the policy type to get, set to null to get all policy types + * @param version the version of the policy type to get, set to null to get all versions * @return the policy types found * @throws PfModelException on errors getting policy types */ - public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey) + public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { // Create the structure of the TOSCA service template to contain the policy type @@ -63,17 +70,39 @@ public class SimpleToscaProvider { serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); // Add the policy type to the TOSCA service template - JpaToscaPolicyType policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey); - if (policyType != null) { - serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType); + List jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); + if (jpaPolicyTypeList != null) { + serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); return serviceTemplate; } else { - String errorMessage = "policy type not found: " + policyTypeKey.getId(); + String errorMessage = "policy type not found: " + name + ":" + version; LOGGER.warn(errorMessage); throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } } + /** + * 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. * @@ -143,11 +172,11 @@ public class SimpleToscaProvider { * @return the TOSCA service template containing the policy types that were deleted * @throws PfModelException on errors deleting policy types */ - public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, - @NonNull final PfConceptKey policyTypeKey) + public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey) throws PfModelException { - JpaToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey); + JpaToscaServiceTemplate serviceTemplate = + getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion()); dao.delete(JpaToscaPolicyType.class, policyTypeKey); @@ -158,12 +187,12 @@ public class SimpleToscaProvider { * Get policies. * * @param dao the DAO to use to access the database - * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified. - * A null local name returns all policies for a parent policy type. + * @param name the name of the policy to get, set to null to get all policy types + * @param version the version of the policy to get, set to null to get all versions * @return the policies found * @throws PfModelException on errors getting policies */ - public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey) + public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { // Create the structure of the TOSCA service template to contain the policy type @@ -171,18 +200,41 @@ public class SimpleToscaProvider { serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - // Add the policy to the TOSCA service template - JpaToscaPolicy policy = dao.get(JpaToscaPolicy.class, policyKey); - if (policy != null) { - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy); + // Add the policy type to the TOSCA service template + List jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version); + if (jpaPolicyList != null) { + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); return serviceTemplate; } else { - String errorMessage = "policy not found: " + policyKey.getId(); + String errorMessage = "policy not found: " + name + ":" + version; LOGGER.warn(errorMessage); throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } } + /** + * 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. * @@ -254,10 +306,25 @@ public class SimpleToscaProvider { public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey) throws PfModelException { - JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey); + JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion()); dao.delete(JpaToscaPolicy.class, policyKey); return serviceTemplate; } + + /** + * Convert a list of concepts to a map of concepts. + * + * @param conceptList the concept list + * @return the concept map + */ + private Map asConceptMap(List conceptList) { + Map conceptMap = new LinkedHashMap<>(); + for (T concept : conceptList) { + conceptMap.put((PfConceptKey) concept.getKey(), concept); + } + + return conceptMap; + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java index 0d486e3ea..dca34b08e 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.simple.provider; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.sql.Connection; @@ -93,26 +94,12 @@ public class SimpleToscaProviderTest { @Test public void testPoliciesGet() throws Exception { try { - new SimpleToscaProvider().getPolicies(null, null); + new SimpleToscaProvider().getPolicies(null, null, null); fail("test should throw an exception here"); } catch (Exception exc) { assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - try { - new SimpleToscaProvider().getPolicies(null, new PfConceptKey()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().getPolicies(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -129,7 +116,7 @@ public class SimpleToscaProviderTest { PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); JpaToscaServiceTemplate gotServiceTemplate = - new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey)); + new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); @@ -254,12 +241,8 @@ public class SimpleToscaProviderTest { assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); - try { - new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey)); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); - } + assertTrue(new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()) + .getTopologyTemplate().getPolicies().getConceptMap().isEmpty()); } @Test -- cgit 1.2.3-korg