2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021, 2023 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
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 jakarta.persistence.ElementCollection;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.Inheritance;
27 import jakarta.persistence.InheritanceType;
28 import jakarta.persistence.Lob;
29 import jakarta.persistence.Table;
30 import java.io.Serial;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
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;
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 JpaToscaWithTypeAndStringProperties<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<@NotNull String, @NotNull String> attributes;
69 private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
72 * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
74 public JpaToscaCapabilityAssignment() {
75 this(new PfConceptKey());
79 * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
83 public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
90 * @param copyConcept the concept to copy from
92 public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
94 this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
95 this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
99 * Authorative constructor.
101 * @param authorativeConcept the authorative concept to copy from
103 public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
104 super(authorativeConcept);
108 public ToscaCapabilityAssignment toAuthorative() {
109 var toscaCapabilityAssignment = new ToscaCapabilityAssignment();
110 super.setToscaEntity(toscaCapabilityAssignment);
111 super.toAuthorative();
113 toscaCapabilityAssignment.setAttributes(
114 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
116 toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
117 if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
118 return AUTHORATIVE_UNBOUNDED_LITERAL;
124 return toscaCapabilityAssignment;
128 public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
129 super.fromAuthorative(toscaCapabilityAssignment);
131 attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
133 occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
134 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
135 return JPA_UNBOUNDED_VALUE;
137 return ((Number) occurrence).intValue();
143 protected Object deserializePropertyValue(String propValue) {
144 return YAML_JSON_TRANSLATOR.fromYaml(propValue, Object.class);
148 protected String serializePropertyValue(Object propValue) {
149 return YAML_JSON_TRANSLATOR.toYaml(propValue);
153 public void clean() {
156 attributes = PfUtils.mapMap(attributes, String::trim);
160 public int compareTo(final PfConcept otherConcept) {
161 if (this == otherConcept) {
165 int result = super.compareTo(otherConcept);
170 final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
172 result = PfUtils.compareMaps(attributes, other.attributes);
177 return PfUtils.compareCollections(occurrences, other.occurrences);