Implement validation and hierarchical get
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTopologyTemplate.java
index 095435a..1766087 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.
@@ -26,8 +27,11 @@ import javax.persistence.CascadeType;
 import javax.persistence.Column;
 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.OneToOne;
 import javax.persistence.Table;
 
@@ -36,13 +40,14 @@ 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.PfValidationMessage;
 import org.onap.policy.models.base.PfValidationResult;
 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 
 /**
  * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
@@ -55,7 +60,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = false)
-public class JpaToscaTopologyTemplate extends PfConcept {
+public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
     private static final long serialVersionUID = 8969698734673232603L;
 
     public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
@@ -66,7 +71,15 @@ public class JpaToscaTopologyTemplate extends PfConcept {
     @Column(name = "description")
     private String description;
 
-    @OneToOne(cascade = CascadeType.ALL)
+    // @formatter:off
+    @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;
 
     /**
@@ -78,8 +91,7 @@ public class JpaToscaTopologyTemplate extends PfConcept {
     }
 
     /**
-     * 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
      */
@@ -94,6 +106,41 @@ public class JpaToscaTopologyTemplate extends PfConcept {
      */
     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
         super(copyConcept);
+        this.key = new PfReferenceKey(copyConcept.key);
+        this.description = copyConcept.description;
+        this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
+    }
+
+    /**
+     * Authorative constructor.
+     *
+     * @param authorativeConcept the authorative concept to copy from
+     */
+    public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
+        this.fromAuthorative(authorativeConcept);
+    }
+
+    @Override
+    public ToscaTopologyTemplate toAuthorative() {
+        final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
+
+        toscaTopologyTemplate.setDescription(description);
+
+        if (policies != null) {
+            toscaTopologyTemplate.setPolicies(policies.toAuthorative());
+        }
+
+        return toscaTopologyTemplate;
+    }
+
+    @Override
+    public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
+        description = toscaTopologyTemplate.getDescription();
+
+        if (toscaTopologyTemplate.getPolicies() != null) {
+            policies = new JpaToscaPolicies();
+            policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
+        }
     }
 
     @Override
@@ -143,6 +190,21 @@ public class JpaToscaTopologyTemplate extends PfConcept {
 
     @Override
     public int compareTo(final PfConcept otherConcept) {
+        int result = compareToWithoutEntities(otherConcept);
+        if (result != 0) {
+            return result;
+        }
+
+        return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
+    }
+
+    /**
+     * Compare this topology template to another topology template, ignoring contained entitites.
+     *
+     * @param otherConcept the other topology template
+     * @return the result of the comparison
+     */
+    public int compareToWithoutEntities(final PfConcept otherConcept) {
         if (otherConcept == null) {
             return -1;
         }
@@ -150,7 +212,7 @@ public class JpaToscaTopologyTemplate extends PfConcept {
             return 0;
         }
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
@@ -158,24 +220,6 @@ public class JpaToscaTopologyTemplate extends PfConcept {
             return key.compareTo(other.key);
         }
 
-        int result = ObjectUtils.compare(description, other.description);
-        if (result != 0) {
-            return result;
-        }
-
-        return ObjectUtils.compare(policies, other.policies);
-    }
-
-    @Override
-    public PfConcept copyTo(@NonNull PfConcept target) {
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfConcept.class);
-
-        final JpaToscaTopologyTemplate copy = ((JpaToscaTopologyTemplate) copyObject);
-        copy.setKey(new PfReferenceKey(key));
-        copy.setDescription(description);
-        copy.setPolicies(policies != null ? new JpaToscaPolicies(policies) : null);
-
-        return copy;
+        return ObjectUtils.compare(description, other.description);
     }
 }