2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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.List;
26 import java.util.Map.Entry;
27 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;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.common.parameters.BeanValidationResult;
39 import org.onap.policy.models.base.PfAuthorative;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfUtils;
44 import org.onap.policy.models.base.Validated;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
48 * Class to represent the EntrySchema of list/map property in TOSCA definition.
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
54 private static final long serialVersionUID = -1330661834220739393L;
57 private PfConceptKey key;
62 @AttributeOverride(name = "name",
63 column = @Column(name = "derived_from_name")),
64 @AttributeOverride(name = "version",
65 column = @Column(name = "derived_from_version"))
67 private PfConceptKey derivedFrom;
70 private Map<String, String> metadata;
73 private String description;
75 private transient T toscaEntity;
79 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
81 public JpaToscaEntityType() {
82 this(new PfConceptKey());
86 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
90 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
97 * @param copyConcept the concept to copy from
99 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
101 this.key = new PfConceptKey(copyConcept.key);
102 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
103 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
104 this.description = copyConcept.description;
108 * Authorative constructor.
110 * @param authorativeConcept the authorative concept to copy from
112 public JpaToscaEntityType(final T authorativeConcept) {
113 this.fromAuthorative(authorativeConcept);
117 public T toAuthorative() {
118 toscaEntity.setName(getKey().getName());
119 toscaEntity.setVersion(getKey().getVersion());
121 if (derivedFrom != null) {
122 toscaEntity.setDerivedFrom(derivedFrom.getName());
125 if (description != null) {
126 toscaEntity.setDescription(description);
129 toscaEntity.setMetadata(PfUtils.mapMap(metadata, item -> item));
135 public void fromAuthorative(T toscaEntity) {
136 key = new PfConceptKey();
138 if (toscaEntity.getName() != null) {
139 key.setName(toscaEntity.getName());
142 if (toscaEntity.getVersion() != null) {
143 key.setVersion(toscaEntity.getVersion());
146 if (toscaEntity.getDerivedFrom() != null) {
147 // Check if the derived from field contains a name-version ID
148 if (toscaEntity.getDerivedFrom().contains(":")) {
149 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
151 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
155 if (toscaEntity.getDescription() != null) {
156 description = toscaEntity.getDescription();
159 metadata = PfUtils.mapMap(toscaEntity.getMetadata(), item -> item);
163 public List<PfKey> getKeys() {
164 final List<PfKey> keyList = getKey().getKeys();
165 if (derivedFrom != null) {
166 keyList.addAll(derivedFrom.getKeys());
172 public void clean() {
175 if (derivedFrom != null) {
179 if (metadata != null) {
180 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
181 metadataEntry.setValue(metadataEntry.getValue().trim());
185 description = (description != null ? description.trim() : null);
189 public BeanValidationResult validate(@NonNull String fieldName) {
190 BeanValidationResult result = new BeanValidationResult(fieldName, this);
192 result.addResult(validateKeyNotNull("key", key));
194 if (derivedFrom != null) {
195 result.addResult(validateKeyNotNull("derivedFrom", derivedFrom));
198 validateMap(result, "metadata", metadata, Validated::validateEntryNotBlankNotBlank);
199 result.addResult(validateNotBlank("description", description, false));
205 public int compareTo(final PfConcept otherConcept) {
206 if (otherConcept == null) {
209 if (this == otherConcept) {
212 if (getClass() != otherConcept.getClass()) {
213 return getClass().getName().compareTo(otherConcept.getClass().getName());
216 @SuppressWarnings("unchecked")
217 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
219 int result = key.compareTo(other.key);
224 result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
229 result = PfUtils.compareMaps(metadata, other.metadata);
234 return ObjectUtils.compare(description, other.description);