Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaCapabilityAssignment.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2021, 2023 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2021 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 jakarta.persistence.ElementCollection;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.Inheritance;
27 import jakarta.persistence.InheritanceType;
28 import jakarta.persistence.Lob;
29 import jakarta.persistence.Table;
30 import java.io.Serial;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
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.ToscaCapabilityAssignment;
45
46 /**
47  * Class to represent the parameter in TOSCA definition.
48  */
49 @Entity
50 @Table(name = "ToscaCapabilityAssignment")
51 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
52 @Data
53 @EqualsAndHashCode(callSuper = false)
54 public class JpaToscaCapabilityAssignment extends JpaToscaWithTypeAndStringProperties<ToscaCapabilityAssignment> {
55
56     @Serial
57     private static final long serialVersionUID = 1675770231921107988L;
58
59     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
60     private static final Integer JPA_UNBOUNDED_VALUE = -1;
61
62     private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator();
63
64     @ElementCollection
65     @Lob
66     private Map<@NotNull String, @NotNull String> attributes;
67
68     @ElementCollection
69     private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
70
71     /**
72      * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
73      */
74     public JpaToscaCapabilityAssignment() {
75         this(new PfConceptKey());
76     }
77
78     /**
79      * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
80      *
81      * @param key the key
82      */
83     public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
84         super(key);
85     }
86
87     /**
88      * Copy constructor.
89      *
90      * @param copyConcept the concept to copy from
91      */
92     public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
93         super(copyConcept);
94         this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
95         this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
96     }
97
98     /**
99      * Authorative constructor.
100      *
101      * @param authorativeConcept the authorative concept to copy from
102      */
103     public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
104         super(authorativeConcept);
105     }
106
107     @Override
108     public ToscaCapabilityAssignment toAuthorative() {
109         var toscaCapabilityAssignment = new ToscaCapabilityAssignment();
110         super.setToscaEntity(toscaCapabilityAssignment);
111         super.toAuthorative();
112
113         toscaCapabilityAssignment.setAttributes(
114                 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
115
116         toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
117             if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
118                 return AUTHORATIVE_UNBOUNDED_LITERAL;
119             } else {
120                 return occurrence;
121             }
122         }));
123
124         return toscaCapabilityAssignment;
125     }
126
127     @Override
128     public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
129         super.fromAuthorative(toscaCapabilityAssignment);
130
131         attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
132
133         occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
134             if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
135                 return JPA_UNBOUNDED_VALUE;
136             } else {
137                 return ((Number) occurrence).intValue();
138             }
139         });
140     }
141
142     @Override
143     protected Object deserializePropertyValue(String propValue) {
144         return YAML_JSON_TRANSLATOR.fromYaml(propValue, Object.class);
145     }
146
147     @Override
148     protected String serializePropertyValue(Object propValue) {
149         return YAML_JSON_TRANSLATOR.toYaml(propValue);
150     }
151
152     @Override
153     public void clean() {
154         super.clean();
155
156         attributes = PfUtils.mapMap(attributes, String::trim);
157     }
158
159     @Override
160     public int compareTo(final PfConcept otherConcept) {
161         if (this == otherConcept) {
162             return 0;
163         }
164
165         int result = super.compareTo(otherConcept);
166         if (result != 0) {
167             return result;
168         }
169
170         final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
171
172         result = PfUtils.compareMaps(attributes, other.attributes);
173         if (result != 0) {
174             return result;
175         }
176
177         return PfUtils.compareCollections(occurrences, other.occurrences);
178     }
179 }