Add merge utility for service templates
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfConceptContainer.java
index 65a28cf..949cb96 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 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.
@@ -33,7 +34,10 @@ import java.util.TreeMap;
 import javax.persistence.CascadeType;
 import javax.persistence.EmbeddedId;
 import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
+import javax.persistence.MappedSuperclass;
 import javax.persistence.Table;
 import javax.ws.rs.core.Response;
 
@@ -41,7 +45,6 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 
-import org.onap.policy.common.utils.validation.Assertions;
 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
 // @formatter:off
@@ -57,6 +60,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
  * @param C the concept being contained
  */
 //@formatter:on
+@MappedSuperclass
 @Entity
 @Table(name = "PfConceptContainer")
 @Data
@@ -70,6 +74,18 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     private PfConceptKey key;
 
     @ManyToMany(cascade = CascadeType.ALL)
+    // @formatter:off
+    @JoinTable(
+            joinColumns = {
+                @JoinColumn(name = "conceptContainerMapName",    referencedColumnName = "name"),
+                @JoinColumn(name = "concpetContainerMapVersion", referencedColumnName = "version")
+            },
+            inverseJoinColumns = {
+                @JoinColumn(name = "conceptContainerName",    referencedColumnName = "name"),
+                @JoinColumn(name = "conceptContainerVersion", referencedColumnName = "version")
+            }
+        )
+    // @formatter:on
     private Map<PfConceptKey, C> conceptMap;
 
     /**
@@ -110,6 +126,14 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
      */
     public PfConceptContainer(@NonNull final PfConceptContainer<C, A> copyConcept) {
         super(copyConcept);
+        this.key = new PfConceptKey(copyConcept.key);
+
+        this.conceptMap = new TreeMap<>();
+        for (final Entry<PfConceptKey, C> conceptMapEntry : copyConcept.conceptMap.entrySet()) {
+            PfConceptKey newK = new PfConceptKey(conceptMapEntry.getKey());
+            C newC = PfUtils.makeCopy(conceptMapEntry.getValue());
+            this.conceptMap.put(newK, newC);
+        }
     }
 
     @Override
@@ -125,16 +149,22 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
     @Override
     public List<Map<String, A>> toAuthorative() {
-        Map<String, A> toscaPolicyMap = new LinkedHashMap<>();
+        // The returned list is a list of map singletons with one map for each map
+        // entry in the concept container
+        List<Map<String, A>> toscaPolicyMapList = new ArrayList<>();
 
         for (Entry<PfConceptKey, C> conceptEntry : getConceptMap().entrySet()) {
+            // Create a map to hold this entry
+            Map<String, A> toscaPolicyMap = new LinkedHashMap<>(1);
+
+            // Add the concept container entry to the singleton map
             @SuppressWarnings("unchecked")
             PfAuthorative<A> authoritiveImpl = (PfAuthorative<A>) conceptEntry.getValue();
             toscaPolicyMap.put(conceptEntry.getKey().getName(), authoritiveImpl.toAuthorative());
-        }
 
-        List<Map<String, A>> toscaPolicyMapList = new ArrayList<>();
-        toscaPolicyMapList.add(toscaPolicyMap);
+            // Add the map to the returned list
+            toscaPolicyMapList.add(toscaPolicyMap);
+        }
 
         return toscaPolicyMapList;
     }
@@ -169,8 +199,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
                 if (incomingConceptEntry.getValue().getVersion() != null) {
                     conceptKey.setVersion(incomingConceptEntry.getValue().getVersion());
-                }
-                else {
+                } else {
                     conceptKey.setVersion(PfKey.NULL_KEY_VERSION);
                 }
 
@@ -205,10 +234,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
         result = key.validate(result);
 
-        if (conceptMap.isEmpty()) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "conceptMap may not be empty"));
-        } else {
+        if (!conceptMap.isEmpty()) {
             result = validateConceptMap(result);
         }
 
@@ -228,19 +254,17 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) {
                 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                         "key on concept entry " + conceptEntry.getKey() + " may not be the null key"));
-            } else
-                if (conceptEntry.getValue() == null) {
-                    result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                            "value on concept entry " + conceptEntry.getKey() + " may not be null"));
-                } else
-                    if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) {
-                        result.addValidationMessage(new PfValidationMessage(key, this.getClass(),
-                                ValidationResult.INVALID, "key on concept entry key " + conceptEntry.getKey()
-                                        + " does not equal concept value key " + conceptEntry.getValue().getKey()));
-                        result = conceptEntry.getValue().validate(result);
-                    } else {
-                        result = conceptEntry.getValue().validate(result);
-                    }
+            } else if (conceptEntry.getValue() == null) {
+                result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
+                        "value on concept entry " + conceptEntry.getKey() + " may not be null"));
+            } else if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) {
+                result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
+                        "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key "
+                                + conceptEntry.getValue().getKey()));
+                result = conceptEntry.getValue().validate(result);
+            } else {
+                result = conceptEntry.getValue().validate(result);
+            }
         }
         return result;
     }
@@ -254,7 +278,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
             return 0;
         }
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
         @SuppressWarnings("unchecked")
@@ -271,24 +295,6 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
         return 0;
     }
 
-    @Override
-    public PfConcept copyTo(@NonNull final PfConcept target) {
-        Assertions.instanceOf(target, PfConceptContainer.class);
-
-        @SuppressWarnings("unchecked")
-        final PfConceptContainer<C, A> copy = (PfConceptContainer<C, A>) target;
-        copy.setKey(new PfConceptKey(key));
-        final Map<PfConceptKey, C> newConceptMap = new TreeMap<>();
-        for (final Entry<PfConceptKey, C> conceptMapEntry : conceptMap.entrySet()) {
-            C newC = getConceptNewInstance();
-            conceptMapEntry.getValue().copyTo(newC);
-            newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), newC);
-        }
-        copy.setConceptMap(newConceptMap);
-
-        return copy;
-    }
-
     @Override
     public C get(final PfConceptKey conceptKey) {
         return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
@@ -326,7 +332,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
         try {
             String conceptClassName =
                     ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
-            return (C) Class.forName(conceptClassName).newInstance();
+            return (C) Class.forName(conceptClassName).getDeclaredConstructor().newInstance();
         } catch (Exception ex) {
             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
                     "failed to instantiate instance of container concept class", ex);