JPA concepts for TOSCA
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataType.java
index e959007..58ac9e3 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Model
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * 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.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;
@@ -45,9 +50,9 @@ 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.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.
@@ -67,6 +72,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
     private List<JpaToscaConstraint> constraints;
 
     @ElementCollection
+    @Lob
     private Map<String, JpaToscaProperty> properties;
 
     /**
@@ -112,25 +118,8 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
         super.setToscaEntity(toscaDataType);
         super.toAuthorative();
 
-        if (constraints != null) {
-            List<ToscaConstraint> toscaConstraints = new ArrayList<>();
-
-            for (JpaToscaConstraint constraint : constraints) {
-                toscaConstraints.add(constraint.toAuthorative());
-            }
-
-            toscaDataType.setConstraints(toscaConstraints);
-        }
-
-        if (properties != null) {
-            Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
-
-            for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
-                propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
-            }
-
-            toscaDataType.setProperties(propertyMap);
-        }
+        toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
+        toscaDataType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
 
         return toscaDataType;
     }
@@ -139,13 +128,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
     public void fromAuthorative(final ToscaDataType toscaDataType) {
         super.fromAuthorative(toscaDataType);
 
-        if (toscaDataType.getConstraints() != null) {
-            constraints = new ArrayList<>();
-
-            for (ToscaConstraint toscaConstraint: toscaDataType.getConstraints()) {
-                constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
-            }
-        }
+        constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
 
         if (toscaDataType.getProperties() != null) {
             properties = new LinkedHashMap<>();
@@ -186,7 +169,7 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
         PfValidationResult result = super.validate(resultIn);
 
         if (constraints != null) {
-            result = validateConstraints(result);
+            validateConstraints(result);
         }
 
         if (properties != null) {
@@ -199,25 +182,21 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
     /**
      * Validate the constraints.
      *
-     * @param result The result of validations up to now
-     * @return the validation result
+     * @param result where to put the validation results
      */
-    private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
+    private void validateConstraints(@NonNull final PfValidationResult result) {
         for (JpaToscaConstraint constraint : constraints) {
             if (constraint == null) {
                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
                         "data type constraint may not be null "));
             }
         }
-        return result;
     }
 
     /**
      * Validate the properties.
      *
-     * @param result The result of validations up to now
+     * @param resultIn The result of validations up to now
      * @return the validation result
      */
     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
@@ -247,20 +226,41 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
         }
 
         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
-        if (!super.equals(other)) {
-            return super.compareTo(other);
-        }
-
-        int result = PfUtils.compareObjects(constraints, other.constraints);
+        int result = super.compareTo(other);
         if (result != 0) {
             return result;
         }
 
-        result = PfUtils.compareObjects(properties, other.properties);
+        result = PfUtils.compareCollections(constraints, other.constraints);
         if (result != 0) {
             return result;
         }
 
-        return 0;
+        return PfUtils.compareMaps(properties, other.properties);
+    }
+
+    /**
+     * 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();
+        }
+
+        Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
+
+        for (JpaToscaProperty property : properties.values()) {
+            referencedDataTypes.add(property.getType());
+
+            if (property.getEntrySchema() != null) {
+                referencedDataTypes.add(property.getEntrySchema().getType());
+            }
+        }
+
+        referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
+
+        return referencedDataTypes;
     }
 }