diff options
Diffstat (limited to 'models-tosca/src/main/java')
26 files changed, 2911 insertions, 232 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")); |