2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 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.Entries;
49 import org.onap.policy.common.parameters.annotations.Items;
50 import org.onap.policy.common.parameters.annotations.NotNull;
51 import org.onap.policy.common.parameters.annotations.Valid;
52 import org.onap.policy.models.base.PfAuthorative;
53 import org.onap.policy.models.base.PfConcept;
54 import org.onap.policy.models.base.PfConceptKey;
55 import org.onap.policy.models.base.PfKey;
56 import org.onap.policy.models.base.PfReferenceKey;
57 import org.onap.policy.models.base.PfUtils;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
60 import org.onap.policy.models.tosca.utils.ToscaUtils;
63 * Class to represent the node type in TOSCA definition.
67 @Table(name = "ToscaNodeType")
68 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @EqualsAndHashCode(callSuper = true)
71 public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implements PfAuthorative<ToscaNodeType> {
72 private static final long serialVersionUID = -563659852901842616L;
76 @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
77 private Map<String, JpaToscaProperty> properties;
81 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
82 @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
83 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
86 private JpaToscaRequirements requirements;
89 * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
91 public JpaToscaNodeType() {
92 this(new PfConceptKey());
96 * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
100 public JpaToscaNodeType(@NonNull final PfConceptKey key) {
107 * @param copyConcept the concept to copy from
109 public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
111 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
113 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
117 * Authorative constructor.
119 * @param authorativeConcept the authorative concept to copy from
121 public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
122 this.fromAuthorative(authorativeConcept);
126 public ToscaNodeType toAuthorative() {
127 ToscaNodeType toscaNodeType = new ToscaNodeType();
128 super.setToscaEntity(toscaNodeType);
129 super.toAuthorative();
131 toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
133 if (requirements != null) {
134 toscaNodeType.setRequirements(requirements.toAuthorative());
137 return toscaNodeType;
141 public void fromAuthorative(final ToscaNodeType toscaNodeType) {
142 super.fromAuthorative(toscaNodeType);
145 if (toscaNodeType.getProperties() != null) {
146 properties = new LinkedHashMap<>();
147 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
148 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
149 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
150 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
154 if (toscaNodeType.getRequirements() != null) {
155 requirements = new JpaToscaRequirements();
156 requirements.fromAuthorative(toscaNodeType.getRequirements());
161 public List<PfKey> getKeys() {
162 final List<PfKey> keyList = super.getKeys();
164 if (properties != null) {
165 for (JpaToscaProperty property : properties.values()) {
166 keyList.addAll(property.getKeys());
170 if (requirements != null) {
171 keyList.addAll(requirements.getKeys());
178 public void clean() {
181 if (properties != null) {
182 for (JpaToscaProperty property : properties.values()) {
187 if (requirements != null) {
188 requirements.clean();
193 public BeanValidationResult validate(String fieldName) {
194 BeanValidationResult result = super.validate(fieldName);
196 result.addResult(validateKeyVersionNotNull("key", getKey()));
202 public int compareTo(final PfConcept otherConcept) {
203 if (otherConcept == null) {
206 if (this == otherConcept) {
209 if (getClass() != otherConcept.getClass()) {
210 return getClass().getName().compareTo(otherConcept.getClass().getName());
213 final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
214 int result = super.compareTo(other);
219 result = PfUtils.compareMaps(properties, other.properties);
224 return ObjectUtils.compare(requirements, other.requirements);
228 * Get the data types referenced in a node type.
230 * @return the data types referenced in a node type
232 public Collection<PfConceptKey> getReferencedDataTypes() {
233 if (properties == null) {
234 return CollectionUtils.emptyCollection();
237 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
239 for (JpaToscaProperty property : properties.values()) {
240 referencedDataTypes.add(property.getType());
242 if (property.getEntrySchema() != null) {
243 referencedDataTypes.add(property.getEntrySchema().getType());
247 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
249 return referencedDataTypes;