2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021, 2023 Nordix Foundation.
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.Lob;
28 import jakarta.persistence.MappedSuperclass;
29 import jakarta.ws.rs.core.Response;
30 import java.io.Serial;
31 import java.util.LinkedHashMap;
32 import java.util.List;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
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.PfModelRuntimeException;
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.ToscaWithTypeAndObjectProperties;
49 * Class to represent JPA TOSCA classes containing property maps whose values are Strings.
53 @EqualsAndHashCode(callSuper = true)
54 public abstract class JpaToscaWithTypeAndStringProperties<T extends ToscaWithTypeAndObjectProperties>
55 extends JpaToscaEntityType<T> implements PfAuthorative<T> {
58 private static final long serialVersionUID = 2785481541573683089L;
61 @AttributeOverride(name = "name", column = @Column(name = "type_name"))
62 @AttributeOverride(name = "version", column = @Column(name = "type_version"))
65 private PfConceptKey type;
69 private Map<@NotNull String, @NotNull String> properties;
72 * The Default Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with a null key.
74 protected JpaToscaWithTypeAndStringProperties() {
75 this(new PfConceptKey());
79 * The Key Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with the given concept key.
83 protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key) {
84 this(key, new PfConceptKey());
88 * The full Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with all mandatory fields.
91 * @param type the type of the policy
93 protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
101 * @param copyConcept the concept to copy from
103 protected JpaToscaWithTypeAndStringProperties(@NonNull final JpaToscaWithTypeAndStringProperties<T> copyConcept) {
105 this.type = new PfConceptKey(copyConcept.type);
106 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
110 * Authorative constructor.
112 * @param authorativeConcept the authorative concept to copy from
114 protected JpaToscaWithTypeAndStringProperties(final T authorativeConcept) {
115 super(new PfConceptKey());
116 type = new PfConceptKey();
117 this.fromAuthorative(authorativeConcept);
121 public T toAuthorative() {
122 var tosca = super.toAuthorative();
124 tosca.setType(type.getName());
126 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
127 tosca.setTypeVersion(type.getVersion());
129 tosca.setTypeVersion(null);
132 tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
138 public void fromAuthorative(@NonNull final T authorativeConcept) {
139 super.fromAuthorative(authorativeConcept);
141 if (authorativeConcept.getType() != null) {
142 type.setName(authorativeConcept.getType());
144 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
145 "Type not specified, the type of this TOSCA entity must be specified in the type field");
148 if (authorativeConcept.getTypeVersion() != null) {
149 type.setVersion(authorativeConcept.getTypeVersion());
151 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
152 "Version not specified, the version of this TOSCA entity must be specified"
153 + " in the type_version field");
156 properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
160 * Deserializes a property value.
162 * @param propValue value to be deserialized
163 * @return the deserialized property value
165 protected abstract Object deserializePropertyValue(String propValue);
168 * Serializes a property value.
170 * @param propValue value to be serialized
171 * @return the serialized property value
173 protected abstract String serializePropertyValue(Object propValue);
176 public List<PfKey> getKeys() {
177 final List<PfKey> keyList = super.getKeys();
179 keyList.addAll(type.getKeys());
185 public void clean() {
190 properties = PfUtils.mapMap(properties, String::trim);
194 * Validates the fields of the object, including its key.
196 * @param fieldName name of the field containing this
197 * @return the result, or {@code null}
199 protected BeanValidationResult validateWithKey(String fieldName) {
200 BeanValidationResult result = super.validate(fieldName);
202 validateKeyVersionNotNull(result, "key", getKey());
208 public int compareTo(final PfConcept otherConcept) {
209 if (this == otherConcept) {
213 int result = super.compareTo(otherConcept);
218 @SuppressWarnings("unchecked")
219 final JpaToscaWithTypeAndStringProperties<T> other = (JpaToscaWithTypeAndStringProperties<T>) otherConcept;
221 result = type.compareTo(other.type);
226 return PfUtils.compareMaps(properties, other.properties);