diff options
Diffstat (limited to 'models-tosca/src')
36 files changed, 3222 insertions, 321 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java new file mode 100644 index 000000000..63945174a --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java @@ -0,0 +1,271 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.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.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.onap.policy.common.utils.coder.YamlJsonTranslator; +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.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment; + +/** + * Class to represent the parameter in TOSCA definition. + */ +@Entity +@Table(name = "ToscaCapabilityAssignment") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabilityAssignment> + implements PfAuthorative<ToscaCapabilityAssignment> { + + private static final long serialVersionUID = 1675770231921107988L; + + private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED"; + private static final Integer JPA_UNBOUNDED_VALUE = -1; + + private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator(); + + @ElementCollection + @Lob + private Map<String, String> properties; + + @ElementCollection + @Lob + private Map<String, String> attributes; + + @ElementCollection + private List<Integer> occurrences; + + /** + * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key. + */ + public JpaToscaCapabilityAssignment() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key. + * + * @param key the key + */ + public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) { + super(copyConcept); + this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties); + this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes); + this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) { + super(new PfConceptKey()); + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaCapabilityAssignment toAuthorative() { + ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment(); + super.setToscaEntity(toscaCapabilityAssignment); + super.toAuthorative(); + + toscaCapabilityAssignment.setProperties( + PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class))); + + toscaCapabilityAssignment.setAttributes( + PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class))); + + toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> { + if (occurrence.equals(JPA_UNBOUNDED_VALUE)) { + return AUTHORATIVE_UNBOUNDED_LITERAL; + } else { + return occurrence; + } + })); + + return toscaCapabilityAssignment; + } + + @Override + public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) { + super.fromAuthorative(toscaCapabilityAssignment); + + + properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml); + attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml); + + occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> { + if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) { + return JPA_UNBOUNDED_VALUE; + } else { + return ((Number) occurrence).intValue(); + } + }); + } + + @Override + public void clean() { + super.clean(); + + properties = PfUtils.mapMap(properties, String::trim); + attributes = PfUtils.mapMap(attributes, String::trim); + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (properties != null) { + result.append(validateProperties(new PfValidationResult())); + } + + if (attributes != null) { + result.append(validateAttributes(new PfValidationResult())); + } + + if (occurrences != null) { + result.append(validateOccurrences(new PfValidationResult())); + } + + return result; + } + + /** + * Validate the properties. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (Entry<String, String> propertyEntry : properties.entrySet()) { + if (propertyEntry.getValue() == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "capability assignment property " + propertyEntry.getKey() + " value may not be null")); + } + } + return result; + } + + /** + * Validate the attributes. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateAttributes(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (Entry<String, String> attributeEntry : attributes.entrySet()) { + if (attributeEntry.getValue() == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "capability assignment attribute " + attributeEntry.getKey() + " value may not be null")); + } + } + return result; + } + + /** + * Validate the occurrences. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateOccurrences(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (Integer occurrence : occurrences) { + if (occurrence == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "capability assignment occurrence value may not be null ")); + } else if (occurrence < 0 && !occurrence.equals(JPA_UNBOUNDED_VALUE)) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "capability assignment occurrence value may not be negative")); + } + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + + if (this == otherConcept) { + return 0; + } + + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept; + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + result = PfUtils.compareMaps(properties, other.properties); + if (result != 0) { + return result; + } + + result = PfUtils.compareMaps(attributes, other.attributes); + if (result != 0) { + return result; + } + + return PfUtils.compareCollections(occurrences, other.occurrences); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignments.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignments.java new file mode 100644 index 000000000..5afc89275 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignments.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * This class is a container for TOSCA capability assignments. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaCapabilityAssignments") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class JpaToscaCapabilityAssignments + extends PfConceptContainer<JpaToscaCapabilityAssignment, ToscaCapabilityAssignment> + implements PfAuthorative<List<Map<String, ToscaCapabilityAssignment>>> { + public static final String DEFAULT_NAME = "ToscaCapabilityAssignmentsSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + private static final long serialVersionUID = -7526648702327776101L; + + /** + * The Default Constructor creates a {@link JpaToscaCapabilityAssignments} object with a null + * artifact key and creates an empty concept map. + */ + public JpaToscaCapabilityAssignments() { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + } + + /** + * The Key Constructor creates a {@link JpaToscaCapabilityAssignments} object with the given + * artifact key and creates an empty concept map. + * + * @param key the concept key + */ + public JpaToscaCapabilityAssignments(final PfConceptKey key) { + super(key, new TreeMap<>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public JpaToscaCapabilityAssignments(final PfConceptKey key, + final Map<PfConceptKey, JpaToscaCapabilityAssignment> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaCapabilityAssignments(final JpaToscaCapabilityAssignments copyConcept) { + super(copyConcept); + } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaCapabilityAssignments(final List<Map<String, ToscaCapabilityAssignment>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + this.fromAuthorative(authorativeConceptMapList); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (JpaToscaCapabilityAssignment assignment : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, assignment, result); + } + + return result; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityType.java new file mode 100644 index 000000000..4db779557 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityType.java @@ -0,0 +1,239 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.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; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * Class to represent the capability type in TOSCA definition. + */ + +@Entity +@Table(name = "ToscaCapabilityType") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class JpaToscaCapabilityType extends JpaToscaEntityType<ToscaCapabilityType> + implements PfAuthorative<ToscaCapabilityType> { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map<String, JpaToscaProperty> properties; + + /** + * The Default Constructor creates a {@link JpaToscaCapabilityType} object with a null key. + */ + public JpaToscaCapabilityType() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaCapabilityType} object with the given concept key. + * + * @param key the key + */ + public JpaToscaCapabilityType(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaCapabilityType(final JpaToscaCapabilityType copyConcept) { + super(copyConcept); + this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaCapabilityType(final ToscaCapabilityType authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaCapabilityType toAuthorative() { + ToscaCapabilityType toscaCapabilityType = new ToscaCapabilityType(); + super.setToscaEntity(toscaCapabilityType); + super.toAuthorative(); + + toscaCapabilityType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); + + return toscaCapabilityType; + } + + @Override + public void fromAuthorative(final ToscaCapabilityType toscaCapabilityType) { + super.fromAuthorative(toscaCapabilityType); + + // Set properties + if (toscaCapabilityType.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaCapabilityType.getProperties().entrySet()) { + JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); + jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys())); + + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + keyList.addAll(property.getKeys()); + } + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + property.clean(); + } + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (getKey().isNullVersion()) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "key version is a null version")); + } + + if (properties != null) { + result = validateProperties(result); + } + + return result; + } + + /** + * Validate the capabiltiy type properties. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (JpaToscaProperty property : properties.values()) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "capability type property may not be null ")); + } else { + result = property.validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaCapabilityType other = (JpaToscaCapabilityType) otherConcept; + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + return PfUtils.compareMaps(properties, other.properties); + } + + /** + * Get the data types referenced in a capability type. + * + * @return the data types referenced in a capability 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/JpaToscaCapabilityTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityTypes.java new file mode 100644 index 000000000..6ec6de56e --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityTypes.java @@ -0,0 +1,112 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * This class is a container for TOSCA capability types. + */ +@Entity +@Table(name = "ToscaCapabilityTypes") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class JpaToscaCapabilityTypes extends PfConceptContainer<JpaToscaCapabilityType, ToscaCapabilityType> { + public static final String DEFAULT_NAME = "ToscaCapabilityTypesSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + private static final long serialVersionUID = -4157979965271220098L; + + /** + * The Default Constructor creates a {@link JpaToscaCapabilityTypes} object with a null artifact key and creates an + * empty concept map. + */ + public JpaToscaCapabilityTypes() { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + } + + /** + * The Key Constructor creates a {@link JpaToscaCapabilityTypes} object with the given artifact key and creates an + * empty concept map. + * + * @param key the concept key + */ + public JpaToscaCapabilityTypes(final PfConceptKey key) { + super(key, new TreeMap<>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public JpaToscaCapabilityTypes(final PfConceptKey key, final Map<PfConceptKey, JpaToscaCapabilityType> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaCapabilityTypes(final JpaToscaCapabilityTypes copyConcept) { + super(copyConcept); + } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaCapabilityTypes(final List<Map<String, ToscaCapabilityType>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + this.fromAuthorative(authorativeConceptMapList); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + // Check that all ancestors of this policy type exist + for (JpaToscaCapabilityType capabilityType : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, capabilityType, result); + } + + return result; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java index 4e4cb8e84..58ac9e326 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java @@ -50,7 +50,6 @@ import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; -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; @@ -70,11 +69,11 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen private static final long serialVersionUID = -3922690413436539164L; @ElementCollection - private List<JpaToscaConstraint> constraints = new ArrayList<>(); + private List<JpaToscaConstraint> constraints; @ElementCollection @Lob - private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>(); + private Map<String, JpaToscaProperty> properties; /** * The Default Constructor creates a {@link JpaToscaDataType} object with a null key. @@ -119,25 +118,8 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen super.setToscaEntity(toscaDataType); super.toAuthorative(); - if (constraints != null) { - List<ToscaConstraint> toscaConstraints = new ArrayList<>(); - - for (JpaToscaConstraint constraint : constraints) { - toscaConstraints.add(constraint.toAuthorative()); - } - - toscaDataType.setConstraints(toscaConstraints); - } - - if (properties != null) { - Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>(); - - for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) { - propertyMap.put(entry.getKey(), entry.getValue().toAuthorative()); - } - - toscaDataType.setProperties(propertyMap); - } + toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative)); + toscaDataType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); return toscaDataType; } @@ -146,13 +128,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen public void fromAuthorative(final ToscaDataType toscaDataType) { super.fromAuthorative(toscaDataType); - if (toscaDataType.getConstraints() != null) { - constraints = new ArrayList<>(); - - for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) { - constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); - } - } + constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance); if (toscaDataType.getProperties() != null) { properties = new LinkedHashMap<>(); @@ -220,7 +196,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen /** * Validate the properties. * - * @param result The result of validations up to now + * @param resultIn The result of validations up to now * @return the validation result */ private PfValidationResult validateProperties(final PfValidationResult resultIn) { @@ -250,21 +226,17 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen } final JpaToscaDataType other = (JpaToscaDataType) otherConcept; - if (!super.equals(other)) { - return super.compareTo(other); - } - - int result = PfUtils.compareObjects(constraints, other.constraints); + int result = super.compareTo(other); if (result != 0) { return result; } - result = PfUtils.compareObjects(properties, other.properties); + result = PfUtils.compareCollections(constraints, other.constraints); if (result != 0) { return result; } - return 0; + return PfUtils.compareMaps(properties, other.properties); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java index 76d2ff951..549d93e8a 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java @@ -21,7 +21,6 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -70,7 +69,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme private PfConceptKey derivedFrom; @ElementCollection - private Map<String, String> metadata = new TreeMap<>(); + private Map<String, String> metadata; @Column private String description; @@ -129,15 +128,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme toscaEntity.setDescription(description); } - if (metadata != null) { - Map<String, String> metadataMap = new LinkedHashMap<>(); - - for (Entry<String, String> entry : metadata.entrySet()) { - metadataMap.put(entry.getKey(), entry.getValue()); - } - - toscaEntity.setMetadata(metadataMap); - } + toscaEntity.setMetadata(PfUtils.mapMap(metadata, item -> item)); return toscaEntity; } @@ -155,7 +146,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme } if (toscaEntity.getDerivedFrom() != null) { - // CHeck if the derived from field contains a name-version ID + // Check if the derived from field contains a name-version ID if (toscaEntity.getDerivedFrom().contains(":")) { derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom()); } else { @@ -167,13 +158,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme description = toscaEntity.getDescription(); } - if (toscaEntity.getMetadata() != null) { - metadata = new LinkedHashMap<>(); - - for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) { - metadata.put(metadataEntry.getKey(), metadataEntry.getValue()); - } - } + metadata = PfUtils.mapMap(toscaEntity.getMetadata(), item -> item); } @Override @@ -253,16 +238,18 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme @SuppressWarnings("unchecked") final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); + + int result = key.compareTo(other.key); + if (result != 0) { + return result; } - int result = ObjectUtils.compare(derivedFrom, other.derivedFrom); + result = ObjectUtils.compare(derivedFrom, other.derivedFrom); if (result != 0) { return result; } - result = PfUtils.compareObjects(metadata, other.metadata); + result = PfUtils.compareMaps(metadata, other.metadata); if (result != 0) { return result; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilter.java index 716813799..90bb0a89e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilter.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. @@ -165,19 +165,21 @@ public class JpaToscaEventFilter extends PfConcept { } final JpaToscaEventFilter other = (JpaToscaEventFilter) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); + int result = key.compareTo(other.key); + if (result != 0) { + return result; } - if (!node.equals(other.node)) { - return node.compareTo(other.node); + result = node.compareTo(other.node); + if (result != 0) { + return result; } - int result = ObjectUtils.compare(requirement, other.requirement); + result = ObjectUtils.compare(requirement, other.requirement); if (result != 0) { return result; } return ObjectUtils.compare(capability, other.capability); } -}
\ No newline at end of file +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java index cc9b1bc96..d65689d8e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -40,9 +40,8 @@ import org.onap.policy.models.base.PfModelService; import org.onap.policy.models.base.PfValidationResult; /** - * A container class for a TOSCA model with multiple service templates. This class is a container - * class that allows a model with many service templates to be constructed that contains a well - * formed overall TOSCA model. + * A container class for a TOSCA model with multiple service templates. This class is a container class that allows a + * model with many service templates to be constructed that contains a well formed overall TOSCA model. * * <p>Validation runs {@link JpaToscaModel} validation on the model and all its sub concepts. */ @@ -59,16 +58,16 @@ public class JpaToscaModel extends PfModel { private JpaToscaServiceTemplates serviceTemplates; /** - * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and - * creates an empty TOSCA model. + * The Default Constructor creates a {@link JpaToscaModel} object with a null concept key and creates an empty TOSCA + * model. */ public JpaToscaModel() { this(new PfConceptKey()); } /** - * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and - * creates an empty TOSCA model. + * The Key Constructor creates a {@link JpaToscaModel} object with the given concept key and creates an empty TOSCA + * model. * * @param key the TOSCA model key */ @@ -139,8 +138,9 @@ public class JpaToscaModel extends PfModel { } final JpaToscaModel other = (JpaToscaModel) otherConcept; - if (!super.equals(other)) { - return super.compareTo(other); + int result = super.compareTo(other); + if (result != 0) { + return result; } return serviceTemplates.compareTo(other.serviceTemplates); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java new file mode 100644 index 000000000..5fccbe27a --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java @@ -0,0 +1,310 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.Lob; +import javax.persistence.OneToOne; +import javax.persistence.Table; +import javax.ws.rs.core.Response; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +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.base.PfKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; + +/** + * Class to represent the node template in TOSCA definition. + */ +@Entity +@Table(name = "ToscaNodeTemplate") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate> + implements PfAuthorative<ToscaNodeTemplate> { + private static final long serialVersionUID = 1675770231921107988L; + + private static final StandardCoder STANDARD_CODER = new StandardCoder(); + + @Column + private String type; + + @ElementCollection + @Lob + private Map<String, String> properties; + + // formatter:off + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"), + @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")}) + private JpaToscaRequirements requirements; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"), + @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")}) + private JpaToscaCapabilityAssignments capabilities; + // @formatter:on + + /** + * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key. + */ + public JpaToscaNodeTemplate() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key. + * + * @param key the key + */ + public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) { + this(key, null); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) { + super(copyConcept); + this.type = copyConcept.type; + this.properties = PfUtils.mapMap(copyConcept.properties, String::new); + this.requirements = + (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null); + this.capabilities = + (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null); + } + + /** + * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key. + * + * @param key the key + * @param type the node template type + */ + public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) { + super(key); + this.type = type; + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaNodeTemplate toAuthorative() { + ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate(); + super.setToscaEntity(toscaNodeTemplate); + super.toAuthorative(); + + toscaNodeTemplate.setType(type); + + toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> { + try { + return STANDARD_CODER.decode(property, Object.class); + } catch (CoderException ce) { + String errorMessage = "error decoding property JSON value read from database: " + property; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } + })); + + if (requirements != null) { + toscaNodeTemplate.setRequirements(requirements.toAuthorative()); + } + + if (capabilities != null) { + toscaNodeTemplate.setCapabilities(new LinkedHashMap<>()); + List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative(); + for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) { + toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap); + } + } + + return toscaNodeTemplate; + } + + @Override + public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) { + super.fromAuthorative(toscaNodeTemplate); + + type = toscaNodeTemplate.getType(); + + properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> { + try { + return STANDARD_CODER.encode(property); + } catch (CoderException ce) { + String errorMessage = "error encoding property JSON value for database: " + property; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } + }); + + if (toscaNodeTemplate.getRequirements() != null) { + requirements = new JpaToscaRequirements(); + requirements.fromAuthorative(toscaNodeTemplate.getRequirements()); + } + + if (toscaNodeTemplate.getCapabilities() != null) { + capabilities = new JpaToscaCapabilityAssignments(); + capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities())); + } + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (requirements != null) { + keyList.addAll(requirements.getKeys()); + } + + if (capabilities != null) { + keyList.addAll(capabilities.getKeys()); + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + type = type.trim(); + + properties = PfUtils.mapMap(properties, String::trim); + + if (requirements != null) { + requirements.clean(); + } + + if (capabilities != null) { + capabilities.clean(); + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (StringUtils.isBlank(type)) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "node template type may not be null")); + } + + if (properties != null) { + result.append(validateProperties(new PfValidationResult())); + } + + if (requirements != null) { + result.append(requirements.validate(result)); + } + + if (capabilities != null) { + result.append(validateProperties(capabilities.validate(result))); + } + + return result; + } + + /** + * Validate the properties. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (String property : properties.values()) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "topology template property may not be null ")); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept; + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + result = type.compareTo(other.type); + if (result != 0) { + return result; + } + + result = PfUtils.compareMaps(properties, other.properties); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(requirements, other.requirements); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(capabilities, other.capabilities); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplates.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplates.java new file mode 100644 index 000000000..6c83f67c5 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplates.java @@ -0,0 +1,112 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * This class is a container for TOSCA node templates. + */ +@Entity +@Table(name = "ToscaNodeTemplates") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class JpaToscaNodeTemplates extends PfConceptContainer<JpaToscaNodeTemplate, ToscaNodeTemplate> { + public static final String DEFAULT_NAME = "ToscaNodeTemplatesSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + private static final long serialVersionUID = -4157979965271220098L; + + /** + * The Default Constructor creates a {@link JpaToscaNodeTemplates} object with a null artifact key and creates an + * empty concept map. + */ + public JpaToscaNodeTemplates() { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + } + + /** + * The Key Constructor creates a {@link JpaToscaNodeTemplates} object with the given artifact key and creates an + * empty concept map. + * + * @param key the concept key + */ + public JpaToscaNodeTemplates(final PfConceptKey key) { + super(key, new TreeMap<>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public JpaToscaNodeTemplates(final PfConceptKey key, final Map<PfConceptKey, JpaToscaNodeTemplate> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaNodeTemplates(final JpaToscaNodeTemplates copyConcept) { + super(copyConcept); + } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaNodeTemplates(final List<Map<String, ToscaNodeTemplate>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + this.fromAuthorative(authorativeConceptMapList); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + // Check that all ancestors of this node template exist + for (JpaToscaNodeTemplate nodeTemplate : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, nodeTemplate, result); + } + + return result; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java new file mode 100644 index 000000000..26684b583 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java @@ -0,0 +1,277 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.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.CascadeType; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.Lob; +import javax.persistence.OneToOne; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; +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.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * Class to represent the node type in TOSCA definition. + */ + +@Entity +@Table(name = "ToscaNodeType") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implements PfAuthorative<ToscaNodeType> { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map<String, JpaToscaProperty> properties; + + + // formatter:off + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"), + @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")}) + // @formatter:on + private JpaToscaRequirements requirements; + + /** + * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key. + */ + public JpaToscaNodeType() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key. + * + * @param key the key + */ + public JpaToscaNodeType(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaNodeType(final JpaToscaNodeType copyConcept) { + super(copyConcept); + this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new); + this.requirements = + (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaNodeType(final ToscaNodeType authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaNodeType toAuthorative() { + ToscaNodeType toscaNodeType = new ToscaNodeType(); + super.setToscaEntity(toscaNodeType); + super.toAuthorative(); + + toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); + + if (requirements != null) { + toscaNodeType.setRequirements(requirements.toAuthorative()); + } + + return toscaNodeType; + } + + @Override + public void fromAuthorative(final ToscaNodeType toscaNodeType) { + super.fromAuthorative(toscaNodeType); + + // Set properties + if (toscaNodeType.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) { + JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); + jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + + if (toscaNodeType.getRequirements() != null) { + requirements = new JpaToscaRequirements(); + requirements.fromAuthorative(toscaNodeType.getRequirements()); + } + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + keyList.addAll(property.getKeys()); + } + } + + if (requirements != null) { + keyList.addAll(requirements.getKeys()); + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + property.clean(); + } + } + + if (requirements != null) { + requirements.clean(); + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "key version is a null version")); + } + + if (properties != null) { + result = validateProperties(result); + } + + if (requirements != null) { + result = requirements.validate(result); + } + + return result; + } + + /** + * Validate the capabiltiy type properties. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (JpaToscaProperty property : properties.values()) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "node type property may not be null ")); + } else { + result = property.validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept; + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + result = PfUtils.compareMaps(properties, other.properties); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(requirements, other.requirements); + } + + /** + * Get the data types referenced in a node type. + * + * @return the data types referenced in a node 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/JpaToscaNodeTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTypes.java new file mode 100644 index 000000000..5eb5a2c49 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTypes.java @@ -0,0 +1,112 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * This class is a container for TOSCA node types. + */ +@Entity +@Table(name = "ToscaNodeTypes") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class JpaToscaNodeTypes extends PfConceptContainer<JpaToscaNodeType, ToscaNodeType> { + public static final String DEFAULT_NAME = "ToscaNodeTypesSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + private static final long serialVersionUID = -4157979965271220098L; + + /** + * The Default Constructor creates a {@link JpaToscaNodeTypes} object with a null artifact key and creates an + * empty concept map. + */ + public JpaToscaNodeTypes() { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + } + + /** + * The Key Constructor creates a {@link JpaToscaNodeTypes} object with the given artifact key and creates an + * empty concept map. + * + * @param key the concept key + */ + public JpaToscaNodeTypes(final PfConceptKey key) { + super(key, new TreeMap<>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public JpaToscaNodeTypes(final PfConceptKey key, final Map<PfConceptKey, JpaToscaNodeType> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaNodeTypes(final JpaToscaNodeTypes copyConcept) { + super(copyConcept); + } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaNodeTypes(final List<Map<String, ToscaNodeType>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + this.fromAuthorative(authorativeConceptMapList); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + // Check that all ancestors of this policy type exist + for (JpaToscaNodeType nodeType : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, nodeType, result); + } + + return result; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaParameter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaParameter.java new file mode 100644 index 000000000..216e877cc --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaParameter.java @@ -0,0 +1,202 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; +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.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter; + +/** + * Class to represent the parameter in TOSCA definition. + */ +@Entity +@Table(name = "ToscaParameter") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaParameter> { + private static final long serialVersionUID = 1675770231921107988L; + + @EmbeddedId + private PfReferenceKey key; + + @Column + private PfConceptKey type; + + @Column + private String value; + + /** + * The Default Constructor creates a {@link JpaToscaParameter} object with a null key. + */ + public JpaToscaParameter() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key. + * + * @param key the key + */ + public JpaToscaParameter(@NonNull final PfReferenceKey key) { + this(key, new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key. + * + * @param key the key + * @param type the key of the parameter type + */ + public JpaToscaParameter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) { + this.key = key; + this.type = type; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaParameter(final JpaToscaParameter copyConcept) { + super(copyConcept); + this.key = new PfReferenceKey(copyConcept.key); + this.type = new PfConceptKey(copyConcept.type); + this.value = copyConcept.value; + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaParameter(final ToscaParameter authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaParameter toAuthorative() { + ToscaParameter toscaParameter = new ToscaParameter(); + + toscaParameter.setName(key.getLocalName()); + + toscaParameter.setType(type.getName()); + toscaParameter.setTypeVersion(type.getVersion()); + + if (!StringUtils.isBlank(value)) { + toscaParameter.setValue(new YamlJsonTranslator().fromYaml(value, Object.class)); + } + + return toscaParameter; + } + + @Override + public void fromAuthorative(ToscaParameter toscaParameter) { + this.setKey(new PfReferenceKey()); + getKey().setLocalName(toscaParameter.getName()); + + if (toscaParameter.getTypeVersion() != null) { + type = new PfConceptKey(toscaParameter.getType(), toscaParameter.getTypeVersion()); + } else { + type = new PfConceptKey(toscaParameter.getType(), PfKey.NULL_KEY_VERSION); + } + + value = new YamlJsonTranslator().toYaml(toscaParameter.getValue()); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + + keyList.addAll(type.getKeys()); + + return keyList; + } + + @Override + public void clean() { + key.clean(); + + type.clean(); + + if (value != null) { + value = value.trim(); + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "paremeter key is a null key")); + } + + result = key.validate(result); + + if (type == null || type.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "parameter type may not be null")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaParameter other = (JpaToscaParameter) otherConcept; + int result = key.compareTo(other.key); + if (result != 0) { + return result; + } + + return value.compareTo(other.value); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java index 689d03506..518a0884b 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java @@ -23,7 +23,6 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -73,6 +72,8 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P private static final String METADATA_POLICY_ID_TAG = "policy-id"; private static final String METADATA_POLICY_VERSION_TAG = "policy-version"; + private static final StandardCoder STANDARD_CODER = new StandardCoder(); + // @formatter:off @Column @AttributeOverrides({ @@ -85,10 +86,10 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P @ElementCollection @Lob - private Map<String, String> properties = new LinkedHashMap<>(); + private Map<String, String> properties; @ElementCollection - private List<PfConceptKey> targets = new ArrayList<>(); + private List<PfConceptKey> targets; // @formatter:on /** @@ -155,28 +156,14 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P toscaPolicy.setTypeVersion(null); } - if (properties != null) { - Map<String, Object> propertyMap = new LinkedHashMap<>(); - - final StandardCoder coder = new StandardCoder(); - - for (Entry<String, String> entry : properties.entrySet()) { - try { - // TODO: This is a HACK, we need to validate the properties against their - // TODO: their data type in their policy type definition in TOSCA, which means reading - // TODO: the policy type from the database and parsing the property value object correctly - // TODO: Here we are simply reading a JSON string from the database and deserializing the - // TODO: property value from JSON - propertyMap.put(entry.getKey(), coder.decode(entry.getValue(), Object.class)); - } catch (CoderException ce) { - String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey() - + ", value=" + entry.getValue(); - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); - } + toscaPolicy.setProperties(PfUtils.mapMap(properties, property -> { + try { + return STANDARD_CODER.decode(property, Object.class); + } catch (CoderException ce) { + String errorMessage = "error decoding property JSON value read from database: " + property; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); } - - toscaPolicy.setProperties(propertyMap); - } + })); return toscaPolicy; } @@ -189,38 +176,26 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P type.setName(toscaPolicy.getType()); } else { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, - "PolicyType type not specified, the type of the PolicyType for this policy must be specified in " - + "the type field"); + "PolicyType type not specified, the type of the PolicyType for this policy must be specified in " + + "the type field"); } if (toscaPolicy.getTypeVersion() != null) { type.setVersion(toscaPolicy.getTypeVersion()); } else { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, - "PolicyType version not specified, the version of the PolicyType for this policy must be specified in " - + "the type_version field"); + "PolicyType version not specified, the version of the PolicyType for this policy must be specified" + + " in the type_version field"); } - if (toscaPolicy.getProperties() != null) { - properties = new LinkedHashMap<>(); - - final StandardCoder coder = new StandardCoder(); - - for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) { - // TODO: This is a HACK, we need to validate the properties against their - // TODO: their data type in their policy type definition in TOSCA, which means reading - // TODO: the policy type from the database and parsing the property value object correctly - // TODO: Here we are simply serializing the property value into a string and storing it - // TODO: unvalidated into the database - try { - properties.put(propertyEntry.getKey(), coder.encode(propertyEntry.getValue())); - } catch (CoderException ce) { - String errorMessage = "error encoding property JSON value for database: key=" - + propertyEntry.getKey() + ", value=" + propertyEntry.getValue(); - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); - } + properties = PfUtils.mapMap(toscaPolicy.getProperties(), property -> { + try { + return STANDARD_CODER.encode(property); + } catch (CoderException ce) { + String errorMessage = "error encoding property JSON value for database: " + property; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); } - } + }); // Add the property metadata if it doesn't exist already if (toscaPolicy.getMetadata() == null) { @@ -264,12 +239,12 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "key version is a null version")); + "key version is a null version")); } if (type == null || type.isNullKey()) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "type is null or a null key")); + "type is null or a null key")); } else { result = type.validate(result); } @@ -295,10 +270,10 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P for (Entry<String, String> propertyEntry : properties.entrySet()) { if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "policy property key may not be null ")); + "policy property key may not be null ")); } else if (propertyEntry.getValue() == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "policy property value may not be null ")); + "policy property value may not be null ")); } } } @@ -306,7 +281,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P /** * Validate the policy targets. * - * @param result The result of validations up to now + * @param resultIn The result of validations up to now * @return the validation result */ private PfValidationResult validateTargets(final PfValidationResult resultIn) { @@ -315,7 +290,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P for (PfConceptKey target : targets) { if (target == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "policy target may not be null ")); + "policy target may not be null ")); } else { result = target.validate(result); } @@ -338,19 +313,21 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P } final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept; - if (!super.equals(other)) { - return super.compareTo(other); + int result = super.compareTo(other); + if (result != 0) { + return result; } - if (!type.equals(other.type)) { - return type.compareTo(other.type); + result = type.compareTo(other.type); + if (result != 0) { + return result; } - int retVal = PfUtils.compareObjects(properties, other.properties); - if (retVal != 0) { - return retVal; + result = PfUtils.compareMaps(properties, other.properties); + if (result != 0) { + return result; } - return PfUtils.compareObjects(targets, other.targets); + return PfUtils.compareCollections(targets, other.targets); } } 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 93bfd2a94..7a5493ce1 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 @@ -23,7 +23,6 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -71,13 +70,13 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl @ElementCollection @Lob - private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>(); + private Map<String, JpaToscaProperty> properties; @ElementCollection - private List<PfConceptKey> targets = new ArrayList<>(); + private List<PfConceptKey> targets; @ElementCollection - private List<JpaToscaTrigger> triggers = new ArrayList<>(); + private List<JpaToscaTrigger> triggers; /** * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key. @@ -122,15 +121,7 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl super.setToscaEntity(toscaPolicyType); super.toAuthorative(); - if (properties != null) { - Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>(); - - for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) { - propertyMap.put(entry.getKey(), entry.getValue().toAuthorative()); - } - - toscaPolicyType.setProperties(propertyMap); - } + toscaPolicyType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); return toscaPolicyType; } @@ -293,21 +284,22 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl } final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept; - if (!super.equals(other)) { - return super.compareTo(other); + int result = super.compareTo(other); + if (result != 0) { + return result; } - int retVal = PfUtils.compareObjects(properties, other.properties); - if (retVal != 0) { - return retVal; + result = PfUtils.compareMaps(properties, other.properties); + if (result != 0) { + return result; } - retVal = PfUtils.compareObjects(targets, other.targets); - if (retVal != 0) { - return retVal; + result = PfUtils.compareCollections(targets, other.targets); + if (result != 0) { + return result; } - return PfUtils.compareObjects(triggers, other.triggers); + return PfUtils.compareCollections(triggers, other.triggers); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java index 48ee9d4a2..c56dc6a42 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; @@ -49,7 +48,6 @@ import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status; @@ -86,13 +84,13 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr private Status status = Status.SUPPORTED; @ElementCollection - private List<JpaToscaConstraint> constraints = new ArrayList<>(); + private List<JpaToscaConstraint> constraints; @Column - private JpaToscaEntrySchema entrySchema; + private JpaToscaSchemaDefinition entrySchema; @ElementCollection - private Map<String, String> metadata = new LinkedHashMap<>(); + private Map<String, String> metadata; /** * The Default Constructor creates a {@link JpaToscaProperty} object with a null key. @@ -113,7 +111,7 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr /** * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key. * - * @param key the key + * @param key the key * @param type the key of the property type */ public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) { @@ -136,7 +134,8 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr this.status = copyConcept.status; // Constraints are immutable this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null); - this.entrySchema = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null); + this.entrySchema = + (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null); this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null); } @@ -166,23 +165,13 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class)); } - if (constraints != null) { - List<ToscaConstraint> toscaConstraints = new ArrayList<>(); - - for (JpaToscaConstraint constraint : constraints) { - toscaConstraints.add(constraint.toAuthorative()); - } - - toscaProperty.setConstraints(toscaConstraints); - } + toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative)); if (entrySchema != null) { toscaProperty.setEntrySchema(entrySchema.toAuthorative()); } - if (metadata != null) { - toscaProperty.setMetadata(new LinkedHashMap<>(metadata)); - } + toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem)); return toscaProperty; } @@ -208,23 +197,13 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr defaultValue = null; } - if (toscaProperty.getConstraints() != null) { - constraints = new ArrayList<>(); - - for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) { - constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); - } - } + constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance); if (toscaProperty.getEntrySchema() != null) { - entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema()); - } - - // Add the property metadata if it doesn't exist already - if (toscaProperty.getMetadata() != null) { - metadata = new LinkedHashMap<>(toscaProperty.getMetadata()); + entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema()); } + metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem); } @Override @@ -258,11 +237,7 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr entrySchema.clean(); } - if (metadata != null) { - for (Entry<String, String> metadataEntry : metadata.entrySet()) { - metadataEntry.setValue(metadataEntry.getValue().trim()); - } - } + metadata = PfUtils.mapMap(metadata, String::trim); } @Override @@ -271,14 +246,14 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr if (key.isNullKey()) { result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); } result = key.validate(result); if (type == null || type.isNullKey()) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property type may not be null")); + "property type may not be null")); } return validateFields(result); @@ -295,19 +270,19 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr if (description != null && description.trim().length() == 0) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property description may not be blank")); + "property description may not be blank")); } if (defaultValue != null && defaultValue.trim().length() == 0) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property default value may not be null")); + "property default value may not be null")); } if (constraints != null) { for (JpaToscaConstraint constraint : constraints) { if (constraint == null) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property constraint may not be null ")); + "property constraint may not be null ")); } } } @@ -364,11 +339,16 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr return result; } - result = PfUtils.compareObjects(constraints, other.constraints); + result = PfUtils.compareCollections(constraints, other.constraints); + if (result != 0) { + return result; + } + + result = entrySchema.compareTo(other.entrySchema); if (result != 0) { return result; } - return entrySchema.compareTo(other.entrySchema); + return PfUtils.compareMaps(metadata, other.metadata); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java new file mode 100644 index 000000000..d3d06d4a0 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java @@ -0,0 +1,236 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.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; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; +import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * Class to represent the relationship type in TOSCA definition. + */ + +@Entity +@Table(name = "ToscaRelationshipType") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class JpaToscaRelationshipType extends JpaToscaEntityType<ToscaRelationshipType> + implements PfAuthorative<ToscaRelationshipType> { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map<String, JpaToscaProperty> properties; + + /** + * The Default Constructor creates a {@link JpaToscaRelationshipType} object with a null key. + */ + public JpaToscaRelationshipType() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaRelationshipType} object with the given concept key. + * + * @param key the key + */ + public JpaToscaRelationshipType(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaRelationshipType(final JpaToscaRelationshipType copyConcept) { + super(copyConcept); + this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaRelationshipType(final ToscaRelationshipType authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaRelationshipType toAuthorative() { + ToscaRelationshipType toscaRelationshipType = new ToscaRelationshipType(); + super.setToscaEntity(toscaRelationshipType); + super.toAuthorative(); + + toscaRelationshipType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative)); + + return toscaRelationshipType; + } + + @Override + public void fromAuthorative(final ToscaRelationshipType toscaRelationshipType) { + super.fromAuthorative(toscaRelationshipType); + + // Set properties + if (toscaRelationshipType.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaRelationshipType.getProperties().entrySet()) { + JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); + jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + keyList.addAll(property.getKeys()); + } + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { + property.clean(); + } + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "key version is a null version")); + } + + if (properties != null) { + result = validateProperties(result); + } + + return result; + } + + /** + * Validate the capabiltiy type properties. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (JpaToscaProperty property : properties.values()) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "relationship type property may not be null ")); + } else { + result = property.validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaRelationshipType other = (JpaToscaRelationshipType) otherConcept; + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + return PfUtils.compareMaps(properties, other.properties); + } + + /** + * Get the data types referenced in a relationship type. + * + * @return the data types referenced in a relationship 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/JpaToscaRelationshipTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipTypes.java new file mode 100644 index 000000000..28e1d602c --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipTypes.java @@ -0,0 +1,113 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * This class is a container for TOSCA relationship types. + */ +@Entity +@Table(name = "ToscaRelationshipTypes") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class JpaToscaRelationshipTypes extends PfConceptContainer<JpaToscaRelationshipType, ToscaRelationshipType> { + public static final String DEFAULT_NAME = "ToscaRelationshipTypesSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + private static final long serialVersionUID = -4157979965271220098L; + + /** + * The Default Constructor creates a {@link JpaToscaRelationshipTypes} object with a null artifact key and creates + * an empty concept map. + */ + public JpaToscaRelationshipTypes() { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + } + + /** + * The Key Constructor creates a {@link JpaToscaRelationshipTypes} object with the given artifact key and creates an + * empty concept map. + * + * @param key the concept key + */ + public JpaToscaRelationshipTypes(final PfConceptKey key) { + super(key, new TreeMap<>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public JpaToscaRelationshipTypes(final PfConceptKey key, + final Map<PfConceptKey, JpaToscaRelationshipType> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaRelationshipTypes(final JpaToscaRelationshipTypes copyConcept) { + super(copyConcept); + } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaRelationshipTypes(final List<Map<String, ToscaRelationshipType>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + this.fromAuthorative(authorativeConceptMapList); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + // Check that all ancestors of this policy type exist + for (JpaToscaRelationshipType relationshipType : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, relationshipType, result); + } + + return result; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java new file mode 100644 index 000000000..aebc31caf --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java @@ -0,0 +1,303 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Requirement Model + * ================================================================================ + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * 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. + * 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.tosca.simple.concepts; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.persistence.Column; +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.onap.policy.common.utils.coder.YamlJsonTranslator; +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.base.PfKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement; + +/** + * Class to represent the requirement in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaRequirement") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement> + implements PfAuthorative<ToscaRequirement> { + + private static final long serialVersionUID = 2785481541573683089L; + private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED"; + private static final Double JPA_UNBOUNDED_VALUE = -1.0; + + @Column + private String capability; + + @Column + private String node; + + @Column + private String relationship; + + @ElementCollection + private List<Double> occurrences; + + @ElementCollection + @Lob + private Map<String, String> properties; + + /** + * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key. + */ + public JpaToscaRequirement() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key. + * + * @param key the key + */ + public JpaToscaRequirement(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) { + super(copyConcept); + this.capability = copyConcept.capability; + this.node = copyConcept.node; + this.relationship = copyConcept.relationship; + this.occurrences = new ArrayList<>(copyConcept.occurrences); + this.properties = PfUtils.mapMap(copyConcept.properties, String::new); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaRequirement(final ToscaRequirement authorativeConcept) { + super(new PfConceptKey()); + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaRequirement toAuthorative() { + ToscaRequirement toscaRequirement = new ToscaRequirement(); + super.setToscaEntity(toscaRequirement); + super.toAuthorative(); + + toscaRequirement.setCapability(capability); + toscaRequirement.setNode(node); + toscaRequirement.setRelationship(relationship); + + if (occurrences != null) { + List<Object> occurrencesList = new ArrayList<>(occurrences); + for (Double occurrence : occurrences) { + if (occurrence == JPA_UNBOUNDED_VALUE) { + occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL); + } else { + occurrencesList.add(occurrence.doubleValue()); + } + } + toscaRequirement.setOccurrences(occurrencesList); + } + + if (properties != null) { + Map<String, Object> propertiesMap = new LinkedHashMap<>(); + + for (Map.Entry<String, String> entry : properties.entrySet()) { + propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class)); + } + + toscaRequirement.setProperties(propertiesMap); + } + + return toscaRequirement; + } + + @Override + public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) { + super.fromAuthorative(toscaRequirement); + + capability = toscaRequirement.getCapability(); + node = toscaRequirement.getNode(); + relationship = toscaRequirement.getRelationship(); + + if (toscaRequirement.getOccurrences() != null) { + occurrences = new ArrayList<>(); + for (Object occurrence : toscaRequirement.getOccurrences()) { + if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) { + occurrences.add(JPA_UNBOUNDED_VALUE); + } else { + occurrences.add((Double) occurrence); + } + } + } + + if (toscaRequirement.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) { + String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue()); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + + } + + @Override + public List<PfKey> getKeys() { + return super.getKeys(); + } + + @Override + public void clean() { + super.clean(); + + capability = capability.trim(); + node = node.trim(); + relationship = relationship.trim(); + + properties = PfUtils.mapMap(properties, String::trim); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (properties != null) { + result = validateProperties(result); + } + + if (occurrences != null) { + result = validateOccurrences(result); + } + + return result; + } + + /** + * Validate the properties. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (String property : properties.values()) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), + PfValidationResult.ValidationResult.INVALID, "topology template property may not be null ")); + } + } + return result; + } + + /** + * Validate the occurrences. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateOccurrences(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (Double occurrence : occurrences) { + if (occurrence == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), + PfValidationResult.ValidationResult.INVALID, "requirement occurrence value may not be null ")); + } + if (occurrence < -1.0) { + result.addValidationMessage( + new PfValidationMessage(getKey(), this.getClass(), PfValidationResult.ValidationResult.INVALID, + "requirement occurrence value may not be negative")); + } + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + + if (this == otherConcept) { + return 0; + } + + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept; + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + result = capability.compareTo(other.capability); + if (result != 0) { + return result; + } + + result = node.compareTo(other.node); + if (result != 0) { + return result; + } + + result = relationship.compareTo(other.relationship); + if (result != 0) { + return result; + } + + result = PfUtils.compareCollections(occurrences, other.occurrences); + if (result != 0) { + return result; + } + + return PfUtils.compareMaps(properties, other.properties); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirements.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirements.java new file mode 100644 index 000000000..08dbb43de --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirements.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.tosca.simple.concepts; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement; +import org.onap.policy.models.tosca.utils.ToscaUtils; + +/** + * This class is a container for TOSCA requirements. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaRequirements") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) +public class JpaToscaRequirements extends PfConceptContainer<JpaToscaRequirement, ToscaRequirement> + implements PfAuthorative<List<Map<String, ToscaRequirement>>> { + private static final long serialVersionUID = -7526648702327776101L; + + public static final String DEFAULT_NAME = "ToscaRequirementsSimple"; + public static final String DEFAULT_VERSION = "1.0.0"; + + /** + * The Default Constructor creates a {@link JpaToscaRequirements} object with a null artifact key and creates an + * empty concept map. + */ + public JpaToscaRequirements() { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + } + + /** + * The Key Constructor creates a {@link JpaToscaRequirements} object with the given artifact key and creates an + * empty concept map. + * + * @param key the concept key + */ + public JpaToscaRequirements(final PfConceptKey key) { + super(key, new TreeMap<>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public JpaToscaRequirements(final PfConceptKey key, final Map<PfConceptKey, JpaToscaRequirement> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaRequirements(final JpaToscaRequirements copyConcept) { + super(copyConcept); + } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaRequirements(final List<Map<String, ToscaRequirement>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); + this.fromAuthorative(authorativeConceptMapList); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (JpaToscaRequirement requirement : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, requirement, result); + } + + return result; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinition.java index ef6aa7e92..6bd1b44cd 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinition.java @@ -53,8 +53,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition; @Data @EqualsAndHashCode(callSuper = false) @NoArgsConstructor -public class JpaToscaEntrySchema - implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaEntrySchema> { +public class JpaToscaSchemaDefinition + implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaSchemaDefinition> { private static final long serialVersionUID = 3645882081163287058L; @@ -68,14 +68,14 @@ public class JpaToscaEntrySchema private String description; @ElementCollection - private List<JpaToscaConstraint> constraints = new ArrayList<>(); + private List<JpaToscaConstraint> constraints; /** - * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields. + * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields. * * @param type the type of the entry schema */ - public JpaToscaEntrySchema(@NonNull final PfConceptKey type) { + public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) { this.type = type; } @@ -84,7 +84,7 @@ public class JpaToscaEntrySchema * * @param copyConcept the concept to copy from */ - public JpaToscaEntrySchema(@NonNull final JpaToscaEntrySchema copyConcept) { + public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) { copyConcept.copyTo(this); } @@ -93,7 +93,7 @@ public class JpaToscaEntrySchema * * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaEntrySchema(final ToscaSchemaDefinition authorativeConcept) { + public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) { this.fromAuthorative(authorativeConcept); } @@ -180,7 +180,7 @@ public class JpaToscaEntrySchema } @Override - public int compareTo(final JpaToscaEntrySchema other) { + public int compareTo(final JpaToscaSchemaDefinition other) { if (other == null) { return -1; } @@ -193,7 +193,7 @@ public class JpaToscaEntrySchema return result; } - return PfUtils.compareObjects(constraints, other.constraints); + return PfUtils.compareCollections(constraints, other.constraints); } /** @@ -202,10 +202,10 @@ public class JpaToscaEntrySchema * @param target the other schemaa * @return the copied concept */ - public JpaToscaEntrySchema copyTo(@NonNull final JpaToscaEntrySchema target) { - Assertions.instanceOf(target, JpaToscaEntrySchema.class); + public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) { + Assertions.instanceOf(target, JpaToscaSchemaDefinition.class); - final JpaToscaEntrySchema copy = (target); + final JpaToscaSchemaDefinition copy = (target); copy.setType(new PfConceptKey(type)); copy.setDescription(description); 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 f1a6a395f..791801616 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 @@ -49,8 +49,11 @@ import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType; import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -91,6 +94,36 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumns( { + @JoinColumn(name = "capabilityTypesName", referencedColumnName = "name"), + @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version") + } + ) + @SerializedName("capability_types") + private JpaToscaCapabilityTypes capabilityTypes; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns( + { + @JoinColumn(name = "relationshipTypesName", referencedColumnName = "name"), + @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version") + } + ) + @SerializedName("relationship_types") + private JpaToscaRelationshipTypes relationshipTypes; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns( + { + @JoinColumn(name = "nodeTypesName", referencedColumnName = "name"), + @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version") + } + ) + @SerializedName("node_types") + private JpaToscaNodeTypes nodeTypes; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns( + { @JoinColumn(name = "policyTypesName", referencedColumnName = "name"), @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version") } @@ -147,6 +180,12 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp super(copyConcept); this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion; this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null); + this.capabilityTypes = + (copyConcept.capabilityTypes != null ? new JpaToscaCapabilityTypes(copyConcept.capabilityTypes) : null); + this.relationshipTypes = + (copyConcept.relationshipTypes != null ? new JpaToscaRelationshipTypes(copyConcept.relationshipTypes) + : null); + this.nodeTypes = (copyConcept.nodeTypes != null ? new JpaToscaNodeTypes(copyConcept.nodeTypes) : null); this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null); this.topologyTemplate = (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate) @@ -179,6 +218,30 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp } } + if (capabilityTypes != null) { + toscaServiceTemplate.setCapabilityTypes(new LinkedHashMap<>()); + List<Map<String, ToscaCapabilityType>> capabilityTypeMapList = capabilityTypes.toAuthorative(); + for (Map<String, ToscaCapabilityType> capabilityTypeMap : capabilityTypeMapList) { + toscaServiceTemplate.getCapabilityTypes().putAll(capabilityTypeMap); + } + } + + if (relationshipTypes != null) { + toscaServiceTemplate.setRelationshipTypes(new LinkedHashMap<>()); + List<Map<String, ToscaRelationshipType>> relationshipTypeMapList = relationshipTypes.toAuthorative(); + for (Map<String, ToscaRelationshipType> relationshipTypeMap : relationshipTypeMapList) { + toscaServiceTemplate.getRelationshipTypes().putAll(relationshipTypeMap); + } + } + + if (nodeTypes != null) { + toscaServiceTemplate.setNodeTypes(new LinkedHashMap<>()); + List<Map<String, ToscaNodeType>> nodeTypeMapList = nodeTypes.toAuthorative(); + for (Map<String, ToscaNodeType> nodeTypeMap : nodeTypeMapList) { + toscaServiceTemplate.getNodeTypes().putAll(nodeTypeMap); + } + } + if (policyTypes != null) { toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative(); @@ -213,6 +276,21 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes())); } + if (toscaServiceTemplate.getCapabilityTypes() != null) { + capabilityTypes = new JpaToscaCapabilityTypes(); + capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes())); + } + + if (toscaServiceTemplate.getRelationshipTypes() != null) { + relationshipTypes = new JpaToscaRelationshipTypes(); + relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes())); + } + + if (toscaServiceTemplate.getNodeTypes() != null) { + nodeTypes = new JpaToscaNodeTypes(); + nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes())); + } + if (toscaServiceTemplate.getPolicyTypes() != null) { policyTypes = new JpaToscaPolicyTypes(); policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes())); @@ -232,6 +310,18 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp keyList.addAll(dataTypes.getKeys()); } + if (capabilityTypes != null) { + keyList.addAll(capabilityTypes.getKeys()); + } + + if (relationshipTypes != null) { + keyList.addAll(relationshipTypes.getKeys()); + } + + if (nodeTypes != null) { + keyList.addAll(nodeTypes.getKeys()); + } + if (policyTypes != null) { keyList.addAll(policyTypes.getKeys()); } @@ -251,6 +341,18 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp dataTypes.clean(); } + if (capabilityTypes != null) { + capabilityTypes.clean(); + } + + if (relationshipTypes != null) { + relationshipTypes.clean(); + } + + if (nodeTypes != null) { + nodeTypes.clean(); + } + if (policyTypes != null) { policyTypes.clean(); } @@ -273,6 +375,18 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp result = dataTypes.validate(result); } + if (capabilityTypes != null) { + result = capabilityTypes.validate(result); + } + + if (relationshipTypes != null) { + result = relationshipTypes.validate(result); + } + + if (nodeTypes != null) { + result = nodeTypes.validate(result); + } + if (policyTypes != null) { result = policyTypes.validate(result); } @@ -305,6 +419,21 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp return result; } + result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(nodeTypes, other.nodeTypes); + if (result != 0) { + return result; + } + result = ObjectUtils.compare(policyTypes, other.policyTypes); if (result != 0) { return result; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeInterval.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeInterval.java index 5e5d01460..783e2b576 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeInterval.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeInterval.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. @@ -159,13 +159,14 @@ public class JpaToscaTimeInterval extends PfConcept { } final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); + int result = key.compareTo(other.key); + if (result != 0) { + return result; } - int returnVal = PfUtils.compareObjects(startTime, other.startTime); - if (returnVal != 0) { - return returnVal; + result = PfUtils.compareObjects(startTime, other.startTime); + if (result != 0) { + return result; } return PfUtils.compareObjects(endTime, other.endTime); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java index 6072054bc..32c459cc9 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java @@ -21,9 +21,14 @@ package org.onap.policy.models.tosca.simple.concepts; +import com.google.gson.annotations.SerializedName; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; +import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; @@ -31,6 +36,7 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; +import javax.persistence.Lob; import javax.persistence.OneToOne; import javax.persistence.Table; import lombok.Data; @@ -41,9 +47,12 @@ import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; /** @@ -69,6 +78,20 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative private String description; // @formatter:off + @ElementCollection + @Lob + private Map<String, JpaToscaParameter> inputs; + + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns( + { + @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name"), + @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version") + } + ) + @SerializedName("data_types") + private JpaToscaNodeTemplates nodeTemplates; + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumns( { @@ -105,6 +128,9 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative super(copyConcept); this.key = new PfReferenceKey(copyConcept.key); this.description = copyConcept.description; + this.inputs = PfUtils.mapMap(copyConcept.inputs, JpaToscaParameter::new); + this.nodeTemplates = + (copyConcept.nodeTemplates != null ? new JpaToscaNodeTemplates(copyConcept.nodeTemplates) : null); this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null); } @@ -123,6 +149,24 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative toscaTopologyTemplate.setDescription(description); + if (inputs != null) { + Map<String, ToscaParameter> inputMap = new LinkedHashMap<>(); + + for (Map.Entry<String, JpaToscaParameter> entry : inputs.entrySet()) { + inputMap.put(entry.getKey(), entry.getValue().toAuthorative()); + } + + toscaTopologyTemplate.setInputs(inputMap); + } + + if (nodeTemplates != null) { + toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>()); + List<Map<String, ToscaNodeTemplate>> nodeTemplateMapList = nodeTemplates.toAuthorative(); + for (Map<String, ToscaNodeTemplate> nodeTemplateMap : nodeTemplateMapList) { + toscaTopologyTemplate.getNodeTemplates().putAll(nodeTemplateMap); + } + } + if (policies != null) { toscaTopologyTemplate.setPolicies(policies.toAuthorative()); } @@ -134,6 +178,20 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) { description = toscaTopologyTemplate.getDescription(); + if (toscaTopologyTemplate.getInputs() != null) { + inputs = new LinkedHashMap<>(); + for (Map.Entry<String, ToscaParameter> toscaInputEntry : toscaTopologyTemplate.getInputs().entrySet()) { + JpaToscaParameter jpaInput = new JpaToscaParameter(toscaInputEntry.getValue()); + jpaInput.setKey(new PfReferenceKey(getKey(), toscaInputEntry.getKey())); + inputs.put(toscaInputEntry.getKey(), jpaInput); + } + } + + if (toscaTopologyTemplate.getNodeTemplates() != null) { + nodeTemplates = new JpaToscaNodeTemplates(); + nodeTemplates.fromAuthorative(Collections.singletonList(toscaTopologyTemplate.getNodeTemplates())); + } + if (toscaTopologyTemplate.getPolicies() != null) { policies = new JpaToscaPolicies(); policies.fromAuthorative(toscaTopologyTemplate.getPolicies()); @@ -144,6 +202,16 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative public List<PfKey> getKeys() { final List<PfKey> keyList = getKey().getKeys(); + if (inputs != null) { + for (JpaToscaParameter input : inputs.values()) { + keyList.addAll(input.getKeys()); + } + } + + if (nodeTemplates != null) { + keyList.addAll(nodeTemplates.getKeys()); + } + if (policies != null) { keyList.addAll(policies.getKeys()); } @@ -157,6 +225,16 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative description = (description != null ? description.trim() : null); + if (inputs != null) { + for (JpaToscaParameter input : inputs.values()) { + input.clean(); + } + } + + if (nodeTemplates != null) { + nodeTemplates.clean(); + } + if (policies != null) { policies.clean(); } @@ -178,6 +256,15 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative "property description may not be blank")); } + if (inputs != null) { + result = validateInputs(result); + } + + + if (nodeTemplates != null) { + result = nodeTemplates.validate(result); + } + if (policies != null) { result = policies.validate(result); } @@ -185,6 +272,26 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative return result; } + /** + * Validate the inputs. + * + * @param resultIn The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateInputs(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (JpaToscaParameter input : inputs.values()) { + if (input == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "topology template input may not be null ")); + } else { + result = input.validate(result); + } + } + return result; + } + @Override public int compareTo(final PfConcept otherConcept) { int result = compareToWithoutEntities(otherConcept); @@ -192,11 +299,23 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative return result; } - return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies); + final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept; + + result = PfUtils.compareObjects(inputs, other.inputs); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(nodeTemplates, other.nodeTemplates); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(policies, other.policies); } /** - * Compare this topology template to another topology template, ignoring contained entitites. + * Compare this topology template to another topology template, ignoring contained entities. * * @param otherConcept the other topology template * @return the result of the comparison diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java index 417e83fe0..10a9d2f66 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.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. @@ -137,7 +137,7 @@ public class JpaToscaTrigger extends PfConcept { this.eventType = copyConcept.eventType; this.schedule = (copyConcept.schedule != null ? new JpaToscaTimeInterval(copyConcept.schedule) : null); this.targetFilter = - (copyConcept.targetFilter != null ? new JpaToscaEventFilter(copyConcept.targetFilter) : null); + (copyConcept.targetFilter != null ? new JpaToscaEventFilter(copyConcept.targetFilter) : null); this.condition = copyConcept.condition; this.constraint = copyConcept.constraint; this.period = copyConcept.period; @@ -245,16 +245,16 @@ public class JpaToscaTrigger extends PfConcept { } final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); + int result = key.compareTo(other.key); + if (result != 0) { + return result; } return compareFields(other); } /** - * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty - * object. + * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty object. * * @param other the other ToscaTrigger object */ diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java index 5b6855d95..7bf4d29e8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java @@ -125,9 +125,9 @@ public class ToscaServiceTemplateUtils { for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer .getConceptMap().entrySet()) { - JpaToscaEntityType<? extends ToscaEntity> containerEntry = + JpaToscaEntityType<? extends ToscaEntity> containerEntity = compositeContainer.getConceptMap().get(fragmentEntry.getKey()); - if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) { + if (containerEntity != null && containerEntity.compareTo(fragmentEntry.getValue()) != 0) { result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(), ToscaServiceTemplateUtils.class, ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity")); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignmentTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignmentTest.java new file mode 100644 index 000000000..ee2fee86b --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignmentTest.java @@ -0,0 +1,158 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.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.assertNotNull; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment; + +/** + * DAO test for JpaToscaCapabilityAssignment. + */ +public class JpaToscaCapabilityAssignmentTest { + + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; + + @Test + public void testPropertyPojo() { + assertNotNull(new JpaToscaCapabilityAssignment()); + assertNotNull(new JpaToscaCapabilityAssignment(new PfConceptKey())); + assertNotNull(new JpaToscaCapabilityAssignment(new JpaToscaCapabilityAssignment())); + assertNotNull(new JpaToscaCapabilityAssignment(new ToscaCapabilityAssignment())); + + assertThatThrownBy(() -> new JpaToscaCapabilityAssignment((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaCapabilityAssignment((JpaToscaCapabilityAssignment) null)) + .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); + assertThatThrownBy(() -> new JpaToscaCapabilityAssignment((ToscaCapabilityAssignment) null)) + .hasMessageMatching("authorativeConcept is marked .*on.*ull but is null"); + + PfConceptKey caKey = new PfConceptKey("tParentKey", "0.0.1"); + + JpaToscaCapabilityAssignment caNull = new JpaToscaCapabilityAssignment(caKey); + caNull.setProperties(null); + caNull.setAttributes(null); + caNull.setOccurrences(null); + + assertEquals(caNull, new JpaToscaCapabilityAssignment(caNull)); + + JpaToscaCapabilityAssignment ca = new JpaToscaCapabilityAssignment(caKey); + + assertEquals(ca, new JpaToscaCapabilityAssignment(ca)); + assertEquals(caKey, ca.getKeys().get(0)); + + ca.clean(); + ca.validate(new PfValidationResult()); + assertThat(ca.getProperties()).isNullOrEmpty(); + assertThat(ca.getAttributes()).isNullOrEmpty(); + + ca.setProperties(null); + ca.setAttributes(null); + ca.setOccurrences(null); + ca.clean(); + ca.validate(new PfValidationResult()); + assertEquals(null, ca.getProperties()); + assertEquals(null, ca.getAttributes()); + + Map<String, String> properties = new LinkedHashMap<>(); + properties.put("Key0", " Untrimmed Value "); + ca.setProperties(properties); + + Map<String, String> attributes = new LinkedHashMap<>(); + attributes.put("Key0", " Untrimmed Value "); + ca.setAttributes(attributes); + + List<Integer> occurrences = new ArrayList<>(); + occurrences.add(12345); + ca.setOccurrences(occurrences); + + ca.clean(); + ca.validate(new PfValidationResult()); + assertEquals("Untrimmed Value", ca.getProperties().get("Key0")); + assertEquals("Untrimmed Value", ca.getAttributes().get("Key0")); + + ca.getProperties().put("Key1", null); + ca.getAttributes().put("Key1", null); + ca.getOccurrences().add(null); + ca.getOccurrences().add(-12345); + PfValidationResult result = ca.validate(new PfValidationResult()); + assertThat(result.toString()).contains("capability assignment property Key1 value may not be null"); + assertThat(result.toString()).contains("capability assignment attribute Key1 value may not be null"); + assertThat(result.toString()).contains("capability assignment occurrence value may not be negative"); + } + + @Test + public void testCompareTo() { + assertEquals(-1, new JpaToscaCapabilityAssignment().compareTo(null)); + assertEquals(0, new JpaToscaCapabilityAssignment().compareTo(new JpaToscaCapabilityAssignment())); + + JpaToscaCapabilityAssignment ca = new JpaToscaCapabilityAssignment(); + assertEquals(0, ca.compareTo(ca)); + assertEquals(18, ca.compareTo(new PfConceptKey())); + + JpaToscaCapabilityAssignment ca2 = new JpaToscaCapabilityAssignment(); + ca2.getKey().setName("ca2"); + assertEquals(-21, ca.compareTo(ca2)); + + ca.getKey().setName("ca"); + ca2.getKey().setName("ca"); + + ca.setProperties(new LinkedHashMap<>()); + ca2.setProperties(new LinkedHashMap<>()); + ca.getProperties().put("Key0", "Value0"); + assertEquals(-1737938642, ca.compareTo(ca2)); + ca2.getProperties().put("Key0", "Value0"); + assertEquals(0, ca.compareTo(ca2)); + + ca.setAttributes(new LinkedHashMap<>()); + ca2.setAttributes(new LinkedHashMap<>()); + ca.getAttributes().put("Key0", "Value0"); + assertEquals(-1737938642, ca.compareTo(ca2)); + ca2.getAttributes().put("Key0", "Value0"); + assertEquals(0, ca.compareTo(ca2)); + + ca.setOccurrences(new ArrayList<>()); + ca2.setOccurrences(new ArrayList<>()); + ca.getOccurrences().add(12345); + assertEquals(12375, ca.compareTo(ca2)); + ca2.getOccurrences().add(12345); + assertEquals(0, ca.compareTo(ca2)); + } + + @Test + public void testAuthorative() { + ToscaCapabilityAssignment tca = + new JpaToscaCapabilityAssignment(new ToscaCapabilityAssignment()).toAuthorative(); + + JpaToscaCapabilityAssignment jtca = new JpaToscaCapabilityAssignment(tca); + ToscaCapabilityAssignment tca2 = jtca.toAuthorative(); + assertEquals(tca, tca2); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java index e00e48f21..7168f488c 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java @@ -174,7 +174,7 @@ public class JpaToscaDataTypeTest { JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4")); prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1")); - prop3.setEntrySchema(new JpaToscaEntrySchema()); + prop3.setEntrySchema(new JpaToscaSchemaDefinition()); prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1")); assertTrue(prop3.validate(new PfValidationResult()).isValid()); @@ -183,7 +183,7 @@ public class JpaToscaDataTypeTest { JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4")); prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1")); - prop4.setEntrySchema(new JpaToscaEntrySchema()); + prop4.setEntrySchema(new JpaToscaSchemaDefinition()); prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1")); assertTrue(prop4.validate(new PfValidationResult()).isValid()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java index 14a0b6a96..7f356224f 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java @@ -30,11 +30,13 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @@ -60,8 +62,8 @@ public class JpaToscaPolicyTest { assertThatThrownBy(() -> { new JpaToscaPolicy(pol); }).hasMessage( - "PolicyType version not specified, the version of the PolicyType for this policy must be specified in the " - + "type_version field"); + "PolicyType version not specified, the version of the PolicyType for this policy must be specified in" + + " the type_version field"); assertThatThrownBy(() -> { new JpaToscaPolicy((PfConceptKey) null); @@ -182,4 +184,44 @@ public class JpaToscaPolicyTest { tp.fromAuthorative(pol1); assertEquals("2.2.3", tp.getType().getVersion()); } + + @Test + public void testPolicyProperties() { + + Map<String, Object> properties = new LinkedHashMap<>(); + + // @formatter:off + properties.put("byte", Byte.valueOf("2")); + properties.put("short", Short.valueOf("1234")); + properties.put("int", Integer.valueOf("12345678")); + properties.put("long", Long.valueOf("1234567890")); + properties.put("float", Float.valueOf("12345.678")); + properties.put("double", Double.valueOf("-12345.6789")); + properties.put("char", '%'); + properties.put("string", "hello"); + properties.put("boolean", false); + // @formatter:on + + ToscaPolicy tp = new ToscaPolicy(); + tp.setType("org.onap.Policy"); + tp.setTypeVersion("1.2.3"); + tp.setProperties(properties); + + JpaToscaPolicy jtp = new JpaToscaPolicy(tp); + assertEquals(0, PfUtils.compareCollections(tp.getProperties().keySet(), jtp.getProperties().keySet())); + + ToscaPolicy tpFromTo = jtp.toAuthorative(); + + // @formatter:off + assertEquals(2, tpFromTo.getProperties().get("byte")); + assertEquals(1234, tpFromTo.getProperties().get("short")); + assertEquals(12345678, tpFromTo.getProperties().get("int")); + assertEquals(1234567890, tpFromTo.getProperties().get("long")); + assertEquals(12345.678, tpFromTo.getProperties().get("float")); + assertEquals(-12345.6789, tpFromTo.getProperties().get("double")); + assertEquals("%", tpFromTo.getProperties().get("char")); + assertEquals("hello", tpFromTo.getProperties().get("string")); + assertEquals(false, tpFromTo.getProperties().get("boolean")); + // @formatter:on + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java index 5a18902d0..43cacda19 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java @@ -221,7 +221,7 @@ public class JpaToscaPolicyTypeTest { JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4")); prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1")); - prop3.setEntrySchema(new JpaToscaEntrySchema()); + prop3.setEntrySchema(new JpaToscaSchemaDefinition()); prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1")); assertTrue(prop3.validate(new PfValidationResult()).isValid()); @@ -230,7 +230,7 @@ public class JpaToscaPolicyTypeTest { JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4")); prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1")); - prop4.setEntrySchema(new JpaToscaEntrySchema()); + prop4.setEntrySchema(new JpaToscaSchemaDefinition()); prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1")); assertTrue(prop4.validate(new PfValidationResult()).isValid()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java index ac2f6030f..0fa0d3f14 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java @@ -91,7 +91,7 @@ public class JpaToscaPropertyTest { assertEquals(constraints, tp.getConstraints()); PfConceptKey typeKey = new PfConceptKey("type", VERSION_001); - JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey); + JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey); tp.setEntrySchema(tes); TreeMap<String, String> metadata = new TreeMap<>(); @@ -134,6 +134,8 @@ public class JpaToscaPropertyTest { otherDt.setConstraints(constraints); assertNotEquals(0, tp.compareTo(otherDt)); otherDt.setEntrySchema(tes); + assertNotEquals(0, tp.compareTo(otherDt)); + otherDt.setMetadata(metadata); assertEquals(0, tp.compareTo(otherDt)); otherDt.setRequired(true); @@ -220,7 +222,7 @@ public class JpaToscaPropertyTest { tp.setConstraints(constraints); PfConceptKey typeKey = new PfConceptKey("type", VERSION_001); - JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey); + JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey); tp.setEntrySchema(tes); TreeMap<String, String> metadata = new TreeMap<>(); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchemaTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinitionTest.java index 83d6aad97..e7163f756 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchemaTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinitionTest.java @@ -39,23 +39,23 @@ import org.onap.policy.models.base.PfValidationResult; * * @author Liam Fallon (liam.fallon@est.tech) */ -public class JpaToscaEntrySchemaTest { +public class JpaToscaSchemaDefinitionTest { private static final String A_DESCRIPTION = "A Description"; @Test public void testEntrySchemaPojo() { - assertNotNull(new JpaToscaEntrySchema(new PfConceptKey())); - assertNotNull(new JpaToscaEntrySchema(new JpaToscaEntrySchema(new PfConceptKey()))); + assertNotNull(new JpaToscaSchemaDefinition(new PfConceptKey())); + assertNotNull(new JpaToscaSchemaDefinition(new JpaToscaSchemaDefinition(new PfConceptKey()))); - assertThatThrownBy(() -> new JpaToscaEntrySchema((PfConceptKey) null)) + assertThatThrownBy(() -> new JpaToscaSchemaDefinition((PfConceptKey) null)) .hasMessageMatching("type is marked .*on.*ull but is null"); - assertThatThrownBy(() -> new JpaToscaEntrySchema((JpaToscaEntrySchema) null)) + assertThatThrownBy(() -> new JpaToscaSchemaDefinition((JpaToscaSchemaDefinition) null)) .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); PfConceptKey typeKey = new PfConceptKey("type", "0.0.1"); - JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey); + JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey); tes.setDescription(A_DESCRIPTION); assertEquals(A_DESCRIPTION, tes.getDescription()); @@ -66,18 +66,18 @@ public class JpaToscaEntrySchemaTest { tes.setConstraints(constraints); assertEquals(constraints, tes.getConstraints()); - JpaToscaEntrySchema tdtClone0 = new JpaToscaEntrySchema(tes); + JpaToscaSchemaDefinition tdtClone0 = new JpaToscaSchemaDefinition(tes); assertEquals(tes, tdtClone0); assertEquals(0, tes.compareTo(tdtClone0)); - JpaToscaEntrySchema tdtClone1 = new JpaToscaEntrySchema(tes); + JpaToscaSchemaDefinition tdtClone1 = new JpaToscaSchemaDefinition(tes); assertEquals(tes, tdtClone1); assertEquals(0, tes.compareTo(tdtClone1)); assertEquals(-1, tes.compareTo(null)); assertEquals(0, tes.compareTo(tes)); - JpaToscaEntrySchema otherEs = new JpaToscaEntrySchema(typeKey); + JpaToscaSchemaDefinition otherEs = new JpaToscaSchemaDefinition(typeKey); assertNotEquals(0, tes.compareTo(otherEs)); otherEs.setType(typeKey); @@ -90,13 +90,13 @@ public class JpaToscaEntrySchemaTest { assertThatThrownBy(() -> tes.copyTo(null)).hasMessageMatching("target is marked .*on.*ull but is null"); assertEquals(1, tes.getKeys().size()); - assertEquals(1, new JpaToscaEntrySchema(typeKey).getKeys().size()); + assertEquals(1, new JpaToscaSchemaDefinition(typeKey).getKeys().size()); - new JpaToscaEntrySchema(typeKey).clean(); + new JpaToscaSchemaDefinition(typeKey).clean(); tes.clean(); assertEquals(tdtClone0, tes); - assertTrue(new JpaToscaEntrySchema(typeKey).validate(new PfValidationResult()).isValid()); + assertTrue(new JpaToscaSchemaDefinition(typeKey).validate(new PfValidationResult()).isValid()); assertTrue(tes.validate(new PfValidationResult()).isValid()); tes.setType(PfConceptKey.getNullKey()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java index 65d832b98..bb0d2dcb5 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java @@ -29,6 +29,7 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; import org.junit.Test; @@ -154,6 +155,8 @@ public class JpaToscaServiceTemplateTest { JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1")); JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0")); prop0.setType(dt0.getKey()); + + pt0.setProperties(new LinkedHashMap<>()); pt0.getProperties().put(prop0.getKey().getLocalName(), prop0); result = tst.validate(new PfValidationResult()); assertFalse(result.isOk()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java index 1e18f12d4..3c363b364 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import java.util.LinkedHashMap; import java.util.Properties; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; @@ -119,7 +120,7 @@ public class SimpleToscaProviderTest { serviceTemplate.getDataTypes().getConceptMap().put(dataType0Key, dataType0); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); assertEquals(1, createdServiceTemplate.getDataTypes().getConceptMap().size()); assertEquals(dataType0, createdServiceTemplate.getDataTypes().get(dataType0Key)); @@ -128,19 +129,19 @@ public class SimpleToscaProviderTest { dataType0.setDescription("Updated Description"); JpaToscaServiceTemplate updatedServiceTemplate = - new SimpleToscaProvider().updateDataTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().updateDataTypes(pfDao, serviceTemplate); assertEquals(dataType0, updatedServiceTemplate.getDataTypes().get(dataType0Key)); assertEquals("Updated Description", updatedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); JpaToscaServiceTemplate gotServiceTemplate = - new SimpleToscaProvider().getDataTypes(pfDao, dataType0Key.getName(), dataType0Key.getVersion()); + new SimpleToscaProvider().getDataTypes(pfDao, dataType0Key.getName(), dataType0Key.getVersion()); assertEquals(dataType0, gotServiceTemplate.getDataTypes().get(dataType0Key)); assertEquals("Updated Description", gotServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, new PfConceptKey("IDontExist:0.0.1"))) - .hasMessage("data type IDontExist:0.0.1 not found"); + .hasMessage("data type IDontExist:0.0.1 not found"); JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key); @@ -161,7 +162,7 @@ public class SimpleToscaProviderTest { assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key)) - .hasMessage("no data types found"); + .hasMessage("no data types found"); // Create the data type again new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); @@ -169,12 +170,13 @@ public class SimpleToscaProviderTest { JpaToscaPolicyType pt0v2 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.2")); JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0v2.getKey(), "prop0")); prop0.setType(dataType0Key); + pt0v2.setProperties(new LinkedHashMap<>()); pt0v2.getProperties().put(prop0.getKey().getLocalName(), prop0); updatedServiceTemplate.getPolicyTypes().getConceptMap().put(pt0v2.getKey(), pt0v2); new SimpleToscaProvider().createPolicyTypes(pfDao, updatedServiceTemplate); assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key)) - .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2"); + .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2"); JpaToscaDataType dataType0v2 = new JpaToscaDataType(new PfConceptKey("DataType0:0.0.2")); updatedServiceTemplate.getDataTypes().getConceptMap().put(dataType0v2.getKey(), dataType0v2); @@ -186,17 +188,18 @@ public class SimpleToscaProviderTest { assertNull(deletedServiceTemplate.getDataTypes().get(dataType0v2.getKey()).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key)) - .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2"); + .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2"); JpaToscaDataType dataType1 = new JpaToscaDataType(new PfConceptKey("DataType1:0.0.3")); JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dataType1.getKey(), "prop1")); prop1.setType(dataType0v2.getKey()); + dataType1.setProperties(new LinkedHashMap<>()); dataType1.getProperties().put(prop1.getKey().getLocalName(), prop1); updatedServiceTemplate.getDataTypes().getConceptMap().put(dataType1.getKey(), dataType1); new SimpleToscaProvider().createDataTypes(pfDao, updatedServiceTemplate); assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0v2.getKey())) - .hasMessage("data type DataType0:0.0.2 is in use, it is referenced in data type DataType1:0.0.3"); + .hasMessage("data type DataType0:0.0.2 is in use, it is referenced in data type DataType1:0.0.3"); } @Test @@ -216,7 +219,7 @@ public class SimpleToscaProviderTest { serviceTemplate.getPolicyTypes().getConceptMap().put(policyType0Key, policyType0); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); assertEquals(1, createdServiceTemplate.getPolicyTypes().getConceptMap().size()); assertEquals(policyType0, createdServiceTemplate.getPolicyTypes().get(policyType0Key)); @@ -225,14 +228,14 @@ public class SimpleToscaProviderTest { policyType0.setDescription("Updated Description"); JpaToscaServiceTemplate updatedServiceTemplate = - new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); assertEquals(policyType0, updatedServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", - updatedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + updatedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); JpaToscaServiceTemplate gotServiceTemplate = - new SimpleToscaProvider().getPolicyTypes(pfDao, policyType0Key.getName(), policyType0Key.getVersion()); + new SimpleToscaProvider().getPolicyTypes(pfDao, policyType0Key.getName(), policyType0Key.getVersion()); assertEquals(policyType0, gotServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", gotServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); @@ -247,7 +250,7 @@ public class SimpleToscaProviderTest { new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) - .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2"); + .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2"); serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); @@ -262,10 +265,10 @@ public class SimpleToscaProviderTest { new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) - .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2"); + .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2"); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, pt1.getKey())) - .hasMessage("policy type pt1:0.0.2 is in use, it is referenced in policy p1:0.0.1"); + .hasMessage("policy type pt1:0.0.2 is in use, it is referenced in policy p1:0.0.1"); new SimpleToscaProvider().deletePolicy(pfDao, p1.getKey()); @@ -274,17 +277,17 @@ public class SimpleToscaProviderTest { new SimpleToscaProvider().deletePolicy(pfDao, p0.getKey()); JpaToscaServiceTemplate deletedServiceTemplate = - new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); + new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); assertEquals(policyType0, deletedServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", - deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) - .hasMessage("no policy types found"); + .hasMessage("no policy types found"); JpaToscaServiceTemplate newServiceTemplate = - new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); assertEquals(serviceTemplate, newServiceTemplate); } @@ -299,7 +302,7 @@ public class SimpleToscaProviderTest { serviceTemplate.getPolicyTypes().getConceptMap().put(policyType0Key, policyType0); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); assertEquals(policyType0, createdServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals(null, createdServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); @@ -307,33 +310,33 @@ public class SimpleToscaProviderTest { policyType0.setDescription("Updated Description"); JpaToscaServiceTemplate updatedServiceTemplate = - new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); assertEquals(policyType0, updatedServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", - updatedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + updatedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); JpaToscaServiceTemplate gotServiceTemplate = - new SimpleToscaProvider().getPolicyTypes(pfDao, policyType0Key.getName(), policyType0Key.getVersion()); + new SimpleToscaProvider().getPolicyTypes(pfDao, policyType0Key.getName(), policyType0Key.getVersion()); assertEquals(policyType0, gotServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", gotServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); JpaToscaServiceTemplate deletedServiceTemplate = - new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); + new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); assertEquals(policyType0, deletedServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", - deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) - .hasMessage("no policy types found"); + .hasMessage("no policy types found"); } @Test public void testPoliciesGet() throws Exception { ToscaServiceTemplate toscaServiceTemplate = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -342,18 +345,18 @@ public class SimpleToscaProviderTest { assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); + new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies(), - createdServiceTemplate.getTopologyTemplate().getPolicies()); + createdServiceTemplate.getTopologyTemplate().getPolicies()); PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); JpaToscaServiceTemplate gotServiceTemplate = - new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); - assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), - gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); + assertEquals(0, originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey) + .compareTo(gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey))); JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, policyKey); assertEquals(1, deletedServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size()); @@ -362,7 +365,7 @@ public class SimpleToscaProviderTest { @Test public void testPolicyCreate() throws Exception { ToscaServiceTemplate toscaServiceTemplate = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -371,21 +374,21 @@ public class SimpleToscaProviderTest { assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); + new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies(), - createdServiceTemplate.getTopologyTemplate().getPolicies()); + createdServiceTemplate.getTopologyTemplate().getPolicies()); } @Test public void testPolicyCreateTypeAndVersion() throws Exception { ToscaServiceTemplate toscaServiceTemplate = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); ToscaPolicy toscaPolicy = - toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().values().iterator().next(); + toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().values().iterator().next(); JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); @@ -397,13 +400,13 @@ public class SimpleToscaProviderTest { assertThatThrownBy(() -> { originalServiceTemplate.fromAuthorative(toscaServiceTemplate); }).hasMessage("PolicyType type not specified, the type of the PolicyType for this policy must be " - + "specified in the type field"); + + "specified in the type field"); toscaPolicy.setType("IDontExist"); assertThatThrownBy(() -> { originalServiceTemplate.fromAuthorative(toscaServiceTemplate); }).hasMessage("PolicyType version not specified, the version of the PolicyType for this policy must be " - + "specified in the type_version field"); + + "specified in the type_version field"); toscaPolicy.setTypeVersion("hello"); assertThatThrownBy(() -> { @@ -425,22 +428,22 @@ public class SimpleToscaProviderTest { assertThatThrownBy(() -> { originalServiceTemplate.fromAuthorative(toscaServiceTemplate); }).hasMessage("PolicyType type not specified, the type of the PolicyType for this policy must be " - + "specified in the type field"); + + "specified in the type field"); toscaPolicy.setType(originalPolicyType); toscaPolicy.setTypeVersion(originalPolicyTypeVersion); originalServiceTemplate.fromAuthorative(toscaServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); + new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies(), - createdServiceTemplate.getTopologyTemplate().getPolicies()); + createdServiceTemplate.getTopologyTemplate().getPolicies()); } @Test public void testPolicyUpdate() throws Exception { ToscaServiceTemplate toscaServiceTemplate = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -449,7 +452,7 @@ public class SimpleToscaProviderTest { assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = - new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate); + new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate); assertEquals(originalServiceTemplate, updatedServiceTemplate); } @@ -457,7 +460,7 @@ public class SimpleToscaProviderTest { @Test public void testPoliciesDelete() throws Exception { ToscaServiceTemplate toscaServiceTemplate = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -466,26 +469,26 @@ public class SimpleToscaProviderTest { assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); + new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); assertEquals(originalServiceTemplate.getTopologyTemplate(), createdServiceTemplate.getTopologyTemplate()); PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey("IDontExist:0.0.1"))) - .hasMessage("policy IDontExist:0.0.1 not found"); + .hasMessage("policy IDontExist:0.0.1 not found"); JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, policyKey); - assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), - deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); + assertEquals(0, originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey) + .compareTo(deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey))); assertThatThrownBy(() -> { new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); }).hasMessage("policies for onap.restart.tca:1.0.0 do not exist"); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicy(pfDao, policyKey)) - .hasMessage("no policies found"); + .hasMessage("no policies found"); new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); } @@ -495,21 +498,21 @@ public class SimpleToscaProviderTest { JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate(); assertThatThrownBy(() -> new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate)) - .hasMessage("topology template not specified on service template"); + .hasMessage("topology template not specified on service template"); testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); assertThatThrownBy(() -> new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate)) - .hasMessage("no policies specified on topology template of service template"); + .hasMessage("no policies specified on topology template of service template"); testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); assertThatThrownBy(() -> new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate)) - .hasMessage("list of policies specified on topology template of service template is empty"); + .hasMessage("list of policies specified on topology template of service template is empty"); } @Test public void testGetServiceTemplate() throws PfModelException { assertThatThrownBy(() -> new SimpleToscaProvider().getServiceTemplate(pfDao)) - .hasMessage("service template not found in database"); + .hasMessage("service template not found in database"); } @Test @@ -520,8 +523,8 @@ public class SimpleToscaProviderTest { serviceTemplateFragment.getPolicyTypes().getConceptMap().put(badPt.getKey(), badPt); assertThatThrownBy(() -> new SimpleToscaProvider().appendToServiceTemplate(pfDao, serviceTemplateFragment)) - .hasMessageContaining( - "key on concept entry PfConceptKey(name=NULL, version=0.0.0) may not be the null key"); + .hasMessageContaining( + "key on concept entry PfConceptKey(name=NULL, version=0.0.0) may not be the null key"); } @Test @@ -609,7 +612,7 @@ public class SimpleToscaProviderTest { }).hasMessageMatching("policy types for hello:0.0.1 do not exist"); JpaToscaServiceTemplate gotSt = - new SimpleToscaProvider().getPolicyTypes(pfDao, pt01.getName(), pt01.getVersion()); + new SimpleToscaProvider().getPolicyTypes(pfDao, pt01.getName(), pt01.getVersion()); assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getKey())); assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName())); @@ -674,10 +677,10 @@ public class SimpleToscaProviderTest { JpaToscaServiceTemplate gotSt = new SimpleToscaProvider().getPolicies(pfDao, p01.getName(), p01.getVersion()); - assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getKey())); - assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName())); - assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), null)); - assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p01.getVersion())); + assertEquals(0, p01.compareTo(gotSt.getTopologyTemplate().getPolicies().get(p01.getKey()))); + assertEquals(0, p01.compareTo(gotSt.getTopologyTemplate().getPolicies().get(p01.getName()))); + assertEquals(0, p01.compareTo(gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), null))); + assertEquals(0, p01.compareTo(gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p01.getVersion()))); assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(null).size()); assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(null, null).size()); assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), null).size()); @@ -859,7 +862,7 @@ public class SimpleToscaProviderTest { serviceTemplate.getDataTypes().getConceptMap().put(dataType0Key, dataType0); JpaToscaServiceTemplate createdServiceTemplate = - new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); assertEquals(1, createdServiceTemplate.getDataTypes().getConceptMap().size()); assertEquals(dataType0, createdServiceTemplate.getDataTypes().get(dataType0Key)); @@ -878,12 +881,12 @@ public class SimpleToscaProviderTest { } private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml() - .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); + Object yamlObject = + new Yaml().load(ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = - standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplatePolicyType); new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplatePolicyType); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java index f74fad4b4..ba486035c 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue; import java.util.List; import java.util.Map; +import org.apache.commons.collections4.MapUtils; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; @@ -194,7 +195,7 @@ public class OptimizationPolicyTypeSerializationTest { String testnm = testName + " identity"; assertNotNull(testnm, prop); - assertEquals(testnm + " metadata", 0, prop.getMetadata().size()); + assertEquals(testnm + " metadata", true, MapUtils.isEmpty(prop.getMetadata())); } private void validateMatchable(String testName, Map<String, String> metadata) { diff --git a/models-tosca/src/test/resources/META-INF/persistence.xml b/models-tosca/src/test/resources/META-INF/persistence.xml index de27dd9bc..d6ebf4eb5 100644 --- a/models-tosca/src/test/resources/META-INF/persistence.xml +++ b/models-tosca/src/test/resources/META-INF/persistence.xml @@ -23,19 +23,32 @@ <persistence-unit name="ToscaConceptTest" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <class>org.onap.policy.models.base.PfConceptKey</class> <class>org.onap.policy.models.dao.converters.CDataConditioner</class> <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.JpaToscaCapabilityAssignment</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignments</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityTypes</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.JpaToscaNodeTemplate</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaParameter</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.JpaToscaProperty</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirement</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirements</class> <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate</class> <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty</class> <properties> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> |