* ============LICENSE_START=======================================================
* ONAP Requirement Model
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.models.tosca.simple.concepts;
+import jakarta.persistence.Column;
+import jakarta.persistence.ElementCollection;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.Table;
+import java.io.Serial;
import java.util.ArrayList;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
-import javax.persistence.Column;
-import javax.persistence.ElementCollection;
-import javax.persistence.Entity;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.Lob;
-import javax.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
+import org.onap.policy.common.parameters.annotations.NotNull;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
-import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.PfValidationMessage;
-import org.onap.policy.models.base.PfValidationResult;
+import org.onap.policy.models.base.validation.annotations.PfMin;
import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
/**
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = true)
-public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
- implements PfAuthorative<ToscaRequirement> {
+public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties<ToscaRequirement> {
+ @Serial
private static final long serialVersionUID = 2785481541573683089L;
private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
- private static final Double JPA_UNBOUNDED_VALUE = -1.0;
+ private static final Integer JPA_UNBOUNDED_VALUE = -1;
+ private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator();
@Column
private String capability;
private String relationship;
@ElementCollection
- private List<Double> occurrences;
-
- @ElementCollection
- @Lob
- private Map<String, String> properties;
+ private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
/**
* The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
this.node = copyConcept.node;
this.relationship = copyConcept.relationship;
this.occurrences = new ArrayList<>(copyConcept.occurrences);
- this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
}
/**
* @param authorativeConcept the authorative concept to copy from
*/
public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
- super(new PfConceptKey());
- this.fromAuthorative(authorativeConcept);
+ super(authorativeConcept);
}
@Override
public ToscaRequirement toAuthorative() {
- ToscaRequirement toscaRequirement = new ToscaRequirement();
+ var toscaRequirement = new ToscaRequirement();
super.setToscaEntity(toscaRequirement);
super.toAuthorative();
if (occurrences != null) {
List<Object> occurrencesList = new ArrayList<>(occurrences);
- for (Double occurrence : occurrences) {
- if (occurrence == JPA_UNBOUNDED_VALUE) {
+ for (Integer occurrence : occurrences) {
+ if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
} else {
- occurrencesList.add(occurrence.doubleValue());
+ occurrencesList.add(occurrence);
}
}
toscaRequirement.setOccurrences(occurrencesList);
}
- if (properties != null) {
- Map<String, Object> propertiesMap = new LinkedHashMap<>();
-
- for (Map.Entry<String, String> entry : properties.entrySet()) {
- propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class));
- }
-
- toscaRequirement.setProperties(propertiesMap);
- }
-
return toscaRequirement;
}
if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
occurrences.add(JPA_UNBOUNDED_VALUE);
} else {
- occurrences.add((Double) occurrence);
+ occurrences.add(((Number) occurrence).intValue());
}
}
}
+ }
- if (toscaRequirement.getProperties() != null) {
- properties = new LinkedHashMap<>();
- for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) {
- String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue());
- properties.put(toscaPropertyEntry.getKey(), jpaProperty);
- }
- }
-
+ @Override
+ protected Object deserializePropertyValue(String propValue) {
+ return YAML_TRANSLATOR.fromYaml(propValue, Object.class);
}
@Override
- public List<PfKey> getKeys() {
- return super.getKeys();
+ protected String serializePropertyValue(Object propValue) {
+ return YAML_TRANSLATOR.toYaml(propValue);
}
@Override
capability = capability.trim();
node = node.trim();
relationship = relationship.trim();
-
- properties = PfUtils.mapMap(properties, String::trim);
- }
-
- @Override
- public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
- PfValidationResult result = super.validate(resultIn);
-
- if (properties != null) {
- result = validateProperties(result);
- }
-
- if (occurrences != null) {
- result = validateOccurrences(result);
- }
-
- return result;
- }
-
- /**
- * Validate the properties.
- *
- * @param resultIn The result of validations up to now
- * @return the validation result
- */
- private PfValidationResult validateProperties(final PfValidationResult resultIn) {
- PfValidationResult result = resultIn;
-
- for (String property : properties.values()) {
- if (property == null) {
- result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(),
- PfValidationResult.ValidationResult.INVALID, "topology template property may not be null "));
- }
- }
- return result;
- }
-
- /**
- * Validate the occurrences.
- *
- * @param resultIn The result of validations up to now
- * @return the validation result
- */
- private PfValidationResult validateOccurrences(final PfValidationResult resultIn) {
- PfValidationResult result = resultIn;
-
- for (Double occurrence : occurrences) {
- if (occurrence == null) {
- result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(),
- PfValidationResult.ValidationResult.INVALID, "requirement occurrence value may not be null "));
- }
- if (occurrence < -1.0) {
- result.addValidationMessage(
- new PfValidationMessage(getKey(), this.getClass(), PfValidationResult.ValidationResult.INVALID,
- "requirement occurrence value may not be negative"));
- }
- }
-
- return result;
}
@Override
public int compareTo(final PfConcept otherConcept) {
- if (otherConcept == null) {
- return -1;
- }
-
if (this == otherConcept) {
return 0;
}
- if (getClass() != otherConcept.getClass()) {
- return getClass().getName().compareTo(otherConcept.getClass().getName());
- }
-
- final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
- int result = super.compareTo(other);
+ int result = super.compareTo(otherConcept);
if (result != 0) {
return result;
}
+ final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
+
result = capability.compareTo(other.capability);
if (result != 0) {
return result;
return result;
}
- result = PfUtils.compareCollections(occurrences, other.occurrences);
- if (result != 0) {
- return result;
- }
-
- return PfUtils.compareMaps(properties, other.properties);
+ return PfUtils.compareCollections(occurrences, other.occurrences);
}
}