Use annotations to do validation
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaRequirement.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Requirement 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.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import javax.persistence.Column;
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.onap.policy.common.parameters.annotations.Entries;
41 import org.onap.policy.common.parameters.annotations.Items;
42 import org.onap.policy.common.parameters.annotations.NotNull;
43 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
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.PfUtils;
48 import org.onap.policy.models.base.validation.annotations.PfItems;
49 import org.onap.policy.models.base.validation.annotations.PfMin;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
51
52 /**
53  * Class to represent the requirement in TOSCA definition.
54  *
55  * @author Chenfei Gao (cgao@research.att.com)
56  * @author Liam Fallon (liam.fallon@est.tech)
57  */
58 @Entity
59 @Table(name = "ToscaRequirement")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
64         implements PfAuthorative<ToscaRequirement> {
65
66     private static final long serialVersionUID = 2785481541573683089L;
67     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
68     private static final Integer JPA_UNBOUNDED_VALUE = -1;
69
70     @Column
71     private String capability;
72
73     @Column
74     private String node;
75
76     @Column
77     private String relationship;
78
79     @ElementCollection
80     @PfItems(notNull = {@NotNull}, pfMin = {@PfMin(value = 0, allowed = -1)})
81     private List<Integer> occurrences;
82
83     @ElementCollection
84     @Lob
85     @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
86     private Map<String, String> properties;
87
88     /**
89      * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
90      */
91     public JpaToscaRequirement() {
92         this(new PfConceptKey());
93     }
94
95     /**
96      * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
97      *
98      * @param key the key
99      */
100     public JpaToscaRequirement(@NonNull final PfConceptKey key) {
101         super(key);
102     }
103
104     /**
105      * Copy constructor.
106      *
107      * @param copyConcept the concept to copy from
108      */
109     public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
110         super(copyConcept);
111         this.capability = copyConcept.capability;
112         this.node = copyConcept.node;
113         this.relationship = copyConcept.relationship;
114         this.occurrences = new ArrayList<>(copyConcept.occurrences);
115         this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
116     }
117
118     /**
119      * Authorative constructor.
120      *
121      * @param authorativeConcept the authorative concept to copy from
122      */
123     public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
124         super(new PfConceptKey());
125         this.fromAuthorative(authorativeConcept);
126     }
127
128     @Override
129     public ToscaRequirement toAuthorative() {
130         ToscaRequirement toscaRequirement = new ToscaRequirement();
131         super.setToscaEntity(toscaRequirement);
132         super.toAuthorative();
133
134         toscaRequirement.setCapability(capability);
135         toscaRequirement.setNode(node);
136         toscaRequirement.setRelationship(relationship);
137
138         if (occurrences != null) {
139             List<Object> occurrencesList = new ArrayList<>(occurrences);
140             for (Integer occurrence : occurrences) {
141                 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
142                     occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
143                 } else {
144                     occurrencesList.add(occurrence);
145                 }
146             }
147             toscaRequirement.setOccurrences(occurrencesList);
148         }
149
150         if (properties != null) {
151             Map<String, Object> propertiesMap = new LinkedHashMap<>();
152
153             for (Map.Entry<String, String> entry : properties.entrySet()) {
154                 propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class));
155             }
156
157             toscaRequirement.setProperties(propertiesMap);
158         }
159
160         return toscaRequirement;
161     }
162
163     @Override
164     public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
165         super.fromAuthorative(toscaRequirement);
166
167         capability = toscaRequirement.getCapability();
168         node = toscaRequirement.getNode();
169         relationship = toscaRequirement.getRelationship();
170
171         if (toscaRequirement.getOccurrences() != null) {
172             occurrences = new ArrayList<>();
173             for (Object occurrence : toscaRequirement.getOccurrences()) {
174                 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
175                     occurrences.add(JPA_UNBOUNDED_VALUE);
176                 } else {
177                     occurrences.add(((Number) occurrence).intValue());
178                 }
179             }
180         }
181
182         if (toscaRequirement.getProperties() != null) {
183             properties = new LinkedHashMap<>();
184             for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) {
185                 String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue());
186                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
187             }
188         }
189
190     }
191
192     @Override
193     public void clean() {
194         super.clean();
195
196         capability = capability.trim();
197         node = node.trim();
198         relationship = relationship.trim();
199
200         properties = PfUtils.mapMap(properties, String::trim);
201     }
202
203     @Override
204     public int compareTo(final PfConcept otherConcept) {
205         if (otherConcept == null) {
206             return -1;
207         }
208
209         if (this == otherConcept) {
210             return 0;
211         }
212
213         if (getClass() != otherConcept.getClass()) {
214             return getClass().getName().compareTo(otherConcept.getClass().getName());
215         }
216
217         final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
218         int result = super.compareTo(other);
219         if (result != 0) {
220             return result;
221         }
222
223         result = capability.compareTo(other.capability);
224         if (result != 0) {
225             return result;
226         }
227
228         result = node.compareTo(other.node);
229         if (result != 0) {
230             return result;
231         }
232
233         result = relationship.compareTo(other.relationship);
234         if (result != 0) {
235             return result;
236         }
237
238         result = PfUtils.compareCollections(occurrences, other.occurrences);
239         if (result != 0) {
240             return result;
241         }
242
243         return PfUtils.compareMaps(properties, other.properties);
244     }
245 }