2 * ============LICENSE_START=======================================================
3 * ONAP Requirement Model
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.Entries;
41 import org.onap.policy.common.parameters.annotations.Items;
42 import org.onap.policy.common.parameters.annotations.NotNull;
43 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfUtils;
48 import org.onap.policy.models.base.validation.annotations.PfItems;
49 import org.onap.policy.models.base.validation.annotations.PfMin;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
53 * Class to represent the requirement in TOSCA definition.
55 * @author Chenfei Gao (cgao@research.att.com)
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaRequirement")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
64 implements PfAuthorative<ToscaRequirement> {
66 private static final long serialVersionUID = 2785481541573683089L;
67 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
68 private static final Integer JPA_UNBOUNDED_VALUE = -1;
71 private String capability;
77 private String relationship;
80 @PfItems(notNull = {@NotNull}, pfMin = {@PfMin(value = 0, allowed = -1)})
81 private List<Integer> occurrences;
85 @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
86 private Map<String, String> properties;
89 * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
91 public JpaToscaRequirement() {
92 this(new PfConceptKey());
96 * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
100 public JpaToscaRequirement(@NonNull final PfConceptKey key) {
107 * @param copyConcept the concept to copy from
109 public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
111 this.capability = copyConcept.capability;
112 this.node = copyConcept.node;
113 this.relationship = copyConcept.relationship;
114 this.occurrences = new ArrayList<>(copyConcept.occurrences);
115 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
119 * Authorative constructor.
121 * @param authorativeConcept the authorative concept to copy from
123 public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
124 super(new PfConceptKey());
125 this.fromAuthorative(authorativeConcept);
129 public ToscaRequirement toAuthorative() {
130 ToscaRequirement toscaRequirement = new ToscaRequirement();
131 super.setToscaEntity(toscaRequirement);
132 super.toAuthorative();
134 toscaRequirement.setCapability(capability);
135 toscaRequirement.setNode(node);
136 toscaRequirement.setRelationship(relationship);
138 if (occurrences != null) {
139 List<Object> occurrencesList = new ArrayList<>(occurrences);
140 for (Integer occurrence : occurrences) {
141 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
142 occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
144 occurrencesList.add(occurrence);
147 toscaRequirement.setOccurrences(occurrencesList);
150 if (properties != null) {
151 Map<String, Object> propertiesMap = new LinkedHashMap<>();
153 for (Map.Entry<String, String> entry : properties.entrySet()) {
154 propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class));
157 toscaRequirement.setProperties(propertiesMap);
160 return toscaRequirement;
164 public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
165 super.fromAuthorative(toscaRequirement);
167 capability = toscaRequirement.getCapability();
168 node = toscaRequirement.getNode();
169 relationship = toscaRequirement.getRelationship();
171 if (toscaRequirement.getOccurrences() != null) {
172 occurrences = new ArrayList<>();
173 for (Object occurrence : toscaRequirement.getOccurrences()) {
174 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
175 occurrences.add(JPA_UNBOUNDED_VALUE);
177 occurrences.add(((Number) occurrence).intValue());
182 if (toscaRequirement.getProperties() != null) {
183 properties = new LinkedHashMap<>();
184 for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) {
185 String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue());
186 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
193 public void clean() {
196 capability = capability.trim();
198 relationship = relationship.trim();
200 properties = PfUtils.mapMap(properties, String::trim);
204 public int compareTo(final PfConcept otherConcept) {
205 if (otherConcept == null) {
209 if (this == otherConcept) {
213 if (getClass() != otherConcept.getClass()) {
214 return getClass().getName().compareTo(otherConcept.getClass().getName());
217 final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
218 int result = super.compareTo(other);
223 result = capability.compareTo(other.capability);
228 result = node.compareTo(other.node);
233 result = relationship.compareTo(other.relationship);
238 result = PfUtils.compareCollections(occurrences, other.occurrences);
243 return PfUtils.compareMaps(properties, other.properties);