From 4c4946e339942863e73e20726dd95aaacfcfb5a6 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 7 Dec 2020 16:25:14 +0000 Subject: JPA concepts for TOSCA Implement the Database persistence parts for the new TOSCA concepts so that those concepts can be written to and read from the database. First batch of comment fixes Issue-ID: POLICY-2900 Change-Id: I45191c1ff57cf1ea8563399db6cc8e51b2724813 Signed-off-by: liamfallon --- .../concepts/JpaToscaCapabilityAssignment.java | 271 ++++++++++++++++++ .../concepts/JpaToscaCapabilityAssignments.java | 117 ++++++++ .../simple/concepts/JpaToscaCapabilityType.java | 239 ++++++++++++++++ .../simple/concepts/JpaToscaCapabilityTypes.java | 112 ++++++++ .../tosca/simple/concepts/JpaToscaDataType.java | 46 +-- .../tosca/simple/concepts/JpaToscaEntityType.java | 33 +-- .../tosca/simple/concepts/JpaToscaEntrySchema.java | 222 --------------- .../tosca/simple/concepts/JpaToscaEventFilter.java | 16 +- .../tosca/simple/concepts/JpaToscaModel.java | 20 +- .../simple/concepts/JpaToscaNodeTemplate.java | 310 +++++++++++++++++++++ .../simple/concepts/JpaToscaNodeTemplates.java | 112 ++++++++ .../tosca/simple/concepts/JpaToscaNodeType.java | 277 ++++++++++++++++++ .../tosca/simple/concepts/JpaToscaNodeTypes.java | 112 ++++++++ .../tosca/simple/concepts/JpaToscaParameter.java | 202 ++++++++++++++ .../tosca/simple/concepts/JpaToscaPolicy.java | 99 +++---- .../tosca/simple/concepts/JpaToscaPolicyType.java | 36 +-- .../tosca/simple/concepts/JpaToscaProperty.java | 68 ++--- .../simple/concepts/JpaToscaRelationshipType.java | 236 ++++++++++++++++ .../simple/concepts/JpaToscaRelationshipTypes.java | 113 ++++++++ .../tosca/simple/concepts/JpaToscaRequirement.java | 303 ++++++++++++++++++++ .../simple/concepts/JpaToscaRequirements.java | 116 ++++++++ .../simple/concepts/JpaToscaSchemaDefinition.java | 222 +++++++++++++++ .../simple/concepts/JpaToscaServiceTemplate.java | 129 +++++++++ .../simple/concepts/JpaToscaTimeInterval.java | 13 +- .../simple/concepts/JpaToscaTopologyTemplate.java | 123 +++++++- .../tosca/simple/concepts/JpaToscaTrigger.java | 12 +- .../tosca/utils/ToscaServiceTemplateUtils.java | 4 +- 27 files changed, 3121 insertions(+), 442 deletions(-) create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignments.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityType.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityTypes.java delete mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplates.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTypes.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaParameter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipTypes.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirements.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinition.java (limited to 'models-tosca/src/main/java') 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 + implements PfAuthorative { + + 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 properties; + + @ElementCollection + @Lob + private Map attributes; + + @ElementCollection + private List 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 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 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 + implements PfAuthorative>> { + 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 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> 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 + implements PfAuthorative { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map 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 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 getKeys() { + final List 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 getReferencedDataTypes() { + if (properties == null) { + return CollectionUtils.emptyCollection(); + } + + Set 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 { + 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 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> 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 implemen private static final long serialVersionUID = -3922690413436539164L; @ElementCollection - private List constraints = new ArrayList<>(); + private List constraints; @ElementCollection @Lob - private Map properties = new LinkedHashMap<>(); + private Map properties; /** * The Default Constructor creates a {@link JpaToscaDataType} object with a null key. @@ -119,25 +118,8 @@ public class JpaToscaDataType extends JpaToscaEntityType implemen super.setToscaEntity(toscaDataType); super.toAuthorative(); - if (constraints != null) { - List toscaConstraints = new ArrayList<>(); - - for (JpaToscaConstraint constraint : constraints) { - toscaConstraints.add(constraint.toAuthorative()); - } - - toscaDataType.setConstraints(toscaConstraints); - } - - if (properties != null) { - Map propertyMap = new LinkedHashMap<>(); - - for (Entry 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 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 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 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 extends PfConcept impleme private PfConceptKey derivedFrom; @ElementCollection - private Map metadata = new TreeMap<>(); + private Map metadata; @Column private String description; @@ -129,15 +128,7 @@ public class JpaToscaEntityType extends PfConcept impleme toscaEntity.setDescription(description); } - if (metadata != null) { - Map metadataMap = new LinkedHashMap<>(); - - for (Entry 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 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 extends PfConcept impleme description = toscaEntity.getDescription(); } - if (toscaEntity.getMetadata() != null) { - metadata = new LinkedHashMap<>(); - - for (Entry 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 extends PfConcept impleme @SuppressWarnings("unchecked") final JpaToscaEntityType other = (JpaToscaEntityType) 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/JpaToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java deleted file mode 100644 index ef6aa7e92..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java +++ /dev/null @@ -1,222 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 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.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import javax.persistence.Column; -import javax.persistence.ElementCollection; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; -import lombok.NonNull; -import org.apache.commons.lang3.ObjectUtils; -import org.onap.policy.common.utils.validation.Assertions; -import org.onap.policy.models.base.PfAuthorative; -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.base.PfValidationResult.ValidationResult; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; -import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition; - -/** - * Class to represent the EntrySchema of list/map property in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * @author Liam Fallon (liam.fallon@est.tech) - */ -@Data -@EqualsAndHashCode(callSuper = false) -@NoArgsConstructor -public class JpaToscaEntrySchema - implements PfAuthorative, Serializable, Comparable { - - private static final long serialVersionUID = 3645882081163287058L; - - // Recurring string constants - private static final String ENTRY_SCHEMA = "EntrySchema"; - - @Column - private PfConceptKey type; - - @Column - private String description; - - @ElementCollection - private List constraints = new ArrayList<>(); - - /** - * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields. - * - * @param type the type of the entry schema - */ - public JpaToscaEntrySchema(@NonNull final PfConceptKey type) { - this.type = type; - } - - /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - public JpaToscaEntrySchema(@NonNull final JpaToscaEntrySchema copyConcept) { - copyConcept.copyTo(this); - } - - /** - * Authorative constructor. - * - * @param authorativeConcept the authorative concept to copy from - */ - public JpaToscaEntrySchema(final ToscaSchemaDefinition authorativeConcept) { - this.fromAuthorative(authorativeConcept); - } - - @Override - public ToscaSchemaDefinition toAuthorative() { - ToscaSchemaDefinition toscaEntrySchema = new ToscaSchemaDefinition(); - - toscaEntrySchema.setType(type.getName()); - toscaEntrySchema.setTypeVersion(type.getVersion()); - - toscaEntrySchema.setDescription(description); - - if (constraints != null) { - List toscaConstraints = new ArrayList<>(); - - for (JpaToscaConstraint constraint : constraints) { - toscaConstraints.add(constraint.toAuthorative()); - } - - toscaEntrySchema.setConstraints(toscaConstraints); - } - - return toscaEntrySchema; - } - - @Override - public void fromAuthorative(final ToscaSchemaDefinition toscaEntrySchema) { - if (toscaEntrySchema.getTypeVersion() != null) { - type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion()); - } else { - type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION); - } - - description = toscaEntrySchema.getDescription(); - - if (toscaEntrySchema.getConstraints() != null) { - constraints = new ArrayList<>(); - - for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) { - constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); - } - } - } - - public List getKeys() { - return type.getKeys(); - } - - public void clean() { - type.clean(); - description = (description != null ? description.trim() : null); - } - - /** - * Validate the entry schema. - * - * @param resultIn the incoming result - * @return the ooutput result witht he result of this validation - */ - public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { - PfValidationResult result = resultIn; - - if (type == null || type.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), - this.getClass(), ValidationResult.INVALID, "entry schema type may not be null")); - } - - if (description != null && description.trim().length() == 0) { - result.addValidationMessage(new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), - this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank")); - } - - if (constraints != null) { - for (JpaToscaConstraint constraint : constraints) { - if (constraint == null) { - result.addValidationMessage( - new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), - this.getClass(), ValidationResult.INVALID, "property constraint may not be null ")); - } - } - } - - return result; - } - - @Override - public int compareTo(final JpaToscaEntrySchema other) { - if (other == null) { - return -1; - } - if (this == other) { - return 0; - } - - int result = ObjectUtils.compare(description, other.description); - if (result != 0) { - return result; - } - - return PfUtils.compareObjects(constraints, other.constraints); - } - - /** - * Copy this entry schema to another. - * - * @param target the other schemaa - * @return the copied concept - */ - public JpaToscaEntrySchema copyTo(@NonNull final JpaToscaEntrySchema target) { - Assertions.instanceOf(target, JpaToscaEntrySchema.class); - - final JpaToscaEntrySchema copy = (target); - copy.setType(new PfConceptKey(type)); - copy.setDescription(description); - - if (constraints != null) { - final List newConstraints = new ArrayList<>(); - for (final JpaToscaConstraint constraint : constraints) { - newConstraints.add(constraint); // Constraints are immutable - } - copy.setConstraints(newConstraints); - } - - return copy; - } -} 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. * *

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 + implements PfAuthorative { + private static final long serialVersionUID = 1675770231921107988L; + + private static final StandardCoder STANDARD_CODER = new StandardCoder(); + + @Column + private String type; + + @ElementCollection + @Lob + private Map 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> capabilityAssignmentMapList = capabilities.toAuthorative(); + for (Map 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 getKeys() { + final List 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 { + 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 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> 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 implements PfAuthorative { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map 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 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 getKeys() { + final List 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 getReferencedDataTypes() { + if (properties == null) { + return CollectionUtils.emptyCollection(); + } + + Set 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 { + 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 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> 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 { + 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 getKeys() { + final List 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 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 implements P @ElementCollection @Lob - private Map properties = new LinkedHashMap<>(); + private Map properties; @ElementCollection - private List targets = new ArrayList<>(); + private List targets; // @formatter:on /** @@ -155,28 +156,14 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P toscaPolicy.setTypeVersion(null); } - if (properties != null) { - Map propertyMap = new LinkedHashMap<>(); - - final StandardCoder coder = new StandardCoder(); - - for (Entry 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 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 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 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 implements P for (Entry 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 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 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 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 impl @ElementCollection @Lob - private Map properties = new LinkedHashMap<>(); + private Map properties; @ElementCollection - private List targets = new ArrayList<>(); + private List targets; @ElementCollection - private List triggers = new ArrayList<>(); + private List triggers; /** * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key. @@ -122,15 +121,7 @@ public class JpaToscaPolicyType extends JpaToscaEntityType impl super.setToscaEntity(toscaPolicyType); super.toAuthorative(); - if (properties != null) { - Map propertyMap = new LinkedHashMap<>(); - - for (Entry 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 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 constraints = new ArrayList<>(); + private List constraints; @Column - private JpaToscaEntrySchema entrySchema; + private JpaToscaSchemaDefinition entrySchema; @ElementCollection - private Map metadata = new LinkedHashMap<>(); + private Map metadata; /** * The Default Constructor creates a {@link JpaToscaProperty} object with a null key. @@ -113,7 +111,7 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative(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 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(); - - 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 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 + implements PfAuthorative { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + @Lob + private Map 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 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 getKeys() { + final List 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 getReferencedDataTypes() { + if (properties == null) { + return CollectionUtils.emptyCollection(); + } + + Set 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 { + 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 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> 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 + implements PfAuthorative { + + 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 occurrences; + + @ElementCollection + @Lob + private Map 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 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 propertiesMap = new LinkedHashMap<>(); + + for (Map.Entry 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 toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) { + String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue()); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + + } + + @Override + public List 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 + implements PfAuthorative>> { + 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 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> 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/JpaToscaSchemaDefinition.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinition.java new file mode 100644 index 000000000..6bd1b44cd --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinition.java @@ -0,0 +1,222 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 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.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfAuthorative; +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.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition; + +/** + * Class to represent the EntrySchema of list/map property in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +@EqualsAndHashCode(callSuper = false) +@NoArgsConstructor +public class JpaToscaSchemaDefinition + implements PfAuthorative, Serializable, Comparable { + + private static final long serialVersionUID = 3645882081163287058L; + + // Recurring string constants + private static final String ENTRY_SCHEMA = "EntrySchema"; + + @Column + private PfConceptKey type; + + @Column + private String description; + + @ElementCollection + private List constraints; + + /** + * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields. + * + * @param type the type of the entry schema + */ + public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) { + this.type = type; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) { + copyConcept.copyTo(this); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaSchemaDefinition toAuthorative() { + ToscaSchemaDefinition toscaEntrySchema = new ToscaSchemaDefinition(); + + toscaEntrySchema.setType(type.getName()); + toscaEntrySchema.setTypeVersion(type.getVersion()); + + toscaEntrySchema.setDescription(description); + + if (constraints != null) { + List toscaConstraints = new ArrayList<>(); + + for (JpaToscaConstraint constraint : constraints) { + toscaConstraints.add(constraint.toAuthorative()); + } + + toscaEntrySchema.setConstraints(toscaConstraints); + } + + return toscaEntrySchema; + } + + @Override + public void fromAuthorative(final ToscaSchemaDefinition toscaEntrySchema) { + if (toscaEntrySchema.getTypeVersion() != null) { + type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion()); + } else { + type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION); + } + + description = toscaEntrySchema.getDescription(); + + if (toscaEntrySchema.getConstraints() != null) { + constraints = new ArrayList<>(); + + for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) { + constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); + } + } + } + + public List getKeys() { + return type.getKeys(); + } + + public void clean() { + type.clean(); + description = (description != null ? description.trim() : null); + } + + /** + * Validate the entry schema. + * + * @param resultIn the incoming result + * @return the ooutput result witht he result of this validation + */ + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (type == null || type.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), + this.getClass(), ValidationResult.INVALID, "entry schema type may not be null")); + } + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), + this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank")); + } + + if (constraints != null) { + for (JpaToscaConstraint constraint : constraints) { + if (constraint == null) { + result.addValidationMessage( + new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION), + this.getClass(), ValidationResult.INVALID, "property constraint may not be null ")); + } + } + } + + return result; + } + + @Override + public int compareTo(final JpaToscaSchemaDefinition other) { + if (other == null) { + return -1; + } + if (this == other) { + return 0; + } + + int result = ObjectUtils.compare(description, other.description); + if (result != 0) { + return result; + } + + return PfUtils.compareCollections(constraints, other.constraints); + } + + /** + * Copy this entry schema to another. + * + * @param target the other schemaa + * @return the copied concept + */ + public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) { + Assertions.instanceOf(target, JpaToscaSchemaDefinition.class); + + final JpaToscaSchemaDefinition copy = (target); + copy.setType(new PfConceptKey(type)); + copy.setDescription(description); + + if (constraints != null) { + final List newConstraints = new ArrayList<>(); + for (final JpaToscaConstraint constraint : constraints) { + newConstraints.add(constraint); // Constraints are immutable + } + copy.setConstraints(newConstraints); + } + + return copy; + } +} 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; /** @@ -88,6 +91,36 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType()); + List> capabilityTypeMapList = capabilityTypes.toAuthorative(); + for (Map capabilityTypeMap : capabilityTypeMapList) { + toscaServiceTemplate.getCapabilityTypes().putAll(capabilityTypeMap); + } + } + + if (relationshipTypes != null) { + toscaServiceTemplate.setRelationshipTypes(new LinkedHashMap<>()); + List> relationshipTypeMapList = relationshipTypes.toAuthorative(); + for (Map relationshipTypeMap : relationshipTypeMapList) { + toscaServiceTemplate.getRelationshipTypes().putAll(relationshipTypeMap); + } + } + + if (nodeTypes != null) { + toscaServiceTemplate.setNodeTypes(new LinkedHashMap<>()); + List> nodeTypeMapList = nodeTypes.toAuthorative(); + for (Map nodeTypeMap : nodeTypeMapList) { + toscaServiceTemplate.getNodeTypes().putAll(nodeTypeMap); + } + } + if (policyTypes != null) { toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); List> policyTypeMapList = policyTypes.toAuthorative(); @@ -213,6 +276,21 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType 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 inputMap = new LinkedHashMap<>(); + + for (Map.Entry entry : inputs.entrySet()) { + inputMap.put(entry.getKey(), entry.getValue().toAuthorative()); + } + + toscaTopologyTemplate.setInputs(inputMap); + } + + if (nodeTemplates != null) { + toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>()); + List> nodeTemplateMapList = nodeTemplates.toAuthorative(); + for (Map 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 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 getKeys() { final List 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> fragmentEntry : fragmentContainer .getConceptMap().entrySet()) { - JpaToscaEntityType containerEntry = + JpaToscaEntityType 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")); -- cgit 1.2.3-korg