Use annotations to do validation
[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-2020 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.parameters.annotations.Entries;
39 import org.onap.policy.common.parameters.annotations.Items;
40 import org.onap.policy.common.parameters.annotations.NotBlank;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.base.validation.annotations.VerifyKey;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
49
50 /**
51  * Class to represent the EntrySchema of list/map property in TOSCA definition.
52  */
53 @MappedSuperclass
54 @Data
55 @EqualsAndHashCode(callSuper = false)
56 public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
57     private static final long serialVersionUID = -1330661834220739393L;
58
59     @EmbeddedId
60     @VerifyKey
61     @NotNull
62     private PfConceptKey key;
63
64     // @formatter:off
65     @Column
66     @AttributeOverrides({
67         @AttributeOverride(name = "name",
68                            column = @Column(name = "derived_from_name")),
69         @AttributeOverride(name = "version",
70                            column = @Column(name = "derived_from_version"))
71         })
72     @VerifyKey
73     private PfConceptKey derivedFrom;
74
75     @ElementCollection
76     @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
77                 value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
78     private Map<String, String> metadata;
79
80     @Column
81     @NotBlank
82     private String description;
83
84     private transient T toscaEntity;
85     // @formatter:on
86
87     /**
88      * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
89      */
90     public JpaToscaEntityType() {
91         this(new PfConceptKey());
92     }
93
94     /**
95      * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
96      *
97      * @param key the key
98      */
99     public JpaToscaEntityType(@NonNull final PfConceptKey key) {
100         this.key = key;
101     }
102
103     /**
104      * Copy constructor.
105      *
106      * @param copyConcept the concept to copy from
107      */
108     public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
109         super(copyConcept);
110         this.key = new PfConceptKey(copyConcept.key);
111         this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
112         this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
113         this.description = copyConcept.description;
114     }
115
116     /**
117      * Authorative constructor.
118      *
119      * @param authorativeConcept the authorative concept to copy from
120      */
121     public JpaToscaEntityType(final T authorativeConcept) {
122         this.fromAuthorative(authorativeConcept);
123     }
124
125     @Override
126     public T toAuthorative() {
127         toscaEntity.setName(getKey().getName());
128         toscaEntity.setVersion(getKey().getVersion());
129
130         if (derivedFrom != null) {
131             toscaEntity.setDerivedFrom(derivedFrom.getName());
132         }
133
134         if (description != null) {
135             toscaEntity.setDescription(description);
136         }
137
138         toscaEntity.setMetadata(PfUtils.mapMap(metadata, item -> item));
139
140         return toscaEntity;
141     }
142
143     @Override
144     public void fromAuthorative(T toscaEntity) {
145         key = new PfConceptKey();
146
147         if (toscaEntity.getName() != null) {
148             key.setName(toscaEntity.getName());
149         }
150
151         if (toscaEntity.getVersion() != null) {
152             key.setVersion(toscaEntity.getVersion());
153         }
154
155         if (toscaEntity.getDerivedFrom() != null) {
156             // Check if the derived from field contains a name-version ID
157             if (toscaEntity.getDerivedFrom().contains(":")) {
158                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
159             } else {
160                 derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
161             }
162         }
163
164         if (toscaEntity.getDescription() != null) {
165             description = toscaEntity.getDescription();
166         }
167
168         metadata = PfUtils.mapMap(toscaEntity.getMetadata(), item -> item);
169     }
170
171     @Override
172     public List<PfKey> getKeys() {
173         final List<PfKey> keyList = getKey().getKeys();
174         if (derivedFrom != null) {
175             keyList.addAll(derivedFrom.getKeys());
176         }
177         return keyList;
178     }
179
180     @Override
181     public void clean() {
182         key.clean();
183
184         if (derivedFrom != null) {
185             derivedFrom.clean();
186         }
187
188         if (metadata != null) {
189             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
190                 metadataEntry.setValue(metadataEntry.getValue().trim());
191             }
192         }
193
194         description = (description != null ? description.trim() : null);
195     }
196
197     @Override
198     public int compareTo(final PfConcept otherConcept) {
199         if (otherConcept == null) {
200             return -1;
201         }
202         if (this == otherConcept) {
203             return 0;
204         }
205         if (getClass() != otherConcept.getClass()) {
206             return getClass().getName().compareTo(otherConcept.getClass().getName());
207         }
208
209         @SuppressWarnings("unchecked")
210         final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
211
212         int result = key.compareTo(other.key);
213         if (result != 0) {
214             return result;
215         }
216
217         result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
218         if (result != 0) {
219             return result;
220         }
221
222         result = PfUtils.compareMaps(metadata, other.metadata);
223         if (result != 0) {
224             return result;
225         }
226
227         return ObjectUtils.compare(description, other.description);
228     }
229 }