Implement validation and hierarchical get
[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-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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 java.util.Map.Entry;
28 import java.util.TreeMap;
29
30 import javax.persistence.AttributeOverride;
31 import javax.persistence.AttributeOverrides;
32 import javax.persistence.Column;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.EmbeddedId;
35 import javax.persistence.MappedSuperclass;
36
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40
41 import org.apache.commons.lang3.ObjectUtils;
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 = new TreeMap<>();
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         this.key = new PfConceptKey(copyConcept.key);
108         this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
109         this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
110         this.description = copyConcept.description;
111     }
112
113     /**
114      * Authorative constructor.
115      *
116      * @param authorativeConcept the authorative concept to copy from
117      */
118     public JpaToscaEntityType(final T authorativeConcept) {
119         this.fromAuthorative(authorativeConcept);
120     }
121
122     @Override
123     public T toAuthorative() {
124         toscaEntity.setName(getKey().getName());
125         toscaEntity.setVersion(getKey().getVersion());
126
127         if (derivedFrom != null) {
128             toscaEntity.setDerivedFrom(derivedFrom.getName());
129         }
130
131         if (description != null) {
132             toscaEntity.setDescription(description);
133         }
134
135         if (metadata != null) {
136             Map<String, String> metadataMap = new LinkedHashMap<>();
137
138             for (Entry<String, String> entry : metadata.entrySet()) {
139                 metadataMap.put(entry.getKey(), entry.getValue());
140             }
141
142             toscaEntity.setMetadata(metadataMap);
143         }
144
145         return toscaEntity;
146     }
147
148     @Override
149     public void fromAuthorative(T toscaEntity) {
150         key = new PfConceptKey();
151
152         if (toscaEntity.getName() != null) {
153             key.setName(toscaEntity.getName());
154         }
155
156         if (toscaEntity.getVersion() != null) {
157             key.setVersion(toscaEntity.getVersion());
158         }
159
160         if (toscaEntity.getDerivedFrom() != null) {
161             // CHeck if the derived from field contains a name-version ID
162             if (toscaEntity.getDerivedFrom().contains(":")) {
163                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
164             } else {
165                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
166             }
167         }
168
169         if (toscaEntity.getDescription() != null) {
170             description = toscaEntity.getDescription();
171         }
172
173         if (toscaEntity.getMetadata() != null) {
174             metadata = new LinkedHashMap<>();
175
176             for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) {
177                 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
178             }
179         }
180     }
181
182     @Override
183     public List<PfKey> getKeys() {
184         final List<PfKey> keyList = getKey().getKeys();
185         if (derivedFrom != null) {
186             keyList.addAll(derivedFrom.getKeys());
187         }
188         return keyList;
189     }
190
191     @Override
192     public void clean() {
193         key.clean();
194
195         if (derivedFrom != null) {
196             derivedFrom.clean();
197         }
198
199         if (metadata != null) {
200             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
201                 metadataEntry.setValue(metadataEntry.getValue().trim());
202             }
203         }
204
205         description = (description != null ? description.trim() : null);
206     }
207
208     @Override
209     public PfValidationResult validate(PfValidationResult resultIn) {
210         PfValidationResult result = resultIn;
211
212         if (key.isNullKey()) {
213             result.addValidationMessage(
214                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
215         }
216
217         result = key.validate(result);
218
219         if (derivedFrom != null && derivedFrom.isNullKey()) {
220             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
221                     "derived from key is a null key"));
222         }
223
224         if (metadata != null) {
225             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
226                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
227                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
228                             "property metadata key may not be null"));
229                 }
230                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
231                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
232                             "property metadata value may not be null"));
233                 }
234             }
235         }
236
237         if (description != null && description.trim().length() == 0) {
238             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
239                     "property description may not be blank"));
240         }
241
242         return result;
243     }
244
245     @Override
246     public int compareTo(final PfConcept otherConcept) {
247         if (otherConcept == null) {
248             return -1;
249         }
250         if (this == otherConcept) {
251             return 0;
252         }
253         if (getClass() != otherConcept.getClass()) {
254             return getClass().getName().compareTo(otherConcept.getClass().getName());
255         }
256
257         @SuppressWarnings("unchecked")
258         final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
259         if (!key.equals(other.key)) {
260             return key.compareTo(other.key);
261         }
262
263         int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
264         if (result != 0) {
265             return result;
266         }
267
268         result = PfUtils.compareObjects(metadata, other.metadata);
269         if (result != 0) {
270             return result;
271         }
272
273         return ObjectUtils.compare(description, other.description);
274     }
275 }