2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021 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.Column;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.Lob;
31 import javax.persistence.MappedSuperclass;
32 import javax.ws.rs.core.Response;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.models.base.PfAuthorative;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfModelRuntimeException;
43 import org.onap.policy.models.base.PfUtils;
44 import org.onap.policy.models.base.validation.annotations.VerifyKey;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
48 * Class to represent JPA TOSCA classes containing property maps whose values are Strings.
52 @EqualsAndHashCode(callSuper = true)
53 public abstract class JpaToscaWithTypeAndStringProperties<T extends ToscaWithTypeAndObjectProperties>
54 extends JpaToscaEntityType<T> implements PfAuthorative<T> {
56 private static final long serialVersionUID = 2785481541573683089L;
59 @AttributeOverride(name = "name", column = @Column(name = "type_name"))
60 @AttributeOverride(name = "version", column = @Column(name = "type_version"))
63 private PfConceptKey type;
67 private Map<@NotNull String, @NotNull String> properties;
70 * The Default Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with a null key.
72 protected JpaToscaWithTypeAndStringProperties() {
73 this(new PfConceptKey());
77 * The Key Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with the given concept key.
81 protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key) {
82 this(key, new PfConceptKey());
86 * The full Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with all mandatory fields.
89 * @param type the type of the policy
91 protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
99 * @param copyConcept the concept to copy from
101 protected JpaToscaWithTypeAndStringProperties(@NonNull final JpaToscaWithTypeAndStringProperties<T> copyConcept) {
103 this.type = new PfConceptKey(copyConcept.type);
104 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
108 * Authorative constructor.
110 * @param authorativeConcept the authorative concept to copy from
112 protected JpaToscaWithTypeAndStringProperties(final T authorativeConcept) {
113 super(new PfConceptKey());
114 type = new PfConceptKey();
115 this.fromAuthorative(authorativeConcept);
119 public T toAuthorative() {
120 T tosca = super.toAuthorative();
122 tosca.setType(type.getName());
124 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
125 tosca.setTypeVersion(type.getVersion());
127 tosca.setTypeVersion(null);
130 tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
136 public void fromAuthorative(@NonNull final T authorativeConcept) {
137 super.fromAuthorative(authorativeConcept);
139 if (authorativeConcept.getType() != null) {
140 type.setName(authorativeConcept.getType());
142 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
143 "Type not specified, the type of this TOSCA entity must be specified in the type field");
146 if (authorativeConcept.getTypeVersion() != null) {
147 type.setVersion(authorativeConcept.getTypeVersion());
149 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
150 "Version not specified, the version of this TOSCA entity must be specified"
151 + " in the type_version field");
154 properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
158 * Deserializes a property value.
160 * @param propValue value to be deserialized
161 * @return the deserialized property value
163 protected abstract Object deserializePropertyValue(String propValue);
166 * Serializes a property value.
168 * @param propValue value to be serialized
169 * @return the serialized property value
171 protected abstract String serializePropertyValue(Object propValue);
174 public List<PfKey> getKeys() {
175 final List<PfKey> keyList = super.getKeys();
177 keyList.addAll(type.getKeys());
183 public void clean() {
188 properties = PfUtils.mapMap(properties, String::trim);
192 * Validates the fields of the object, including its key.
194 * @param fieldName name of the field containing this
195 * @return the result, or {@code null}
197 protected BeanValidationResult validateWithKey(String fieldName) {
198 BeanValidationResult result = super.validate(fieldName);
200 validateKeyVersionNotNull(result, "key", getKey());
206 public int compareTo(final PfConcept otherConcept) {
207 if (this == otherConcept) {
211 int result = super.compareTo(otherConcept);
216 @SuppressWarnings("unchecked")
217 final JpaToscaWithTypeAndStringProperties<T> other = (JpaToscaWithTypeAndStringProperties<T>) otherConcept;
219 result = type.compareTo(other.type);
224 return PfUtils.compareMaps(properties, other.properties);