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 java.util.LinkedHashMap;
25 import java.util.List;
27 import javax.persistence.AttributeOverride;
28 import javax.persistence.AttributeOverrides;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Lob;
32 import javax.persistence.MappedSuperclass;
33 import javax.ws.rs.core.Response;
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> {
57 private static final long serialVersionUID = 2785481541573683089L;
60 @AttributeOverrides ({
61 @AttributeOverride(name = "name", column = @Column(name = "type_name")),
62 @AttributeOverride(name = "version", column = @Column(name = "type_version"))
66 private PfConceptKey type;
70 private Map<@NotNull String, @NotNull String> properties;
73 * The Default Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with a null key.
75 protected JpaToscaWithTypeAndStringProperties() {
76 this(new PfConceptKey());
80 * The Key Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with the given concept key.
84 protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key) {
85 this(key, new PfConceptKey());
89 * The full Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with all mandatory fields.
92 * @param type the type of the policy
94 protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
102 * @param copyConcept the concept to copy from
104 protected JpaToscaWithTypeAndStringProperties(@NonNull final JpaToscaWithTypeAndStringProperties<T> copyConcept) {
106 this.type = new PfConceptKey(copyConcept.type);
107 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
111 * Authorative constructor.
113 * @param authorativeConcept the authorative concept to copy from
115 protected JpaToscaWithTypeAndStringProperties(final T authorativeConcept) {
116 super(new PfConceptKey());
117 type = new PfConceptKey();
118 this.fromAuthorative(authorativeConcept);
122 public T toAuthorative() {
123 var tosca = super.toAuthorative();
125 tosca.setType(type.getName());
127 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
128 tosca.setTypeVersion(type.getVersion());
130 tosca.setTypeVersion(null);
133 tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
139 public void fromAuthorative(@NonNull final T authorativeConcept) {
140 super.fromAuthorative(authorativeConcept);
142 if (authorativeConcept.getType() != null) {
143 type.setName(authorativeConcept.getType());
145 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
146 "Type not specified, the type of this TOSCA entity must be specified in the type field");
149 if (authorativeConcept.getTypeVersion() != null) {
150 type.setVersion(authorativeConcept.getTypeVersion());
152 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
153 "Version not specified, the version of this TOSCA entity must be specified"
154 + " in the type_version field");
157 properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
161 * Deserializes a property value.
163 * @param propValue value to be deserialized
164 * @return the deserialized property value
166 protected abstract Object deserializePropertyValue(String propValue);
169 * Serializes a property value.
171 * @param propValue value to be serialized
172 * @return the serialized property value
174 protected abstract String serializePropertyValue(Object propValue);
177 public List<PfKey> getKeys() {
178 final List<PfKey> keyList = super.getKeys();
180 keyList.addAll(type.getKeys());
186 public void clean() {
191 properties = PfUtils.mapMap(properties, String::trim);
195 * Validates the fields of the object, including its key.
197 * @param fieldName name of the field containing this
198 * @return the result, or {@code null}
200 protected BeanValidationResult validateWithKey(String fieldName) {
201 BeanValidationResult result = super.validate(fieldName);
203 validateKeyVersionNotNull(result, "key", getKey());
209 public int compareTo(final PfConcept otherConcept) {
210 if (this == otherConcept) {
214 int result = super.compareTo(otherConcept);
219 @SuppressWarnings("unchecked")
220 final JpaToscaWithTypeAndStringProperties<T> other = (JpaToscaWithTypeAndStringProperties<T>) otherConcept;
222 result = type.compareTo(other.type);
227 return PfUtils.compareMaps(properties, other.properties);