2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 com.google.gson.annotations.SerializedName;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.TreeMap;
30 import javax.persistence.CascadeType;
31 import javax.persistence.Column;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.OneToMany;
37 import javax.persistence.Table;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
43 import org.apache.commons.lang3.ObjectUtils;
44 import org.onap.policy.common.utils.validation.Assertions;
45 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfConceptKey;
48 import org.onap.policy.models.base.PfKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.PfValidationMessage;
51 import org.onap.policy.models.base.PfValidationResult;
52 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
55 * Class to represent the EntrySchema of list/map property in TOSCA definition.
58 @Table(name = "ToscaEntityType")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = false)
62 public class ToscaEntityType extends PfConcept {
63 private static final long serialVersionUID = -1330661834220739393L;
66 private PfConceptKey key;
68 @SerializedName("derived_from")
69 @Column(name = "derivedFrom")
70 private PfConceptKey derivedFrom;
72 @OneToMany(cascade = CascadeType.ALL)
73 private Map<String, String> metadata;
75 @Column(name = "description")
76 private String description;
79 * The Default Constructor creates a {@link ToscaEntityType} object with a null key.
81 public ToscaEntityType() {
82 this(new PfConceptKey());
86 * The Key Constructor creates a {@link ToscaEntityType} object with the given concept key.
90 public ToscaEntityType(@NonNull final PfConceptKey key) {
97 * @param copyConcept the concept to copy from
99 public ToscaEntityType(final ToscaEntityType copyConcept) {
104 public List<PfKey> getKeys() {
105 final List<PfKey> keyList = getKey().getKeys();
106 if (derivedFrom != null) {
107 keyList.addAll(derivedFrom.getKeys());
113 public void clean() {
116 if (derivedFrom != null) {
120 if (metadata != null) {
121 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
122 metadataEntry.setValue(metadataEntry.getValue().trim());
126 description = (description != null ? description.trim() : null);
130 public PfValidationResult validate(PfValidationResult resultIn) {
131 PfValidationResult result = resultIn;
133 if (key.isNullKey()) {
134 result.addValidationMessage(
135 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
138 result = key.validate(result);
140 if (derivedFrom != null && derivedFrom.isNullKey()) {
141 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
142 "derived from key is a null key"));
145 if (metadata != null) {
146 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
147 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
148 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
149 "property metadata key may not be null"));
151 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
152 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
153 "property metadata value may not be null"));
158 if (description != null && description.trim().length() == 0) {
159 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
160 "property description may not be blank"));
167 public int compareTo(final PfConcept otherConcept) {
168 if (otherConcept == null) {
171 if (this == otherConcept) {
174 if (getClass() != otherConcept.getClass()) {
175 return this.hashCode() - otherConcept.hashCode();
178 final ToscaEntityType other = (ToscaEntityType) otherConcept;
179 if (!key.equals(other.key)) {
180 return key.compareTo(other.key);
183 int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
188 result = PfUtils.compareObjects(metadata, other.metadata);
193 return ObjectUtils.compare(description, other.description);
197 public PfConcept copyTo(@NonNull PfConcept target) {
198 final Object copyObject = target;
199 Assertions.instanceOf(copyObject, PfConcept.class);
201 final ToscaEntityType copy = ((ToscaEntityType) copyObject);
202 copy.setKey(new PfConceptKey(key));
203 copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
205 if (metadata != null) {
206 final Map<String, String> newMatadata = new TreeMap<>();
207 for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
208 newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
210 copy.setMetadata(newMatadata);
213 copy.setDescription(description);