diff options
author | liamfallon <liam.fallon@est.tech> | 2019-04-04 12:16:12 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-04-04 12:16:12 +0000 |
commit | fc297c8a98df155971e2fa485c7724a61b70c69c (patch) | |
tree | 4ee7cd37b8535417b467d8680455f87c52d534be /models-tosca/src/main/java/org/onap | |
parent | 162cb586fe139460dd3d0404274e85b3f4ead15b (diff) |
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 <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java/org/onap')
4 files changed, 202 insertions, 69 deletions
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<ToscaPolicyType> 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<ToscaPolicyType> getLatestPolicyTypeList(@NonNull final PfDao dao, final String name) - throws PfModelException { - return new ArrayList<>(); + public List<ToscaPolicyType> 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<ToscaPolicy> 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<ToscaPolicy> 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<ToscaPolicy> getLatestPolicyList(@NonNull final PfDao dao, final String name) throws PfModelException { - return new ArrayList<>(); + public List<ToscaPolicy> 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 <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) { + if (listOfMaps == null) { + return Collections.emptyList(); + } + + List<T> returnList = new ArrayList<>(); + for (Map<String, T> 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,18 +70,40 @@ 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<JpaToscaPolicyType> 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<JpaToscaPolicyType> jpaPolicyTypeList = dao.getAll(JpaToscaPolicyType.class); + // TODO: The actual filtering + + serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); + return serviceTemplate; + } + + /** * Create policy types. * * @param dao the DAO to use to access the database @@ -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,19 +200,42 @@ 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<JpaToscaPolicy> 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<JpaToscaPolicy> jpaPolicyList = dao.getAll(JpaToscaPolicy.class); + // TODO: Do the actual filtering + + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); + return serviceTemplate; + } + + /** * Create policies. * * @param dao the DAO to use to access the database @@ -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 <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) { + Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>(); + for (T concept : conceptList) { + conceptMap.put((PfConceptKey) concept.getKey(), concept); + } + + return conceptMap; + } } |