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