JPA concepts for TOSCA
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
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.utils.coder.YamlJsonTranslator;
38 import org.onap.policy.models.base.PfAuthorative;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.base.PfValidationMessage;
43 import org.onap.policy.models.base.PfValidationResult;
44 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
46
47 /**
48  * Class to represent the parameter in TOSCA definition.
49  */
50 @Entity
51 @Table(name = "ToscaCapabilityAssignment")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @Data
54 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabilityAssignment>
56         implements PfAuthorative<ToscaCapabilityAssignment> {
57
58     private static final long serialVersionUID = 1675770231921107988L;
59
60     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
61     private static final Integer JPA_UNBOUNDED_VALUE = -1;
62
63     private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator();
64
65     @ElementCollection
66     @Lob
67     private Map<String, String> properties;
68
69     @ElementCollection
70     @Lob
71     private Map<String, String> attributes;
72
73     @ElementCollection
74     private List<Integer> occurrences;
75
76     /**
77      * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
78      */
79     public JpaToscaCapabilityAssignment() {
80         this(new PfConceptKey());
81     }
82
83     /**
84      * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
85      *
86      * @param key the key
87      */
88     public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
89         super(key);
90     }
91
92     /**
93      * Copy constructor.
94      *
95      * @param copyConcept the concept to copy from
96      */
97     public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
98         super(copyConcept);
99         this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
100         this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
101         this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
102     }
103
104     /**
105      * Authorative constructor.
106      *
107      * @param authorativeConcept the authorative concept to copy from
108      */
109     public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
110         super(new PfConceptKey());
111         this.fromAuthorative(authorativeConcept);
112     }
113
114     @Override
115     public ToscaCapabilityAssignment toAuthorative() {
116         ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment();
117         super.setToscaEntity(toscaCapabilityAssignment);
118         super.toAuthorative();
119
120         toscaCapabilityAssignment.setProperties(
121                 PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class)));
122
123         toscaCapabilityAssignment.setAttributes(
124                 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
125
126         toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
127             if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
128                 return AUTHORATIVE_UNBOUNDED_LITERAL;
129             } else {
130                 return occurrence;
131             }
132         }));
133
134         return toscaCapabilityAssignment;
135     }
136
137     @Override
138     public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
139         super.fromAuthorative(toscaCapabilityAssignment);
140
141
142         properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml);
143         attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
144
145         occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
146             if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
147                 return JPA_UNBOUNDED_VALUE;
148             } else {
149                 return ((Number) occurrence).intValue();
150             }
151         });
152     }
153
154     @Override
155     public void clean() {
156         super.clean();
157
158         properties = PfUtils.mapMap(properties, String::trim);
159         attributes = PfUtils.mapMap(attributes, String::trim);
160     }
161
162     @Override
163     public PfValidationResult validate(final PfValidationResult resultIn) {
164         PfValidationResult result = super.validate(resultIn);
165
166         if (properties != null) {
167             result.append(validateProperties(new PfValidationResult()));
168         }
169
170         if (attributes != null) {
171             result.append(validateAttributes(new PfValidationResult()));
172         }
173
174         if (occurrences != null) {
175             result.append(validateOccurrences(new PfValidationResult()));
176         }
177
178         return result;
179     }
180
181     /**
182      * Validate the properties.
183      *
184      * @param resultIn The result of validations up to now
185      * @return the validation result
186      */
187     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
188         PfValidationResult result = resultIn;
189
190         for (Entry<String, String> propertyEntry : properties.entrySet()) {
191             if (propertyEntry.getValue() == null) {
192                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
193                         "capability assignment property " + propertyEntry.getKey() + " value may not be null"));
194             }
195         }
196         return result;
197     }
198
199     /**
200      * Validate the attributes.
201      *
202      * @param resultIn The result of validations up to now
203      * @return the validation result
204      */
205     private PfValidationResult validateAttributes(final PfValidationResult resultIn) {
206         PfValidationResult result = resultIn;
207
208         for (Entry<String, String> attributeEntry : attributes.entrySet()) {
209             if (attributeEntry.getValue() == null) {
210                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
211                         "capability assignment attribute " + attributeEntry.getKey() + " value may not be null"));
212             }
213         }
214         return result;
215     }
216
217     /**
218      * Validate the occurrences.
219      *
220      * @param resultIn The result of validations up to now
221      * @return the validation result
222      */
223     private PfValidationResult validateOccurrences(final PfValidationResult resultIn) {
224         PfValidationResult result = resultIn;
225
226         for (Integer occurrence : occurrences) {
227             if (occurrence == null) {
228                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
229                         "capability assignment occurrence value may not be null "));
230             } else if (occurrence < 0 && !occurrence.equals(JPA_UNBOUNDED_VALUE)) {
231                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
232                         "capability assignment occurrence value may not be negative"));
233             }
234         }
235
236         return result;
237     }
238
239     @Override
240     public int compareTo(final PfConcept otherConcept) {
241         if (otherConcept == null) {
242             return -1;
243         }
244
245         if (this == otherConcept) {
246             return 0;
247         }
248
249         if (getClass() != otherConcept.getClass()) {
250             return getClass().getName().compareTo(otherConcept.getClass().getName());
251         }
252
253         final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
254         int result = super.compareTo(other);
255         if (result != 0) {
256             return result;
257         }
258
259         result = PfUtils.compareMaps(properties, other.properties);
260         if (result != 0) {
261             return result;
262         }
263
264         result = PfUtils.compareMaps(attributes, other.attributes);
265         if (result != 0) {
266             return result;
267         }
268
269         return PfUtils.compareCollections(occurrences, other.occurrences);
270     }
271 }