5102875811a0c0ccec977bd42a33224041d5ad31
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithToscaProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Lob;
32 import javax.persistence.MappedSuperclass;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.apache.commons.collections4.CollectionUtils;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotBlank;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfReferenceKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
49 import org.onap.policy.models.tosca.utils.ToscaUtils;
50
51 /**
52  * Class to represent JPA TOSCA classes containing TOSCA properties.
53  */
54 @MappedSuperclass
55 @Data
56 @EqualsAndHashCode(callSuper = true)
57 public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProperties> extends JpaToscaEntityType<T>
58                 implements PfAuthorative<T> {
59     private static final long serialVersionUID = -563659852901842616L;
60
61     @ElementCollection
62     @Lob
63     private Map<@NotNull @NotBlank String, @NotNull @Valid JpaToscaProperty> properties;
64
65     /**
66      * The Default Constructor creates a {@link JpaToscaWithToscaProperties} object with a
67      * null key.
68      */
69     protected JpaToscaWithToscaProperties() {
70         this(new PfConceptKey());
71     }
72
73     /**
74      * The Key Constructor creates a {@link JpaToscaWithToscaProperties} object with the
75      * given concept key.
76      *
77      * @param key the key
78      */
79     protected JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) {
80         super(key);
81     }
82
83     /**
84      * Copy constructor.
85      *
86      * @param copyConcept the concept to copy from
87      */
88     protected JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties<T> copyConcept) {
89         super(copyConcept);
90         this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
91     }
92
93     /**
94      * Authorative constructor.
95      *
96      * @param authorativeConcept the authorative concept to copy from
97      */
98     protected JpaToscaWithToscaProperties(final T authorativeConcept) {
99         this.fromAuthorative(authorativeConcept);
100     }
101
102     @Override
103     public void fromAuthorative(final T authorativeConcept) {
104         super.fromAuthorative(authorativeConcept);
105
106         // Set properties
107         if (authorativeConcept.getProperties() != null) {
108             properties = new LinkedHashMap<>();
109             for (Entry<String, ToscaProperty> toscaPropertyEntry : authorativeConcept.getProperties().entrySet()) {
110                 var jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
111                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
112                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
113             }
114         }
115     }
116
117     @Override
118     public T toAuthorative() {
119         var tosca = super.toAuthorative();
120
121         tosca.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
122
123         return tosca;
124     }
125
126     @Override
127     public List<PfKey> getKeys() {
128         final List<PfKey> keyList = super.getKeys();
129
130         PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys()));
131
132         return keyList;
133     }
134
135     @Override
136     public void clean() {
137         super.clean();
138
139         if (properties != null) {
140             for (JpaToscaProperty property : properties.values()) {
141                 property.clean();
142             }
143         }
144     }
145
146     /**
147      * Validates the fields of the object, including its key.
148      *
149      * @param fieldName name of the field containing this
150      * @return the result, or {@code null}
151      */
152     protected BeanValidationResult validateWithKey(@NonNull String fieldName) {
153         BeanValidationResult result = super.validate(fieldName);
154
155         validateKeyVersionNotNull(result, "key", getKey());
156
157         return result;
158     }
159
160     @Override
161     public int compareTo(final PfConcept otherConcept) {
162         if (this == otherConcept) {
163             return 0;
164         }
165
166         int result = super.compareTo(otherConcept);
167         if (result != 0) {
168             return result;
169         }
170
171         @SuppressWarnings("unchecked")
172         final JpaToscaWithToscaProperties<T> other = (JpaToscaWithToscaProperties<T>) otherConcept;
173
174         return PfUtils.compareMaps(properties, other.properties);
175     }
176
177     /**
178      * Get the referenced data types.
179      *
180      * @return the referenced data types
181      */
182     public Collection<PfConceptKey> getReferencedDataTypes() {
183         if (properties == null) {
184             return CollectionUtils.emptyCollection();
185         }
186
187         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
188
189         for (JpaToscaProperty property : properties.values()) {
190             referencedDataTypes.add(property.getType());
191
192             if (property.getEntrySchema() != null) {
193                 referencedDataTypes.add(property.getEntrySchema().getType());
194             }
195         }
196
197         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
198
199         return referencedDataTypes;
200     }
201 }