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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
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.parameters.BeanValidationResult;
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.Validated;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
47 * Class to represent the parameter in TOSCA definition.
50 @Table(name = "ToscaCapabilityAssignment")
51 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @EqualsAndHashCode(callSuper = false)
54 public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabilityAssignment>
55 implements PfAuthorative<ToscaCapabilityAssignment> {
57 private static final long serialVersionUID = 1675770231921107988L;
59 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
60 private static final Integer JPA_UNBOUNDED_VALUE = -1;
62 private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator();
66 private Map<String, String> properties;
70 private Map<String, String> attributes;
73 private List<Integer> occurrences;
76 * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
78 public JpaToscaCapabilityAssignment() {
79 this(new PfConceptKey());
83 * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
87 public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
94 * @param copyConcept the concept to copy from
96 public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment 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);
104 * Authorative constructor.
106 * @param authorativeConcept the authorative concept to copy from
108 public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
109 super(new PfConceptKey());
110 this.fromAuthorative(authorativeConcept);
114 public ToscaCapabilityAssignment toAuthorative() {
115 ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment();
116 super.setToscaEntity(toscaCapabilityAssignment);
117 super.toAuthorative();
119 toscaCapabilityAssignment.setProperties(
120 PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class)));
122 toscaCapabilityAssignment.setAttributes(
123 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
125 toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
126 if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
127 return AUTHORATIVE_UNBOUNDED_LITERAL;
133 return toscaCapabilityAssignment;
137 public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
138 super.fromAuthorative(toscaCapabilityAssignment);
141 properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml);
142 attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
144 occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
145 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
146 return JPA_UNBOUNDED_VALUE;
148 return ((Number) occurrence).intValue();
154 public void clean() {
157 properties = PfUtils.mapMap(properties, String::trim);
158 attributes = PfUtils.mapMap(attributes, String::trim);
162 public BeanValidationResult validate(String fieldName) {
163 BeanValidationResult result = super.validate(fieldName);
165 validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
166 validateMap(result, "attributes", attributes, Validated::validateEntryValueNotNull);
168 validateList(result, "occurrences", occurrences, validateMin(0, JPA_UNBOUNDED_VALUE, true));
174 public int compareTo(final PfConcept otherConcept) {
175 if (otherConcept == null) {
179 if (this == otherConcept) {
183 if (getClass() != otherConcept.getClass()) {
184 return getClass().getName().compareTo(otherConcept.getClass().getName());
187 final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
188 int result = super.compareTo(other);
193 result = PfUtils.compareMaps(properties, other.properties);
198 result = PfUtils.compareMaps(attributes, other.attributes);
203 return PfUtils.compareCollections(occurrences, other.occurrences);