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 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 java.util.ArrayList;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
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.validation.annotations.PfMin;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
46 * Class to represent the requirement in TOSCA definition.
48 * @author Chenfei Gao (cgao@research.att.com)
49 * @author Liam Fallon (liam.fallon@est.tech)
52 @Table(name = "ToscaRequirement")
53 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @EqualsAndHashCode(callSuper = true)
56 public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties<ToscaRequirement> {
58 private static final long serialVersionUID = 2785481541573683089L;
59 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
60 private static final Integer JPA_UNBOUNDED_VALUE = -1;
61 private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator();
64 private String capability;
70 private String relationship;
73 private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
76 * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
78 public JpaToscaRequirement() {
79 this(new PfConceptKey());
83 * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
87 public JpaToscaRequirement(@NonNull final PfConceptKey key) {
94 * @param copyConcept the concept to copy from
96 public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
98 this.capability = copyConcept.capability;
99 this.node = copyConcept.node;
100 this.relationship = copyConcept.relationship;
101 this.occurrences = new ArrayList<>(copyConcept.occurrences);
105 * Authorative constructor.
107 * @param authorativeConcept the authorative concept to copy from
109 public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
110 super(authorativeConcept);
114 public ToscaRequirement toAuthorative() {
115 ToscaRequirement toscaRequirement = new ToscaRequirement();
116 super.setToscaEntity(toscaRequirement);
117 super.toAuthorative();
119 toscaRequirement.setCapability(capability);
120 toscaRequirement.setNode(node);
121 toscaRequirement.setRelationship(relationship);
123 if (occurrences != null) {
124 List<Object> occurrencesList = new ArrayList<>(occurrences);
125 for (Integer occurrence : occurrences) {
126 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
127 occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
129 occurrencesList.add(occurrence);
132 toscaRequirement.setOccurrences(occurrencesList);
135 return toscaRequirement;
139 public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
140 super.fromAuthorative(toscaRequirement);
142 capability = toscaRequirement.getCapability();
143 node = toscaRequirement.getNode();
144 relationship = toscaRequirement.getRelationship();
146 if (toscaRequirement.getOccurrences() != null) {
147 occurrences = new ArrayList<>();
148 for (Object occurrence : toscaRequirement.getOccurrences()) {
149 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
150 occurrences.add(JPA_UNBOUNDED_VALUE);
152 occurrences.add(((Number) occurrence).intValue());
159 protected Object deserializePropertyValue(String propValue) {
160 return YAML_TRANSLATOR.fromYaml(propValue, Object.class);
164 protected String serializePropertyValue(Object propValue) {
165 return YAML_TRANSLATOR.toYaml(propValue);
169 public void clean() {
172 capability = capability.trim();
174 relationship = relationship.trim();
178 public int compareTo(final PfConcept otherConcept) {
179 if (this == otherConcept) {
183 int result = super.compareTo(otherConcept);
188 final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
190 result = capability.compareTo(other.capability);
195 result = node.compareTo(other.node);
200 result = relationship.compareTo(other.relationship);
205 return PfUtils.compareCollections(occurrences, other.occurrences);