Use annotations on parameterized types
[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 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 java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import javax.persistence.ElementCollection;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Lob;
33 import javax.persistence.Table;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.models.base.PfAuthorative;
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 JpaToscaEntityType<ToscaCapabilityAssignment>
55         implements PfAuthorative<ToscaCapabilityAssignment> {
56
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> properties;
67
68     @ElementCollection
69     @Lob
70     private Map<@NotNull String, @NotNull String> attributes;
71
72     @ElementCollection
73     private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
74
75     /**
76      * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
77      */
78     public JpaToscaCapabilityAssignment() {
79         this(new PfConceptKey());
80     }
81
82     /**
83      * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
84      *
85      * @param key the key
86      */
87     public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
88         super(key);
89     }
90
91     /**
92      * Copy constructor.
93      *
94      * @param copyConcept the concept to copy from
95      */
96     public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
97         super(copyConcept);
98         this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
99         this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
100         this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
101     }
102
103     /**
104      * Authorative constructor.
105      *
106      * @param authorativeConcept the authorative concept to copy from
107      */
108     public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
109         super(new PfConceptKey());
110         this.fromAuthorative(authorativeConcept);
111     }
112
113     @Override
114     public ToscaCapabilityAssignment toAuthorative() {
115         ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment();
116         super.setToscaEntity(toscaCapabilityAssignment);
117         super.toAuthorative();
118
119         toscaCapabilityAssignment.setProperties(
120                 PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class)));
121
122         toscaCapabilityAssignment.setAttributes(
123                 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
124
125         toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
126             if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
127                 return AUTHORATIVE_UNBOUNDED_LITERAL;
128             } else {
129                 return occurrence;
130             }
131         }));
132
133         return toscaCapabilityAssignment;
134     }
135
136     @Override
137     public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
138         super.fromAuthorative(toscaCapabilityAssignment);
139
140
141         properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml);
142         attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
143
144         occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
145             if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
146                 return JPA_UNBOUNDED_VALUE;
147             } else {
148                 return ((Number) occurrence).intValue();
149             }
150         });
151     }
152
153     @Override
154     public void clean() {
155         super.clean();
156
157         properties = PfUtils.mapMap(properties, String::trim);
158         attributes = PfUtils.mapMap(attributes, String::trim);
159     }
160
161     @Override
162     public int compareTo(final PfConcept otherConcept) {
163         if (otherConcept == null) {
164             return -1;
165         }
166
167         if (this == otherConcept) {
168             return 0;
169         }
170
171         if (getClass() != otherConcept.getClass()) {
172             return getClass().getName().compareTo(otherConcept.getClass().getName());
173         }
174
175         final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
176         int result = super.compareTo(other);
177         if (result != 0) {
178             return result;
179         }
180
181         result = PfUtils.compareMaps(properties, other.properties);
182         if (result != 0) {
183             return result;
184         }
185
186         result = PfUtils.compareMaps(attributes, other.attributes);
187         if (result != 0) {
188             return result;
189         }
190
191         return PfUtils.compareCollections(occurrences, other.occurrences);
192     }
193 }