aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-05-07 12:42:26 +0000
committerliamfallon <liam.fallon@est.tech>2019-05-07 12:42:26 +0000
commitf53879588a464c727ece62f87c7625b47e6de7f1 (patch)
tree980cddab937c38610587b50cc45a9c36540a2470
parente936413c9082afed0fef4646b8f12d351c87800c (diff)
Set default and check existance of Policy Type
The TOSCA specification has a "bug" in that it does not have a field to specify the version of a policy type to use. We already had introduced the "type_version" field for this. This review introduces setting of the default version of a policy type to be be used by a policy as the latest version of the policy type in the database. As a side effect of this, we now have to check for existence of the policy type of a policy in the database. This means that creation/update of a policy with a non-existant policy type specified will now fail. Issue-ID: POLICY-1738 Change-Id: I27080cf6cd358948810dab6897c72dfe4d41fe91 Signed-off-by: liamfallon <liam.fallon@est.tech>
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConcept.java18
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptFilter.java68
-rw-r--r--models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json2
-rw-r--r--models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.yaml2
-rw-r--r--models-examples/src/main/resources/policytypes/onap.policies.controlloop.Operational.yaml6
-rw-r--r--models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.Blacklist.yaml40
-rw-r--r--models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml50
-rw-r--r--models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.MinMax.yaml44
-rw-r--r--models-examples/src/main/resources/policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml (renamed from models-examples/src/main/resources/policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml)2
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java29
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java21
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java36
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java36
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java2
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java71
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java5
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java4
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java27
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java35
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java28
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java1
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java23
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java6
26 files changed, 535 insertions, 27 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
index c41b0de56..9a376feff 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
@@ -105,6 +105,24 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> {
}
/**
+ * Gets the name of this concept.
+ *
+ * @return the name of this concept
+ */
+ public String getName() {
+ return getKey().getName();
+ }
+
+ /**
+ * Gets the version of this concept.
+ *
+ * @return the version of this concept
+ */
+ public String getVersion() {
+ return getKey().getVersion();
+ }
+
+ /**
* Checks if this key matches the given key ID.
*
* @param id the key ID to match against
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptFilter.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptFilter.java
new file mode 100644
index 000000000..8a9fbc414
--- /dev/null
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptFilter.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.base;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import lombok.Builder;
+import lombok.Data;
+import lombok.NonNull;
+
+/**
+ * Filter class for searches for {@link ToscaPolicy} instances. If any fields are null, they are ignored.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+@Builder
+@Data
+public class PfConceptFilter implements PfObjectFilter<PfConcept> {
+ public static final String LATEST_VERSION = "LATEST";
+
+ // Exact expression
+ private String name;
+
+ // Exact match, set to LATEST_VERSION to get the latest version
+ private String version;
+
+ // version prefix
+ private String versionPrefix;
+
+ @Override
+ public List<PfConcept> filter(@NonNull final List<PfConcept> originalList) {
+
+ // @formatter:off
+ List<PfConcept> returnList = originalList.stream()
+ .filter(filterStringPred(name, PfConcept::getName))
+ .filter(filterStringPred((LATEST_VERSION.equals(version) ? null : version), PfConcept::getVersion))
+ .filter(filterPrefixPred(versionPrefix, PfConcept::getVersion))
+ .collect(Collectors.toList());
+ // @formatter:off
+
+ if (LATEST_VERSION.equals(version)) {
+ return this.latestVersionFilter(returnList);
+ }
+ else {
+ return returnList;
+ }
+ }
+}
diff --git a/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json b/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json
index aef04c99c..cdc40eb5b 100644
--- a/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json
+++ b/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json
@@ -7,7 +7,7 @@
{
"onap.vfirewall.tca":
{
- "type": "onap.policy.monitoring.cdap.tca.hi.lo.app",
+ "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
"version": "1.0.0",
"metadata":
{
diff --git a/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.yaml b/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.yaml
index bce8b366f..6ac547058 100644
--- a/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.yaml
+++ b/models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.yaml
@@ -3,7 +3,7 @@ topology_template:
policies:
-
onap.vfirewall.tca:
- type: onap.policy.monitoring.cdap.tca.hi.lo.app
+ type: onap.policies.monitoring.cdap.tca.hi.lo.app
version: 1.0.0
metadata:
policy-id: onap.vfirewall.tca
diff --git a/models-examples/src/main/resources/policytypes/onap.policies.controlloop.Operational.yaml b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.Operational.yaml
new file mode 100644
index 000000000..e18c16a6d
--- /dev/null
+++ b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.Operational.yaml
@@ -0,0 +1,6 @@
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+policy_types:
+ - onap.policies.controlloop.Operational:
+ derived_from: tosca.policies.Root
+ version: 1.0.0
+ description: Operational Policy for Control Loops \ No newline at end of file
diff --git a/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.Blacklist.yaml b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.Blacklist.yaml
new file mode 100644
index 000000000..e09861428
--- /dev/null
+++ b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.Blacklist.yaml
@@ -0,0 +1,40 @@
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+policy_types:
+ - onap.policies.controlloop.Guard:
+ derived_from: tosca.policies.Root
+ version: 1.0.0
+ description: Guard Policies for Control Loop Operational Policies
+ - onap.policies.controlloop.guard.Blacklist:
+ derived_from: onap.policies.controlloop.Guard
+ version: 1.0.0
+ description: Supports blacklist of VNF's from performing control loop actions on.
+ properties:
+ blacklist_policy:
+ type: map
+ description:
+ entry_schema:
+ type: onap.datatypes.guard.Blacklist
+data_types:
+ - onap.datatypes.guard.Blacklist:
+ derived_from: tosca.datatypes.Root
+ properties:
+ actor:
+ type: string
+ description: Specifies the Actor
+ required: true
+ recipe:
+ type: string
+ description: Specified the Recipe
+ required: true
+ time_range:
+ type: tosca.datatypes.TimeInterval
+ description: An optional range of time during the day the blacklist is valid for.
+ required: false
+ controlLoopName:
+ type: string
+ description: An optional specific control loop to apply this guard to.
+ required: false
+ blacklist:
+ type: list
+ description: List of VNF's
+ required: true \ No newline at end of file
diff --git a/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml
new file mode 100644
index 000000000..2a7b6247f
--- /dev/null
+++ b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml
@@ -0,0 +1,50 @@
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+policy_types:
+ - onap.policies.controlloop.Guard:
+ derived_from: tosca.policies.Root
+ version: 1.0.0
+ description: Guard Policies for Control Loop Operational Policies
+ - onap.policies.controlloop.guard.FrequencyLimiter:
+ derived_from: onap.policies.controlloop.Guard
+ version: 1.0.0
+ description: Supports limiting the frequency of actions being taken by a Actor.
+ properties:
+ frequency_policy:
+ type: map
+ description:
+ entry_schema:
+ type: onap.datatypes.guard.FrequencyLimiter
+data_types:
+ - onap.datatypes.guard.FrequencyLimiter:
+ derived_from: tosca.datatypes.Root
+ properties:
+ actor:
+ type: string
+ description: Specifies the Actor
+ required: true
+ recipe:
+ type: string
+ description: Specified the Recipe
+ required: true
+ time_window:
+ type: scalar-unit.time
+ description: The time window to count the actions against.
+ required: true
+ limit:
+ type: integer
+ description: The limit
+ required: true
+ constraints:
+ - greater_than: 0
+ time_range:
+ type: tosca.datatypes.TimeInterval
+ description: An optional range of time during the day the frequency is valid for.
+ required: false
+ controlLoopName:
+ type: string
+ description: An optional specific control loop to apply this guard to.
+ required: false
+ target:
+ type: string
+ description: An optional specific VNF to apply this guard to.
+ required: false \ No newline at end of file
diff --git a/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.MinMax.yaml b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.MinMax.yaml
new file mode 100644
index 000000000..0a1aa9b72
--- /dev/null
+++ b/models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.MinMax.yaml
@@ -0,0 +1,44 @@
+policy_types:
+ - onap.policies.controlloop.Guard:
+ derived_from: tosca.policies.Root
+ version: 1.0.0
+ description: Guard Policies for Control Loop Operational Policies
+ - onap.policies.controlloop.guard.MinMax:
+ derived_from: onap.policies.controlloop.Guard
+ version: 1.0.0
+ description: Supports Min/Max number of VF Modules
+ properties:
+ minmax_policy:
+ type: map
+ description:
+ entry_schema:
+ type: onap.datatypes.guard.MinMax
+data_types:
+ - onap.datatypes.guard.MinMax:
+ derived_from: tosca.datatypes.Root
+ properties:
+ actor:
+ type: string
+ description: Specifies the Actor
+ required: true
+ recipe:
+ type: string
+ description: Specified the Recipe
+ required: true
+ time_range:
+ type: tosca.datatypes.TimeInterval
+ description: An optional range of time during the day the Min/Max limit is valid for.
+ required: false
+ controlLoopName:
+ type: string
+ description: An optional specific control loop to apply this guard to.
+ required: false
+ min_vf_module_instances:
+ type: integer
+ required: true
+ description: The minimum instances of this VF-Module
+
+ max_vf_module_instances:
+ type: integer
+ required: false
+ description: The maximum instances of this VF-Module \ No newline at end of file
diff --git a/models-examples/src/main/resources/policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml b/models-examples/src/main/resources/policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml
index f8e9b7521..2985603af 100644
--- a/models-examples/src/main/resources/policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml
+++ b/models-examples/src/main/resources/policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml
@@ -5,7 +5,7 @@ policy_types:
derived_from: tosca.policies.Root
description: a base policy type for all policies that governs monitoring provisioning
-
- onap.policy.monitoring.cdap.tca.hi.lo.app:
+ onap.policies.monitoring.cdap.tca.hi.lo.app:
derived_from: onap.policies.Monitoring
version: 1.0.0
properties:
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java
index 741ae8998..16956ce0a 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java
@@ -32,16 +32,19 @@ import lombok.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.provider.PolicyModelsProviderFactory;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
/**
* Test persistence of monitoring policies to and from the database.
@@ -72,9 +75,10 @@ public class PolicyLegacyGuardPersistenceTest {
* Initialize provider.
*
* @throws PfModelException on exceptions in the tests
+ * @throws CoderException on JSON encoding and decoding errors
*/
@Before
- public void setupParameters() throws PfModelException {
+ public void setupParameters() throws PfModelException, CoderException {
PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
parameters.setDatabaseDriver("org.h2.Driver");
parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
@@ -83,6 +87,8 @@ public class PolicyLegacyGuardPersistenceTest {
parameters.setPersistenceUnit("ToscaConceptTest");
databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+ createPolicyTypes();
}
/**
@@ -150,4 +156,25 @@ public class PolicyLegacyGuardPersistenceTest {
// All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters
assertEquals(policyOutputString.replaceAll("\\s+", ""), actualRetrievedJson.replaceAll("\\s+", ""));
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+
+ yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.MinMax.yaml"));
+ yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java
index 2e33a11a0..1cb64a835 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java
@@ -31,15 +31,18 @@ import lombok.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.provider.PolicyModelsProviderFactory;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
/**
* Test persistence of monitoring policies to and from the database.
@@ -72,9 +75,10 @@ public class PolicyLegacyOperationalPersistenceTest {
* Initialize provider.
*
* @throws PfModelException on exceptions in the tests
+ * @throws CoderException on JSON encoding and decoding errors
*/
@Before
- public void setupParameters() throws PfModelException {
+ public void setupParameters() throws PfModelException, CoderException {
PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
parameters.setDatabaseDriver("org.h2.Driver");
parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
@@ -83,6 +87,8 @@ public class PolicyLegacyOperationalPersistenceTest {
parameters.setPersistenceUnit("ToscaConceptTest");
databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+ createPolicyTypes();
}
/**
@@ -145,4 +151,17 @@ public class PolicyLegacyOperationalPersistenceTest {
actualRetrievedJson.replaceAll("\\s+", "").replaceAll("u0027", "_-_-_-_").replaceAll("\\\\_-_-_-_",
"'"));
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java
index 668f63497..a855d5d24 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java
@@ -34,6 +34,7 @@ import lombok.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
@@ -81,9 +82,10 @@ public class PolicyPersistenceTest {
* Initialize provider.
*
* @throws PfModelException on exceptions in the tests
+ * @throws CoderException on JSON encoding and decoding errors
*/
@Before
- public void setupParameters() throws PfModelException {
+ public void setupParameters() throws PfModelException, CoderException {
PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
parameters.setDatabaseDriver("org.h2.Driver");
parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
@@ -92,6 +94,8 @@ public class PolicyPersistenceTest {
parameters.setPersistenceUnit("ToscaConceptTest");
databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+ createPolicyTypes();
}
/**
@@ -167,4 +171,34 @@ public class PolicyPersistenceTest {
}
}
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+
+ yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
+ yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+
+ yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
+ yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
index 613c5a2c6..140194276 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
@@ -33,6 +33,7 @@ import lombok.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
@@ -79,9 +80,10 @@ public class PolicyToscaPersistenceTest {
* Initialize provider.
*
* @throws PfModelException on exceptions in the tests
+ * @throws CoderException on JSON encoding and decoding errors
*/
@Before
- public void setupParameters() throws PfModelException {
+ public void setupParameters() throws PfModelException, CoderException {
PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
parameters.setDatabaseDriver("org.h2.Driver");
parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
@@ -90,6 +92,8 @@ public class PolicyToscaPersistenceTest {
parameters.setPersistenceUnit("ToscaConceptTest");
databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+ createPolicyTypes();
}
/**
@@ -151,4 +155,34 @@ public class PolicyToscaPersistenceTest {
assertEquals(incomingPolicy.getType(), databasePolicy.getType());
}
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+
+ yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
+ yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+
+ yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
+ yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ databaseProvider.createPolicyTypes(toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
index 7b541e128..7b7690009 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
@@ -72,7 +72,7 @@ public class PolicyTypePersistenceTest {
"policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
"policytypes/onap.policies.optimization.Vim_fit.yaml",
"policytypes/onap.policies.optimization.VnfPolicy.yaml",
- "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"
+ "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"
};
// @formatter:on
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 ef8ac05ec..81a41aa05 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,15 +20,21 @@
package org.onap.policy.models.tosca.simple.provider;
+import java.util.ArrayList;
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.PfConceptFilter;
import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfKey;
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.simple.concepts.JpaToscaPolicies;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
@@ -37,6 +43,8 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
import org.onap.policy.models.tosca.utils.ToscaUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class provides the provision of information on TOSCA concepts in the database to callers.
@@ -44,6 +52,8 @@ import org.onap.policy.models.tosca.utils.ToscaUtils;
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class SimpleToscaProvider {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
+
/**
* Get policy types.
*
@@ -184,6 +194,8 @@ public class SimpleToscaProvider {
ToscaUtils.assertPoliciesExist(serviceTemplate);
for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+ verifyPolicyTypeForPolicy(dao, policy);
+
dao.create(policy);
}
@@ -262,4 +274,63 @@ public class SimpleToscaProvider {
return conceptMap;
}
+
+ /**
+ * Verify the policy type for a policy exists.
+ *
+ * @param dao the DAO to use to access policy types in the database
+ * @param policy the policy to check the policy type for
+ */
+ private void verifyPolicyTypeForPolicy(final PfDao dao, final JpaToscaPolicy policy) {
+ PfConceptKey policyTypeKey = policy.getType();
+
+ JpaToscaPolicyType policyType = null;
+
+ if (PfKey.NULL_KEY_VERSION.equals(policyTypeKey.getVersion())) {
+ policyType = getLatestPolicyTypeVersion(dao, policyTypeKey.getName());
+ } else {
+ policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey);
+ }
+
+ if (policyType == null) {
+ String errorMessage =
+ "policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist";
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+ }
+
+ /**
+ * Get the latest version of the policy type for the given policy type name.
+ *
+ * @param dao the DAO to use to access policy types in the database
+ * @param policyTypeName the name of the policy type
+ * @return the latest policy type
+ */
+ private JpaToscaPolicyType getLatestPolicyTypeVersion(final PfDao dao, final String policyTypeName) {
+ // Policy type version is not specified, get the latest version from the database
+ List<JpaToscaPolicyType> jpaPolicyTypeList =
+ dao.getFiltered(JpaToscaPolicyType.class, policyTypeName, null);
+
+ if (jpaPolicyTypeList.isEmpty()) {
+ return null;
+ }
+
+ // Create a filter to get the latest version of the policy type
+ PfConceptFilter pfConceptFilter = PfConceptFilter.builder().version(PfConceptFilter.LATEST_VERSION).build();
+
+ // FIlter the returned policy type list
+ List<PfConcept> policyTypeKeyList = new ArrayList<>(jpaPolicyTypeList);
+ List<PfConcept> filterdPolicyTypeList = pfConceptFilter.filter(policyTypeKeyList);
+
+ // We should have one and only one returned entry
+ if (filterdPolicyTypeList.size() != 1 ) {
+ String errorMessage =
+ "search for lates policy type " + policyTypeName + " returned more than one entry";
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ return (JpaToscaPolicyType) filterdPolicyTypeList.get(0);
+ }
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
index 93333c4e3..a73fb85e9 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -38,8 +38,7 @@ public final class ToscaUtils {
/**
* Private constructor to prevent subclassing.
*/
- private ToscaUtils() {
- }
+ private ToscaUtils() {}
/**
* Check if policy types have been specified is initialized.
@@ -80,6 +79,4 @@ public final class ToscaUtils {
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
}
-
-
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java
index f7c9c7ef0..cba3fe591 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java
@@ -211,7 +211,7 @@ public class ToscaPolicyFilterTest {
filter = ToscaPolicyFilter.builder().type("onap.policies.monitoring.cdap.tca.hi.lo.app").build();
filteredList = filter.filter(policyList);
- assertEquals(2, filteredList.size());
+ assertEquals(3, filteredList.size());
filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.NonOperational").build();
filteredList = filter.filter(policyList);
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java
index 12d81ed53..e0143e676 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java
@@ -63,7 +63,7 @@ public class ToscaPolicyTypeFilterTest {
"policytypes/onap.policies.optimization.SubscriberPolicy.yaml",
"policytypes/onap.policies.optimization.Vim_fit.yaml",
"policytypes/onap.policies.optimization.VnfPolicy.yaml",
- "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"
+ "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"
};
// @formatter:on
@@ -152,7 +152,7 @@ public class ToscaPolicyTypeFilterTest {
List<ToscaPolicyType> filteredList = filter.filter(typeList);
assertEquals(2, filteredList.size());
- filter = ToscaPolicyTypeFilter.builder().name("onap.policy.monitoring.cdap.tca.hi.lo.app").build();
+ filter = ToscaPolicyTypeFilter.builder().name("onap.policies.monitoring.cdap.tca.hi.lo.app").build();
filteredList = filter.filter(typeList);
assertEquals(1, filteredList.size());
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java
index 5ad314ae6..a7f3761a5 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java
@@ -33,6 +33,7 @@ import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfConceptKey;
@@ -45,6 +46,7 @@ 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.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
+import org.yaml.snakeyaml.Yaml;
/**
* Test of the {@link AuthorativeToscaProvider} class.
@@ -75,7 +77,7 @@ public class AuthorativeToscaProviderPolicyTest {
jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
- daoParameters.setJdbcProperties(jdbcProperties );
+ daoParameters.setJdbcProperties(jdbcProperties);
pfDao = new PfDaoFactory().createPfDao(daoParameters);
pfDao.init(daoParameters);
@@ -104,6 +106,8 @@ public class AuthorativeToscaProviderPolicyTest {
new AuthorativeToscaProvider().getPolicyList(null, null, null);
}).hasMessage("dao is marked @NonNull but is null");
+ createPolicyTypes();
+
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
@@ -176,6 +180,8 @@ public class AuthorativeToscaProviderPolicyTest {
new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null);
}).hasMessage("filter is marked @NonNull but is null");
+ createPolicyTypes();
+
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
@@ -250,6 +256,8 @@ public class AuthorativeToscaProviderPolicyTest {
new AuthorativeToscaProvider().createPolicies(pfDao, null);
}).hasMessage("serviceTemplate is marked @NonNull but is null");
+ createPolicyTypes();
+
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
@@ -268,7 +276,6 @@ public class AuthorativeToscaProviderPolicyTest {
assertTrue(beforePolicy.getType().equals(createdPolicy.getType()));
}
-
@Test
public void testPolicyUpdate() throws Exception {
assertThatThrownBy(() -> {
@@ -287,6 +294,8 @@ public class AuthorativeToscaProviderPolicyTest {
new AuthorativeToscaProvider().updatePolicies(pfDao, null);
}).hasMessage("serviceTemplate is marked @NonNull but is null");
+ createPolicyTypes();
+
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
@@ -343,6 +352,8 @@ public class AuthorativeToscaProviderPolicyTest {
new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null);
}).hasMessage("version is marked @NonNull but is null");
+ createPolicyTypes();
+
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
@@ -396,4 +407,16 @@ public class AuthorativeToscaProviderPolicyTest {
new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate);
}).hasMessage("An incoming list of concepts must have at least one entry");
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java
index a4458a874..b44853428 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java
@@ -82,7 +82,7 @@ public class ToscaServiceTemplateMappingTest {
try {
Yaml yaml = new Yaml();
String inputYaml = ResourceUtils.getResourceAsString(
- "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml");
+ "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml");
Object yamlObject = yaml.load(inputYaml);
String yamlAsJsonString = standardCoder.encode(yamlObject);
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java
index 71254ec6f..6ddf1aeeb 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java
@@ -31,15 +31,20 @@ import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.dao.PfDaoFactory;
import org.onap.policy.models.dao.impl.DefaultPfDao;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
+import org.yaml.snakeyaml.Yaml;
/**
* Test the {@link LegacyProvider} class for legacy guard policies.
@@ -108,6 +113,8 @@ public class LegacyProvider4LegacyGuardTest {
new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist");
}).hasMessage("no policy found for policy ID: I Dont Exist");
+ createPolicyTypes();
+
LegacyGuardPolicyInput originalGip = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
LegacyGuardPolicyInput.class);
@@ -148,6 +155,8 @@ public class LegacyProvider4LegacyGuardTest {
new LegacyProvider().createGuardPolicy(pfDao, null);
}).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
+ createPolicyTypes();
+
LegacyGuardPolicyInput originalGip = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
LegacyGuardPolicyInput.class);
@@ -174,7 +183,6 @@ public class LegacyProvider4LegacyGuardTest {
assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
}
-
@Test
public void testPolicyUpdate() throws Exception {
assertThatThrownBy(() -> {
@@ -193,6 +201,8 @@ public class LegacyProvider4LegacyGuardTest {
new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput());
}).hasMessage("policy type for guard policy \"null\" unknown");
+ createPolicyTypes();
+
LegacyGuardPolicyInput originalGip = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
LegacyGuardPolicyInput.class);
@@ -246,6 +256,8 @@ public class LegacyProvider4LegacyGuardTest {
new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist");
}).hasMessage("no policy found for policy ID: I Dont Exist");
+ createPolicyTypes();
+
LegacyGuardPolicyInput originalGip = standardCoder.decode(
ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
LegacyGuardPolicyInput.class);
@@ -294,4 +306,25 @@ public class LegacyProvider4LegacyGuardTest {
new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId());
}).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
+
+ yamlObject = new Yaml()
+ .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.guard.Blacklist.yaml"));
+ yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ toscaServiceTemplatePolicyType = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java
index c018aae7b..7ab5c581e 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java
@@ -30,13 +30,18 @@ import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.dao.PfDaoFactory;
import org.onap.policy.models.dao.impl.DefaultPfDao;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
+import org.yaml.snakeyaml.Yaml;
/**
* Test the {@link LegacyProvider} class for legacy operational policies.
@@ -104,6 +109,8 @@ public class LegacyProvider4LegacyOperationalTest {
new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
}).hasMessage("no policy found for policy ID: I Dont Exist");
+ createPolicyTypes();
+
LegacyOperationalPolicy originalLop =
standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
LegacyOperationalPolicy.class);
@@ -142,6 +149,8 @@ public class LegacyProvider4LegacyOperationalTest {
new LegacyProvider().createOperationalPolicy(pfDao, null);
}).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
+ createPolicyTypes();
+
LegacyOperationalPolicy originalLop =
standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
LegacyOperationalPolicy.class);
@@ -162,7 +171,6 @@ public class LegacyProvider4LegacyOperationalTest {
assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
}
-
@Test
public void testPolicyUpdate() throws Exception {
assertThatThrownBy(() -> {
@@ -181,6 +189,8 @@ public class LegacyProvider4LegacyOperationalTest {
new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
}).hasMessage("no policy found for policy ID: null");
+ createPolicyTypes();
+
LegacyOperationalPolicy originalLop =
standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
LegacyOperationalPolicy.class);
@@ -203,7 +213,6 @@ public class LegacyProvider4LegacyOperationalTest {
assertEquals("Some New Content", gotUpdatedLop.getContent());
}
-
@Test
public void testPoliciesDelete() throws Exception {
assertThatThrownBy(() -> {
@@ -212,17 +221,19 @@ public class LegacyProvider4LegacyOperationalTest {
assertThatThrownBy(() -> {
new LegacyProvider().deleteOperationalPolicy(null, "");
+
}).hasMessage("dao is marked @NonNull but is null");
assertThatThrownBy(() -> {
new LegacyProvider().deleteOperationalPolicy(pfDao, null);
}).hasMessage("policyId is marked @NonNull but is null");
-
assertThatThrownBy(() -> {
new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
}).hasMessage("no policy found for policy ID: I Dont Exist");
+ createPolicyTypes();
+
LegacyOperationalPolicy originalLop =
standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
LegacyOperationalPolicy.class);
@@ -260,6 +271,17 @@ public class LegacyProvider4LegacyOperationalTest {
assertThatThrownBy(() -> {
new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
}).hasMessage("no policy found for policy ID: operational.restart");
+ }
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
}
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java
index ae38ab916..924cdab53 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java
@@ -129,7 +129,6 @@ public class JpaToscaPolicyTest {
assertEquals(tdtClone0, tp);
assertFalse(new JpaToscaPolicy().validate(new PfValidationResult()).isValid());
- System.err.println(tp.validate(new PfValidationResult()));
assertTrue(tp.validate(new PfValidationResult()).isValid());
tp.getProperties().put(null, null);
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 1f582cf84..4d71d0ddd 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
@@ -32,6 +32,7 @@ import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfConceptKey;
@@ -41,9 +42,11 @@ import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.dao.PfDaoFactory;
import org.onap.policy.models.dao.impl.DefaultPfDao;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
+import org.yaml.snakeyaml.Yaml;
/**
* Test the {@link SimpleToscaProvider} class.
@@ -99,6 +102,8 @@ public class SimpleToscaProviderTest {
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
@@ -124,6 +129,8 @@ public class SimpleToscaProviderTest {
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
@@ -140,6 +147,8 @@ public class SimpleToscaProviderTest {
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
@@ -156,6 +165,8 @@ public class SimpleToscaProviderTest {
ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
ToscaServiceTemplate.class);
+ createPolicyTypes();
+
JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
@@ -288,4 +299,16 @@ public class SimpleToscaProviderTest {
new SimpleToscaProvider().deletePolicy(pfDao, null);
}).hasMessage("policyKey is marked @NonNull but is null");
}
+
+ private void createPolicyTypes() throws CoderException, PfModelException {
+ Object yamlObject = new Yaml().load(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml"));
+ String yamlAsJsonString = new StandardCoder().encode(yamlObject);
+
+ ToscaServiceTemplate toscaServiceTemplatePolicyType =
+ standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
+
+ assertNotNull(toscaServiceTemplatePolicyType);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType);
+ }
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
index 9d9ee608d..f05e2e6ef 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
@@ -297,7 +297,7 @@ public class MonitoringPolicySerializationTest {
JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject();
assertNotNull(policy.get("onap.vfirewall.tca"));
JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject();
- assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
+ assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString());
assertEquals("1.0.0", policyVal.get("version").getAsString());
assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id")
.getAsString());
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java
index 569e1ad41..0e053f182 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java
@@ -57,7 +57,7 @@ public class MonitoringPolicyTypeSerializationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicyTypeSerializationTest.class);
- private static final String MONITORING_TCA_YAML = "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml";
+ private static final String MONITORING_TCA_YAML = "policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml";
private static final String MONITORING_COLLECTORS_YAML =
"policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml";
@@ -155,13 +155,13 @@ public class MonitoringPolicyTypeSerializationTest {
firstPolicyType.getValue().getDescription());
Entry<PfConceptKey, JpaToscaPolicyType> secondPolicyType = policyTypesIter.next();
- assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", secondPolicyType.getKey().getName());
+ assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", secondPolicyType.getKey().getName());
assertEquals("1.0.0", secondPolicyType.getKey().getVersion());
assertEquals("onap.policies.Monitoring", secondPolicyType.getValue().getDerivedFrom().getName());
assertTrue(secondPolicyType.getValue().getProperties().size() == 1);
JpaToscaProperty property = secondPolicyType.getValue().getProperties().values().iterator().next();
- assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", property.getKey().getParentKeyName());
+ assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", property.getKey().getParentKeyName());
assertEquals("1.0.0", property.getKey().getParentKeyVersion());
assertEquals("tca_policy", property.getKey().getLocalName());
assertEquals("map", property.getType().getName());