* ============LICENSE_START=======================================================
* ONAP Policy Model
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
+ * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
* ================================================================================
* 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 com.google.gson.annotations.SerializedName;
-
+import jakarta.persistence.Column;
+import jakarta.persistence.ElementCollection;
+import jakarta.persistence.EmbeddedId;
+import java.io.Serial;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.List;
-
-import javax.persistence.Column;
-import javax.persistence.ElementCollection;
-import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.Table;
-
+import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
-
import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.utils.validation.Assertions;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.parameters.annotations.Valid;
+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.PfReferenceKey;
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.PfValidationResult.ValidationResult;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
/**
* Class to represent the property in TOSCA definition.
* @author Chenfei Gao (cgao@research.att.com)
* @author Liam Fallon (liam.fallon@est.tech)
*/
-@Entity
-@Table(name = "ToscaProperty")
-@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = false)
-public class JpaToscaProperty extends PfConcept {
+public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
+ @Serial
private static final long serialVersionUID = 1675770231921107988L;
- public enum Status {
- SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED
- }
-
@EmbeddedId
+ @VerifyKey
+ @NotNull
private PfReferenceKey key;
@Column
+ @VerifyKey
+ @NotNull
private PfConceptKey type;
@Column
+ @NotBlank
private String description;
@Column
private boolean required = false;
- @Column(name = "default")
- @SerializedName("default")
+ @Column
+ @NotBlank
private String defaultValue;
@Column
- @NonNull
private Status status = Status.SUPPORTED;
@ElementCollection
- private List<JpaToscaConstraint> constraints;
+ private List<@NotNull @Valid JpaToscaConstraint> constraints;
@Column
- @SerializedName("entry_schema")
- private JpaToscaEntrySchema entrySchema;
+ @Valid
+ private JpaToscaSchemaDefinition entrySchema;
+
+ @ElementCollection
+ private Map<String, String> metadata;
/**
* The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
*/
public JpaToscaProperty(final JpaToscaProperty copyConcept) {
super(copyConcept);
+ this.key = new PfReferenceKey(copyConcept.key);
+ this.type = new PfConceptKey(copyConcept.type);
+ this.description = copyConcept.description;
+ this.required = copyConcept.required;
+ this.defaultValue = copyConcept.defaultValue;
+ this.status = copyConcept.status;
+ // Constraints are immutable
+ this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
+ this.entrySchema =
+ (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null);
+ this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
+ }
+
+ /**
+ * Authorative constructor.
+ *
+ * @param authorativeConcept the authorative concept to copy from
+ */
+ public JpaToscaProperty(final ToscaProperty authorativeConcept) {
+ this.fromAuthorative(authorativeConcept);
}
@Override
- public List<PfKey> getKeys() {
- final List<PfKey> keyList = getKey().getKeys();
+ public ToscaProperty toAuthorative() {
+ var toscaProperty = new ToscaProperty();
- keyList.addAll(type.getKeys());
+ toscaProperty.setName(key.getLocalName());
- if (constraints != null) {
- for (JpaToscaConstraint constraint : constraints) {
- keyList.addAll(constraint.getKeys());
- }
+ toscaProperty.setType(type.getName());
+ toscaProperty.setTypeVersion(type.getVersion());
+
+ toscaProperty.setDescription(description);
+ toscaProperty.setRequired(required);
+ toscaProperty.setStatus(status);
+
+ if (defaultValue != null) {
+ toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
}
+ toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
+
if (entrySchema != null) {
- keyList.addAll(entrySchema.getKeys());
+ toscaProperty.setEntrySchema(entrySchema.toAuthorative());
}
- return keyList;
+ toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));
+
+ return toscaProperty;
}
@Override
- public void clean() {
- key.clean();
+ public void fromAuthorative(ToscaProperty toscaProperty) {
+ this.setKey(new PfReferenceKey());
+ getKey().setLocalName(toscaProperty.getName());
+
+ if (toscaProperty.getTypeVersion() != null) {
+ type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
+ } else {
+ type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
+ }
- type.clean();
+ description = toscaProperty.getDescription();
+ required = toscaProperty.isRequired();
+ status = toscaProperty.getStatus();
- if (description != null) {
- description = description.trim();
+ if (toscaProperty.getDefaultValue() != null) {
+ defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
+ } else {
+ defaultValue = null;
}
- if (defaultValue != null) {
- defaultValue = defaultValue.trim();
- }
+ constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);
- if (constraints != null) {
- for (JpaToscaConstraint constraint : constraints) {
- constraint.clean();
- }
+ if (toscaProperty.getEntrySchema() != null) {
+ entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
}
- if (entrySchema != null) {
- entrySchema.clean();
- }
+ metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
}
@Override
- public PfValidationResult validate(final PfValidationResult resultIn) {
- PfValidationResult result = resultIn;
-
- if (key.isNullKey()) {
- result.addValidationMessage(
- new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
- }
+ public List<PfKey> getKeys() {
+ final List<PfKey> keyList = getKey().getKeys();
- result = key.validate(result);
+ keyList.addAll(type.getKeys());
- if (type == null || type.isNullKey()) {
- result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
- "property type may not be null"));
+ if (entrySchema != null) {
+ keyList.addAll(entrySchema.getKeys());
}
- return validateFields(result);
+ return keyList;
}
- /**
- * Validate the property fields.
- *
- * @param resultIn the incoming validation results so far
- * @return the validation results including this validation
- */
- private PfValidationResult validateFields(final PfValidationResult resultIn) {
- PfValidationResult result = resultIn;
+ @Override
+ public void clean() {
+ key.clean();
+
+ type.clean();
- if (description != null && description.trim().length() == 0) {
- result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
- "property description may not be blank"));
+ if (description != null) {
+ description = description.trim();
}
- if (defaultValue != null && defaultValue.trim().length() == 0) {
- result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
- "property default value may not be null"));
+ if (defaultValue != null) {
+ defaultValue = defaultValue.trim();
}
- if (constraints != null) {
- for (JpaToscaConstraint constraint : constraints) {
- if (constraint == null) {
- result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
- "property constraint may not be null "));
- } else {
- result = constraint.validate(result);
- }
- }
+ if (entrySchema != null) {
+ entrySchema.clean();
}
- return (entrySchema != null ? entrySchema.validate(result) : result);
+
+ metadata = PfUtils.mapMap(metadata, String::trim);
}
@Override
- public int compareTo(final PfConcept otherConcept) {
- if (otherConcept == null) {
- return -1;
- }
+ public int compareTo(@NonNull final PfConcept otherConcept) {
if (this == otherConcept) {
return 0;
}
if (getClass() != otherConcept.getClass()) {
- return this.hashCode() - otherConcept.hashCode();
+ return getClass().getName().compareTo(otherConcept.getClass().getName());
}
final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
}
/**
- * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty
- * object.
+ * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
*
* @param other the other ToscaProperty object
*/
return result;
}
- result = PfUtils.compareObjects(constraints, other.constraints);
+ result = PfUtils.compareCollections(constraints, other.constraints);
if (result != 0) {
return result;
}
- return entrySchema.compareTo(other.entrySchema);
- }
+ result = entrySchema.compareTo(other.entrySchema);
+ if (result != 0) {
+ return result;
+ }
- @Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- Assertions.instanceOf(target, JpaToscaProperty.class);
-
- final JpaToscaProperty copy = ((JpaToscaProperty) target);
- copy.setKey(new PfReferenceKey(key));
- copy.setType(new PfConceptKey(type));
- copy.setDescription(description);
- copy.setRequired(required);
- copy.setDefaultValue(defaultValue);
- copy.setStatus(status);
- copy.constraints = constraints; // Constraints are immutable
- copy.setEntrySchema(entrySchema != null ? new JpaToscaEntrySchema(entrySchema) : null);
-
- return copy;
+ return PfUtils.compareMaps(metadata, other.metadata);
}
}