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