2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020,2022 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 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.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Lob;
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.annotations.NotBlank;
39 import org.onap.policy.common.parameters.annotations.NotNull;
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.PfUtils;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
49 * Class to represent the EntrySchema of list/map property in TOSCA definition.
53 @EqualsAndHashCode(callSuper = false)
54 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
55 private static final long serialVersionUID = -1330661834220739393L;
60 private PfConceptKey key;
64 @AttributeOverride(name = "name", column = @Column(name = "derived_from_name"))
65 @AttributeOverride(name = "version", column = @Column(name = "derived_from_version"))
67 private PfConceptKey derivedFrom;
71 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> metadata;
75 private String description;
77 private transient T toscaEntity;
81 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
83 public JpaToscaEntityType() {
84 this(new PfConceptKey());
88 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
92 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
99 * @param copyConcept the concept to copy from
101 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
103 this.key = new PfConceptKey(copyConcept.key);
104 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
105 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
106 this.description = copyConcept.description;
110 * Authorative constructor.
112 * @param authorativeConcept the authorative concept to copy from
114 public JpaToscaEntityType(final T authorativeConcept) {
115 this.fromAuthorative(authorativeConcept);
119 public T toAuthorative() {
120 toscaEntity.setName(getKey().getName());
121 toscaEntity.setVersion(getKey().getVersion());
123 if (derivedFrom != null) {
124 toscaEntity.setDerivedFrom(derivedFrom.getName());
127 if (description != null) {
128 toscaEntity.setDescription(description);
131 toscaEntity.setMetadata(PfUtils.mapMap(metadata, item -> item));
137 public void fromAuthorative(T toscaEntity) {
138 key = new PfConceptKey();
140 if (toscaEntity.getName() != null) {
141 key.setName(toscaEntity.getName());
144 if (toscaEntity.getVersion() != null) {
145 key.setVersion(toscaEntity.getVersion());
148 if (toscaEntity.getDerivedFrom() != null) {
149 // Check if the derived from field contains a name-version ID
150 if (toscaEntity.getDerivedFrom().contains(":")) {
151 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
153 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
157 if (toscaEntity.getDescription() != null) {
158 description = toscaEntity.getDescription();
161 metadata = PfUtils.mapMap(toscaEntity.getMetadata(), Object::toString);
165 public List<PfKey> getKeys() {
166 final List<PfKey> keyList = getKey().getKeys();
167 if (derivedFrom != null) {
168 keyList.addAll(derivedFrom.getKeys());
174 public void clean() {
177 if (derivedFrom != null) {
181 if (metadata != null) {
182 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
183 metadataEntry.setValue(metadataEntry.getValue().trim());
187 description = (description != null ? description.trim() : null);
191 public int compareTo(final PfConcept otherConcept) {
192 if (otherConcept == null) {
195 if (this == otherConcept) {
198 if (getClass() != otherConcept.getClass()) {
199 return getClass().getName().compareTo(otherConcept.getClass().getName());
202 @SuppressWarnings("unchecked")
203 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
205 int result = key.compareTo(other.key);
210 result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
215 result = PfUtils.compareMaps(metadata, other.metadata);
220 return ObjectUtils.compare(description, other.description);