2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
29 import java.util.Map.Entry;
31 import javax.persistence.CascadeType;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.Lob;
39 import javax.persistence.OneToOne;
40 import javax.persistence.Table;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
44 import org.apache.commons.collections4.CollectionUtils;
45 import org.apache.commons.lang3.ObjectUtils;
46 import org.onap.policy.common.parameters.BeanValidationResult;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
57 import org.onap.policy.models.tosca.utils.ToscaUtils;
60 * Class to represent the node type in TOSCA definition.
64 @Table(name = "ToscaNodeType")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @EqualsAndHashCode(callSuper = true)
68 public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implements PfAuthorative<ToscaNodeType> {
69 private static final long serialVersionUID = -563659852901842616L;
73 private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties;
77 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
78 @JoinColumn(name = "requirementsName", referencedColumnName = "name")
79 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
82 private JpaToscaRequirements requirements;
85 * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
87 public JpaToscaNodeType() {
88 this(new PfConceptKey());
92 * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
96 public JpaToscaNodeType(@NonNull final PfConceptKey key) {
103 * @param copyConcept the concept to copy from
105 public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
107 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
109 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
113 * Authorative constructor.
115 * @param authorativeConcept the authorative concept to copy from
117 public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
118 this.fromAuthorative(authorativeConcept);
122 public ToscaNodeType toAuthorative() {
123 ToscaNodeType toscaNodeType = new ToscaNodeType();
124 super.setToscaEntity(toscaNodeType);
125 super.toAuthorative();
127 toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
129 if (requirements != null) {
130 toscaNodeType.setRequirements(requirements.toAuthorative());
133 return toscaNodeType;
137 public void fromAuthorative(final ToscaNodeType toscaNodeType) {
138 super.fromAuthorative(toscaNodeType);
141 if (toscaNodeType.getProperties() != null) {
142 properties = new LinkedHashMap<>();
143 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
144 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
145 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
146 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
150 if (toscaNodeType.getRequirements() != null) {
151 requirements = new JpaToscaRequirements();
152 requirements.fromAuthorative(toscaNodeType.getRequirements());
157 public List<PfKey> getKeys() {
158 final List<PfKey> keyList = super.getKeys();
160 if (properties != null) {
161 for (JpaToscaProperty property : properties.values()) {
162 keyList.addAll(property.getKeys());
166 if (requirements != null) {
167 keyList.addAll(requirements.getKeys());
174 public void clean() {
177 if (properties != null) {
178 for (JpaToscaProperty property : properties.values()) {
183 if (requirements != null) {
184 requirements.clean();
189 public BeanValidationResult validate(String fieldName) {
190 BeanValidationResult result = super.validate(fieldName);
192 validateKeyVersionNotNull(result, "key", getKey());
198 public int compareTo(final PfConcept otherConcept) {
199 if (otherConcept == null) {
202 if (this == otherConcept) {
205 if (getClass() != otherConcept.getClass()) {
206 return getClass().getName().compareTo(otherConcept.getClass().getName());
209 final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
210 int result = super.compareTo(other);
215 result = PfUtils.compareMaps(properties, other.properties);
220 return ObjectUtils.compare(requirements, other.requirements);
224 * Get the data types referenced in a node type.
226 * @return the data types referenced in a node type
228 public Collection<PfConceptKey> getReferencedDataTypes() {
229 if (properties == null) {
230 return CollectionUtils.emptyCollection();
233 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
235 for (JpaToscaProperty property : properties.values()) {
236 referencedDataTypes.add(property.getType());
238 if (property.getEntrySchema() != null) {
239 referencedDataTypes.add(property.getEntrySchema().getType());
243 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
245 return referencedDataTypes;