Implement validation and hierarchical get
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataType.java
index cf1150a..a44d765 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 java.util.ArrayList;
+import java.util.Collection;
 import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 
 import javax.persistence.ElementCollection;
 import javax.persistence.Entity;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
+import javax.persistence.Lob;
 import javax.persistence.Table;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 
-import org.onap.policy.common.utils.validation.Assertions;
+import org.apache.commons.collections4.CollectionUtils;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfConceptKey;
@@ -52,6 +56,7 @@ 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.ToscaDataType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
 
 /**
  * Class to represent custom data type in TOSCA definition.
@@ -68,10 +73,11 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
     private static final long serialVersionUID = -3922690413436539164L;
 
     @ElementCollection
-    private List<JpaToscaConstraint> constraints;
+    private List<JpaToscaConstraint> constraints = new ArrayList<>();
 
     @ElementCollection
-    private Map<String, JpaToscaProperty> properties;
+    @Lob
+    private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
 
     /**
      * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
@@ -96,6 +102,9 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
      */
     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
         super(copyConcept);
+        // Constraints are immutable
+        this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
+        this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
     }
 
     /**
@@ -143,7 +152,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
         if (toscaDataType.getConstraints() != null) {
             constraints = new ArrayList<>();
 
-            for (ToscaConstraint toscaConstraint: toscaDataType.getConstraints()) {
+            for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) {
                 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
             }
         }
@@ -244,7 +253,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
             return 0;
         }
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
@@ -265,34 +274,28 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
         return 0;
     }
 
-    @Override
-    public PfConcept copyTo(@NonNull PfConcept target) {
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfConcept.class);
-
-        final JpaToscaDataType copy = ((JpaToscaDataType) copyObject);
-        super.copyTo(target);
-
-        if (constraints == null) {
-            copy.setConstraints(null);
-        } else {
-            final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
-            for (final JpaToscaConstraint constraint : constraints) {
-                newConstraints.add(constraint); // Constraints are immutable
-            }
-            copy.setConstraints(newConstraints);
+    /**
+     * Get the data types referenced in a data type.
+     *
+     * @return the data types referenced in a data type
+     */
+    public Collection<PfConceptKey> getReferencedDataTypes() {
+        if (properties == null) {
+            return CollectionUtils.emptyCollection();
         }
 
-        if (properties == null) {
-            copy.setProperties(null);
-        } else {
-            final Map<String, JpaToscaProperty> newProperties = new LinkedHashMap<>();
-            for (final Entry<String, JpaToscaProperty> propertyEntry : properties.entrySet()) {
-                newProperties.put(propertyEntry.getKey(), new JpaToscaProperty(propertyEntry.getValue()));
+        Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
+
+        for (JpaToscaProperty property : properties.values()) {
+            referencedDataTypes.add(property.getType());
+
+            if (property.getEntrySchema() != null) {
+                referencedDataTypes.add(property.getEntrySchema().getType());
             }
-            copy.setProperties(newProperties);
         }
 
-        return copy;
+        referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
+
+        return referencedDataTypes;
     }
 }