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 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.Lob;
34 import javax.persistence.MappedSuperclass;
35 import javax.ws.rs.core.Response;
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> {
60 private static final long serialVersionUID = -1330661834220739393L;
62 private static final StandardCoder STANDARD_CODER = new StandardCoder();
67 private PfConceptKey key;
72 @AttributeOverride(name = "name", column = @Column(name = "derived_from_name")),
73 @AttributeOverride(name = "version", column = @Column(name = "derived_from_version"))
76 private PfConceptKey derivedFrom;
80 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> metadata;
84 private String description;
86 private transient T toscaEntity;
90 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
92 public JpaToscaEntityType() {
93 this(new PfConceptKey());
97 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
101 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
108 * @param copyConcept the concept to copy from
110 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
112 this.key = new PfConceptKey(copyConcept.key);
113 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
114 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
115 this.description = copyConcept.description;
119 * Authorative constructor.
121 * @param authorativeConcept the authorative concept to copy from
123 public JpaToscaEntityType(final T authorativeConcept) {
124 this.fromAuthorative(authorativeConcept);
128 public T toAuthorative() {
129 toscaEntity.setName(getKey().getName());
130 toscaEntity.setVersion(getKey().getVersion());
132 if (derivedFrom != null) {
133 toscaEntity.setDerivedFrom(derivedFrom.getName());
136 if (description != null) {
137 toscaEntity.setDescription(description);
140 toscaEntity.setMetadata(PfUtils.mapMap(metadata, this::deserializeMetadataValue));
146 public void fromAuthorative(T toscaEntity) {
147 key = new PfConceptKey();
149 if (toscaEntity.getName() != null) {
150 key.setName(toscaEntity.getName());
153 if (toscaEntity.getVersion() != null) {
154 key.setVersion(toscaEntity.getVersion());
157 if (toscaEntity.getDerivedFrom() != null) {
158 // Check if the derived from field contains a name-version ID
159 if (toscaEntity.getDerivedFrom().contains(":")) {
160 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
162 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
166 if (toscaEntity.getDescription() != null) {
167 description = toscaEntity.getDescription();
170 metadata = PfUtils.mapMap(toscaEntity.getMetadata(), this::serializeMetadataValue);
174 public List<PfKey> getKeys() {
175 final List<PfKey> keyList = getKey().getKeys();
176 if (derivedFrom != null) {
177 keyList.addAll(derivedFrom.getKeys());
183 public void clean() {
186 if (derivedFrom != null) {
190 if (metadata != null) {
191 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
192 metadataEntry.setValue(metadataEntry.getValue().trim());
196 description = (description != null ? description.trim() : null);
200 public int compareTo(final PfConcept otherConcept) {
201 if (otherConcept == null) {
204 if (this == otherConcept) {
207 if (getClass() != otherConcept.getClass()) {
208 return getClass().getName().compareTo(otherConcept.getClass().getName());
211 @SuppressWarnings("unchecked")
212 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
214 int result = key.compareTo(other.key);
219 result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
224 result = PfUtils.compareMaps(metadata, other.metadata);
229 return ObjectUtils.compare(description, other.description);
232 protected Object deserializeMetadataValue(String metadataValue) {
234 return STANDARD_CODER.decode(metadataValue, Object.class);
235 } catch (CoderException ce) {
236 String errorMessage = "error decoding metadata JSON value read from database: " + metadataValue;
237 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
241 protected String serializeMetadataValue(Object metadataValue) {
243 return STANDARD_CODER.encode(metadataValue);
244 } catch (CoderException ce) {
245 String errorMessage = "error encoding metadata JSON value for database: " + metadataValue;
246 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);