Set default and check existance of Policy Type 38/87138/2
authorliamfallon <liam.fallon@est.tech>
Tue, 7 May 2019 12:42:26 +0000 (12:42 +0000)
committerliamfallon <liam.fallon@est.tech>
Tue, 7 May 2019 12:42:26 +0000 (12:42 +0000)
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>
26 files changed:
models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
models-base/src/main/java/org/onap/policy/models/base/PfConceptFilter.java [new file with mode: 0644]
models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json
models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.yaml
models-examples/src/main/resources/policytypes/onap.policies.controlloop.Operational.yaml [new file with mode: 0644]
models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.Blacklist.yaml [new file with mode: 0644]
models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml [new file with mode: 0644]
models-examples/src/main/resources/policytypes/onap.policies.controlloop.guard.MinMax.yaml [new file with mode: 0644]
models-examples/src/main/resources/policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml [moved from models-examples/src/main/resources/policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml with 99% similarity]
models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java

index c41b0de..9a376fe 100644 (file)
@@ -104,6 +104,24 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> {
         return getKey().getId();
     }
 
+    /**
+     * 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.
      *
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 (file)
index 0000000..8a9fbc4
--- /dev/null
@@ -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;
+        }
+    }
+}
index aef04c9..cdc40eb 100644 (file)
@@ -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": 
                     {
index bce8b36..6ac5470 100644 (file)
@@ -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 (file)
index 0000000..e18c16a
--- /dev/null
@@ -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 (file)
index 0000000..e098614
--- /dev/null
@@ -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 (file)
index 0000000..2a7b624
--- /dev/null
@@ -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 (file)
index 0000000..0a1aa9b
--- /dev/null
@@ -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
@@ -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:
index 741ae89..16956ce 100644 (file)
@@ -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);
+    }
 }
index 2e33a11..1cb64a8 100644 (file)
@@ -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);
+    }
 }
index 668f634..a855d5d 100644 (file)
@@ -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);
+    }
 }
index 613c5a2..1401942 100644 (file)
@@ -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);
+    }
 }
index 7b541e1..7b76900 100644 (file)
@@ -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
 
index ef8ac05..81a41aa 100644 (file)
 
 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);
+    }
 }
index 93333c4..a73fb85 100644 (file)
@@ -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);
         }
     }
-
-
 }
index f7c9c7e..cba3fe5 100644 (file)
@@ -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);
index 12d81ed..e0143e6 100644 (file)
@@ -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());
 
index 5ad314a..a7f3761 100644 (file)
@@ -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);
+    }
 }
index a4458a8..b448534 100644 (file)
@@ -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);
 
index 71254ec..6ddf1ae 100644 (file)
@@ -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);
+    }
 }
index c018aae..7ab5c58 100644 (file)
@@ -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);
     }
 }
index ae38ab9..924cdab 100644 (file)
@@ -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);
index 1f582cf..4d71d0d 100644 (file)
@@ -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);
+    }
 }
index 9d9ee60..f05e2e6 100644 (file)
@@ -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());
index 569e1ad..0e053f1 100644 (file)
@@ -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());