Use annotations to do validation
[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.common.parameters.annotations.Entries;
45 import org.onap.policy.common.parameters.annotations.Items;
46 import org.onap.policy.common.parameters.annotations.NotNull;
47 import org.onap.policy.common.parameters.annotations.Valid;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
56 import org.onap.policy.models.tosca.utils.ToscaUtils;
57
58 /**
59  * Class to represent the policy type in TOSCA definition.
60  *
61  * @author Chenfei Gao (cgao@research.att.com)
62  * @author Liam Fallon (liam.fallon@est.tech)
63  */
64
65 @Entity
66 @Table(name = "ToscaPolicyType")
67 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @Data
69 @EqualsAndHashCode(callSuper = true)
70 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
71     private static final long serialVersionUID = -563659852901842616L;
72
73     @ElementCollection
74     @Lob
75     @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
76     private Map<String, JpaToscaProperty> properties;
77
78     @ElementCollection
79     @Items(notNull = {@NotNull}, valid = {@Valid})
80     private List<PfConceptKey> targets;
81
82     @ElementCollection
83     @Items(notNull = {@NotNull}, valid = {@Valid})
84     private List<JpaToscaTrigger> triggers;
85
86     /**
87      * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
88      */
89     public JpaToscaPolicyType() {
90         this(new PfConceptKey());
91     }
92
93     /**
94      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
95      *
96      * @param key the key
97      */
98     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
99         super(key);
100     }
101
102     /**
103      * Copy constructor.
104      *
105      * @param copyConcept the concept to copy from
106      */
107     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
108         super(copyConcept);
109         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
110         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
111         this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
112     }
113
114     /**
115      * Authorative constructor.
116      *
117      * @param authorativeConcept the authorative concept to copy from
118      */
119     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
120         this.fromAuthorative(authorativeConcept);
121     }
122
123     @Override
124     public ToscaPolicyType toAuthorative() {
125         ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
126         super.setToscaEntity(toscaPolicyType);
127         super.toAuthorative();
128
129         toscaPolicyType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
130
131         return toscaPolicyType;
132     }
133
134     @Override
135     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
136         super.fromAuthorative(toscaPolicyType);
137
138         // Set properties
139         if (toscaPolicyType.getProperties() != null) {
140             properties = new LinkedHashMap<>();
141             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
142                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
143                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
144                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
145             }
146         }
147     }
148
149     @Override
150     public List<PfKey> getKeys() {
151         final List<PfKey> keyList = super.getKeys();
152
153         if (properties != null) {
154             for (JpaToscaProperty property : properties.values()) {
155                 keyList.addAll(property.getKeys());
156             }
157         }
158
159         if (targets != null) {
160             keyList.addAll(targets);
161         }
162
163         if (triggers != null) {
164             for (JpaToscaTrigger trigger : triggers) {
165                 keyList.addAll(trigger.getKeys());
166             }
167         }
168
169         return keyList;
170     }
171
172     @Override
173     public void clean() {
174         super.clean();
175
176         if (properties != null) {
177             for (JpaToscaProperty property : properties.values()) {
178                 property.clean();
179             }
180         }
181
182         if (targets != null) {
183             for (PfConceptKey target : targets) {
184                 target.clean();
185             }
186         }
187
188         if (triggers != null) {
189             for (JpaToscaTrigger trigger : triggers) {
190                 trigger.clean();
191             }
192         }
193     }
194
195     @Override
196     public BeanValidationResult validate(String fieldName) {
197         BeanValidationResult result = super.validate(fieldName);
198
199         result.addResult(validateKeyVersionNotNull("key", getKey()));
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         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
217         int result = super.compareTo(other);
218         if (result != 0) {
219             return result;
220         }
221
222         result = PfUtils.compareMaps(properties, other.properties);
223         if (result != 0) {
224             return result;
225         }
226
227         result = PfUtils.compareCollections(targets, other.targets);
228         if (result != 0) {
229             return result;
230         }
231
232         return PfUtils.compareCollections(triggers, other.triggers);
233     }
234
235     /**
236      * Get the data types referenced in a policy type.
237      *
238      * @return the data types referenced in a policy type
239      */
240     public Collection<PfConceptKey> getReferencedDataTypes() {
241         if (properties == null) {
242             return CollectionUtils.emptyCollection();
243         }
244
245         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
246
247         for (JpaToscaProperty property : properties.values()) {
248             referencedDataTypes.add(property.getType());
249
250             if (property.getEntrySchema() != null) {
251                 referencedDataTypes.add(property.getEntrySchema().getType());
252             }
253         }
254
255         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
256
257         return referencedDataTypes;
258     }
259 }