Java 17 Upgrade
[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-2021, 2023 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 jakarta.persistence.Column;
27 import jakarta.persistence.ElementCollection;
28 import jakarta.persistence.Entity;
29 import jakarta.persistence.Inheritance;
30 import jakarta.persistence.InheritanceType;
31 import jakarta.persistence.Table;
32 import java.io.Serial;
33 import java.util.ArrayList;
34 import java.util.List;
35 import lombok.Data;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.base.validation.annotations.PfMin;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
45
46 /**
47  * Class to represent the requirement in TOSCA definition.
48  *
49  * @author Chenfei Gao (cgao@research.att.com)
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 @Entity
53 @Table(name = "ToscaRequirement")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @Data
56 @EqualsAndHashCode(callSuper = true)
57 public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties<ToscaRequirement> {
58
59     @Serial
60     private static final long serialVersionUID = 2785481541573683089L;
61     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
62     private static final Integer JPA_UNBOUNDED_VALUE = -1;
63     private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator();
64
65     @Column
66     private String capability;
67
68     @Column
69     private String node;
70
71     @Column
72     private String relationship;
73
74     @ElementCollection
75     private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
76
77     /**
78      * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
79      */
80     public JpaToscaRequirement() {
81         this(new PfConceptKey());
82     }
83
84     /**
85      * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
86      *
87      * @param key the key
88      */
89     public JpaToscaRequirement(@NonNull final PfConceptKey key) {
90         super(key);
91     }
92
93     /**
94      * Copy constructor.
95      *
96      * @param copyConcept the concept to copy from
97      */
98     public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
99         super(copyConcept);
100         this.capability = copyConcept.capability;
101         this.node = copyConcept.node;
102         this.relationship = copyConcept.relationship;
103         this.occurrences = new ArrayList<>(copyConcept.occurrences);
104     }
105
106     /**
107      * Authorative constructor.
108      *
109      * @param authorativeConcept the authorative concept to copy from
110      */
111     public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
112         super(authorativeConcept);
113     }
114
115     @Override
116     public ToscaRequirement toAuthorative() {
117         var toscaRequirement = new ToscaRequirement();
118         super.setToscaEntity(toscaRequirement);
119         super.toAuthorative();
120
121         toscaRequirement.setCapability(capability);
122         toscaRequirement.setNode(node);
123         toscaRequirement.setRelationship(relationship);
124
125         if (occurrences != null) {
126             List<Object> occurrencesList = new ArrayList<>(occurrences);
127             for (Integer occurrence : occurrences) {
128                 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
129                     occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
130                 } else {
131                     occurrencesList.add(occurrence);
132                 }
133             }
134             toscaRequirement.setOccurrences(occurrencesList);
135         }
136
137         return toscaRequirement;
138     }
139
140     @Override
141     public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
142         super.fromAuthorative(toscaRequirement);
143
144         capability = toscaRequirement.getCapability();
145         node = toscaRequirement.getNode();
146         relationship = toscaRequirement.getRelationship();
147
148         if (toscaRequirement.getOccurrences() != null) {
149             occurrences = new ArrayList<>();
150             for (Object occurrence : toscaRequirement.getOccurrences()) {
151                 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
152                     occurrences.add(JPA_UNBOUNDED_VALUE);
153                 } else {
154                     occurrences.add(((Number) occurrence).intValue());
155                 }
156             }
157         }
158     }
159
160     @Override
161     protected Object deserializePropertyValue(String propValue) {
162         return YAML_TRANSLATOR.fromYaml(propValue, Object.class);
163     }
164
165     @Override
166     protected String serializePropertyValue(Object propValue) {
167         return YAML_TRANSLATOR.toYaml(propValue);
168     }
169
170     @Override
171     public void clean() {
172         super.clean();
173
174         capability = capability.trim();
175         node = node.trim();
176         relationship = relationship.trim();
177     }
178
179     @Override
180     public int compareTo(final PfConcept otherConcept) {
181         if (this == otherConcept) {
182             return 0;
183         }
184
185         int result = super.compareTo(otherConcept);
186         if (result != 0) {
187             return result;
188         }
189
190         final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
191
192         result = capability.compareTo(other.capability);
193         if (result != 0) {
194             return result;
195         }
196
197         result = node.compareTo(other.node);
198         if (result != 0) {
199             return result;
200         }
201
202         result = relationship.compareTo(other.relationship);
203         if (result != 0) {
204             return result;
205         }
206
207         return PfUtils.compareCollections(occurrences, other.occurrences);
208     }
209 }