Use annotations to do validation
[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 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.Entries;
38 import org.onap.policy.common.parameters.annotations.Items;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.validation.annotations.PfItems;
46 import org.onap.policy.models.base.validation.annotations.PfMin;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
48
49 /**
50  * Class to represent the parameter in TOSCA definition.
51  */
52 @Entity
53 @Table(name = "ToscaCapabilityAssignment")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @Data
56 @EqualsAndHashCode(callSuper = false)
57 public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabilityAssignment>
58         implements PfAuthorative<ToscaCapabilityAssignment> {
59
60     private static final long serialVersionUID = 1675770231921107988L;
61
62     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
63     private static final Integer JPA_UNBOUNDED_VALUE = -1;
64
65     private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator();
66
67     @ElementCollection
68     @Lob
69     @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
70     private Map<String, String> properties;
71
72     @ElementCollection
73     @Lob
74     @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
75     private Map<String, String> attributes;
76
77     @ElementCollection
78     @PfItems(notNull = {@NotNull}, pfMin = {@PfMin(value = 0, allowed = -1)})
79     private List<Integer> occurrences;
80
81     /**
82      * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
83      */
84     public JpaToscaCapabilityAssignment() {
85         this(new PfConceptKey());
86     }
87
88     /**
89      * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
90      *
91      * @param key the key
92      */
93     public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
94         super(key);
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
103         super(copyConcept);
104         this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
105         this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
106         this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
107     }
108
109     /**
110      * Authorative constructor.
111      *
112      * @param authorativeConcept the authorative concept to copy from
113      */
114     public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
115         super(new PfConceptKey());
116         this.fromAuthorative(authorativeConcept);
117     }
118
119     @Override
120     public ToscaCapabilityAssignment toAuthorative() {
121         ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment();
122         super.setToscaEntity(toscaCapabilityAssignment);
123         super.toAuthorative();
124
125         toscaCapabilityAssignment.setProperties(
126                 PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class)));
127
128         toscaCapabilityAssignment.setAttributes(
129                 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
130
131         toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
132             if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
133                 return AUTHORATIVE_UNBOUNDED_LITERAL;
134             } else {
135                 return occurrence;
136             }
137         }));
138
139         return toscaCapabilityAssignment;
140     }
141
142     @Override
143     public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
144         super.fromAuthorative(toscaCapabilityAssignment);
145
146
147         properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml);
148         attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
149
150         occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
151             if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
152                 return JPA_UNBOUNDED_VALUE;
153             } else {
154                 return ((Number) occurrence).intValue();
155             }
156         });
157     }
158
159     @Override
160     public void clean() {
161         super.clean();
162
163         properties = PfUtils.mapMap(properties, String::trim);
164         attributes = PfUtils.mapMap(attributes, String::trim);
165     }
166
167     @Override
168     public int compareTo(final PfConcept otherConcept) {
169         if (otherConcept == null) {
170             return -1;
171         }
172
173         if (this == otherConcept) {
174             return 0;
175         }
176
177         if (getClass() != otherConcept.getClass()) {
178             return getClass().getName().compareTo(otherConcept.getClass().getName());
179         }
180
181         final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
182         int result = super.compareTo(other);
183         if (result != 0) {
184             return result;
185         }
186
187         result = PfUtils.compareMaps(properties, other.properties);
188         if (result != 0) {
189             return result;
190         }
191
192         result = PfUtils.compareMaps(attributes, other.attributes);
193         if (result != 0) {
194             return result;
195         }
196
197         return PfUtils.compareCollections(occurrences, other.occurrences);
198     }
199 }