JPA concepts for TOSCA
[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.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.TreeMap;
28 import javax.persistence.AttributeOverride;
29 import javax.persistence.AttributeOverrides;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.MappedSuperclass;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
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.PfUtils;
44 import org.onap.policy.models.base.PfValidationMessage;
45 import org.onap.policy.models.base.PfValidationResult;
46 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
48
49 /**
50  * Class to represent the EntrySchema of list/map property in TOSCA definition.
51  */
52 @MappedSuperclass
53 @Data
54 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
56     private static final long serialVersionUID = -1330661834220739393L;
57
58     @EmbeddedId
59     private PfConceptKey key;
60
61     // @formatter:off
62     @Column
63     @AttributeOverrides({
64         @AttributeOverride(name = "name",
65                            column = @Column(name = "derived_from_name")),
66         @AttributeOverride(name = "version",
67                            column = @Column(name = "derived_from_version"))
68         })
69     private PfConceptKey derivedFrom;
70
71     @ElementCollection
72     private Map<String, String> metadata;
73
74     @Column
75     private String description;
76
77     private transient T toscaEntity;
78     // @formatter:on
79
80     /**
81      * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
82      */
83     public JpaToscaEntityType() {
84         this(new PfConceptKey());
85     }
86
87     /**
88      * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
89      *
90      * @param key the key
91      */
92     public JpaToscaEntityType(@NonNull final PfConceptKey key) {
93         this.key = key;
94     }
95
96     /**
97      * Copy constructor.
98      *
99      * @param copyConcept the concept to copy from
100      */
101     public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
102         super(copyConcept);
103         this.key = new PfConceptKey(copyConcept.key);
104         this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
105         this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
106         this.description = copyConcept.description;
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.getName());
125         }
126
127         if (description != null) {
128             toscaEntity.setDescription(description);
129         }
130
131         toscaEntity.setMetadata(PfUtils.mapMap(metadata, item -> item));
132
133         return toscaEntity;
134     }
135
136     @Override
137     public void fromAuthorative(T toscaEntity) {
138         key = new PfConceptKey();
139
140         if (toscaEntity.getName() != null) {
141             key.setName(toscaEntity.getName());
142         }
143
144         if (toscaEntity.getVersion() != null) {
145             key.setVersion(toscaEntity.getVersion());
146         }
147
148         if (toscaEntity.getDerivedFrom() != null) {
149             // Check if the derived from field contains a name-version ID
150             if (toscaEntity.getDerivedFrom().contains(":")) {
151                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
152             } else {
153                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
154             }
155         }
156
157         if (toscaEntity.getDescription() != null) {
158             description = toscaEntity.getDescription();
159         }
160
161         metadata = PfUtils.mapMap(toscaEntity.getMetadata(), item -> item);
162     }
163
164     @Override
165     public List<PfKey> getKeys() {
166         final List<PfKey> keyList = getKey().getKeys();
167         if (derivedFrom != null) {
168             keyList.addAll(derivedFrom.getKeys());
169         }
170         return keyList;
171     }
172
173     @Override
174     public void clean() {
175         key.clean();
176
177         if (derivedFrom != null) {
178             derivedFrom.clean();
179         }
180
181         if (metadata != null) {
182             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
183                 metadataEntry.setValue(metadataEntry.getValue().trim());
184             }
185         }
186
187         description = (description != null ? description.trim() : null);
188     }
189
190     @Override
191     public PfValidationResult validate(PfValidationResult resultIn) {
192         PfValidationResult result = resultIn;
193
194         if (key.isNullKey()) {
195             result.addValidationMessage(
196                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
197         }
198
199         result = key.validate(result);
200
201         if (derivedFrom != null && derivedFrom.isNullKey()) {
202             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
203                     "derived from key is a null key"));
204         }
205
206         if (metadata != null) {
207             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
208                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
209                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
210                             "property metadata key may not be null"));
211                 }
212                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
213                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214                             "property metadata value may not be null"));
215                 }
216             }
217         }
218
219         if (description != null && description.trim().length() == 0) {
220             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
221                     "property description may not be blank"));
222         }
223
224         return result;
225     }
226
227     @Override
228     public int compareTo(final PfConcept otherConcept) {
229         if (otherConcept == null) {
230             return -1;
231         }
232         if (this == otherConcept) {
233             return 0;
234         }
235         if (getClass() != otherConcept.getClass()) {
236             return getClass().getName().compareTo(otherConcept.getClass().getName());
237         }
238
239         @SuppressWarnings("unchecked")
240         final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
241
242         int result = key.compareTo(other.key);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
248         if (result != 0) {
249             return result;
250         }
251
252         result = PfUtils.compareMaps(metadata, other.metadata);
253         if (result != 0) {
254             return result;
255         }
256
257         return ObjectUtils.compare(description, other.description);
258     }
259 }