summaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-02-06 17:32:48 +0000
committerliamfallon <liam.fallon@est.tech>2020-02-07 11:32:49 +0000
commit532202ce2d8bf83299b3775bba184fb5b3b50df7 (patch)
treefd2466b262411dc1872dc50ae052060dfbf80106 /models-tosca/src/main/java
parent60344d720eef9fc24dc86d7a6adc5f4530f9d099 (diff)
Add data type and policy type reference checking
Full validation including references to policy types and data types added. Unit tests fixed to cope with new stricter validation. Issue-ID: POLICY-1402 Change-Id: I59f37640a99494a53960a54d2fc82cc96861d43b Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java13
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java6
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java35
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java66
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java26
5 files changed, 134 insertions, 12 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java
index a664010b8..b7ebdcec3 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java
@@ -1,6 +1,6 @@
/*-
* ============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.
@@ -58,8 +58,7 @@ public class LegacyGuardPolicyMapper
static {
GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
- GUARD_POLICY_TYPE_MAP.put("guard.minmax.",
- new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
+ GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
}
@@ -93,7 +92,7 @@ public class LegacyGuardPolicyMapper
toscaPolicy.setMetadata(metadata);
final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
- serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
+ serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
@@ -104,8 +103,8 @@ public class LegacyGuardPolicyMapper
}
@Override
- public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
- final JpaToscaServiceTemplate serviceTemplate) {
+ public Map<String, LegacyGuardPolicyOutput>
+ fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
ToscaUtils.assertPoliciesExist(serviceTemplate);
final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
@@ -126,7 +125,7 @@ public class LegacyGuardPolicyMapper
final Map<String, Object> metadata = new LinkedHashMap<>(toscaPolicy.getMetadata());
// if version exists, convert it to int
- metadata.computeIfPresent(POLICY_VERSION, (key,val) -> Integer.parseInt(val.toString()));
+ metadata.computeIfPresent(POLICY_VERSION, (key, val) -> Integer.parseInt(val.toString()));
legacyGuardPolicyOutput.setMetadata(metadata);
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
index e7dabf1b8..41792aeb9 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
@@ -1,6 +1,6 @@
/*-
* ============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.
@@ -71,7 +71,7 @@ public class LegacyOperationalPolicyMapper
toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());
final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
- serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
+ serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
@@ -105,7 +105,7 @@ public class LegacyOperationalPolicyMapper
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
- String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
+ String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
if (content == null) {
String errorMessage = "property \"content\" not defined on TOSCA policy";
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java
index 37e92b58a..43d7ad633 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java
@@ -3,7 +3,7 @@
* 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.
@@ -23,19 +23,26 @@
package org.onap.policy.models.tosca.simple.concepts;
+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 javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.Table;
+
import lombok.Data;
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;
@@ -47,6 +54,7 @@ import org.onap.policy.models.base.PfValidationResult;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
/**
* Class to represent the policy type in TOSCA definition.
@@ -298,4 +306,29 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl
return PfUtils.compareObjects(triggers, other.triggers);
}
+
+ /**
+ * Get the data types referenced in a policy type.
+ *
+ * @return the data types referenced in a policy 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;
+ }
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
index f6139ab2d..786784c09 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
@@ -272,7 +272,18 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
result = policyTypes.validate(result);
}
- return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
+ if (topologyTemplate != null) {
+ result = topologyTemplate.validate(result);
+ }
+
+ // No point in validating cross references if the structure of the individual parts are not valid
+ if (!result.isOk()) {
+ return result;
+ }
+
+ validateDatatypesInPolicyTypes(result);
+
+ return validatePolicyTypesInPolicies(result);
}
@Override
@@ -321,4 +332,57 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
}
+
+ /**
+ * Validate that all data types referenced in policy types exist.
+ *
+ * @param result the validation result object to use for the validation result
+ * @return the validation result object
+ */
+ private PfValidationResult validateDatatypesInPolicyTypes(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"));
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Validate that all policy types referenced in policies exist.
+ *
+ * @param result the validation result object to use for the validation result
+ * @return the validation result object
+ */
+ private PfValidationResult validatePolicyTypesInPolicies(PfValidationResult result) {
+ if (topologyTemplate == null || topologyTemplate.getPolicies() == null) {
+ return result;
+ }
+
+ if (policyTypes == null) {
+ 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"));
+ return result;
+ }
+
+ for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
+ if (policyTypes.get(policy.getType()) == null) {
+ result.addValidationMessage(
+ new PfValidationMessage(policy.getKey(), this.getClass(), ValidationResult.INVALID,
+ "policy type " + policy.getType().getId() + " referenced in policy not found"));
+ }
+ }
+
+ return result;
+ }
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
index 7e106fa53..fffad86e0 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -21,6 +21,7 @@
package org.onap.policy.models.tosca.utils;
import java.util.Collection;
+import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Function;
@@ -32,6 +33,7 @@ import org.apache.commons.collections4.CollectionUtils;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptContainer;
import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.base.PfNameVersion;
import org.onap.policy.models.base.PfValidationMessage;
@@ -52,6 +54,21 @@ public final class ToscaUtils {
private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
+ private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = new LinkedHashSet<>();
+
+ // @formatter:off
+ static {
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("integer", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("float", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("boolean", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("null", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("list", PfKey.NULL_KEY_VERSION));
+ PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("map", PfKey.NULL_KEY_VERSION));
+ }
+ // @formatter:off
+
/**
* Private constructor to prevent subclassing.
*/
@@ -218,4 +235,13 @@ public final class ToscaUtils {
}
return ancestorEntitySet;
}
+
+ /**
+ * Get the predefined policy types.
+ *
+ * @return the predefined policy types
+ */
+ public static Collection<?> getPredefinedDataTypes() {
+ return PREDEFINED_TOSCA_DATA_TYPES;
+ }
}