Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithTypeAndStringProperties.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
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;
33 import java.util.Map;
34 import lombok.Data;
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;
47
48 /**
49  * Class to represent JPA TOSCA classes containing property maps whose values are Strings.
50  */
51 @MappedSuperclass
52 @Data
53 @EqualsAndHashCode(callSuper = true)
54 public abstract class JpaToscaWithTypeAndStringProperties<T extends ToscaWithTypeAndObjectProperties>
55         extends JpaToscaEntityType<T> implements PfAuthorative<T> {
56
57     @Serial
58     private static final long serialVersionUID = 2785481541573683089L;
59
60     @Column
61     @AttributeOverride(name = "name", column = @Column(name = "type_name"))
62     @AttributeOverride(name = "version", column = @Column(name = "type_version"))
63     @VerifyKey
64     @NotNull
65     private PfConceptKey type;
66
67     @ElementCollection
68     @Lob
69     private Map<@NotNull String, @NotNull String> properties;
70
71     /**
72      * The Default Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with a null key.
73      */
74     protected JpaToscaWithTypeAndStringProperties() {
75         this(new PfConceptKey());
76     }
77
78     /**
79      * The Key Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with the given concept key.
80      *
81      * @param key the key
82      */
83     protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key) {
84         this(key, new PfConceptKey());
85     }
86
87     /**
88      * The full Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with all mandatory fields.
89      *
90      * @param key the key
91      * @param type the type of the policy
92      */
93     protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
94         super(key);
95         this.type = type;
96     }
97
98     /**
99      * Copy constructor.
100      *
101      * @param copyConcept the concept to copy from
102      */
103     protected JpaToscaWithTypeAndStringProperties(@NonNull final JpaToscaWithTypeAndStringProperties<T> copyConcept) {
104         super(copyConcept);
105         this.type = new PfConceptKey(copyConcept.type);
106         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
107     }
108
109     /**
110      * Authorative constructor.
111      *
112      * @param authorativeConcept the authorative concept to copy from
113      */
114     protected JpaToscaWithTypeAndStringProperties(final T authorativeConcept) {
115         super(new PfConceptKey());
116         type = new PfConceptKey();
117         this.fromAuthorative(authorativeConcept);
118     }
119
120     @Override
121     public T toAuthorative() {
122         var tosca = super.toAuthorative();
123
124         tosca.setType(type.getName());
125
126         if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
127             tosca.setTypeVersion(type.getVersion());
128         } else {
129             tosca.setTypeVersion(null);
130         }
131
132         tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
133
134         return tosca;
135     }
136
137     @Override
138     public void fromAuthorative(@NonNull final T authorativeConcept) {
139         super.fromAuthorative(authorativeConcept);
140
141         if (authorativeConcept.getType() != null) {
142             type.setName(authorativeConcept.getType());
143         } else {
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");
146         }
147
148         if (authorativeConcept.getTypeVersion() != null) {
149             type.setVersion(authorativeConcept.getTypeVersion());
150         } else {
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");
154         }
155
156         properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
157     }
158
159     /**
160      * Deserializes a property value.
161      *
162      * @param propValue value to be deserialized
163      * @return the deserialized property value
164      */
165     protected abstract Object deserializePropertyValue(String propValue);
166
167     /**
168      * Serializes a property value.
169      *
170      * @param propValue value to be serialized
171      * @return the serialized property value
172      */
173     protected abstract String serializePropertyValue(Object propValue);
174
175     @Override
176     public List<PfKey> getKeys() {
177         final List<PfKey> keyList = super.getKeys();
178
179         keyList.addAll(type.getKeys());
180
181         return keyList;
182     }
183
184     @Override
185     public void clean() {
186         super.clean();
187
188         type.clean();
189
190         properties = PfUtils.mapMap(properties, String::trim);
191     }
192
193     /**
194      * Validates the fields of the object, including its key.
195      *
196      * @param fieldName name of the field containing this
197      * @return the result, or {@code null}
198      */
199     protected BeanValidationResult validateWithKey(String fieldName) {
200         BeanValidationResult result = super.validate(fieldName);
201
202         validateKeyVersionNotNull(result, "key", getKey());
203
204         return result;
205     }
206
207     @Override
208     public int compareTo(final PfConcept otherConcept) {
209         if (this == otherConcept) {
210             return 0;
211         }
212
213         int result = super.compareTo(otherConcept);
214         if (result != 0) {
215             return result;
216         }
217
218         @SuppressWarnings("unchecked")
219         final JpaToscaWithTypeAndStringProperties<T> other = (JpaToscaWithTypeAndStringProperties<T>) otherConcept;
220
221         result = type.compareTo(other.type);
222         if (result != 0) {
223             return result;
224         }
225
226         return PfUtils.compareMaps(properties, other.properties);
227     }
228 }