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 java.util.List;
25 import java.util.Map.Entry;
26 import java.util.TreeMap;
28 import javax.persistence.AttributeOverride;
29 import javax.persistence.AttributeOverrides;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.MappedSuperclass;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
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;
51 * Class to represent the EntrySchema of list/map property in TOSCA definition.
55 @EqualsAndHashCode(callSuper = false)
56 public class ToscaEntityType extends PfConcept {
57 private static final long serialVersionUID = -1330661834220739393L;
60 private PfConceptKey key;
65 @AttributeOverride(name = "name",
66 column = @Column(name = "derived_from_name")),
67 @AttributeOverride(name = "version",
68 column = @Column(name = "derived_from_version"))
70 private PfConceptKey derivedFrom;
73 private Map<String, String> metadata;
76 private String description;
80 * The Default Constructor creates a {@link ToscaEntityType} object with a null key.
82 public ToscaEntityType() {
83 this(new PfConceptKey());
87 * The Key Constructor creates a {@link ToscaEntityType} object with the given concept key.
91 public ToscaEntityType(@NonNull final PfConceptKey key) {
98 * @param copyConcept the concept to copy from
100 public ToscaEntityType(final ToscaEntityType copyConcept) {
105 public List<PfKey> getKeys() {
106 final List<PfKey> keyList = getKey().getKeys();
107 if (derivedFrom != null) {
108 keyList.addAll(derivedFrom.getKeys());
114 public void clean() {
117 if (derivedFrom != null) {
121 if (metadata != null) {
122 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
123 metadataEntry.setValue(metadataEntry.getValue().trim());
127 description = (description != null ? description.trim() : null);
131 public PfValidationResult validate(PfValidationResult resultIn) {
132 PfValidationResult result = resultIn;
134 if (key.isNullKey()) {
135 result.addValidationMessage(
136 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
139 result = key.validate(result);
141 if (derivedFrom != null && derivedFrom.isNullKey()) {
142 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
143 "derived from key is a null key"));
146 if (metadata != null) {
147 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
148 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
149 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
150 "property metadata key may not be null"));
152 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
153 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
154 "property metadata value may not be null"));
159 if (description != null && description.trim().length() == 0) {
160 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
161 "property description may not be blank"));
168 public int compareTo(final PfConcept otherConcept) {
169 if (otherConcept == null) {
172 if (this == otherConcept) {
175 if (getClass() != otherConcept.getClass()) {
176 return this.hashCode() - otherConcept.hashCode();
179 final ToscaEntityType other = (ToscaEntityType) otherConcept;
180 if (!key.equals(other.key)) {
181 return key.compareTo(other.key);
184 int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
189 result = PfUtils.compareObjects(metadata, other.metadata);
194 return ObjectUtils.compare(description, other.description);
198 public PfConcept copyTo(@NonNull PfConcept target) {
199 final Object copyObject = target;
200 Assertions.instanceOf(copyObject, PfConcept.class);
202 final ToscaEntityType copy = ((ToscaEntityType) copyObject);
203 copy.setKey(new PfConceptKey(key));
204 copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
206 if (metadata != null) {
207 final Map<String, String> newMatadata = new TreeMap<>();
208 for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
209 newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
211 copy.setMetadata(newMatadata);
214 copy.setDescription(description);