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