2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.LinkedHashMap;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.TreeMap;
30 import javax.persistence.AttributeOverride;
31 import javax.persistence.AttributeOverrides;
32 import javax.persistence.Column;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.EmbeddedId;
35 import javax.persistence.MappedSuperclass;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfUtils;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
54 * Class to represent the EntrySchema of list/map property in TOSCA definition.
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
60 private static final long serialVersionUID = -1330661834220739393L;
63 private PfConceptKey key;
68 @AttributeOverride(name = "name",
69 column = @Column(name = "derived_from_name")),
70 @AttributeOverride(name = "version",
71 column = @Column(name = "derived_from_version"))
73 private PfConceptKey derivedFrom;
76 private Map<String, String> metadata = new TreeMap<>();
79 private String description;
81 private transient T toscaEntity;
85 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
87 public JpaToscaEntityType() {
88 this(new PfConceptKey());
92 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
96 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
103 * @param copyConcept the concept to copy from
105 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
107 this.key = new PfConceptKey(copyConcept.key);
108 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
109 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
110 this.description = copyConcept.description;
114 * Authorative constructor.
116 * @param authorativeConcept the authorative concept to copy from
118 public JpaToscaEntityType(final T authorativeConcept) {
119 this.fromAuthorative(authorativeConcept);
123 public T toAuthorative() {
124 toscaEntity.setName(getKey().getName());
125 toscaEntity.setVersion(getKey().getVersion());
127 if (derivedFrom != null) {
128 toscaEntity.setDerivedFrom(derivedFrom.getName());
131 if (description != null) {
132 toscaEntity.setDescription(description);
135 if (metadata != null) {
136 Map<String, String> metadataMap = new LinkedHashMap<>();
138 for (Entry<String, String> entry : metadata.entrySet()) {
139 metadataMap.put(entry.getKey(), entry.getValue());
142 toscaEntity.setMetadata(metadataMap);
149 public void fromAuthorative(T toscaEntity) {
150 key = new PfConceptKey();
152 if (toscaEntity.getName() != null) {
153 key.setName(toscaEntity.getName());
156 if (toscaEntity.getVersion() != null) {
157 key.setVersion(toscaEntity.getVersion());
160 if (toscaEntity.getDerivedFrom() != null) {
161 // CHeck if the derived from field contains a name-version ID
162 if (toscaEntity.getDerivedFrom().contains(":")) {
163 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
165 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
169 if (toscaEntity.getDescription() != null) {
170 description = toscaEntity.getDescription();
173 if (toscaEntity.getMetadata() != null) {
174 metadata = new LinkedHashMap<>();
176 for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) {
177 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
183 public List<PfKey> getKeys() {
184 final List<PfKey> keyList = getKey().getKeys();
185 if (derivedFrom != null) {
186 keyList.addAll(derivedFrom.getKeys());
192 public void clean() {
195 if (derivedFrom != null) {
199 if (metadata != null) {
200 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
201 metadataEntry.setValue(metadataEntry.getValue().trim());
205 description = (description != null ? description.trim() : null);
209 public PfValidationResult validate(PfValidationResult resultIn) {
210 PfValidationResult result = resultIn;
212 if (key.isNullKey()) {
213 result.addValidationMessage(
214 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
217 result = key.validate(result);
219 if (derivedFrom != null && derivedFrom.isNullKey()) {
220 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
221 "derived from key is a null key"));
224 if (metadata != null) {
225 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
226 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
227 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
228 "property metadata key may not be null"));
230 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
231 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
232 "property metadata value may not be null"));
237 if (description != null && description.trim().length() == 0) {
238 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
239 "property description may not be blank"));
246 public int compareTo(final PfConcept otherConcept) {
247 if (otherConcept == null) {
250 if (this == otherConcept) {
253 if (getClass() != otherConcept.getClass()) {
254 return getClass().getName().compareTo(otherConcept.getClass().getName());
257 @SuppressWarnings("unchecked")
258 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
259 if (!key.equals(other.key)) {
260 return key.compareTo(other.key);
263 int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
268 result = PfUtils.compareObjects(metadata, other.metadata);
273 return ObjectUtils.compare(description, other.description);