Use annotations to do validation
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaCapabilityType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Nordix Foundation.
4  * Modifications Copyright (C) 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.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Lob;
36 import javax.persistence.Table;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.collections4.CollectionUtils;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.common.parameters.annotations.Entries;
43 import org.onap.policy.common.parameters.annotations.Items;
44 import org.onap.policy.common.parameters.annotations.NotNull;
45 import org.onap.policy.common.parameters.annotations.Valid;
46 import org.onap.policy.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfReferenceKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
54 import org.onap.policy.models.tosca.utils.ToscaUtils;
55
56 /**
57  * Class to represent the capability type in TOSCA definition.
58  */
59
60 @Entity
61 @Table(name = "ToscaCapabilityType")
62 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @Data
64 @EqualsAndHashCode(callSuper = true)
65 public class JpaToscaCapabilityType extends JpaToscaEntityType<ToscaCapabilityType>
66         implements PfAuthorative<ToscaCapabilityType> {
67     private static final long serialVersionUID = -563659852901842616L;
68
69     @ElementCollection
70     @Lob
71     @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
72     private Map<String, JpaToscaProperty> properties;
73
74     /**
75      * The Default Constructor creates a {@link JpaToscaCapabilityType} object with a null key.
76      */
77     public JpaToscaCapabilityType() {
78         this(new PfConceptKey());
79     }
80
81     /**
82      * The Key Constructor creates a {@link JpaToscaCapabilityType} object with the given concept key.
83      *
84      * @param key the key
85      */
86     public JpaToscaCapabilityType(@NonNull final PfConceptKey key) {
87         super(key);
88     }
89
90     /**
91      * Copy constructor.
92      *
93      * @param copyConcept the concept to copy from
94      */
95     public JpaToscaCapabilityType(final JpaToscaCapabilityType copyConcept) {
96         super(copyConcept);
97         this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
98     }
99
100     /**
101      * Authorative constructor.
102      *
103      * @param authorativeConcept the authorative concept to copy from
104      */
105     public JpaToscaCapabilityType(final ToscaCapabilityType authorativeConcept) {
106         this.fromAuthorative(authorativeConcept);
107     }
108
109     @Override
110     public ToscaCapabilityType toAuthorative() {
111         ToscaCapabilityType toscaCapabilityType = new ToscaCapabilityType();
112         super.setToscaEntity(toscaCapabilityType);
113         super.toAuthorative();
114
115         toscaCapabilityType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
116
117         return toscaCapabilityType;
118     }
119
120     @Override
121     public void fromAuthorative(final ToscaCapabilityType toscaCapabilityType) {
122         super.fromAuthorative(toscaCapabilityType);
123
124         // Set properties
125         if (toscaCapabilityType.getProperties() != null) {
126             properties = new LinkedHashMap<>();
127             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaCapabilityType.getProperties().entrySet()) {
128                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
129                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
130                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
131             }
132         }
133     }
134
135     @Override
136     public List<PfKey> getKeys() {
137         final List<PfKey> keyList = super.getKeys();
138
139         PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys()));
140
141
142         if (properties != null) {
143             for (JpaToscaProperty property : properties.values()) {
144                 keyList.addAll(property.getKeys());
145             }
146         }
147
148         return keyList;
149     }
150
151     @Override
152     public void clean() {
153         super.clean();
154
155         if (properties != null) {
156             for (JpaToscaProperty property : properties.values()) {
157                 property.clean();
158             }
159         }
160     }
161
162     @Override
163     public BeanValidationResult validate(@NonNull String fieldName) {
164         BeanValidationResult result = super.validate(fieldName);
165
166         result.addResult(validateKeyVersionNotNull("key", getKey()));
167
168         return result;
169     }
170
171     @Override
172     public int compareTo(final PfConcept otherConcept) {
173         if (otherConcept == null) {
174             return -1;
175         }
176         if (this == otherConcept) {
177             return 0;
178         }
179         if (getClass() != otherConcept.getClass()) {
180             return getClass().getName().compareTo(otherConcept.getClass().getName());
181         }
182
183         final JpaToscaCapabilityType other = (JpaToscaCapabilityType) otherConcept;
184         int result = super.compareTo(other);
185         if (result != 0) {
186             return result;
187         }
188
189         return PfUtils.compareMaps(properties, other.properties);
190     }
191
192     /**
193      * Get the data types referenced in a capability type.
194      *
195      * @return the data types referenced in a capability type
196      */
197     public Collection<PfConceptKey> getReferencedDataTypes() {
198         if (properties == null) {
199             return CollectionUtils.emptyCollection();
200         }
201
202         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
203
204         for (JpaToscaProperty property : properties.values()) {
205             referencedDataTypes.add(property.getType());
206
207             if (property.getEntrySchema() != null) {
208                 referencedDataTypes.add(property.getEntrySchema().getType());
209             }
210         }
211
212         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
213
214         return referencedDataTypes;
215     }
216 }