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.JoinColumns;
39 import javax.persistence.Lob;
40 import javax.persistence.OneToOne;
41 import javax.persistence.Table;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
45 import org.apache.commons.collections4.CollectionUtils;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.parameters.BeanValidationResult;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConcept;
52 import org.onap.policy.models.base.PfConceptKey;
53 import org.onap.policy.models.base.PfKey;
54 import org.onap.policy.models.base.PfReferenceKey;
55 import org.onap.policy.models.base.PfUtils;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
58 import org.onap.policy.models.tosca.utils.ToscaUtils;
61 * Class to represent the node type in TOSCA definition.
65 @Table(name = "ToscaNodeType")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implements PfAuthorative<ToscaNodeType> {
70 private static final long serialVersionUID = -563659852901842616L;
74 private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties;
78 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
79 @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
80 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
83 private JpaToscaRequirements requirements;
86 * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
88 public JpaToscaNodeType() {
89 this(new PfConceptKey());
93 * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
97 public JpaToscaNodeType(@NonNull final PfConceptKey key) {
104 * @param copyConcept the concept to copy from
106 public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
108 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
110 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
114 * Authorative constructor.
116 * @param authorativeConcept the authorative concept to copy from
118 public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
119 this.fromAuthorative(authorativeConcept);
123 public ToscaNodeType toAuthorative() {
124 ToscaNodeType toscaNodeType = new ToscaNodeType();
125 super.setToscaEntity(toscaNodeType);
126 super.toAuthorative();
128 toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
130 if (requirements != null) {
131 toscaNodeType.setRequirements(requirements.toAuthorative());
134 return toscaNodeType;
138 public void fromAuthorative(final ToscaNodeType toscaNodeType) {
139 super.fromAuthorative(toscaNodeType);
142 if (toscaNodeType.getProperties() != null) {
143 properties = new LinkedHashMap<>();
144 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
145 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
146 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
147 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
151 if (toscaNodeType.getRequirements() != null) {
152 requirements = new JpaToscaRequirements();
153 requirements.fromAuthorative(toscaNodeType.getRequirements());
158 public List<PfKey> getKeys() {
159 final List<PfKey> keyList = super.getKeys();
161 if (properties != null) {
162 for (JpaToscaProperty property : properties.values()) {
163 keyList.addAll(property.getKeys());
167 if (requirements != null) {
168 keyList.addAll(requirements.getKeys());
175 public void clean() {
178 if (properties != null) {
179 for (JpaToscaProperty property : properties.values()) {
184 if (requirements != null) {
185 requirements.clean();
190 public BeanValidationResult validate(String fieldName) {
191 BeanValidationResult result = super.validate(fieldName);
193 validateKeyVersionNotNull(result, "key", getKey());
199 public int compareTo(final PfConcept otherConcept) {
200 if (otherConcept == null) {
203 if (this == otherConcept) {
206 if (getClass() != otherConcept.getClass()) {
207 return getClass().getName().compareTo(otherConcept.getClass().getName());
210 final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
211 int result = super.compareTo(other);
216 result = PfUtils.compareMaps(properties, other.properties);
221 return ObjectUtils.compare(requirements, other.requirements);
225 * Get the data types referenced in a node type.
227 * @return the data types referenced in a node type
229 public Collection<PfConceptKey> getReferencedDataTypes() {
230 if (properties == null) {
231 return CollectionUtils.emptyCollection();
234 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
236 for (JpaToscaProperty property : properties.values()) {
237 referencedDataTypes.add(property.getType());
239 if (property.getEntrySchema() != null) {
240 referencedDataTypes.add(property.getEntrySchema().getType());
244 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
246 return referencedDataTypes;