2 * ============LICENSE_START=======================================================
3 * ONAP Requirement Model
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.simple.concepts;
26 import jakarta.persistence.Column;
27 import jakarta.persistence.ElementCollection;
28 import jakarta.persistence.Entity;
29 import jakarta.persistence.Inheritance;
30 import jakarta.persistence.InheritanceType;
31 import jakarta.persistence.Table;
32 import java.io.Serial;
33 import java.util.ArrayList;
34 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.ToscaRequirement;
47 * Class to represent the requirement in TOSCA definition.
49 * @author Chenfei Gao (cgao@research.att.com)
50 * @author Liam Fallon (liam.fallon@est.tech)
53 @Table(name = "ToscaRequirement")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @EqualsAndHashCode(callSuper = true)
57 public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties<ToscaRequirement> {
60 private static final long serialVersionUID = 2785481541573683089L;
61 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
62 private static final Integer JPA_UNBOUNDED_VALUE = -1;
63 private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator();
66 private String capability;
72 private String relationship;
75 private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
78 * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
80 public JpaToscaRequirement() {
81 this(new PfConceptKey());
85 * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
89 public JpaToscaRequirement(@NonNull final PfConceptKey key) {
96 * @param copyConcept the concept to copy from
98 public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
100 this.capability = copyConcept.capability;
101 this.node = copyConcept.node;
102 this.relationship = copyConcept.relationship;
103 this.occurrences = new ArrayList<>(copyConcept.occurrences);
107 * Authorative constructor.
109 * @param authorativeConcept the authorative concept to copy from
111 public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
112 super(authorativeConcept);
116 public ToscaRequirement toAuthorative() {
117 var toscaRequirement = new ToscaRequirement();
118 super.setToscaEntity(toscaRequirement);
119 super.toAuthorative();
121 toscaRequirement.setCapability(capability);
122 toscaRequirement.setNode(node);
123 toscaRequirement.setRelationship(relationship);
125 if (occurrences != null) {
126 List<Object> occurrencesList = new ArrayList<>(occurrences);
127 for (Integer occurrence : occurrences) {
128 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
129 occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
131 occurrencesList.add(occurrence);
134 toscaRequirement.setOccurrences(occurrencesList);
137 return toscaRequirement;
141 public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
142 super.fromAuthorative(toscaRequirement);
144 capability = toscaRequirement.getCapability();
145 node = toscaRequirement.getNode();
146 relationship = toscaRequirement.getRelationship();
148 if (toscaRequirement.getOccurrences() != null) {
149 occurrences = new ArrayList<>();
150 for (Object occurrence : toscaRequirement.getOccurrences()) {
151 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
152 occurrences.add(JPA_UNBOUNDED_VALUE);
154 occurrences.add(((Number) occurrence).intValue());
161 protected Object deserializePropertyValue(String propValue) {
162 return YAML_TRANSLATOR.fromYaml(propValue, Object.class);
166 protected String serializePropertyValue(Object propValue) {
167 return YAML_TRANSLATOR.toYaml(propValue);
171 public void clean() {
174 capability = capability.trim();
176 relationship = relationship.trim();
180 public int compareTo(final PfConcept otherConcept) {
181 if (this == otherConcept) {
185 int result = super.compareTo(otherConcept);
190 final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
192 result = capability.compareTo(other.capability);
197 result = node.compareTo(other.node);
202 result = relationship.compareTo(other.relationship);
207 return PfUtils.compareCollections(occurrences, other.occurrences);