2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020, 2022-2023 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 jakarta.persistence.AttributeOverride;
25 import jakarta.persistence.Column;
26 import jakarta.persistence.ElementCollection;
27 import jakarta.persistence.EmbeddedId;
28 import jakarta.persistence.Lob;
29 import jakarta.persistence.MappedSuperclass;
30 import jakarta.ws.rs.core.Response;
31 import java.io.Serial;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.TreeMap;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.common.parameters.annotations.NotBlank;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfModelRuntimeException;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.validation.annotations.VerifyKey;
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> {
61 private static final long serialVersionUID = -1330661834220739393L;
63 private static final StandardCoder STANDARD_CODER = new StandardCoder();
68 private PfConceptKey key;
72 @AttributeOverride(name = "name", column = @Column(name = "derived_from_name"))
73 @AttributeOverride(name = "version", column = @Column(name = "derived_from_version"))
75 private PfConceptKey derivedFrom;
79 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> metadata;
83 private String description;
85 private transient T toscaEntity;
89 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
91 public JpaToscaEntityType() {
92 this(new PfConceptKey());
96 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
100 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
107 * @param copyConcept the concept to copy from
109 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
111 this.key = new PfConceptKey(copyConcept.key);
112 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
113 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
114 this.description = copyConcept.description;
118 * Authorative constructor.
120 * @param authorativeConcept the authorative concept to copy from
122 public JpaToscaEntityType(final T authorativeConcept) {
123 this.fromAuthorative(authorativeConcept);
127 public T toAuthorative() {
128 toscaEntity.setName(getKey().getName());
129 toscaEntity.setVersion(getKey().getVersion());
131 if (derivedFrom != null) {
132 toscaEntity.setDerivedFrom(derivedFrom.getName());
135 if (description != null) {
136 toscaEntity.setDescription(description);
139 toscaEntity.setMetadata(PfUtils.mapMap(metadata, this::deserializeMetadataValue));
145 public void fromAuthorative(T toscaEntity) {
146 key = new PfConceptKey();
148 if (toscaEntity.getName() != null) {
149 key.setName(toscaEntity.getName());
152 if (toscaEntity.getVersion() != null) {
153 key.setVersion(toscaEntity.getVersion());
156 if (toscaEntity.getDerivedFrom() != null) {
157 // Check if the derived from field contains a name-version ID
158 if (toscaEntity.getDerivedFrom().contains(":")) {
159 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
161 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
165 if (toscaEntity.getDescription() != null) {
166 description = toscaEntity.getDescription();
169 metadata = PfUtils.mapMap(toscaEntity.getMetadata(), this::serializeMetadataValue);
173 public List<PfKey> getKeys() {
174 final List<PfKey> keyList = getKey().getKeys();
175 if (derivedFrom != null) {
176 keyList.addAll(derivedFrom.getKeys());
182 public void clean() {
185 if (derivedFrom != null) {
189 if (metadata != null) {
190 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
191 metadataEntry.setValue(metadataEntry.getValue().trim());
195 description = (description != null ? description.trim() : null);
199 public int compareTo(final PfConcept otherConcept) {
200 if (otherConcept == null) {
203 if (this == otherConcept) {
206 if (getClass() != otherConcept.getClass()) {
207 return getClass().getName().compareTo(otherConcept.getClass().getName());
210 @SuppressWarnings("unchecked")
211 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
213 int result = key.compareTo(other.key);
218 result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
223 result = PfUtils.compareMaps(metadata, other.metadata);
228 return ObjectUtils.compare(description, other.description);
231 protected Object deserializeMetadataValue(String metadataValue) {
233 return STANDARD_CODER.decode(metadataValue, Object.class);
234 } catch (CoderException ce) {
235 String errorMessage = "error decoding metadata JSON value read from database: " + metadataValue;
236 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
240 protected String serializeMetadataValue(Object metadataValue) {
242 return STANDARD_CODER.encode(metadataValue);
243 } catch (CoderException ce) {
244 String errorMessage = "error encoding metadata JSON value for database: " + metadataValue;
245 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);