2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import javax.persistence.CascadeType;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.JoinColumn;
37 import javax.persistence.JoinColumns;
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.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfReferenceKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.base.PfValidationMessage;
53 import org.onap.policy.models.base.PfValidationResult;
54 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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<String, JpaToscaProperty> properties;
77 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
78 @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
79 @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
81 private JpaToscaRequirements requirements;
84 * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
86 public JpaToscaNodeType() {
87 this(new PfConceptKey());
91 * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
95 public JpaToscaNodeType(@NonNull final PfConceptKey key) {
102 * @param copyConcept the concept to copy from
104 public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
106 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
108 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
112 * Authorative constructor.
114 * @param authorativeConcept the authorative concept to copy from
116 public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
117 this.fromAuthorative(authorativeConcept);
121 public ToscaNodeType toAuthorative() {
122 ToscaNodeType toscaNodeType = new ToscaNodeType();
123 super.setToscaEntity(toscaNodeType);
124 super.toAuthorative();
126 toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
128 if (requirements != null) {
129 toscaNodeType.setRequirements(requirements.toAuthorative());
132 return toscaNodeType;
136 public void fromAuthorative(final ToscaNodeType toscaNodeType) {
137 super.fromAuthorative(toscaNodeType);
140 if (toscaNodeType.getProperties() != null) {
141 properties = new LinkedHashMap<>();
142 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
143 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
144 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
145 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
149 if (toscaNodeType.getRequirements() != null) {
150 requirements = new JpaToscaRequirements();
151 requirements.fromAuthorative(toscaNodeType.getRequirements());
156 public List<PfKey> getKeys() {
157 final List<PfKey> keyList = super.getKeys();
159 if (properties != null) {
160 for (JpaToscaProperty property : properties.values()) {
161 keyList.addAll(property.getKeys());
165 if (requirements != null) {
166 keyList.addAll(requirements.getKeys());
173 public void clean() {
176 if (properties != null) {
177 for (JpaToscaProperty property : properties.values()) {
182 if (requirements != null) {
183 requirements.clean();
188 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
189 PfValidationResult result = super.validate(resultIn);
191 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
192 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
193 "key version is a null version"));
196 if (properties != null) {
197 result = validateProperties(result);
200 if (requirements != null) {
201 result = requirements.validate(result);
208 * Validate the capabiltiy type properties.
210 * @param resultIn The result of validations up to now
211 * @return the validation result
213 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
214 PfValidationResult result = resultIn;
216 for (JpaToscaProperty property : properties.values()) {
217 if (property == null) {
218 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
219 "node type property may not be null "));
221 result = property.validate(result);
228 public int compareTo(final PfConcept otherConcept) {
229 if (otherConcept == null) {
232 if (this == otherConcept) {
235 if (getClass() != otherConcept.getClass()) {
236 return getClass().getName().compareTo(otherConcept.getClass().getName());
239 final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
240 int result = super.compareTo(other);
245 result = PfUtils.compareMaps(properties, other.properties);
250 return ObjectUtils.compare(requirements, other.requirements);
254 * Get the data types referenced in a node type.
256 * @return the data types referenced in a node type
258 public Collection<PfConceptKey> getReferencedDataTypes() {
259 if (properties == null) {
260 return CollectionUtils.emptyCollection();
263 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
265 for (JpaToscaProperty property : properties.values()) {
266 referencedDataTypes.add(property.getType());
268 if (property.getEntrySchema() != null) {
269 referencedDataTypes.add(property.getEntrySchema().getType());
273 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
275 return referencedDataTypes;