Implement validation and hierarchical get
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaEntityType.java
index e7d51a5..6544e72 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.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.models.tosca.simple.concepts;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -37,8 +39,8 @@ 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.validation.ParameterValidationUtils;
+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;
@@ -46,6 +48,7 @@ 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.ToscaEntity;
 
 /**
  * Class to represent the EntrySchema of list/map property in TOSCA definition.
@@ -53,7 +56,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 @MappedSuperclass
 @Data
 @EqualsAndHashCode(callSuper = false)
-public class JpaToscaEntityType extends PfConcept {
+public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
     private static final long serialVersionUID = -1330661834220739393L;
 
     @EmbeddedId
@@ -70,10 +73,12 @@ public class JpaToscaEntityType extends PfConcept {
     private PfConceptKey derivedFrom;
 
     @ElementCollection
-    private Map<String, String> metadata;
+    private Map<String, String> metadata = new TreeMap<>();
 
     @Column
     private String description;
+
+    private transient T toscaEntity;
     // @formatter:on
 
     /**
@@ -97,8 +102,81 @@ public class JpaToscaEntityType extends PfConcept {
      *
      * @param copyConcept the concept to copy from
      */
-    public JpaToscaEntityType(final JpaToscaEntityType copyConcept) {
+    public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) {
         super(copyConcept);
+        this.key = new PfConceptKey(copyConcept.key);
+        this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null);
+        this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null);
+        this.description = copyConcept.description;
+    }
+
+    /**
+     * Authorative constructor.
+     *
+     * @param authorativeConcept the authorative concept to copy from
+     */
+    public JpaToscaEntityType(final T authorativeConcept) {
+        this.fromAuthorative(authorativeConcept);
+    }
+
+    @Override
+    public T toAuthorative() {
+        toscaEntity.setName(getKey().getName());
+        toscaEntity.setVersion(getKey().getVersion());
+
+        if (derivedFrom != null) {
+            toscaEntity.setDerivedFrom(derivedFrom.getName());
+        }
+
+        if (description != null) {
+            toscaEntity.setDescription(description);
+        }
+
+        if (metadata != null) {
+            Map<String, String> metadataMap = new LinkedHashMap<>();
+
+            for (Entry<String, String> entry : metadata.entrySet()) {
+                metadataMap.put(entry.getKey(), entry.getValue());
+            }
+
+            toscaEntity.setMetadata(metadataMap);
+        }
+
+        return toscaEntity;
+    }
+
+    @Override
+    public void fromAuthorative(T toscaEntity) {
+        key = new PfConceptKey();
+
+        if (toscaEntity.getName() != null) {
+            key.setName(toscaEntity.getName());
+        }
+
+        if (toscaEntity.getVersion() != null) {
+            key.setVersion(toscaEntity.getVersion());
+        }
+
+        if (toscaEntity.getDerivedFrom() != null) {
+            // CHeck if the derived from field contains a name-version ID
+            if (toscaEntity.getDerivedFrom().contains(":")) {
+                derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom());
+            } else {
+                derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION);
+            }
+        }
+
+        if (toscaEntity.getDescription() != null) {
+            description = toscaEntity.getDescription();
+        }
+
+        if (toscaEntity.getMetadata() != null) {
+            metadata = new LinkedHashMap<>();
+
+            for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) {
+                metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
+            }
+        }
     }
 
     @Override
@@ -173,10 +251,11 @@ public class JpaToscaEntityType extends PfConcept {
             return 0;
         }
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
-        final JpaToscaEntityType other = (JpaToscaEntityType) otherConcept;
+        @SuppressWarnings("unchecked")
+        final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept;
         if (!key.equals(other.key)) {
             return key.compareTo(other.key);
         }
@@ -193,26 +272,4 @@ public class JpaToscaEntityType extends PfConcept {
 
         return ObjectUtils.compare(description, other.description);
     }
-
-    @Override
-    public PfConcept copyTo(@NonNull PfConcept target) {
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfConcept.class);
-
-        final JpaToscaEntityType copy = ((JpaToscaEntityType) copyObject);
-        copy.setKey(new PfConceptKey(key));
-        copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
-
-        if (metadata != null) {
-            final Map<String, String> newMatadata = new TreeMap<>();
-            for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
-                newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
-            }
-            copy.setMetadata(newMatadata);
-        }
-
-        copy.setDescription(description);
-
-        return copy;
-    }
 }