diff options
author | liamfallon <liam.fallon@est.tech> | 2021-02-07 15:20:23 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2021-02-07 15:20:28 +0000 |
commit | 0b0fd8d7c890a75ee1fe829c68b92cf670a4a29f (patch) | |
tree | af4541fb9f4735055db79b69fb68fc1b6265eb8e | |
parent | fdaa8dc48d80d14177d9c955160e7534be505192 (diff) |
Make filters on TOSCA entities generic
The classes PolicyTypeFilter and PolicyFilter only work on Policy types
and policies. However they just filter on name/version/type/typeversion
so there is no reason that the filters could not be applied to other
entities.
This commit adapts the PolicyTypeFilter to EntityFilter and PolicyFilter
to TypedEntityFilter, so the filters can be applied to collections of
any ToscaEntity class.
Issue-ID: POLICY-2995
Change-Id: Ibf12c757758d31ec3075119c56243e0abcf259bf
Signed-off-by: liamfallon <liam.fallon@est.tech>
4 files changed, 33 insertions, 30 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java index 4ba3322d..33df1ccc 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyProvider.java @@ -3,7 +3,7 @@ * ONAP Policy API
* ================================================================================
* Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,8 +25,9 @@ package org.onap.policy.api.main.rest.provider; import org.onap.policy.api.main.rest.PolicyFetchMode;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
/**
* Class to provide all kinds of policy operations.
@@ -56,7 +57,7 @@ public class PolicyProvider extends CommonModelProvider { * @throws PfModelException the PfModel parsing exception
*/
public ToscaServiceTemplate fetchPolicies(final String policyTypeId, final String policyTypeVersion,
- final String policyId, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
+ final String policyId, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);
}
@@ -73,9 +74,10 @@ public class PolicyProvider extends CommonModelProvider { * @throws PfModelException the PfModel parsing exception
*/
public ToscaServiceTemplate fetchLatestPolicies(final String policyTypeId, final String policyTypeVersion,
- final String policyId, final PolicyFetchMode mode) throws PfModelException {
+ final String policyId, final PolicyFetchMode mode) throws PfModelException {
- return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION, mode);
+ return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaTypedEntityFilter.LATEST_VERSION,
+ mode);
}
/**
@@ -90,7 +92,7 @@ public class PolicyProvider extends CommonModelProvider { * @throws PfModelException the PfModel parsing exception
*/
public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
- throws PfModelException {
+ throws PfModelException {
return modelsProvider.createPolicies(body);
}
@@ -121,7 +123,7 @@ public class PolicyProvider extends CommonModelProvider { * @throws PfModelException the PfModel parsing exception
*/
public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,
- String policyVersion) throws PfModelException {
+ String policyVersion) throws PfModelException {
return modelsProvider.deletePolicy(policyId, policyVersion);
}
@@ -140,10 +142,10 @@ public class PolicyProvider extends CommonModelProvider { * @throws PfModelException the PfModel parsing exception
*/
private ToscaServiceTemplate getFilteredPolicies(final String policyTypeName, final String policyTypeVersion,
- final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
+ final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
- ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)
- .type(policyTypeName).typeVersion(policyTypeVersion).build();
+ ToscaTypedEntityFilter<ToscaPolicy> policyFilter = ToscaTypedEntityFilter.<ToscaPolicy>builder()
+ .name(policyName).version(policyVersion).type(policyTypeName).typeVersion(policyTypeVersion).build();
ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java index 9c9c41cc..e56b536c 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/PolicyTypeProvider.java @@ -3,7 +3,7 @@ * ONAP Policy API
* ================================================================================
* Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,8 @@ package org.onap.policy.api.main.rest.provider;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
/**
@@ -68,7 +69,7 @@ public class PolicyTypeProvider extends CommonModelProvider { */
public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {
- return getFilteredPolicyTypes(policyTypeId, ToscaPolicyTypeFilter.LATEST_VERSION);
+ return getFilteredPolicyTypes(policyTypeId, ToscaEntityFilter.LATEST_VERSION);
}
/**
@@ -113,8 +114,8 @@ public class PolicyTypeProvider extends CommonModelProvider { private ToscaServiceTemplate getFilteredPolicyTypes(String policyTypeName, String policyTypeVersion)
throws PfModelException {
- ToscaPolicyTypeFilter policyTypeFilter =
- ToscaPolicyTypeFilter.builder().name(policyTypeName).version(policyTypeVersion).build();
+ ToscaEntityFilter<ToscaPolicyType> policyTypeFilter =
+ ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeName).version(policyTypeVersion).build();
return modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
}
}
diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java index dbcc42cc..d30b0120 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java @@ -3,7 +3,7 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,8 @@ import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.slf4j.Logger; @@ -83,8 +84,8 @@ public class ApiDatabaseInitializer { ToscaServiceTemplate createdPolicyTypes = preloadServiceTemplate(serviceTemplate, apiParameterGroup.getPreloadPolicyTypes(), databaseProvider::createPolicyTypes); - preloadServiceTemplate(createdPolicyTypes, - apiParameterGroup.getPreloadPolicies(), databaseProvider::createPolicies); + preloadServiceTemplate(createdPolicyTypes, apiParameterGroup.getPreloadPolicies(), + databaseProvider::createPolicies); } catch (final PolicyApiException | PfModelException | CoderException exp) { throw new PolicyApiException(exp); } @@ -93,7 +94,7 @@ public class ApiDatabaseInitializer { private boolean alreadyExists(PolicyModelsProvider databaseProvider) throws PfModelException { try { ToscaServiceTemplate serviceTemplate = - databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()); + databaseProvider.getFilteredPolicyTypes(ToscaEntityFilter.<ToscaPolicyType>builder().build()); if (!serviceTemplate.getPolicyTypes().isEmpty()) { return true; } @@ -105,9 +106,9 @@ public class ApiDatabaseInitializer { return false; } - private ToscaServiceTemplate preloadServiceTemplate(ToscaServiceTemplate serviceTemplate, - List<String> entities, FunctionWithEx<ToscaServiceTemplate, ToscaServiceTemplate> getter) - throws PolicyApiException, CoderException, PfModelException { + private ToscaServiceTemplate preloadServiceTemplate(ToscaServiceTemplate serviceTemplate, List<String> entities, + FunctionWithEx<ToscaServiceTemplate, ToscaServiceTemplate> getter) + throws PolicyApiException, CoderException, PfModelException { for (String entity : entities) { String entityAsStringYaml = ResourceUtils.getResourceAsString(entity); @@ -116,8 +117,7 @@ public class ApiDatabaseInitializer { continue; } - ToscaServiceTemplate singleEntity = - coder.decode(entityAsStringYaml, ToscaServiceTemplate.class); + ToscaServiceTemplate singleEntity = coder.decode(entityAsStringYaml, ToscaServiceTemplate.class); if (singleEntity == null) { throw new PolicyApiException("Error deserializaing entity from file: " + entity); } @@ -136,7 +136,7 @@ public class ApiDatabaseInitializer { serviceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); serviceTemplate.getToscaTopologyTemplate().setPolicies(new LinkedList<>()); serviceTemplate.getToscaTopologyTemplate().getPolicies() - .addAll(singleEntity.getToscaTopologyTemplate().getPolicies()); + .addAll(singleEntity.getToscaTopologyTemplate().getPolicies()); } } // Preload the specified entities diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java index 9b2f3b85..95f93865 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java @@ -3,7 +3,7 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -117,11 +117,11 @@ public class TestPolicyTypeProvider { assertThatThrownBy(() -> { policyTypeProvider.fetchPolicyTypes("dummy", null); - }).hasMessage("policy types for filter ToscaPolicyTypeFilter(name=dummy, version=null) do not exist"); + }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist"); assertThatThrownBy(() -> { policyTypeProvider.fetchPolicyTypes("dummy", "dummy"); - }).hasMessage("policy types for filter ToscaPolicyTypeFilter(name=dummy, version=dummy) do not exist"); + }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist"); } @Test @@ -129,7 +129,7 @@ public class TestPolicyTypeProvider { assertThatThrownBy(() -> { policyTypeProvider.fetchLatestPolicyTypes("dummy"); - }).hasMessage("policy types for filter ToscaPolicyTypeFilter(name=dummy, version=LATEST) do not exist"); + }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist"); } @Test |