Default should be an object on TOSCA properties
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaProperty.java
index 376c2b3..60e2fa0 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Model
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 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 com.google.gson.annotations.SerializedName;
-
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
 import java.util.List;
-
+import java.util.Map;
+import java.util.Map.Entry;
 import javax.persistence.Column;
 import javax.persistence.ElementCollection;
 import javax.persistence.EmbeddedId;
@@ -34,21 +35,27 @@ import javax.persistence.Entity;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.persistence.Table;
-
+import javax.ws.rs.core.Response;
 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.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+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.PfModelRuntimeException;
 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.tosca.authorative.concepts.ToscaConstraint;
+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.
@@ -61,13 +68,9 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = false)
-public class JpaToscaProperty extends PfConcept {
+public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
     private static final long serialVersionUID = 1675770231921107988L;
 
-    public enum Status {
-        SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED
-    }
-
     @EmbeddedId
     private PfReferenceKey key;
 
@@ -81,20 +84,20 @@ public class JpaToscaProperty extends PfConcept {
     private boolean required = false;
 
     @Column(name = "default")
-    @SerializedName("default")
     private String defaultValue;
 
     @Column
-    @NonNull
     private Status status = Status.SUPPORTED;
 
     @ElementCollection
-    private List<JpaToscaConstraint> constraints;
+    private List<JpaToscaConstraint> constraints = new ArrayList<>();
 
     @Column
-    @SerializedName("entry_schema")
     private JpaToscaEntrySchema entrySchema;
 
+    @ElementCollection
+    private Map<String, String> metadata = new LinkedHashMap<>();
+
     /**
      * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
      */
@@ -114,7 +117,7 @@ public class JpaToscaProperty extends PfConcept {
     /**
      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
      *
-     * @param key the key
+     * @param key  the key
      * @param type the key of the property type
      */
     public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
@@ -129,20 +132,111 @@ public class JpaToscaProperty extends PfConcept {
      */
     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 JpaToscaEntrySchema(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() {
+        ToscaProperty toscaProperty = new ToscaProperty();
 
-        keyList.addAll(type.getKeys());
+        toscaProperty.setName(key.getLocalName());
+
+        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));
+        }
 
         if (constraints != null) {
+            List<ToscaConstraint> toscaConstraints = new ArrayList<>();
+
             for (JpaToscaConstraint constraint : constraints) {
-                keyList.addAll(constraint.getKeys());
+                toscaConstraints.add(constraint.toAuthorative());
+            }
+
+            toscaProperty.setConstraints(toscaConstraints);
+        }
+
+        if (entrySchema != null) {
+            toscaProperty.setEntrySchema(entrySchema.toAuthorative());
+        }
+
+        if (metadata != null) {
+            toscaProperty.setMetadata(new LinkedHashMap<>(metadata));
+        }
+
+        return toscaProperty;
+    }
+
+    @Override
+    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);
+        }
+
+        description = toscaProperty.getDescription();
+        required = toscaProperty.isRequired();
+        status = toscaProperty.getStatus();
+
+        if (toscaProperty.getDefaultValue() != null) {
+            defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
+        } else {
+            defaultValue = null;
+        }
+
+        if (toscaProperty.getConstraints() != null) {
+            constraints = new ArrayList<>();
+
+            for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
+                constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
             }
         }
 
+        if (toscaProperty.getEntrySchema() != null) {
+            entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
+        }
+
+        // Add the property metadata if it doesn't exist already
+        if (toscaProperty.getMetadata() != null) {
+            metadata = new LinkedHashMap<>(toscaProperty.getMetadata());
+        }
+
+    }
+
+    @Override
+    public List<PfKey> getKeys() {
+        final List<PfKey> keyList = getKey().getKeys();
+
+        keyList.addAll(type.getKeys());
+
         if (entrySchema != null) {
             keyList.addAll(entrySchema.getKeys());
         }
@@ -164,15 +258,15 @@ public class JpaToscaProperty extends PfConcept {
             defaultValue = defaultValue.trim();
         }
 
-        if (constraints != null) {
-            for (JpaToscaConstraint constraint : constraints) {
-                constraint.clean();
-            }
-        }
-
         if (entrySchema != null) {
             entrySchema.clean();
         }
+
+        if (metadata != null) {
+            for (Entry<String, String> metadataEntry : metadata.entrySet()) {
+                metadataEntry.setValue(metadataEntry.getValue().trim());
+            }
+        }
     }
 
     @Override
@@ -181,14 +275,14 @@ public class JpaToscaProperty extends PfConcept {
 
         if (key.isNullKey()) {
             result.addValidationMessage(
-                    new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
+                new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
         }
 
         result = key.validate(result);
 
         if (type == null || type.isNullKey()) {
             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "property type may not be null"));
+                "property type may not be null"));
         }
 
         return validateFields(result);
@@ -205,21 +299,19 @@ public class JpaToscaProperty extends PfConcept {
 
         if (description != null && description.trim().length() == 0) {
             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "property description may not be blank"));
+                "property description may not be blank"));
         }
 
         if (defaultValue != null && defaultValue.trim().length() == 0) {
             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "property default value may not be null"));
+                "property default value may not be null"));
         }
 
         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);
+                        "property constraint may not be null "));
                 }
             }
         }
@@ -235,7 +327,7 @@ public class JpaToscaProperty extends PfConcept {
             return 0;
         }
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
         final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
@@ -247,8 +339,7 @@ public class JpaToscaProperty extends PfConcept {
     }
 
     /**
-     * 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
      */
@@ -284,21 +375,4 @@ public class JpaToscaProperty extends PfConcept {
 
         return entrySchema.compareTo(other.entrySchema);
     }
-
-    @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;
-    }
 }