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.ElementCollection;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Lob;
35 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.apache.commons.collections4.CollectionUtils;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.PfUtils;
46 import org.onap.policy.models.base.PfValidationMessage;
47 import org.onap.policy.models.base.PfValidationResult;
48 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
51 import org.onap.policy.models.tosca.utils.ToscaUtils;
54 * Class to represent the capability type in TOSCA definition.
58 @Table(name = "ToscaCapabilityType")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = true)
62 public class JpaToscaCapabilityType extends JpaToscaEntityType<ToscaCapabilityType>
63 implements PfAuthorative<ToscaCapabilityType> {
64 private static final long serialVersionUID = -563659852901842616L;
68 private Map<String, JpaToscaProperty> properties;
71 * The Default Constructor creates a {@link JpaToscaCapabilityType} object with a null key.
73 public JpaToscaCapabilityType() {
74 this(new PfConceptKey());
78 * The Key Constructor creates a {@link JpaToscaCapabilityType} object with the given concept key.
82 public JpaToscaCapabilityType(@NonNull final PfConceptKey key) {
89 * @param copyConcept the concept to copy from
91 public JpaToscaCapabilityType(final JpaToscaCapabilityType copyConcept) {
93 this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
97 * Authorative constructor.
99 * @param authorativeConcept the authorative concept to copy from
101 public JpaToscaCapabilityType(final ToscaCapabilityType authorativeConcept) {
102 this.fromAuthorative(authorativeConcept);
106 public ToscaCapabilityType toAuthorative() {
107 ToscaCapabilityType toscaCapabilityType = new ToscaCapabilityType();
108 super.setToscaEntity(toscaCapabilityType);
109 super.toAuthorative();
111 toscaCapabilityType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
113 return toscaCapabilityType;
117 public void fromAuthorative(final ToscaCapabilityType toscaCapabilityType) {
118 super.fromAuthorative(toscaCapabilityType);
121 if (toscaCapabilityType.getProperties() != null) {
122 properties = new LinkedHashMap<>();
123 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaCapabilityType.getProperties().entrySet()) {
124 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
125 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
126 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
132 public List<PfKey> getKeys() {
133 final List<PfKey> keyList = super.getKeys();
135 PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys()));
138 if (properties != null) {
139 for (JpaToscaProperty property : properties.values()) {
140 keyList.addAll(property.getKeys());
148 public void clean() {
151 if (properties != null) {
152 for (JpaToscaProperty property : properties.values()) {
159 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
160 PfValidationResult result = super.validate(resultIn);
162 if (getKey().isNullVersion()) {
163 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
164 "key version is a null version"));
167 if (properties != null) {
168 result = validateProperties(result);
175 * Validate the capabiltiy type properties.
177 * @param resultIn The result of validations up to now
178 * @return the validation result
180 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
181 PfValidationResult result = resultIn;
183 for (JpaToscaProperty property : properties.values()) {
184 if (property == null) {
185 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
186 "capability type property may not be null "));
188 result = property.validate(result);
195 public int compareTo(final PfConcept otherConcept) {
196 if (otherConcept == null) {
199 if (this == otherConcept) {
202 if (getClass() != otherConcept.getClass()) {
203 return getClass().getName().compareTo(otherConcept.getClass().getName());
206 final JpaToscaCapabilityType other = (JpaToscaCapabilityType) otherConcept;
207 int result = super.compareTo(other);
212 return PfUtils.compareMaps(properties, other.properties);
216 * Get the data types referenced in a capability type.
218 * @return the data types referenced in a capability type
220 public Collection<PfConceptKey> getReferencedDataTypes() {
221 if (properties == null) {
222 return CollectionUtils.emptyCollection();
225 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
227 for (JpaToscaProperty property : properties.values()) {
228 referencedDataTypes.add(property.getType());
230 if (property.getEntrySchema() != null) {
231 referencedDataTypes.add(property.getEntrySchema().getType());
235 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
237 return referencedDataTypes;