Use ValidationResult for models v2.0
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.Collection;
27 import java.util.LinkedHashMap;
28 import java.util.LinkedHashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Set;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.Entity;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.Lob;
38 import javax.persistence.Table;
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.onap.policy.common.parameters.BeanValidationResult;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfReferenceKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.Validated;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
53 import org.onap.policy.models.tosca.utils.ToscaUtils;
54
55 /**
56  * Class to represent the policy type in TOSCA definition.
57  *
58  * @author Chenfei Gao (cgao@research.att.com)
59  * @author Liam Fallon (liam.fallon@est.tech)
60  */
61
62 @Entity
63 @Table(name = "ToscaPolicyType")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @Data
66 @EqualsAndHashCode(callSuper = true)
67 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
68     private static final long serialVersionUID = -563659852901842616L;
69
70     @ElementCollection
71     @Lob
72     private Map<String, JpaToscaProperty> properties;
73
74     @ElementCollection
75     private List<PfConceptKey> targets;
76
77     @ElementCollection
78     private List<JpaToscaTrigger> triggers;
79
80     /**
81      * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
82      */
83     public JpaToscaPolicyType() {
84         this(new PfConceptKey());
85     }
86
87     /**
88      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
89      *
90      * @param key the key
91      */
92     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
93         super(key);
94     }
95
96     /**
97      * Copy constructor.
98      *
99      * @param copyConcept the concept to copy from
100      */
101     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
102         super(copyConcept);
103         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
104         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
105         this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
106     }
107
108     /**
109      * Authorative constructor.
110      *
111      * @param authorativeConcept the authorative concept to copy from
112      */
113     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
114         this.fromAuthorative(authorativeConcept);
115     }
116
117     @Override
118     public ToscaPolicyType toAuthorative() {
119         ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
120         super.setToscaEntity(toscaPolicyType);
121         super.toAuthorative();
122
123         toscaPolicyType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
124
125         return toscaPolicyType;
126     }
127
128     @Override
129     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
130         super.fromAuthorative(toscaPolicyType);
131
132         // Set properties
133         if (toscaPolicyType.getProperties() != null) {
134             properties = new LinkedHashMap<>();
135             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
136                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
137                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
138                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
139             }
140         }
141     }
142
143     @Override
144     public List<PfKey> getKeys() {
145         final List<PfKey> keyList = super.getKeys();
146
147         if (properties != null) {
148             for (JpaToscaProperty property : properties.values()) {
149                 keyList.addAll(property.getKeys());
150             }
151         }
152
153         if (targets != null) {
154             keyList.addAll(targets);
155         }
156
157         if (triggers != null) {
158             for (JpaToscaTrigger trigger : triggers) {
159                 keyList.addAll(trigger.getKeys());
160             }
161         }
162
163         return keyList;
164     }
165
166     @Override
167     public void clean() {
168         super.clean();
169
170         if (properties != null) {
171             for (JpaToscaProperty property : properties.values()) {
172                 property.clean();
173             }
174         }
175
176         if (targets != null) {
177             for (PfConceptKey target : targets) {
178                 target.clean();
179             }
180         }
181
182         if (triggers != null) {
183             for (JpaToscaTrigger trigger : triggers) {
184                 trigger.clean();
185             }
186         }
187     }
188
189     @Override
190     public BeanValidationResult validate(String fieldName) {
191         BeanValidationResult result = super.validate(fieldName);
192
193         result.addResult(validateKeyVersionNotNull("key", getKey()));
194
195         validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
196         validateList(result, "targets", targets, Validated::validateNotNull);
197         validateList(result, "triggers", triggers, Validated::validateNotNull);
198
199         return result;
200     }
201
202     @Override
203     public int compareTo(final PfConcept otherConcept) {
204         if (otherConcept == null) {
205             return -1;
206         }
207         if (this == otherConcept) {
208             return 0;
209         }
210         if (getClass() != otherConcept.getClass()) {
211             return getClass().getName().compareTo(otherConcept.getClass().getName());
212         }
213
214         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
215         int result = super.compareTo(other);
216         if (result != 0) {
217             return result;
218         }
219
220         result = PfUtils.compareMaps(properties, other.properties);
221         if (result != 0) {
222             return result;
223         }
224
225         result = PfUtils.compareCollections(targets, other.targets);
226         if (result != 0) {
227             return result;
228         }
229
230         return PfUtils.compareCollections(triggers, other.triggers);
231     }
232
233     /**
234      * Get the data types referenced in a policy type.
235      *
236      * @return the data types referenced in a policy type
237      */
238     public Collection<PfConceptKey> getReferencedDataTypes() {
239         if (properties == null) {
240             return CollectionUtils.emptyCollection();
241         }
242
243         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
244
245         for (JpaToscaProperty property : properties.values()) {
246             referencedDataTypes.add(property.getType());
247
248             if (property.getEntrySchema() != null) {
249                 referencedDataTypes.add(property.getEntrySchema().getType());
250             }
251         }
252
253         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
254
255         return referencedDataTypes;
256     }
257 }