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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
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;
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;
48 * Class to represent the parameter in TOSCA definition.
51 @Table(name = "ToscaCapabilityAssignment")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
54 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabilityAssignment>
56 implements PfAuthorative<ToscaCapabilityAssignment> {
58 private static final long serialVersionUID = 1675770231921107988L;
60 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
61 private static final Integer JPA_UNBOUNDED_VALUE = -1;
63 private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator();
67 private Map<String, String> properties;
71 private Map<String, String> attributes;
74 private List<Integer> occurrences;
77 * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
79 public JpaToscaCapabilityAssignment() {
80 this(new PfConceptKey());
84 * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
88 public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
95 * @param copyConcept the concept to copy from
97 public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment 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);
105 * Authorative constructor.
107 * @param authorativeConcept the authorative concept to copy from
109 public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
110 super(new PfConceptKey());
111 this.fromAuthorative(authorativeConcept);
115 public ToscaCapabilityAssignment toAuthorative() {
116 ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment();
117 super.setToscaEntity(toscaCapabilityAssignment);
118 super.toAuthorative();
120 toscaCapabilityAssignment.setProperties(
121 PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class)));
123 toscaCapabilityAssignment.setAttributes(
124 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
126 toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
127 if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
128 return AUTHORATIVE_UNBOUNDED_LITERAL;
134 return toscaCapabilityAssignment;
138 public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
139 super.fromAuthorative(toscaCapabilityAssignment);
142 properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml);
143 attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
145 occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
146 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
147 return JPA_UNBOUNDED_VALUE;
149 return ((Number) occurrence).intValue();
155 public void clean() {
158 properties = PfUtils.mapMap(properties, String::trim);
159 attributes = PfUtils.mapMap(attributes, String::trim);
163 public PfValidationResult validate(final PfValidationResult resultIn) {
164 PfValidationResult result = super.validate(resultIn);
166 if (properties != null) {
167 result.append(validateProperties(new PfValidationResult()));
170 if (attributes != null) {
171 result.append(validateAttributes(new PfValidationResult()));
174 if (occurrences != null) {
175 result.append(validateOccurrences(new PfValidationResult()));
182 * Validate the properties.
184 * @param resultIn The result of validations up to now
185 * @return the validation result
187 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
188 PfValidationResult result = resultIn;
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"));
200 * Validate the attributes.
202 * @param resultIn The result of validations up to now
203 * @return the validation result
205 private PfValidationResult validateAttributes(final PfValidationResult resultIn) {
206 PfValidationResult result = resultIn;
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"));
218 * Validate the occurrences.
220 * @param resultIn The result of validations up to now
221 * @return the validation result
223 private PfValidationResult validateOccurrences(final PfValidationResult resultIn) {
224 PfValidationResult result = resultIn;
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"));
240 public int compareTo(final PfConcept otherConcept) {
241 if (otherConcept == null) {
245 if (this == otherConcept) {
249 if (getClass() != otherConcept.getClass()) {
250 return getClass().getName().compareTo(otherConcept.getClass().getName());
253 final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
254 int result = super.compareTo(other);
259 result = PfUtils.compareMaps(properties, other.properties);
264 result = PfUtils.compareMaps(attributes, other.attributes);
269 return PfUtils.compareCollections(occurrences, other.occurrences);