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