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-2020 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.LinkedHashMap;
28 import java.util.List;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Lob;
36 import javax.persistence.Table;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfUtils;
46 import org.onap.policy.models.base.validation.annotations.PfMin;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
50 * Class to represent the requirement in TOSCA definition.
52 * @author Chenfei Gao (cgao@research.att.com)
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ToscaRequirement")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = true)
60 public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
61 implements PfAuthorative<ToscaRequirement> {
63 private static final long serialVersionUID = 2785481541573683089L;
64 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
65 private static final Integer JPA_UNBOUNDED_VALUE = -1;
68 private String capability;
74 private String relationship;
77 private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
81 private Map<@NotNull String, @NotNull String> properties;
84 * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
86 public JpaToscaRequirement() {
87 this(new PfConceptKey());
91 * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
95 public JpaToscaRequirement(@NonNull final PfConceptKey key) {
102 * @param copyConcept the concept to copy from
104 public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
106 this.capability = copyConcept.capability;
107 this.node = copyConcept.node;
108 this.relationship = copyConcept.relationship;
109 this.occurrences = new ArrayList<>(copyConcept.occurrences);
110 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
114 * Authorative constructor.
116 * @param authorativeConcept the authorative concept to copy from
118 public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
119 super(new PfConceptKey());
120 this.fromAuthorative(authorativeConcept);
124 public ToscaRequirement toAuthorative() {
125 ToscaRequirement toscaRequirement = new ToscaRequirement();
126 super.setToscaEntity(toscaRequirement);
127 super.toAuthorative();
129 toscaRequirement.setCapability(capability);
130 toscaRequirement.setNode(node);
131 toscaRequirement.setRelationship(relationship);
133 if (occurrences != null) {
134 List<Object> occurrencesList = new ArrayList<>(occurrences);
135 for (Integer occurrence : occurrences) {
136 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
137 occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
139 occurrencesList.add(occurrence);
142 toscaRequirement.setOccurrences(occurrencesList);
145 if (properties != null) {
146 Map<String, Object> propertiesMap = new LinkedHashMap<>();
148 for (Map.Entry<String, String> entry : properties.entrySet()) {
149 propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class));
152 toscaRequirement.setProperties(propertiesMap);
155 return toscaRequirement;
159 public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
160 super.fromAuthorative(toscaRequirement);
162 capability = toscaRequirement.getCapability();
163 node = toscaRequirement.getNode();
164 relationship = toscaRequirement.getRelationship();
166 if (toscaRequirement.getOccurrences() != null) {
167 occurrences = new ArrayList<>();
168 for (Object occurrence : toscaRequirement.getOccurrences()) {
169 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
170 occurrences.add(JPA_UNBOUNDED_VALUE);
172 occurrences.add(((Number) occurrence).intValue());
177 if (toscaRequirement.getProperties() != null) {
178 properties = new LinkedHashMap<>();
179 for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) {
180 String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue());
181 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
188 public void clean() {
191 capability = capability.trim();
193 relationship = relationship.trim();
195 properties = PfUtils.mapMap(properties, String::trim);
199 public int compareTo(final PfConcept otherConcept) {
200 if (otherConcept == null) {
204 if (this == otherConcept) {
208 if (getClass() != otherConcept.getClass()) {
209 return getClass().getName().compareTo(otherConcept.getClass().getName());
212 final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
213 int result = super.compareTo(other);
218 result = capability.compareTo(other.capability);
223 result = node.compareTo(other.node);
228 result = relationship.compareTo(other.relationship);
233 result = PfUtils.compareCollections(occurrences, other.occurrences);
238 return PfUtils.compareMaps(properties, other.properties);