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;
34 import javax.ws.rs.core.Response;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.parameters.annotations.NotBlank;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
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.PfModelRuntimeException;
48 import org.onap.policy.models.base.PfUtils;
49 import org.onap.policy.models.base.validation.annotations.VerifyKey;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
53 * Class to represent the EntrySchema of list/map property in TOSCA definition.
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
59 private static final long serialVersionUID = -1330661834220739393L;
61 private static final StandardCoder STANDARD_CODER = new StandardCoder();
66 private PfConceptKey key;
70 @AttributeOverride(name = "name", column = @Column(name = "derived_from_name"))
71 @AttributeOverride(name = "version", column = @Column(name = "derived_from_version"))
73 private PfConceptKey derivedFrom;
77 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> metadata;
81 private String description;
83 private transient T toscaEntity;
87 * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
89 public JpaToscaEntityType() {
90 this(new PfConceptKey());
94 * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
98 public JpaToscaEntityType(@NonNull final PfConceptKey key) {
105 * @param copyConcept the concept to copy from
107 public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
109 this.key = new PfConceptKey(copyConcept.key);
110 this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
111 this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
112 this.description = copyConcept.description;
116 * Authorative constructor.
118 * @param authorativeConcept the authorative concept to copy from
120 public JpaToscaEntityType(final T authorativeConcept) {
121 this.fromAuthorative(authorativeConcept);
125 public T toAuthorative() {
126 toscaEntity.setName(getKey().getName());
127 toscaEntity.setVersion(getKey().getVersion());
129 if (derivedFrom != null) {
130 toscaEntity.setDerivedFrom(derivedFrom.getName());
133 if (description != null) {
134 toscaEntity.setDescription(description);
137 toscaEntity.setMetadata(PfUtils.mapMap(metadata, this::deserializeMetadataValue));
143 public void fromAuthorative(T toscaEntity) {
144 key = new PfConceptKey();
146 if (toscaEntity.getName() != null) {
147 key.setName(toscaEntity.getName());
150 if (toscaEntity.getVersion() != null) {
151 key.setVersion(toscaEntity.getVersion());
154 if (toscaEntity.getDerivedFrom() != null) {
155 // Check if the derived from field contains a name-version ID
156 if (toscaEntity.getDerivedFrom().contains(":")) {
157 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
159 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
163 if (toscaEntity.getDescription() != null) {
164 description = toscaEntity.getDescription();
167 metadata = PfUtils.mapMap(toscaEntity.getMetadata(), this::serializeMetadataValue);
171 public List<PfKey> getKeys() {
172 final List<PfKey> keyList = getKey().getKeys();
173 if (derivedFrom != null) {
174 keyList.addAll(derivedFrom.getKeys());
180 public void clean() {
183 if (derivedFrom != null) {
187 if (metadata != null) {
188 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
189 metadataEntry.setValue(metadataEntry.getValue().trim());
193 description = (description != null ? description.trim() : null);
197 public int compareTo(final PfConcept otherConcept) {
198 if (otherConcept == null) {
201 if (this == otherConcept) {
204 if (getClass() != otherConcept.getClass()) {
205 return getClass().getName().compareTo(otherConcept.getClass().getName());
208 @SuppressWarnings("unchecked")
209 final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
211 int result = key.compareTo(other.key);
216 result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
221 result = PfUtils.compareMaps(metadata, other.metadata);
226 return ObjectUtils.compare(description, other.description);
229 protected Object deserializeMetadataValue(String metadataValue) {
231 return STANDARD_CODER.decode(metadataValue, Object.class);
232 } catch (CoderException ce) {
233 String errorMessage = "error decoding metadata JSON value read from database: " + metadataValue;
234 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
238 protected String serializeMetadataValue(Object metadataValue) {
240 return STANDARD_CODER.encode(metadataValue);
241 } catch (CoderException ce) {
242 String errorMessage = "error encoding metadata JSON value for database: " + metadataValue;
243 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);