a39515b4960ebe55b8e91aafdee8495c6a518325
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaEntityType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.TreeMap;
28
29 import javax.persistence.AttributeOverride;
30 import javax.persistence.AttributeOverrides;
31 import javax.persistence.Column;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.MappedSuperclass;
35
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.common.utils.validation.Assertions;
42 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfUtils;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
52
53 /**
54  * Class to represent the EntrySchema of list/map property in TOSCA definition.
55  */
56 @MappedSuperclass
57 @Data
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
60     private static final long serialVersionUID = -1330661834220739393L;
61
62     @EmbeddedId
63     private PfConceptKey key;
64
65     // @formatter:off
66     @Column
67     @AttributeOverrides({
68         @AttributeOverride(name = "name",
69                            column = @Column(name = "derived_from_name")),
70         @AttributeOverride(name = "version",
71                            column = @Column(name = "derived_from_version"))
72         })
73     private PfConceptKey derivedFrom;
74
75     @ElementCollection
76     private Map<String, String> metadata;
77
78     @Column
79     private String description;
80
81     private transient T toscaEntity;
82     // @formatter:on
83
84     /**
85      * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
86      */
87     public JpaToscaEntityType() {
88         this(new PfConceptKey());
89     }
90
91     /**
92      * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
93      *
94      * @param key the key
95      */
96     public JpaToscaEntityType(@NonNull final PfConceptKey key) {
97         this.key = key;
98     }
99
100     /**
101      * Copy constructor.
102      *
103      * @param copyConcept the concept to copy from
104      */
105     public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
106         super(copyConcept);
107     }
108
109     /**
110      * Authorative constructor.
111      *
112      * @param authorativeConcept the authorative concept to copy from
113      */
114     public JpaToscaEntityType(final T authorativeConcept) {
115         this.fromAuthorative(authorativeConcept);
116     }
117
118     @Override
119     public T toAuthorative() {
120         toscaEntity.setName(getKey().getName());
121         toscaEntity.setVersion(getKey().getVersion());
122
123         if (derivedFrom != null) {
124             toscaEntity.setDerivedFrom(derivedFrom.getId());
125         }
126
127         if (description != null) {
128             toscaEntity.setDescription(description);
129         }
130
131         if (metadata != null) {
132             Map<String, String> metadataMap = new LinkedHashMap<>();
133
134             for (Entry<String, String> entry : metadata.entrySet()) {
135                 metadataMap.put(entry.getKey(), entry.getValue());
136             }
137
138             toscaEntity.setMetadata(metadataMap);
139         }
140
141         return toscaEntity;
142     }
143
144     @Override
145     public void fromAuthorative(T toscaEntity) {
146         key = new PfConceptKey();
147
148         if (toscaEntity.getName() != null) {
149             key.setName(toscaEntity.getName());
150         }
151
152         if (toscaEntity.getVersion() != null) {
153             key.setVersion(toscaEntity.getVersion());
154         }
155
156
157         if (toscaEntity.getDerivedFrom() != null) {
158             // CHeck if the derived from field contains a name-version ID
159             if (toscaEntity.getDerivedFrom().contains(":")) {
160                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
161             } else {
162                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
163             }
164         }
165
166         if (toscaEntity.getDescription() != null) {
167             description = toscaEntity.getDescription();
168         }
169
170         if (toscaEntity.getMetadata() != null) {
171             metadata = new LinkedHashMap<>();
172
173             for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) {
174                 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
175             }
176         }
177     }
178
179     @Override
180     public List<PfKey> getKeys() {
181         final List<PfKey> keyList = getKey().getKeys();
182         if (derivedFrom != null) {
183             keyList.addAll(derivedFrom.getKeys());
184         }
185         return keyList;
186     }
187
188     @Override
189     public void clean() {
190         key.clean();
191
192         if (derivedFrom != null) {
193             derivedFrom.clean();
194         }
195
196         if (metadata != null) {
197             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
198                 metadataEntry.setValue(metadataEntry.getValue().trim());
199             }
200         }
201
202         description = (description != null ? description.trim() : null);
203     }
204
205     @Override
206     public PfValidationResult validate(PfValidationResult resultIn) {
207         PfValidationResult result = resultIn;
208
209         if (key.isNullKey()) {
210             result.addValidationMessage(
211                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
212         }
213
214         result = key.validate(result);
215
216         if (derivedFrom != null && derivedFrom.isNullKey()) {
217             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
218                     "derived from key is a null key"));
219         }
220
221         if (metadata != null) {
222             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
223                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
224                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
225                             "property metadata key may not be null"));
226                 }
227                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
228                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
229                             "property metadata value may not be null"));
230                 }
231             }
232         }
233
234         if (description != null && description.trim().length() == 0) {
235             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
236                     "property description may not be blank"));
237         }
238
239         return result;
240     }
241
242     @Override
243     public int compareTo(final PfConcept otherConcept) {
244         if (otherConcept == null) {
245             return -1;
246         }
247         if (this == otherConcept) {
248             return 0;
249         }
250         if (getClass() != otherConcept.getClass()) {
251             return this.hashCode() - otherConcept.hashCode();
252         }
253
254         @SuppressWarnings("unchecked")
255         final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
256         if (!key.equals(other.key)) {
257             return key.compareTo(other.key);
258         }
259
260         int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
261         if (result != 0) {
262             return result;
263         }
264
265         result = PfUtils.compareObjects(metadata, other.metadata);
266         if (result != 0) {
267             return result;
268         }
269
270         return ObjectUtils.compare(description, other.description);
271     }
272
273     @Override
274     public PfConcept copyTo(@NonNull PfConcept target) {
275         final Object copyObject = target;
276         Assertions.instanceOf(copyObject, PfConcept.class);
277
278         @SuppressWarnings("unchecked")
279         final JpaToscaEntityType<T> copy = ((JpaToscaEntityType<T>) copyObject);
280         copy.setKey(new PfConceptKey(key));
281         copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
282
283         if (metadata != null) {
284             final Map<String, String> newMatadata = new TreeMap<>();
285             for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
286                 newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
287             }
288             copy.setMetadata(newMatadata);
289         }
290
291         copy.setDescription(description);
292
293         return copy;
294     }
295 }