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.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfUtils;
46 import org.onap.policy.models.base.PfValidationMessage;
47 import org.onap.policy.models.base.PfValidationResult;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
51 * Class to represent the requirement in TOSCA definition.
53 * @author Chenfei Gao (cgao@research.att.com)
54 * @author Liam Fallon (liam.fallon@est.tech)
57 @Table(name = "ToscaRequirement")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = true)
61 public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
62 implements PfAuthorative<ToscaRequirement> {
64 private static final long serialVersionUID = 2785481541573683089L;
65 private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
66 private static final Double JPA_UNBOUNDED_VALUE = -1.0;
69 private String capability;
75 private String relationship;
78 private List<Double> occurrences;
82 private Map<String, String> properties;
85 * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
87 public JpaToscaRequirement() {
88 this(new PfConceptKey());
92 * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
96 public JpaToscaRequirement(@NonNull final PfConceptKey key) {
103 * @param copyConcept the concept to copy from
105 public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
107 this.capability = copyConcept.capability;
108 this.node = copyConcept.node;
109 this.relationship = copyConcept.relationship;
110 this.occurrences = new ArrayList<>(copyConcept.occurrences);
111 this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
115 * Authorative constructor.
117 * @param authorativeConcept the authorative concept to copy from
119 public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
120 super(new PfConceptKey());
121 this.fromAuthorative(authorativeConcept);
125 public ToscaRequirement toAuthorative() {
126 ToscaRequirement toscaRequirement = new ToscaRequirement();
127 super.setToscaEntity(toscaRequirement);
128 super.toAuthorative();
130 toscaRequirement.setCapability(capability);
131 toscaRequirement.setNode(node);
132 toscaRequirement.setRelationship(relationship);
134 if (occurrences != null) {
135 List<Object> occurrencesList = new ArrayList<>(occurrences);
136 for (Double occurrence : occurrences) {
137 if (occurrence == JPA_UNBOUNDED_VALUE) {
138 occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
140 occurrencesList.add(occurrence.doubleValue());
143 toscaRequirement.setOccurrences(occurrencesList);
146 if (properties != null) {
147 Map<String, Object> propertiesMap = new LinkedHashMap<>();
149 for (Map.Entry<String, String> entry : properties.entrySet()) {
150 propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class));
153 toscaRequirement.setProperties(propertiesMap);
156 return toscaRequirement;
160 public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
161 super.fromAuthorative(toscaRequirement);
163 capability = toscaRequirement.getCapability();
164 node = toscaRequirement.getNode();
165 relationship = toscaRequirement.getRelationship();
167 if (toscaRequirement.getOccurrences() != null) {
168 occurrences = new ArrayList<>();
169 for (Object occurrence : toscaRequirement.getOccurrences()) {
170 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
171 occurrences.add(JPA_UNBOUNDED_VALUE);
173 occurrences.add((Double) occurrence);
178 if (toscaRequirement.getProperties() != null) {
179 properties = new LinkedHashMap<>();
180 for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) {
181 String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue());
182 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
189 public List<PfKey> getKeys() {
190 return super.getKeys();
194 public void clean() {
197 capability = capability.trim();
199 relationship = relationship.trim();
201 properties = PfUtils.mapMap(properties, String::trim);
205 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
206 PfValidationResult result = super.validate(resultIn);
208 if (properties != null) {
209 result = validateProperties(result);
212 if (occurrences != null) {
213 result = validateOccurrences(result);
220 * Validate the properties.
222 * @param resultIn The result of validations up to now
223 * @return the validation result
225 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
226 PfValidationResult result = resultIn;
228 for (String property : properties.values()) {
229 if (property == null) {
230 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(),
231 PfValidationResult.ValidationResult.INVALID, "topology template property may not be null "));
238 * Validate the occurrences.
240 * @param resultIn The result of validations up to now
241 * @return the validation result
243 private PfValidationResult validateOccurrences(final PfValidationResult resultIn) {
244 PfValidationResult result = resultIn;
246 for (Double occurrence : occurrences) {
247 if (occurrence == null) {
248 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(),
249 PfValidationResult.ValidationResult.INVALID, "requirement occurrence value may not be null "));
251 if (occurrence < -1.0) {
252 result.addValidationMessage(
253 new PfValidationMessage(getKey(), this.getClass(), PfValidationResult.ValidationResult.INVALID,
254 "requirement occurrence value may not be negative"));
262 public int compareTo(final PfConcept otherConcept) {
263 if (otherConcept == null) {
267 if (this == otherConcept) {
271 if (getClass() != otherConcept.getClass()) {
272 return getClass().getName().compareTo(otherConcept.getClass().getName());
275 final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
276 int result = super.compareTo(other);
281 result = capability.compareTo(other.capability);
286 result = node.compareTo(other.node);
291 result = relationship.compareTo(other.relationship);
296 result = PfUtils.compareCollections(occurrences, other.occurrences);
301 return PfUtils.compareMaps(properties, other.properties);