/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
// The very fist key that could have this name
- final PfConceptKey lowestArtifactKey = new PfConceptKey(conceptKeyName, "0.0.1");
+ final PfConceptKey lowestArtifactKey = new PfConceptKey(conceptKeyName, PfKey.NULL_KEY_VERSION);
if (conceptKeyVersion != null) {
lowestArtifactKey.setVersion(conceptKeyVersion);
}
import java.util.Date;
import java.util.List;
import java.util.Map;
+
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.ws.rs.core.Response;
+
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
final EntityManager mg = getEntityManager();
try {
final T t = mg.find(someClass, key);
+ if (t != null) {
+ mg.refresh(t);
+ }
return checkAndReturn(someClass, t);
} finally {
mg.close();
final EntityManager mg = getEntityManager();
try {
final T t = mg.find(someClass, key);
+ if (t != null) {
+ mg.refresh(t);
+ }
return checkAndReturn(someClass, t);
} finally {
mg.close();
final EntityManager mg = getEntityManager();
try {
final T t = mg.find(someClass, key);
+ if (t != null) {
+ mg.refresh(t);
+ }
return checkAndReturn(someClass, t);
} finally {
mg.close();
metadata:
matchable: true
naming-recipe:
- type: String
+ type: string
required: true
name-operation:
type: string
description: Operational Policy for Control Loop execution
properties:
id:
- type: String
+ type: string
description: The unique control loop id.
required: true
timeout:
- type: Integer
+ type: integer
description: |
Overall timeout for executing all the operations. This timeout should equal or exceed the total
timeout for each operation listed.
required: true
abatement:
- type: Boolean
+ type: boolean
description: Whether an abatement event message will be expected for the control loop from DCAE.
required: true
default: false
trigger:
- type: String
+ type: string
description: Initial operation to execute upon receiving an Onset event message for the Control Loop.
required: true
operations:
- type: List
+ type: list
description: List of operations to be performed when Control Loop is triggered.
required: true
entry_schema:
description: Operational policies for Drools PDP
properties:
controllerName:
- type: String
+ type: string
description: Drools controller properties
required: false
description: Captures information of an operational failure performed for control loop
properties:
messages:
- type: String
+ type: string
description: error message
required: true
category:
- type: String
+ type: string
description: |
The category the error occurred in. Whether this is a general error from the actor, or the operation
timed out, retries were exhausted in trying to execute the operation, a guard policy prevented the
description: Definition for a entity in A&AI to perform a control loop operation on
properties:
targetType:
- type: String
+ type: string
description: Category for the target type
required: true
constraints:
- valid_values: [VNF, VM, VFMODULE, PNF]
entityIds:
- type: Map
+ type: map
description: |
Map of values that identify the resource. If none are provided, it is assumed that the
entity that generated the ONSET event will be the target.
description: An actor/operation/target definition
properties:
actor:
- type: String
+ type: string
description: The actor performing the operation.
required: true
operation:
- type: String
+ type: string
description: The operation the actor is performing.
required: true
target:
- type: String
+ type: string
description: The resource the operation should be performed on.
required: true
metadata:
clamp_possible_values: <string:see clamp project for syntax>
payload:
- type: Map
+ type: map
description: Name/value pairs of payload information passed by Policy to the actor
required: false
entry_schema:
- type: String
+ type: string
onap.datatype.controlloop.Operation:
derived_from: tosca.datatypes.Root
description: An operation supported by an actor
properties:
id:
- type: String
+ type: string
description: Unique identifier for the operation
required: true
description:
- type: String
+ type: string
description: A user-friendly description of the intent for the operation
required: false
operation:
metadata:
clamp_possible_values: <string:see clamp project for syntax>
timeout:
- type: Integer
+ type: integer
description: The amount of time for the actor to perform the operation.
required: true
retries:
- type: Integer
+ type: integer
description: The number of retries the actor should attempt to perform the operation.
required: true
default: 0
success:
- type: String
+ type: string
description: Points to the operation to invoke on success. A value of "final_success" indicates and end to the operation.
required: false
default: final_success
failure:
- type: String
+ type: string
description: Points to the operation to invoke on Actor operation failure.
required: false
default: final_failure
failure_timeout:
- type: String
+ type: string
description: Points to the operation to invoke when the time out for the operation occurs.
required: false
default: final_failure_timeout
failure_retries:
- type: String
+ type: string
description: Points to the operation to invoke when the current operation has exceeded its max retries.
required: false
default: final_failure_retries
failure_exception:
- type: String
+ type: string
description: Points to the operation to invoke when the current operation causes an exception.
required: false
default: final_failure_exception
failure_guard:
- type: String
+ type: string
description: Points to the operation to invoke when the current operation is blocked due to guard policy enforcement.
required: false
default: final_failure_guard
description: Operational policies for Apex PDP
properties:
engineServiceParameters:
- type: String
+ type: string
description: The engine parameters like name, instanceCount, policy implementation, parameters etc.
required: true
eventInputParameters:
- type: String
+ type: string
description: The event input parameters.
required: true
eventOutputParameters:
- type: String
+ type: string
description: The event output parameters.
required: true
javaProperties:
- type: String
+ type: string
description: Name/value pairs of properties to be set for APEX if needed.
required: false
required: false
default: "1.0.0"
id:
- type: int
+ type: integer
description: Specifies the engine id
required: true
instance_count:
- type: int
+ type: integer
description: Specifies the number of engine threads that should be run
required: true
deployment_port:
- type: int
+ type: integer
description: Specifies the port to connect to for engine administration
required: false
default: 1
type: string
description: The name of the file from which to read the APEX policy model
required: false
- default: ""
policy_type_impl:
type: string
description: The policy type implementation from which to read the APEX policy model
required: false
- default: ""
periodic_event_period:
type: string
description: The time interval in milliseconds for the periodic scanning event, 0 means don't scan
required: false
- default: 0
engine:
type: onap.datatypes.native.apex.engineservice.Engine
description: The parameters for all engines in the APEX engine service
description: Specifies a filter as a regular expression, events that do not match the filter are dropped, the default is to let all events through
required: false
synchronous_mode:
- type: bool
+ type: boolean
description: Specifies the event handler is syncronous (receive event and send response)
required: false
default: false
type: string
description: The peer event handler (output for input or input for output) of this event handler in synchronous mode, this parameter is mandatory if the event handler is in synchronous mode
required: false
- default: ""
synchronous_timeout:
- type: int
+ type: integer
description: The timeout in milliseconds for responses to be issued by APEX torequests, this parameter is mandatory if the event handler is in synchronous mode
required: false
- default: ""
requestor_mode:
- type: bool
+ type: boolean
description: Specifies the event handler is in requestor mode (send event and wait for response mode)
required: false
default: false
type: string
description: The peer event handler (output for input or input for output) of this event handler in requestor mode, this parameter is mandatory if the event handler is in requestor mode
required: false
- default: ""
requestor_timeout:
- type: int
+ type: integer
description: The timeout in milliseconds for wait for responses to requests, this parameter is mandatory if the event handler is in requestor mode
required: false
- default: ""
onap.datatypes.native.apex.CarrierTechnology:
derived_from: tosca.datatypes.Root
properties:
type: string
description: The class name of the class that overrides default handling of the event protocol for this carrier technology, defaults to the supplied event protocol class
required: false
- onap.datatypes.native.apex.Environmental:
+ onap.datatypes.native.apex.Environment:
derived_from: tosca.datatypes.Root
properties:
name:
entry_schema:
type: onap.datatypes.native.apex.Plugin
locking:
- type: onap.datatypes.native.apex.plugin
+ type: onap.datatypes.native.apex.Plugin
description: The plugin to be used for locking context in and between APEX PDPs at runtime
required: false
persistence:
version: 1.0.0
properties:
policy:
- type: String
+ type: string
required: true
description: The XML XACML 3.0 PolicySet or Policy
metadata:
+++ /dev/null
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 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.provider.impl;
-
-import static org.junit.Assert.assertEquals;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-
-import org.eclipse.persistence.config.PersistenceUnitProperties;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-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.ToscaEntityKey;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
-import org.yaml.snakeyaml.Yaml;
-
-/**
- * Test of the {@link AuthorativeToscaProvider} class.
- *
- * @author Liam Fallon (liam.fallon@est.tech)
- */
-public class AuthorativeToscaProviderReferenceTest {
- private static PfDao pfDao;
- private static final StandardCoder coder = new StandardCoder();
-
- // @formatter:off
- private static final String[] EXAMPLE_POLICY_TYPES = {
- "onap.policies.controlloop.guard.Blacklist#1.0.0"
- + "#policytypes/onap.policies.controlloop.guard.Blacklist.yaml",
- "onap.policies.controlloop.guard.coordination.FirstBlocksSecond#1.0.0"
- + "#policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml",
- "onap.policies.controlloop.guard.FrequencyLimiter#1.0.0"
- + "#policytypes/onap.policies.controlloop.guard.FrequencyLimiter.yaml",
- "onap.policies.controlloop.guard.MinMax#1.0.0"
- + "#policytypes/onap.policies.controlloop.guard.MinMax.yaml",
- "onap.policies.controlloop.operational.Common#1.0.0"
- + "#policytypes/onap.policies.controlloop.operational.Common.yaml",
- "onap.policies.controlloop.Operational#1.0.0"
- + "#policytypes/onap.policies.controlloop.Operational.yaml",
- "onap.policies.drools.Controller#1.0.0"
- + "#policytypes/onap.policies.drools.Controller.yaml",
- "onap.policies.monitoring.cdap.tca.hi.lo.app#1.0.0"
- + "#policytypes/onap.policies.monitoring.cdap.tca.hi.lo.app.yaml",
- "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server#1.0.0"
- + "#policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml",
- "onap.policies.Naming#1.0.0"
- + "#policytypes/onap.policies.Naming.yaml",
- "onap.policies.native.Apex#1.0.0"
- + "#policytypes/onap.policies.native.Apex.yaml",
- "onap.policies.native.Drools#1.0.0"
- + "#policytypes/onap.policies.native.Drools.yaml",
- "onap.policies.native.Xacml#1.0.0"
- + "#policytypes/onap.policies.native.Xacml.yaml",
- "onap.policies.optimization.resource.AffinityPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml",
- "onap.policies.optimization.resource.DistancePolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.DistancePolicy.yaml",
- "onap.policies.optimization.resource.HpaPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.HpaPolicy.yaml",
- "onap.policies.optimization.resource.OptimizationPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml",
- "onap.policies.optimization.resource.PciPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.PciPolicy.yaml",
- "onap.policies.optimization.resource.Vim_fit#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.Vim_fit.yaml",
- "onap.policies.optimization.resource.VnfPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.resource.VnfPolicy.yaml",
- "onap.policies.optimization.Resource#1.0.0"
- + "#policytypes/onap.policies.optimization.Resource.yaml",
- "onap.policies.optimization.service.QueryPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.service.QueryPolicy.yaml",
- "onap.policies.optimization.service.SubscriberPolicy#1.0.0"
- + "#policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml",
- "onap.policies.optimization.Service#1.0.0"
- + "#policytypes/onap.policies.optimization.Service.yaml",
- "onap.policies.Optimization#1.0.0"
- + "#policytypes/onap.policies.Optimization.yaml"};
- // @formatter:on
-
- private static final Map<ToscaEntityKey, ToscaServiceTemplate> policyTypeMap = new LinkedHashMap<>();
-
- /**
- * Set up the DAO towards the database.
- *
- * @throws Exception on database errors
- */
- @BeforeClass
- public static void beforeSetupDao() throws Exception {
- final DaoParameters daoParameters = new DaoParameters();
- daoParameters.setPluginClass(DefaultPfDao.class.getName());
-
- daoParameters.setPersistenceUnit("ToscaConceptTest");
-
- Properties jdbcProperties = new Properties();
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
-
- // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
-
- daoParameters.setJdbcProperties(jdbcProperties);
-
- pfDao = new PfDaoFactory().createPfDao(daoParameters);
- pfDao.init(daoParameters);
- }
-
- /**
- * Populate the database.
- *
- * @throws PfModelException on database exceptions
- * @throws CoderException on JSON encoding/decoding errors
- */
- @BeforeClass
- public static void beforeFillDatabase() throws PfModelException, CoderException {
- for (String policyTypeDataString : EXAMPLE_POLICY_TYPES) {
- String[] policyTypeDataArray = policyTypeDataString.split("#");
- String policyTypeYamlDefinition = ResourceUtils.getResourceAsString(policyTypeDataArray[2]);
-
- Object yamlObject = new Yaml().load(policyTypeYamlDefinition);
- String policyTypeJsonDefinition = coder.encode(yamlObject);
-
- ToscaServiceTemplate serviceTemplate = coder.decode(policyTypeJsonDefinition, ToscaServiceTemplate.class);
- policyTypeMap.put(new ToscaEntityKey(policyTypeDataArray[0], policyTypeDataArray[1]), serviceTemplate);
- new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
- }
- }
-
- @AfterClass
- public static void afterTeardown() {
- pfDao.close();
- }
-
- @Test
- public void testPolicyTypeRead() throws PfModelException, CoderException {
- for (Entry<ToscaEntityKey, ToscaServiceTemplate> policyTypeMapEntry : policyTypeMap.entrySet()) {
- ToscaServiceTemplate serviceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
- policyTypeMapEntry.getKey().getName(), policyTypeMapEntry.getKey().getVersion());
-
- assertEquals(1, serviceTemplate.getPolicyTypes().size());
-
- String originalJson = coder.encode(policyTypeMapEntry.getValue());
- String databaseJson = coder.encode(serviceTemplate);
-
- // TODO: This test has no chance of passing yet but must eventually pass to prove that the policy types
- // TODO: that were retrieved are the same as the policy types that were stored
- // assertEquals(originalJson, databaseJson);
- }
- }
-}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
+
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
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.LegacyOperationalPolicy;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Test the database models provider implementation.
private static final String ORDER = "DESC";
- private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class);
-
PolicyModelsProviderParameters parameters;
/**
@Test
public void testProviderMethodsNull() throws Exception {
- try (PolicyModelsProvider databaseProvider =
- new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
+ PolicyModelsProvider databaseProvider =
+ new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
- assertThatThrownBy(() -> {
- databaseProvider.getFilteredPolicyTypes(null);
- }).hasMessageMatching(FILTER_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getFilteredPolicyTypes(null);
+ }).hasMessageMatching(FILTER_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getFilteredPolicyTypeList(null);
- }).hasMessageMatching(FILTER_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getFilteredPolicyTypeList(null);
+ }).hasMessageMatching(FILTER_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.createPolicyTypes(null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.createPolicyTypes(null);
+ }).hasMessageMatching(TEMPLATE_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePolicyTypes(null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePolicyTypes(null);
+ }).hasMessageMatching(TEMPLATE_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deletePolicyType(null, null);
- }).hasMessageMatching(NAME_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePolicyType(null, null);
+ }).hasMessageMatching(NAME_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deletePolicyType("aaa", null);
- }).hasMessageMatching("^version is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePolicyType("aaa", null);
+ }).hasMessageMatching("^version is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.deletePolicyType(null, "aaa");
- }).hasMessageMatching(NAME_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePolicyType(null, "aaa");
+ }).hasMessageMatching(NAME_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getFilteredPolicies(null);
- }).hasMessageMatching(FILTER_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getFilteredPolicies(null);
+ }).hasMessageMatching(FILTER_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getFilteredPolicyList(null);
- }).hasMessageMatching(FILTER_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getFilteredPolicyList(null);
+ }).hasMessageMatching(FILTER_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.createPolicies(null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.createPolicies(null);
+ }).hasMessageMatching(TEMPLATE_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePolicies(null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePolicies(null);
+ }).hasMessageMatching(TEMPLATE_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deletePolicy(null, null);
- }).hasMessageMatching(NAME_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePolicy(null, null);
+ }).hasMessageMatching(NAME_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deletePolicy(null, "aaa");
- }).hasMessageMatching(NAME_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePolicy(null, "aaa");
+ }).hasMessageMatching(NAME_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deletePolicy("aaa", null);
- }).hasMessageMatching("^version is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePolicy("aaa", null);
+ }).hasMessageMatching("^version is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.getOperationalPolicy(null, null);
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getOperationalPolicy(null, null);
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getOperationalPolicy(null, "");
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getOperationalPolicy(null, "");
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getOperationalPolicy("", null);
- }).hasMessage("no policy found for policy: :null");
+ assertThatThrownBy(() -> {
+ databaseProvider.getOperationalPolicy("", null);
+ }).hasMessage("no policy found for policy: :null");
- assertThatThrownBy(() -> {
- databaseProvider.createOperationalPolicy(null);
- }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.createOperationalPolicy(null);
+ }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.updateOperationalPolicy(null);
- }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.updateOperationalPolicy(null);
+ }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.deleteOperationalPolicy(null, null);
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteOperationalPolicy(null, null);
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deleteOperationalPolicy(null, "");
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteOperationalPolicy(null, "");
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deleteOperationalPolicy("", null);
- }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteOperationalPolicy("", null);
+ }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.getGuardPolicy(null, null);
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getGuardPolicy(null, null);
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getGuardPolicy(null, "");
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getGuardPolicy(null, "");
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getGuardPolicy("", null);
- }).hasMessage("no policy found for policy: :null");
+ assertThatThrownBy(() -> {
+ databaseProvider.getGuardPolicy("", null);
+ }).hasMessage("no policy found for policy: :null");
- assertThatThrownBy(() -> {
- databaseProvider.createGuardPolicy(null);
- }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.createGuardPolicy(null);
+ }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.updateGuardPolicy(null);
- }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.updateGuardPolicy(null);
+ }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.deleteGuardPolicy(null, null);
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteGuardPolicy(null, null);
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deleteGuardPolicy(null, "");
- }).hasMessageMatching(POLICY_ID_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteGuardPolicy(null, "");
+ }).hasMessageMatching(POLICY_ID_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.deleteGuardPolicy("", null);
- }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteGuardPolicy("", null);
+ }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.getFilteredPdpGroups(null);
- }).hasMessageMatching(FILTER_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getFilteredPdpGroups(null);
+ }).hasMessageMatching(FILTER_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.createPdpGroups(null);
- }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.createPdpGroups(null);
+ }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.updatePdpGroups(null);
- }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdpGroups(null);
+ }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.updatePdpSubGroup(null, null);
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdpSubGroup(null, null);
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdpSubGroup(null, new PdpSubGroup());
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdpSubGroup(null, new PdpSubGroup());
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdpSubGroup(NAME, null);
- }).hasMessageMatching(SUBGROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdpSubGroup(NAME, null);
+ }).hasMessageMatching(SUBGROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup());
- }).hasMessage("parameter \"localName\" is null");
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup());
+ }).hasMessage("parameter \"localName\" is null");
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(null, null, null);
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(null, null, null);
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(null, null, new Pdp());
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(null, null, new Pdp());
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(null, "sub", null);
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(null, "sub", null);
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(null, "sub", new Pdp());
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(null, "sub", new Pdp());
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(NAME, null, null);
- }).hasMessageMatching(SUBGROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(NAME, null, null);
+ }).hasMessageMatching(SUBGROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(NAME, null, new Pdp());
- }).hasMessageMatching(SUBGROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(NAME, null, new Pdp());
+ }).hasMessageMatching(SUBGROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(NAME, "sub", null);
- }).hasMessageMatching("^pdp is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(NAME, "sub", null);
+ }).hasMessageMatching("^pdp is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.updatePdp(NAME, "sub", new Pdp());
- }).hasMessage("parameter \"localName\" is null");
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdp(NAME, "sub", new Pdp());
+ }).hasMessage("parameter \"localName\" is null");
- assertThatThrownBy(() -> {
- databaseProvider.deletePdpGroup(null);
- }).hasMessageMatching(NAME_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePdpGroup(null);
+ }).hasMessageMatching(NAME_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0);
- }).hasMessageMatching(GROUP_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0);
+ }).hasMessageMatching(GROUP_IS_NULL);
- assertThatThrownBy(() -> {
- databaseProvider.createPdpStatistics(null);
- }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.createPdpStatistics(null);
+ }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.updatePdpStatistics(null);
- }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePdpStatistics(null);
+ }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
- assertThatThrownBy(() -> {
- databaseProvider.deletePdpStatistics(null, TIMESTAMP);
- }).hasMessageMatching(NAME_IS_NULL);
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePdpStatistics(null, TIMESTAMP);
+ }).hasMessageMatching(NAME_IS_NULL);
- }
+ databaseProvider.close();
}
@Test
}
@Test
- public void testProviderMethods() {
- try (PolicyModelsProvider databaseProvider =
- new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
-
- assertTrue(databaseProvider.getPolicyTypes(NAME, VERSION_100).getPolicyTypes().isEmpty());
- assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty());
- assertEquals(0, databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())
- .getPolicyTypes().size());
- assertEquals(0, databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).size());
-
- assertThatThrownBy(() -> {
- databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
- }).hasMessage("no policy types specified on service template");
-
- assertThatThrownBy(() -> {
- databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
- }).hasMessage("no policy types specified on service template");
-
- assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
-
- assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
-
- assertTrue(
- databaseProvider.getPolicies(NAME, VERSION_100).getToscaTopologyTemplate().getPolicies().isEmpty());
- assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty());
- assertEquals(0, databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())
- .getToscaTopologyTemplate().getPolicies().size());
- assertEquals(0, databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).size());
-
- assertThatThrownBy(() -> {
- databaseProvider.createPolicies(new ToscaServiceTemplate());
- }).hasMessage("topology template not specified on service template");
-
- assertThatThrownBy(() -> {
- databaseProvider.updatePolicies(new ToscaServiceTemplate());
- }).hasMessage("topology template not specified on service template");
-
- assertTrue(databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate().getPolicies()
- .isEmpty());
-
- assertThatThrownBy(() -> {
- databaseProvider.getOperationalPolicy(POLICY_ID, null);
- }).hasMessage("no policy found for policy: policy_id:null");
-
- assertThatThrownBy(() -> {
- databaseProvider.getOperationalPolicy(POLICY_ID, "10");
- }).hasMessage("no policy found for policy: policy_id:10");
-
- assertThatThrownBy(() -> {
- databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
- }).hasMessageMatching(NAME_IS_NULL);
-
- assertThatThrownBy(() -> {
- databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
- }).hasMessageMatching(NAME_IS_NULL);
-
- assertThatThrownBy(() -> {
- databaseProvider.deleteOperationalPolicy(POLICY_ID, "55");
- }).hasMessage("no policy found for policy: policy_id:55");
-
- assertThatThrownBy(() -> {
- databaseProvider.getGuardPolicy(POLICY_ID, null);
- }).hasMessage("no policy found for policy: policy_id:null");
-
- assertThatThrownBy(() -> {
- databaseProvider.getGuardPolicy(POLICY_ID, "6");
- }).hasMessage("no policy found for policy: policy_id:6");
-
- assertThatThrownBy(() -> {
- databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput());
- }).hasMessage("policy type for guard policy \"null\" unknown");
-
- assertThatThrownBy(() -> {
- databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput());
- }).hasMessage("policy type for guard policy \"null\" unknown");
-
- assertThatThrownBy(() -> {
- databaseProvider.deleteGuardPolicy(POLICY_ID, "33");
- }).hasMessage("no policy found for policy: policy_id:33");
-
- assertEquals(0, databaseProvider.getPdpGroups(NAME).size());
- assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
-
- assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
- assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
-
- PdpGroup pdpGroup = new PdpGroup();
- pdpGroup.setName(GROUP);
- pdpGroup.setVersion("1.2.3");
- pdpGroup.setPdpGroupState(PdpState.ACTIVE);
- pdpGroup.setPdpSubgroups(new ArrayList<>());
- List<PdpGroup> groupList = new ArrayList<>();
- groupList.add(pdpGroup);
-
- PdpSubGroup pdpSubGroup = new PdpSubGroup();
- pdpSubGroup.setPdpType("type");
- pdpSubGroup.setDesiredInstanceCount(123);
- pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
- pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9"));
- pdpGroup.getPdpSubgroups().add(pdpSubGroup);
-
- Pdp pdp = new Pdp();
- pdp.setInstanceId("type-0");
- pdp.setMessage("Hello");
- pdp.setPdpState(PdpState.ACTIVE);
- pdp.setHealthy(PdpHealthStatus.UNKNOWN);
- pdpSubGroup.setPdpInstances(new ArrayList<>());
- pdpSubGroup.getPdpInstances().add(pdp);
-
- PdpStatistics pdpStatistics = new PdpStatistics();
- pdpStatistics.setPdpInstanceId(NAME);
- pdpStatistics.setTimeStamp(new Date());
- pdpStatistics.setPdpGroupName(GROUP);
- pdpStatistics.setPdpSubGroupName("type");
- ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>();
- statisticsArrayList.add(pdpStatistics);
-
- assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
- .getDesiredInstanceCount());
- assertEquals(1, databaseProvider.getPdpGroups(GROUP).size());
-
- pdpSubGroup.setDesiredInstanceCount(234);
- databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup);
- assertEquals(234,
- databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
-
- assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
- .get(0).getMessage());
- pdp.setMessage("Howdy");
- databaseProvider.updatePdp(GROUP, "type", pdp);
- assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
- .get(0).getMessage());
-
- assertThatThrownBy(() -> {
- databaseProvider.deletePdpGroup(NAME);
- }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
-
- assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName());
-
- assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
- assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size());
- assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size());
-
- assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId());
- assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0)
- .getPdpInstanceId());
- assertEquals(0,
- databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size());
- assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0)
- .get(0).getPdpInstanceId());
- assertEquals(0, databaseProvider
- .getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size());
-
- assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0)
- .getPdpInstanceId());
- assertEquals(0, databaseProvider
- .getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size());
-
- assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0)
- .get(0).getPdpInstanceId());
- assertEquals(0, databaseProvider
- .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0).size());
-
- assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1)
- .get(0).getPdpInstanceId());
- assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5)
- .get(0).getPdpInstanceId());
- assertEquals(0, databaseProvider
- .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5).size());
-
- assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId());
- assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
- } catch (Exception exc) {
- LOGGER.warn("test should not throw an exception", exc);
- fail("test should not throw an exception");
- }
+ public void testProviderMethods() throws PfModelException {
+ PolicyModelsProvider databaseProvider =
+ new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+ assertThatThrownBy(() -> databaseProvider.getPolicyTypes(NAME, VERSION_100))
+ .hasMessage("service template not found in database");
+
+ assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty());
+
+ assertThatThrownBy(() -> databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()))
+ .hasMessage("service template not found in database");
+
+ assertTrue(databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).isEmpty());
+
+ assertThatThrownBy(() -> {
+ databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
+ }).hasMessage("no policy types specified on service template");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
+ }).hasMessage("no policy types specified on service template");
+
+ assertThatThrownBy(() -> databaseProvider.deletePolicyType(NAME, VERSION_100))
+ .hasMessage("service template not found in database");
+
+ assertThatThrownBy(() -> databaseProvider.getPolicies(NAME, VERSION_100))
+ .hasMessage("service template not found in database");
+
+ assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty());
+
+ assertThatThrownBy(() -> databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()))
+ .hasMessage("service template not found in database");
+
+ assertTrue(databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).isEmpty());
+
+ assertThatThrownBy(() -> {
+ databaseProvider.createPolicies(new ToscaServiceTemplate());
+ }).hasMessage("topology template not specified on service template");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.updatePolicies(new ToscaServiceTemplate());
+ }).hasMessage("topology template not specified on service template");
+
+ assertThatThrownBy(() -> databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate())
+ .hasMessage("service template not found in database");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.getOperationalPolicy(POLICY_ID, null);
+ }).hasMessage("no policy found for policy: policy_id:null");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.getOperationalPolicy(POLICY_ID, "10");
+ }).hasMessage("no policy found for policy: policy_id:10");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
+ }).hasMessageMatching(NAME_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
+ }).hasMessageMatching(NAME_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteOperationalPolicy(POLICY_ID, "55");
+ }).hasMessage("no policy found for policy: policy_id:55");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.getGuardPolicy(POLICY_ID, null);
+ }).hasMessage("no policy found for policy: policy_id:null");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.getGuardPolicy(POLICY_ID, "6");
+ }).hasMessage("no policy found for policy: policy_id:6");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput());
+ }).hasMessage("policy type for guard policy \"null\" unknown");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput());
+ }).hasMessage("policy type for guard policy \"null\" unknown");
+
+ assertThatThrownBy(() -> {
+ databaseProvider.deleteGuardPolicy(POLICY_ID, "33");
+ }).hasMessage("no policy found for policy: policy_id:33");
+
+ assertEquals(0, databaseProvider.getPdpGroups(NAME).size());
+ assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
+
+ assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
+ assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
+
+ PdpGroup pdpGroup = new PdpGroup();
+ pdpGroup.setName(GROUP);
+ pdpGroup.setVersion("1.2.3");
+ pdpGroup.setPdpGroupState(PdpState.ACTIVE);
+ pdpGroup.setPdpSubgroups(new ArrayList<>());
+ List<PdpGroup> groupList = new ArrayList<>();
+ groupList.add(pdpGroup);
+
+ PdpSubGroup pdpSubGroup = new PdpSubGroup();
+ pdpSubGroup.setPdpType("type");
+ pdpSubGroup.setDesiredInstanceCount(123);
+ pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
+ pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9"));
+ pdpGroup.getPdpSubgroups().add(pdpSubGroup);
+
+ Pdp pdp = new Pdp();
+ pdp.setInstanceId("type-0");
+ pdp.setMessage("Hello");
+ pdp.setPdpState(PdpState.ACTIVE);
+ pdp.setHealthy(PdpHealthStatus.UNKNOWN);
+ pdpSubGroup.setPdpInstances(new ArrayList<>());
+ pdpSubGroup.getPdpInstances().add(pdp);
+
+ PdpStatistics pdpStatistics = new PdpStatistics();
+ pdpStatistics.setPdpInstanceId(NAME);
+ pdpStatistics.setTimeStamp(new Date());
+ pdpStatistics.setPdpGroupName(GROUP);
+ pdpStatistics.setPdpSubGroupName("type");
+ ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>();
+ statisticsArrayList.add(pdpStatistics);
+
+ assertEquals(123,
+ databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
+ assertEquals(1, databaseProvider.getPdpGroups(GROUP).size());
+
+ pdpSubGroup.setDesiredInstanceCount(234);
+ databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup);
+ assertEquals(234,
+ databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
+
+ assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
+ .get(0).getMessage());
+ pdp.setMessage("Howdy");
+ databaseProvider.updatePdp(GROUP, "type", pdp);
+ assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
+ .get(0).getMessage());
+
+ assertThatThrownBy(() -> {
+ databaseProvider.deletePdpGroup(NAME);
+ }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
+
+ assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName());
+
+ assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
+ assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size());
+ assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size());
+
+ assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId());
+ assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0)
+ .getPdpInstanceId());
+ assertEquals(0,
+ databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size());
+ assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0)
+ .get(0).getPdpInstanceId());
+ assertEquals(0,
+ databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size());
+
+ assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0)
+ .getPdpInstanceId());
+ assertEquals(0,
+ databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size());
+
+ assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0).get(0)
+ .getPdpInstanceId());
+ assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0)
+ .size());
+
+ assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1).get(0)
+ .getPdpInstanceId());
+ assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5).get(0)
+ .getPdpInstanceId());
+ assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5)
+ .size());
+
+ assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId());
+ assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
+
+ databaseProvider.close();
}
}
parameters.setDatabaseUrl("jdbc:dummy");
parameters.setPersistenceUnit("dummy");
- try (PolicyModelsProvider dummyProvider =
- new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
- dummyProvider.init();
-
- assertNotNull(dummyProvider.getPolicyTypes("name", VERSION));
- assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()));
- assertNotNull(dummyProvider.getPolicyTypeList("name", VERSION));
- assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()));
- assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate()));
- assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate()));
- assertNotNull(dummyProvider.deletePolicyType("name", VERSION));
-
- assertNotNull(dummyProvider.getPolicies("name", VERSION));
- assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()));
- assertNotNull(dummyProvider.getPolicyList("name", VERSION));
- assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()));
- assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate()));
- assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate()));
- assertNotNull(dummyProvider.deletePolicy("name", VERSION));
-
- assertNotNull(dummyProvider.getOperationalPolicy(POLICY_ID, "1"));
- assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
- assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
- assertNotNull(dummyProvider.deleteOperationalPolicy(POLICY_ID, "1"));
-
- assertNotNull(dummyProvider.getGuardPolicy(POLICY_ID, "1"));
- assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput()));
- assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput()));
- assertNotNull(dummyProvider.deleteGuardPolicy(POLICY_ID, "1"));
-
- assertTrue(dummyProvider.getPdpGroups("name").isEmpty());
- assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty());
- assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty());
- assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty());
- assertNull(dummyProvider.deletePdpGroup("name"));
-
- dummyProvider.updatePdpSubGroup("name", new PdpSubGroup());
- dummyProvider.updatePdp("name", "type", new Pdp());
- dummyProvider.updatePdpStatistics(new ArrayList<>());
- assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty());
- }
+ PolicyModelsProvider dummyProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+ dummyProvider.init();
+
+ assertNotNull(dummyProvider.getPolicyTypes("name", VERSION));
+ assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()));
+ assertNotNull(dummyProvider.getPolicyTypeList("name", VERSION));
+ assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()));
+ assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate()));
+ assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate()));
+ assertNotNull(dummyProvider.deletePolicyType("name", VERSION));
+
+ assertNotNull(dummyProvider.getPolicies("name", VERSION));
+ assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()));
+ assertNotNull(dummyProvider.getPolicyList("name", VERSION));
+ assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()));
+ assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate()));
+ assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate()));
+ assertNotNull(dummyProvider.deletePolicy("name", VERSION));
+
+ assertNotNull(dummyProvider.getOperationalPolicy(POLICY_ID, "1"));
+ assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
+ assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
+ assertNotNull(dummyProvider.deleteOperationalPolicy(POLICY_ID, "1"));
+
+ assertNotNull(dummyProvider.getGuardPolicy(POLICY_ID, "1"));
+ assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput()));
+ assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput()));
+ assertNotNull(dummyProvider.deleteGuardPolicy(POLICY_ID, "1"));
+
+ assertTrue(dummyProvider.getPdpGroups("name").isEmpty());
+ assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty());
+ assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty());
+ assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty());
+ assertNull(dummyProvider.deletePdpGroup("name"));
+
+ dummyProvider.updatePdpSubGroup("name", new PdpSubGroup());
+ dummyProvider.updatePdp("name", "type", new Pdp());
+ dummyProvider.updatePdpStatistics(new ArrayList<>());
+ assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty());
}
@Test
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
@Test
- public void testPolicyPersistence() throws Exception {
+ public void testLegacyGuardPolicyPersistence() throws Exception {
for (int i = 0; i < policyInputResourceNames.length; i++) {
String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
@Test
- public void testPolicyPersistence() throws Exception {
+ public void testLegacyOperationalPolicyPersistence() throws Exception {
for (int i = 0; i < policyInputResourceNames.length; i++) {
String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]);
String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]);
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
@Test
- public void testPolicyPersistence() throws Exception {
+ public void testToscaPolicyPersistence() throws Exception {
Set<String> policyResources = ResourceUtils.getDirectoryContents("policies");
for (String policyResource : policyResources) {
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.models.provider.impl;
+import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Base64;
-import java.util.List;
import java.util.Set;
-import lombok.NonNull;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
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.ToscaPolicyType;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.yaml.snakeyaml.Yaml;
/**
* Test persistence of monitoring policies to and from the database.
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class PolicyTypePersistenceTest {
- private StandardCoder standardCoder;
-
+ private YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
private PolicyModelsProvider databaseProvider;
/**
databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
}
- /**
- * Set up standard coder.
- */
- @Before
- public void setupStandardCoder() {
- standardCoder = new StandardCoder();
- }
-
@After
public void teardown() throws Exception {
databaseProvider.close();
public void testPolicyTypePersistence() throws Exception {
Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
+ ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
+
for (String policyTypeFilePath : policyTypeDirectoryContents) {
String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
- testYamlStringPolicyTypePersistence(policyTypeString);
- }
- }
-
- private void testYamlStringPolicyTypePersistence(final String policyTypeString) throws Exception {
- Object yamlObject = new Yaml().load(policyTypeString);
- String yamlAsJsonString = new StandardCoder().encode(yamlObject);
- testJsonStringPolicyTypePersistence(yamlAsJsonString);
- }
-
- /**
- * Check persistence of a policy.
- *
- * @param policyTypeString the policy as a string
- * @throws Exception any exception thrown
- */
- public void testJsonStringPolicyTypePersistence(@NonNull final String policyTypeString) throws Exception {
- ToscaServiceTemplate serviceTemplate = standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
-
- assertNotNull(serviceTemplate);
- ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().values().iterator().next();
-
- databaseProvider.createPolicyTypes(serviceTemplate);
- checkPolicyTypePersistence(inPolicyType);
-
- databaseProvider.updatePolicyTypes(serviceTemplate);
- checkPolicyTypePersistence(inPolicyType);
- }
+ ToscaServiceTemplate foundPolicyTypeSt =
+ yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
+
+ serviceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
+ serviceTemplate.setDescription(foundPolicyTypeSt.getDescription());
+ serviceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
+ serviceTemplate.setName(foundPolicyTypeSt.getName());
+ serviceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
+ serviceTemplate.setToscaTopologyTemplate(foundPolicyTypeSt.getToscaTopologyTemplate());
+ serviceTemplate.setVersion(foundPolicyTypeSt.getVersion());
+
+ if (foundPolicyTypeSt.getDataTypes() != null) {
+ if (serviceTemplate.getDataTypes() == null) {
+ serviceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
+ } else {
+ serviceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
+ }
+ }
+
+ if (serviceTemplate.getPolicyTypes() == null) {
+ serviceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
+ } else {
+ serviceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
+ }
+ }
- private void checkPolicyTypePersistence(ToscaPolicyType inPolicyType) throws PfModelException {
- List<ToscaPolicyType> policyTypeList =
- databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion());
+ assertThatCode(() -> databaseProvider.createPolicyTypes(serviceTemplate)).doesNotThrowAnyException();
- policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder()
- .name(inPolicyType.getName()).version(inPolicyType.getVersion()).build());
+ ToscaEntityKey resourceOptimizationPtKey =
+ new ToscaEntityKey("onap.policies.optimization.resource.OptimizationPolicy", "1.0.0");
- assertEquals(1, policyTypeList.size());
- assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
+ ToscaServiceTemplate resOptPolicyTypeSt = databaseProvider.getPolicyTypes(resourceOptimizationPtKey.getName(),
+ resourceOptimizationPtKey.getVersion());
- policyTypeList = databaseProvider
- .getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().name(inPolicyType.getName()).build());
+ assertEquals(3, resOptPolicyTypeSt.getPolicyTypesAsMap().size());
+ assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourceOptimizationPtKey));
- assertEquals(1, policyTypeList.size());
- assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
+ ToscaEntityKey resourcePtKey = new ToscaEntityKey("onap.policies.optimization.Resource", "1.0.0");
+ assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourcePtKey));
- policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build());
- assertTrue(policyTypeList.size() <= 3);
- assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName());
+ ToscaEntityKey optimizationPtKey = new ToscaEntityKey("onap.policies.Optimization", "1.0.0");
+ assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(optimizationPtKey));
- for (ToscaPolicyType policyType : databaseProvider.getPolicyTypeList(null, null)) {
- databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion());
- }
+ assertEquals(2, resOptPolicyTypeSt.getDataTypesAsMap().size());
}
}
<class>org.onap.policy.models.dao.converters.Uuid2String</class>
<class>org.onap.policy.models.base.PfConceptKey</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class>
- <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class>
<class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class>
package org.onap.policy.models.tosca.authorative.provider;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
+import javax.ws.rs.core.Response.Status;
+
import lombok.NonNull;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
- List<ToscaPolicyType> policyTypeList = new ArrayList<>(
- new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values());
+ List<ToscaPolicyType> policyTypeList;
+
+ try {
+ policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version)
+ .toAuthorative().getPolicyTypes().values());
+ } catch (PfModelRuntimeException pfme) {
+ return handlePfModelRuntimeException(pfme);
+ }
LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
return policyTypeList;
LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
filteredPolicyTypeList);
+
return filteredPolicyTypeList;
}
throws PfModelException {
LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
- List<ToscaPolicy> policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version)
- .toAuthorative().getToscaTopologyTemplate().getPolicies());
+ List<ToscaPolicy> policyList;
+
+ try {
+ policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative()
+ .getToscaTopologyTemplate().getPolicies());
+ } catch (PfModelRuntimeException pfme) {
+ return handlePfModelRuntimeException(pfme);
+ }
LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
return policyList;
return conceptMap;
}
+
+ /**
+ * Handle a PfModelRuntimeException on a list call.
+ *
+ * @param pfme the model exception
+ * @return an empty list on 404
+ */
+ private <T extends ToscaEntity> List<T> handlePfModelRuntimeException(final PfModelRuntimeException pfme) {
+ if (Status.NOT_FOUND.equals(pfme.getErrorResponse().getResponseCode())) {
+ return Collections.emptyList();
+ } else {
+ throw pfme;
+ }
+ }
}
* ONAP Policy Model
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.models.tosca.simple.concepts;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
+import org.apache.commons.collections4.CollectionUtils;
import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
/**
* Class to represent custom data type in TOSCA definition.
private static final long serialVersionUID = -3922690413436539164L;
@ElementCollection
- private List<JpaToscaConstraint> constraints;
+ private List<JpaToscaConstraint> constraints = new ArrayList<>();
@ElementCollection
@Lob
- private Map<String, JpaToscaProperty> properties;
+ private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
/**
* The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
return 0;
}
+
+ /**
+ * Get the data types referenced in a data type.
+ *
+ * @return the data types referenced in a data type
+ */
+ public Collection<PfConceptKey> getReferencedDataTypes() {
+ if (properties == null) {
+ return CollectionUtils.emptyCollection();
+ }
+
+ Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
+
+ for (JpaToscaProperty property : properties.values()) {
+ referencedDataTypes.add(property.getType());
+
+ if (property.getEntrySchema() != null) {
+ referencedDataTypes.add(property.getEntrySchema().getType());
+ }
+ }
+
+ referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
+
+ return referencedDataTypes;
+ }
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
+
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.EmbeddedId;
import javax.persistence.MappedSuperclass;
+
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
+
import org.apache.commons.lang3.ObjectUtils;
import org.onap.policy.common.utils.validation.ParameterValidationUtils;
import org.onap.policy.models.base.PfAuthorative;
private PfConceptKey derivedFrom;
@ElementCollection
- private Map<String, String> metadata;
+ private Map<String, String> metadata = new TreeMap<>();
@Column
private String description;
key.setVersion(toscaEntity.getVersion());
}
-
if (toscaEntity.getDerivedFrom() != null) {
// CHeck if the derived from field contains a name-version ID
if (toscaEntity.getDerivedFrom().contains(":")) {
* ONAP Policy Model
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.models.tosca.simple.concepts;
+import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.ws.rs.core.Response;
+
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
+
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.validation.ParameterValidationUtils;
private Map<String, String> properties;
@ElementCollection
- private List<PfConceptKey> targets;
+ private List<PfConceptKey> targets = new ArrayList<>();
// @formatter:on
/**
if (toscaPolicy.getType() != null) {
type.setName(toscaPolicy.getType());
- }
- else {
+ } else {
type.setName(PfKey.NULL_KEY_NAME);
}
if (toscaPolicy.getTypeVersion() != null) {
type.setVersion(toscaPolicy.getTypeVersion());
- }
- else {
+ } else {
type.setVersion(PfKey.NULL_KEY_VERSION);
}
package org.onap.policy.models.tosca.simple.concepts;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ElementCollection
@Lob
- private Map<String, JpaToscaProperty> properties;
+ private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
@ElementCollection
- private List<PfConceptKey> targets;
+ private List<PfConceptKey> targets = new ArrayList<>();
@ElementCollection
- private List<JpaToscaTrigger> triggers;
+ private List<JpaToscaTrigger> triggers = new ArrayList<>();
/**
* The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
PfValidationResult result = super.validate(resultIn);
+ // Check that all ancestors of this policy type exist
for (JpaToscaPolicyType policyType : this.getConceptMap().values()) {
ToscaUtils.getEntityTypeAncestors(this, policyType, result);
}
import com.google.gson.annotations.SerializedName;
+import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@SerializedName("policy_types")
private JpaToscaPolicyTypes policyTypes;
- @Column
+ @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
+ @JoinColumns(
+ {
+ @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName"),
+ @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
+ @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName"),
+ @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName")
+ }
+ )
@SerializedName("topology_template")
private JpaToscaTopologyTemplate topologyTemplate;
// @formatter:on
return result;
}
- validateDatatypesInPolicyTypes(result);
+ validateReferencedDataTypes(result);
return validatePolicyTypesInPolicies(result);
}
* @param result the validation result object to use for the validation result
* @return the validation result object
*/
- private PfValidationResult validateDatatypesInPolicyTypes(final PfValidationResult result) {
+ private PfValidationResult validateReferencedDataTypes(final PfValidationResult result) {
if (policyTypes == null) {
return result;
}
- for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
- for (PfConceptKey datatypeKey : policyType.getReferencedDataTypes()) {
- if (dataTypes == null || dataTypes.get(datatypeKey) == null) {
- result.addValidationMessage(
- new PfValidationMessage(policyType.getKey(), this.getClass(), ValidationResult.INVALID,
- "data type " + datatypeKey + " referenced in policy type not found"));
- }
+ if (dataTypes != null) {
+ for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
+ validateReferencedDataTypesExists(dataType.getKey(), dataType.getReferencedDataTypes(), result);
}
}
+ for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
+ validateReferencedDataTypesExists(policyType.getKey(), policyType.getReferencedDataTypes(), result);
+ }
+
return result;
}
+ /**
+ * Validate that the referenced data types exist for a collection of data type keys.
+ *
+ * @param referencingEntityKey the key of the referencing entity
+ * @param dataTypeKeyCollection the data type key collection
+ * @param result the result of the validation
+ */
+ private void validateReferencedDataTypesExists(final PfConceptKey referencingEntityKey,
+ final Collection<PfConceptKey> dataTypeKeyCollection, final PfValidationResult result) {
+ for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
+ if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
+ result.addValidationMessage(new PfValidationMessage(referencingEntityKey, this.getClass(),
+ ValidationResult.INVALID, "referenced data type " + dataTypeKey.getId() + " not found"));
+ }
+ }
+ }
+
/**
* Validate that all policy types referenced in policies exist.
*
* @return the validation result object
*/
private PfValidationResult validatePolicyTypesInPolicies(PfValidationResult result) {
- if (topologyTemplate == null || topologyTemplate.getPolicies() == null) {
+ if (topologyTemplate == null || topologyTemplate.getPolicies() == null
+ || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
return result;
}
- if (policyTypes == null) {
+ if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
result.addValidationMessage(new PfValidationMessage(this.getKey(), this.getClass(),
ValidationResult.INVALID,
"no policy types are defined on the service template for the policies in the topology template"));
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Column(name = "description")
private String description;
+ // @formatter:off
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
+ @JoinColumns(
+ {
+ @JoinColumn(name = "policyName", referencedColumnName = "name"),
+ @JoinColumn(name = "policyVersion", referencedColumnName = "version")
+ }
+ )
+ // @formatter:on
private JpaToscaPolicies policies;
/**
package org.onap.policy.models.tosca.simple.provider;
import java.util.ArrayList;
-import java.util.LinkedHashMap;
+import java.util.Collection;
import java.util.List;
-import java.util.Map;
import javax.ws.rs.core.Response;
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.base.PfValidationResult;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
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.ToscaServiceTemplateUtils;
import org.onap.policy.models.tosca.utils.ToscaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SimpleToscaProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
+ // Recurring string constants
+ private static final String SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE = "service template not found in database";
+ private static final String DO_NOT_EXIST = " do not exist";
+
+ /**
+ * Get Service Template.
+ *
+ * @param dao the DAO to use to access the database
+ * @return the service template
+ * @throws PfModelException on errors getting the service template
+ */
+ public JpaToscaServiceTemplate getServiceTemplate(@NonNull final PfDao dao) throws PfModelException {
+ LOGGER.debug("->getServiceTemplate");
+
+ JpaToscaServiceTemplate serviceTemplate = new SimpleToscaServiceTemplateProvider().read(dao);
+ if (serviceTemplate == null) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND, SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE);
+ }
+
+ LOGGER.debug("<-getServiceTemplate: serviceTemplate={}", serviceTemplate);
+ return serviceTemplate;
+ }
+
+ /**
+ * Append a service template fragment to the service template in the database.
+ *
+ * @param dao the DAO to use to access the database
+ * @param incomingServiceTemplateFragment the service template containing the definition of the entities to be
+ * created
+ * @return the TOSCA service template in the database after the operation
+ * @throws PfModelException on errors appending a service template to the template in the database
+ */
+ public JpaToscaServiceTemplate appendToServiceTemplate(@NonNull final PfDao dao,
+ @NonNull final JpaToscaServiceTemplate incomingServiceTemplateFragment) throws PfModelException {
+ LOGGER.debug("->appendServiceTemplateFragment: incomingServiceTemplateFragment={}",
+ incomingServiceTemplateFragment);
+
+ JpaToscaServiceTemplate dbServiceTemplate = new SimpleToscaServiceTemplateProvider().read(dao);
+
+ JpaToscaServiceTemplate serviceTemplateToWrite;
+ if (dbServiceTemplate == null) {
+ serviceTemplateToWrite = incomingServiceTemplateFragment;
+ } else {
+ serviceTemplateToWrite =
+ ToscaServiceTemplateUtils.addFragment(dbServiceTemplate, incomingServiceTemplateFragment);
+ }
+
+ PfValidationResult result = serviceTemplateToWrite.validate(new PfValidationResult());
+ if (!result.isValid()) {
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.toString());
+ }
+
+ new SimpleToscaServiceTemplateProvider().write(dao, serviceTemplateToWrite);
+
+ LOGGER.debug("<-appendServiceTemplateFragment: returnServiceTempalate={}", serviceTemplateToWrite);
+ return serviceTemplateToWrite;
+ }
+
/**
* Get data types.
*
throws PfModelException {
LOGGER.debug("->getDataTypes: name={}, version={}", name, version);
- // Create the structure of the TOSCA service template to contain the data type
- JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
- serviceTemplate.setDataTypes(new JpaToscaDataTypes());
+ JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+ if (!ToscaUtils.doDataTypesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "data types for " + name + ":" + version + DO_NOT_EXIST);
+ }
- // Add the data type to the TOSCA service template
- List<JpaToscaDataType> jpaDataTypeList = dao.getFiltered(JpaToscaDataType.class, name, version);
- serviceTemplate.getDataTypes().getConceptMap().putAll(asConceptMap(jpaDataTypeList));
+ serviceTemplate.setPolicyTypes(null);
+ serviceTemplate.setTopologyTemplate(null);
+
+ ToscaUtils.getEntityTree(serviceTemplate.getDataTypes(), name, version);
+
+ if (!ToscaUtils.doDataTypesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "data types for " + name + ":" + version + DO_NOT_EXIST);
+ }
+
+ for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getConceptMap().values()) {
+ Collection<PfConceptKey> referencedDataTypeKeys = dataType.getReferencedDataTypes();
+
+ for (PfConceptKey referencedDataTypeKey : referencedDataTypeKeys) {
+ JpaToscaServiceTemplate dataTypeEntityTreeServiceTemplate =
+ getDataTypes(dao, referencedDataTypeKey.getName(), referencedDataTypeKey.getVersion());
+
+ serviceTemplate =
+ ToscaServiceTemplateUtils.addFragment(serviceTemplate, dataTypeEntityTreeServiceTemplate);
+ }
+ }
LOGGER.debug("<-getDataTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
return serviceTemplate;
* Create data types.
*
* @param dao the DAO to use to access the database
- * @param serviceTemplate the service template containing the definition of the data types to be created
+ * @param incomingServiceTemplate the service template containing the definition of the data types to be created
* @return the TOSCA service template containing the created data types
* @throws PfModelException on errors creating data types
*/
public JpaToscaServiceTemplate createDataTypes(@NonNull final PfDao dao,
- @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
- LOGGER.debug("->createDataTypes: serviceTempalate={}", serviceTemplate);
-
- ToscaUtils.assertDataTypesExist(serviceTemplate);
-
- for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) {
- dao.create(dataType);
- }
+ @NonNull final JpaToscaServiceTemplate incomingServiceTemplate) throws PfModelException {
+ LOGGER.debug("->createDataTypes: incomingServiceTemplate={}", incomingServiceTemplate);
- // Return the created Data types
- JpaToscaDataTypes returnDataTypes = new JpaToscaDataTypes();
+ ToscaUtils.assertDataTypesExist(incomingServiceTemplate);
- for (PfConceptKey dataTypeKey : serviceTemplate.getDataTypes().getConceptMap().keySet()) {
- returnDataTypes.getConceptMap().put(dataTypeKey, dao.get(JpaToscaDataType.class, dataTypeKey));
- }
+ JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, incomingServiceTemplate);
- JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
- returnServiceTemplate.setDataTypes(returnDataTypes);
-
- LOGGER.debug("<-createDataTypes: returnServiceTempalate={}", returnServiceTemplate);
- return returnServiceTemplate;
+ LOGGER.debug("<-createDataTypes: returnServiceTempalate={}", writtenServiceTemplate);
+ return writtenServiceTemplate;
}
/**
throws PfModelException {
LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
- // Create the structure of the TOSCA service template to contain the policy type
- JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
- serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+ JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+ serviceTemplate.setDataTypes(null);
+ serviceTemplate.setTopologyTemplate(null);
+
+ if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policy types for " + name + ":" + version + DO_NOT_EXIST);
+ }
+
+ ToscaUtils.getEntityTree(serviceTemplate.getPolicyTypes(), name, version);
- // Add the policy type to the TOSCA service template
- List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
- serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
+ if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policy types for " + name + ":" + version + DO_NOT_EXIST);
+ }
+
+ for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getConceptMap().values()) {
+ Collection<PfConceptKey> referencedDataTypeKeys = policyType.getReferencedDataTypes();
- // Return all data types
- // TODO: In an upcoming review, return just the data types used by the policy types on the policy type list
- List<JpaToscaDataType> jpaDataTypeList = dao.getFiltered(JpaToscaDataType.class, null, null);
- if (!CollectionUtils.isEmpty(jpaDataTypeList)) {
- serviceTemplate.setDataTypes(new JpaToscaDataTypes());
- serviceTemplate.getDataTypes().getConceptMap().putAll(asConceptMap(jpaDataTypeList));
+ for (PfConceptKey referencedDataTypeKey : referencedDataTypeKeys) {
+ JpaToscaServiceTemplate dataTypeEntityTreeServiceTemplate =
+ getDataTypes(dao, referencedDataTypeKey.getName(), referencedDataTypeKey.getVersion());
+
+ serviceTemplate =
+ ToscaServiceTemplateUtils.addFragment(serviceTemplate, dataTypeEntityTreeServiceTemplate);
+ }
}
LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
* Create policy types.
*
* @param dao the DAO to use to access the database
- * @param serviceTemplate the service template containing the definition of the policy types to be created
+ * @param incomingServiceTemplate the service template containing the definition of the policy types to be created
* @return the TOSCA service template containing the created policy types
* @throws PfModelException on errors creating policy types
*/
public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
- @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
- LOGGER.debug("->createPolicyTypes: serviceTempalate={}", serviceTemplate);
-
- ToscaUtils.assertPolicyTypesExist(serviceTemplate);
+ @NonNull final JpaToscaServiceTemplate incomingServiceTemplate) throws PfModelException {
+ LOGGER.debug("->createPolicyTypes: serviceTempalate={}", incomingServiceTemplate);
- // Create the data types on the policy type
- if (ToscaUtils.doDataTypesExist(serviceTemplate)) {
- createDataTypes(dao, serviceTemplate);
- }
+ ToscaUtils.assertPolicyTypesExist(incomingServiceTemplate);
- for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
- dao.create(policyType);
- }
-
- // Return the created policy types
- JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
+ JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, incomingServiceTemplate);
- for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
- returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
- }
-
- JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
- returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
-
- LOGGER.debug("<-createPolicyTypes: returnServiceTempalate={}", returnServiceTemplate);
- return returnServiceTemplate;
+ LOGGER.debug("<-createPolicyTypes: returnServiceTempalate={}", writtenServiceTemplate);
+ return writtenServiceTemplate;
}
/**
throws PfModelException {
LOGGER.debug("->getPolicies: name={}, version={}", name, version);
- // Create the structure of the TOSCA service template to contain the policy type
- JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
- serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
- serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
+ JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+ if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policies for " + name + ":" + version + DO_NOT_EXIST);
+ }
+
+ ToscaUtils.getEntityTree(serviceTemplate.getTopologyTemplate().getPolicies(), name, version);
+
+ if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policies for " + name + ":" + version + DO_NOT_EXIST);
+ }
- // Add the policy type to the TOSCA service template
- List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
- serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
+ for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) {
+ if (policy.getDerivedFrom() != null) {
+ JpaToscaServiceTemplate referencedEntitiesServiceTemplate =
+ getPolicyTypes(dao, policy.getDerivedFrom().getName(), policy.getDerivedFrom().getVersion());
+
+ serviceTemplate =
+ ToscaServiceTemplateUtils.addFragment(serviceTemplate, referencedEntitiesServiceTemplate);
+ }
+ }
LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
return serviceTemplate;
* Create policies.
*
* @param dao the DAO to use to access the database
- * @param serviceTemplate the service template containing the definitions of the new policies to be created.
+ * @param incomingServiceTemplate the service template containing the definitions of the new policies to be created.
* @return the TOSCA service template containing the policy types that were created
* @throws PfModelException on errors creating policies
*/
public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
- @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
- LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
-
- ToscaUtils.assertPoliciesExist(serviceTemplate);
-
- for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
- verifyPolicyTypeForPolicy(dao, policy);
- dao.create(policy);
- }
-
- // Return the created policy types
- JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
- returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
+ @NonNull final JpaToscaServiceTemplate incomingServiceTemplate) throws PfModelException {
+ LOGGER.debug("->createPolicies: incomingServiceTemplate={}", incomingServiceTemplate);
- for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
- returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
- }
+ ToscaUtils.assertPoliciesExist(incomingServiceTemplate);
- serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
+ JpaToscaServiceTemplate writtenServiceTemplate = appendToServiceTemplate(dao, incomingServiceTemplate);
- LOGGER.debug("<-createPolicies: serviceTemplate={}", serviceTemplate);
- return serviceTemplate;
+ LOGGER.debug("<-createPolicies: serviceTemplate={}", writtenServiceTemplate);
+ return writtenServiceTemplate;
}
/**
return serviceTemplate;
}
- /**
- * Convert a list of concepts to a map of concepts.
- *
- * @param conceptList the concept list
- * @return the concept map
- */
- private <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) {
- Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>();
- for (T concept : conceptList) {
- conceptMap.put((PfConceptKey) concept.getKey(), concept);
- }
-
- return conceptMap;
- }
-
/**
* Verify the policy type for a policy exists.
*
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);
}
}
// 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);
+ String errorMessage = "search for latest policy type " + policyTypeName + " returned more than one entry";
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
// @formatter:off
private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
- new PfConceptKey("string", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("integer", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("float", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("null", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("list", PfKey.NULL_KEY_VERSION),
- new PfConceptKey("map", PfKey.NULL_KEY_VERSION)
+ new PfConceptKey("string", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("integer", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("float", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("null", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("list", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("map", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("scalar-unit.size", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("scalar-unit.time", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("scalar-unit.frequency", PfKey.NULL_KEY_VERSION),
+ new PfConceptKey("tosca.datatypes.TimeInterval", PfKey.NULL_KEY_VERSION)
);
// @formatter:on
* Get the entity tree from a concept container for a given entity key.
*
* @param entityTypes the concept container containing entity types
- * @param searchKey the key to search for
+ * @param entityName the name of the entity
+ * @param entityVersion the version of the entity
*/
public static void getEntityTree(
@NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
- @NonNull final PfConceptKey searchKey) {
+ final String entityName, final String entityVersion) {
PfValidationResult result = new PfValidationResult();
@SuppressWarnings("unchecked")
Set<JpaToscaEntityType<?>> filteredEntitySet =
- (Set<JpaToscaEntityType<?>>) entityTypes.getAll(searchKey.getName(), searchKey.getVersion());
+ (Set<JpaToscaEntityType<?>>) entityTypes.getAll(entityName, entityVersion);
for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
filteredEntitySet.addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
}
assertEquals(1, gotPolicyList.size());
assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0)));
- gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", VERSION_100);
- assertEquals(0, gotPolicyList.size());
+ assertTrue(new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", VERSION_100).isEmpty());
}
@Test
assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy));
assertTrue(beforePolicy.getType().equals(deletedPolicy.getType()));
- ToscaServiceTemplate gotServiceTemplate =
- new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
-
- assertTrue(gotServiceTemplate.getToscaTopologyTemplate().getPolicies().isEmpty());
+ // @formatter:off
+ assertThatThrownBy(
+ () -> new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()))
+ .hasMessageMatching("policies for onap.restart.tca:1.0.0 do not exist");
+ // @formatter:on
}
@Test
*/
public class AuthorativeToscaProviderPolicyTypeTest {
private static final String VERSION = "version";
- private static final String POLICY_AFFINITY_VERSION0 = "onap.policies.NoVersion:0.0.0";
- private static final String POLICY_AFFINITY = "onap.policies.NoVersion";
+ private static final String POLICY_NO_VERSION_VERSION0 = "onap.policies.NoVersion:0.0.0";
+ private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
private static final String VERSION_000 = "0.0.0";
ToscaServiceTemplate createdServiceTemplate =
new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
- PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
+ PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
List<ToscaPolicyType> gotPolicyTypeList =
- new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, VERSION_000);
- assertEquals(1, gotPolicyTypeList.size());
+ new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_000);
+ assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, null);
- assertEquals(1, gotPolicyTypeList.size());
+ gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
+ assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
- assertEquals(1, gotPolicyTypeList.size());
+ assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_000);
- assertEquals(1, gotPolicyTypeList.size());
+ assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
+
+ assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
+ VERSION_000)).hasMessageContaining("Policy Framework DAO has not been initialized");
+
+ assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_000).isEmpty());
}
@Test
new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
}).hasMessageMatching("^filter is marked .*on.*ull but is null$");
+ assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
+ ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()))
+ .hasMessageContaining("Policy Framework DAO has not been initialized");
+
+ assertTrue(new AuthorativeToscaProvider()
+ .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build())
+ .isEmpty());
+
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
assertNotNull(toscaServiceTemplate);
ToscaServiceTemplate createdServiceTemplate =
new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
- PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
+ PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
List<ToscaPolicyType> gotPolicyTypeList =
- new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, VERSION_000);
- assertEquals(1, gotPolicyTypeList.size());
+ new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_000);
+ assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
ToscaPolicyTypeFilter.builder().build());
- assertEquals(1, gotPolicyTypeList.size());
+ assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
ToscaPolicyTypeFilter.builder().version("1.0.0").build());
- assertEquals(0, gotPolicyTypeList.size());
+ assertEquals(1, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
}
ToscaServiceTemplate createdServiceTemplate =
new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
- PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
+ PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaServiceTemplate createdServiceTemplate =
new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
- PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
+ PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaServiceTemplate createdServiceTemplate =
new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
- PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
+ PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION0);
ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
- ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
- policyTypeKey.getName(), policyTypeKey.getVersion());
-
- assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty());
+ assertThatThrownBy(() -> {
+ new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
+ }).hasMessage("policy types for onap.policies.NoVersion:0.0.0 do not exist");
}
@Test
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.LinkedHashMap;
import java.util.Map;
+
import org.junit.Test;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "2.0.0"));
serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy);
- assertThatThrownBy(() -> {
- new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
- }).hasMessageContaining("no metadata defined on TOSCA policy");
-
policy.setMetadata(new LinkedHashMap<>());
assertThatThrownBy(() -> {
new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019=-2020 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import org.junit.Test;
import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfReferenceKey;
import org.onap.policy.models.base.PfValidationResult;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
/**
- * DAO test for ToscaDatatype.
+ * DAO test for JpaToscaDatatype.
*
* @author Liam Fallon (liam.fallon@est.tech)
*/
ToscaDataType datOut = tdta.toAuthorative();
assertNotNull(datOut);
}
+
+ @Test
+ public void testGetReferencedDataTypes() {
+ JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
+
+ assertTrue(dt0.getReferencedDataTypes().isEmpty());
+
+ dt0.setProperties(new LinkedHashMap<>());
+ assertTrue(dt0.getReferencedDataTypes().isEmpty());
+
+ JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop0"));
+ prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
+ assertTrue(prop0.validate(new PfValidationResult()).isValid());
+
+ dt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
+ assertTrue(dt0.getReferencedDataTypes().isEmpty());
+
+ JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop1"));
+ prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
+ assertTrue(prop1.validate(new PfValidationResult()).isValid());
+
+ dt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
+ assertEquals(1, dt0.getReferencedDataTypes().size());
+
+ JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop2"));
+ prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
+ assertTrue(prop2.validate(new PfValidationResult()).isValid());
+
+ dt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
+ assertEquals(1, dt0.getReferencedDataTypes().size());
+
+ JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
+ prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
+ prop3.setEntrySchema(new JpaToscaEntrySchema());
+ prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
+ assertTrue(prop3.validate(new PfValidationResult()).isValid());
+
+ dt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
+ assertEquals(3, dt0.getReferencedDataTypes().size());
+
+ JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
+ prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
+ prop4.setEntrySchema(new JpaToscaEntrySchema());
+ prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
+ assertTrue(prop4.validate(new PfValidationResult()).isValid());
+
+ dt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
+ assertEquals(3, dt0.getReferencedDataTypes().size());
+ }
}
package org.onap.policy.models.tosca.simple.concepts;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
assertTrue(tst.validate(new PfValidationResult()).isValid());
assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
+
+ tst.setToscaDefinitionsVersion(null);
+ PfValidationResult result = tst.validate(new PfValidationResult());
+ assertThat(result.toString()).contains("service template tosca definitions version may not be null");
+
+ tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
+ tst.setDataTypes(null);
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
+ tst.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
+ JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
+ prop0.setType(dt0.getKey());
+ pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
+
+ tst.setDataTypes(null);
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
+
+ tst.setDataTypes(new JpaToscaDataTypes());
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
+
+ tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ tst.setTopologyTemplate(null);
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ tst.setPolicyTypes(null);
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ JpaToscaPolicy pol0 = new JpaToscaPolicy(new PfConceptKey("pol0:0.0.1"));
+ tst.getTopologyTemplate().getPolicies().getConceptMap().put(pol0.getKey(), pol0);
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains("type is null or a null key");
+
+ pol0.setType(new PfConceptKey("i.dont.Exist:0.0.1"));
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains(
+ "no policy types are defined on the service template for the policies in the topology template");
+
+ tst.setPolicyTypes(policyTypes);
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains("policy type i.dont.Exist:0.0.1 referenced in policy not found");
+
+ pol0.setType(dt0.getKey());
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains("policy type dt0:0.0.1 referenced in policy not found");
+
+ pol0.setType(pt0.getKey());
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ tst.setPolicyTypes(null);
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains(
+ "no policy types are defined on the service template for the policies in the topology template");
+
+ tst.setPolicyTypes(policyTypes);
+ pol0.setType(pt0.getKey());
+ result = tst.validate(new PfValidationResult());
+ assertTrue(result.isOk());
+
+ tst.setPolicyTypes(new JpaToscaPolicyTypes());
+ result = tst.validate(new PfValidationResult());
+ assertFalse(result.isOk());
+ assertThat(result.toString()).contains(
+ "no policy types are defined on the service template for the policies in the topology template");
+
}
}
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
import java.util.Properties;
*/
public class SimpleToscaProviderTest {
private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$";
+ private static final String INCOMING_TEMPLATE_IS_NULL = "^incomingServiceTemplate is marked .*on.*ull but is null$";
private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key));
assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
- JpaToscaServiceTemplate doesNotExistServiceTemplate =
- new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key);
-
- assertEquals(null, doesNotExistServiceTemplate.getDataTypes().get(dataType0Key));
+ assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key))
+ .hasMessage("data types for DataType0:0.0.1 do not exist");
}
@Test
assertEquals("Updated Description",
deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription());
- JpaToscaServiceTemplate doesNotExistServiceTemplate =
- new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key);
-
- assertEquals(null, doesNotExistServiceTemplate.getPolicyTypes().get(policyType0Key));
+ assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key))
+ .hasMessage("policy types for PolicyType0:0.0.1 do not exist");
}
@Test
assertEquals("Updated Description",
deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription());
- JpaToscaServiceTemplate doesNotExistServiceTemplate =
- new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key);
-
- assertEquals(null, doesNotExistServiceTemplate.getPolicyTypes().get(policyType0Key));
+ assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key))
+ .hasMessage("policy types for PolicyType0:0.0.1 do not exist");
}
@Test
JpaToscaServiceTemplate createdServiceTemplate =
new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
- assertEquals(originalServiceTemplate, createdServiceTemplate);
+ assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies(),
+ createdServiceTemplate.getTopologyTemplate().getPolicies());
PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
-
}
@Test
JpaToscaServiceTemplate createdServiceTemplate =
new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
- assertEquals(originalServiceTemplate, createdServiceTemplate);
+ assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies(),
+ createdServiceTemplate.getTopologyTemplate().getPolicies());
}
@Test
JpaToscaServiceTemplate createdServiceTemplate =
new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
- assertEquals(originalServiceTemplate, createdServiceTemplate);
+ assertEquals(originalServiceTemplate.getTopologyTemplate(), createdServiceTemplate.getTopologyTemplate());
PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
- assertTrue(new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion())
- .getTopologyTemplate().getPolicies().getConceptMap().isEmpty());
+ // @formatter:off
+ assertThatThrownBy(
+ () -> new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()))
+ .hasMessage("policies for onap.restart.tca:1.0.0 do not exist");
+ // @formatter:on
}
@Test
assertThatThrownBy(() -> {
new SimpleToscaProvider().createDataTypes(pfDao, null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ }).hasMessageMatching(INCOMING_TEMPLATE_IS_NULL);
assertThatThrownBy(() -> {
new SimpleToscaProvider().updateDataTypes(null, null);
assertThatThrownBy(() -> {
new SimpleToscaProvider().createPolicyTypes(pfDao, null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ }).hasMessageMatching(INCOMING_TEMPLATE_IS_NULL);
assertThatThrownBy(() -> {
new SimpleToscaProvider().updatePolicyTypes(null, null);
assertThatThrownBy(() -> {
new SimpleToscaProvider().createPolicies(pfDao, null);
- }).hasMessageMatching(TEMPLATE_IS_NULL);
+ }).hasMessageMatching(INCOMING_TEMPLATE_IS_NULL);
assertThatThrownBy(() -> {
new SimpleToscaProvider().updatePolicies(null, null);
@Test
public void testgetEntityTree() {
assertThatThrownBy(() -> {
- ToscaUtils.getEntityTree(null, null);
+ ToscaUtils.getEntityTree(null, null, null);
}).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
- assertThatThrownBy(() -> {
- ToscaUtils.getEntityTree(null, new PfConceptKey());
- }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
-
- assertThatThrownBy(() -> {
- ToscaUtils.getEntityTree(new JpaToscaDataTypes(), null);
- }).hasMessageMatching("searchKey is marked .*on.*ull but is null");
-
JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
- ToscaUtils.getEntityTree(filteredDataTypes, new PfConceptKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
assertEquals(dataTypes, filteredDataTypes);
JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
dataTypes.getConceptMap().put(dt0.getKey(), dt0);
filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
- ToscaUtils.getEntityTree(filteredDataTypes, new PfConceptKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
assertNotEquals(dataTypes, filteredDataTypes);
assertTrue(filteredDataTypes.getConceptMap().isEmpty());
filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
- ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
assertEquals(dataTypes, filteredDataTypes);
JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
dataTypes.getConceptMap().put(dt8.getKey(), dt8);
dataTypes.getConceptMap().put(dt9.getKey(), dt9);
- ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
assertEquals(1, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey().getName(), dt1.getKey().getVersion());
assertEquals(2, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey().getName(), dt2.getKey().getVersion());
assertEquals(2, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey().getName(), dt3.getKey().getVersion());
assertEquals(2, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey().getName(), dt4.getKey().getVersion());
assertEquals(3, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey().getName(), dt5.getKey().getVersion());
assertEquals(4, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey().getName(), dt6.getKey().getVersion());
assertEquals(5, filteredDataTypes.getConceptMap().size());
assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey().getName(), dt7.getKey().getVersion());
assertEquals(1, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey().getName(), dt8.getKey().getVersion());
assertEquals(2, filteredDataTypes.getConceptMap().size());
filteredDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey());
+ ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
assertEquals(3, filteredDataTypes.getConceptMap().size());
dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
assertThatThrownBy(() -> {
final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
- ToscaUtils.getEntityTree(badDataTypes, dt9.getKey());
+ ToscaUtils.getEntityTree(badDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
}).hasMessageContaining("parent i.dont.Exist:0.0.0 of entity not found");
}
}
<class>org.onap.policy.models.base.PfConceptKey</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class>
<properties>
tosca_definitions_version: tosca_simple_yaml_1_0_0
policy_types:
+ onap.policies.Optimization:
+ derived_from: tosca.policies.Root
+ version: 1.0.0
+ description: The base policy type for all policies that govern optimization
onap.policies.NoVersion:
derived_from: onap.policies.Optimization
properties: