JPA concepts for TOSCA
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTopologyTemplate.java
index b1c3f20..32c459c 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. 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 java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.List;
-
+import java.util.Map;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
+import javax.persistence.ElementCollection;
 import javax.persistence.EmbeddedId;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.Lob;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
-
 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.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
 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.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 
 /**
@@ -69,7 +77,29 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
     @Column(name = "description")
     private String description;
 
+    // @formatter:off
+    @ElementCollection
+    @Lob
+    private Map<String, JpaToscaParameter> inputs;
+
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
+    @JoinColumns(
+        {
+            @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name"),
+            @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version")
+        }
+    )
+    @SerializedName("data_types")
+    private JpaToscaNodeTemplates nodeTemplates;
+
+    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
+    @JoinColumns(
+            {
+                @JoinColumn(name = "policyName",    referencedColumnName = "name"),
+                @JoinColumn(name = "policyVersion", referencedColumnName = "version")
+            }
+        )
+    // @formatter:on
     private JpaToscaPolicies policies;
 
     /**
@@ -81,8 +111,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
     }
 
     /**
-     * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept
-     * key.
+     * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
      *
      * @param key the key
      */
@@ -97,6 +126,12 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
      */
     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
         super(copyConcept);
+        this.key = new PfReferenceKey(copyConcept.key);
+        this.description = copyConcept.description;
+        this.inputs = PfUtils.mapMap(copyConcept.inputs, JpaToscaParameter::new);
+        this.nodeTemplates =
+                (copyConcept.nodeTemplates != null ? new JpaToscaNodeTemplates(copyConcept.nodeTemplates) : null);
+        this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
     }
 
     /**
@@ -114,6 +149,24 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
 
         toscaTopologyTemplate.setDescription(description);
 
+        if (inputs != null) {
+            Map<String, ToscaParameter> inputMap = new LinkedHashMap<>();
+
+            for (Map.Entry<String, JpaToscaParameter> entry : inputs.entrySet()) {
+                inputMap.put(entry.getKey(), entry.getValue().toAuthorative());
+            }
+
+            toscaTopologyTemplate.setInputs(inputMap);
+        }
+
+        if (nodeTemplates != null) {
+            toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>());
+            List<Map<String, ToscaNodeTemplate>> nodeTemplateMapList = nodeTemplates.toAuthorative();
+            for (Map<String, ToscaNodeTemplate> nodeTemplateMap : nodeTemplateMapList) {
+                toscaTopologyTemplate.getNodeTemplates().putAll(nodeTemplateMap);
+            }
+        }
+
         if (policies != null) {
             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
         }
@@ -125,6 +178,20 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
         description = toscaTopologyTemplate.getDescription();
 
+        if (toscaTopologyTemplate.getInputs() != null) {
+            inputs = new LinkedHashMap<>();
+            for (Map.Entry<String, ToscaParameter> toscaInputEntry : toscaTopologyTemplate.getInputs().entrySet()) {
+                JpaToscaParameter jpaInput = new JpaToscaParameter(toscaInputEntry.getValue());
+                jpaInput.setKey(new PfReferenceKey(getKey(), toscaInputEntry.getKey()));
+                inputs.put(toscaInputEntry.getKey(), jpaInput);
+            }
+        }
+
+        if (toscaTopologyTemplate.getNodeTemplates() != null) {
+            nodeTemplates = new JpaToscaNodeTemplates();
+            nodeTemplates.fromAuthorative(Collections.singletonList(toscaTopologyTemplate.getNodeTemplates()));
+        }
+
         if (toscaTopologyTemplate.getPolicies() != null) {
             policies = new JpaToscaPolicies();
             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
@@ -135,6 +202,16 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
     public List<PfKey> getKeys() {
         final List<PfKey> keyList = getKey().getKeys();
 
+        if (inputs != null) {
+            for (JpaToscaParameter input : inputs.values()) {
+                keyList.addAll(input.getKeys());
+            }
+        }
+
+        if (nodeTemplates != null) {
+            keyList.addAll(nodeTemplates.getKeys());
+        }
+
         if (policies != null) {
             keyList.addAll(policies.getKeys());
         }
@@ -148,6 +225,16 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
 
         description = (description != null ? description.trim() : null);
 
+        if (inputs != null) {
+            for (JpaToscaParameter input : inputs.values()) {
+                input.clean();
+            }
+        }
+
+        if (nodeTemplates != null) {
+            nodeTemplates.clean();
+        }
+
         if (policies != null) {
             policies.clean();
         }
@@ -169,6 +256,15 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
                     "property description may not be blank"));
         }
 
+        if (inputs != null) {
+            result = validateInputs(result);
+        }
+
+
+        if (nodeTemplates != null) {
+            result = nodeTemplates.validate(result);
+        }
+
         if (policies != null) {
             result = policies.validate(result);
         }
@@ -176,24 +272,41 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
         return result;
     }
 
+    /**
+     * Validate the inputs.
+     *
+     * @param resultIn The result of validations up to now
+     * @return the validation result
+     */
+    private PfValidationResult validateInputs(final PfValidationResult resultIn) {
+        PfValidationResult result = resultIn;
+
+        for (JpaToscaParameter input : inputs.values()) {
+            if (input == null) {
+                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
+                        "topology template input may not be null "));
+            } else {
+                result = input.validate(result);
+            }
+        }
+        return result;
+    }
+
     @Override
     public int compareTo(final PfConcept otherConcept) {
-        if (otherConcept == null) {
-            return -1;
-        }
-        if (this == otherConcept) {
-            return 0;
-        }
-        if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+        int result = compareToWithoutEntities(otherConcept);
+        if (result != 0) {
+            return result;
         }
 
         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
-        if (!key.equals(other.key)) {
-            return key.compareTo(other.key);
+
+        result = PfUtils.compareObjects(inputs, other.inputs);
+        if (result != 0) {
+            return result;
         }
 
-        int result = ObjectUtils.compare(description, other.description);
+        result = ObjectUtils.compare(nodeTemplates, other.nodeTemplates);
         if (result != 0) {
             return result;
         }
@@ -201,16 +314,28 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
         return ObjectUtils.compare(policies, other.policies);
     }
 
-    @Override
-    public PfConcept copyTo(@NonNull PfConcept target) {
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfConcept.class);
+    /**
+     * Compare this topology template to another topology template, ignoring contained entities.
+     *
+     * @param otherConcept the other topology template
+     * @return the result of the comparison
+     */
+    public int compareToWithoutEntities(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 JpaToscaTopologyTemplate copy = ((JpaToscaTopologyTemplate) copyObject);
-        copy.setKey(new PfReferenceKey(key));
-        copy.setDescription(description);
-        copy.setPolicies(policies != null ? new JpaToscaPolicies(policies) : null);
+        final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
+        if (!key.equals(other.key)) {
+            return key.compareTo(other.key);
+        }
 
-        return copy;
+        return ObjectUtils.compare(description, other.description);
     }
 }