Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaCapabilityAssignment.java
index 6394517..7fb741d 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2020 Nordix Foundation.
+ * Copyright (C) 2020-2021, 2023 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021 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.
 
 package org.onap.policy.models.tosca.simple.concepts;
 
+import jakarta.persistence.ElementCollection;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.Lob;
+import jakarta.persistence.Table;
+import java.io.Serial;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
-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.parameters.annotations.NotNull;
 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
-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.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.base.validation.annotations.PfMin;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
 
 /**
@@ -52,9 +51,9 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignme
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = false)
-public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabilityAssignment>
-        implements PfAuthorative<ToscaCapabilityAssignment> {
+public class JpaToscaCapabilityAssignment extends JpaToscaWithTypeAndStringProperties<ToscaCapabilityAssignment> {
 
+    @Serial
     private static final long serialVersionUID = 1675770231921107988L;
 
     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
@@ -64,14 +63,10 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
 
     @ElementCollection
     @Lob
-    private Map<String, String> properties;
+    private Map<@NotNull String, @NotNull String> attributes;
 
     @ElementCollection
-    @Lob
-    private Map<String, String> attributes;
-
-    @ElementCollection
-    private List<Integer> occurrences;
+    private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
 
     /**
      * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
@@ -96,7 +91,6 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
      */
     public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
         super(copyConcept);
-        this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
         this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
         this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
     }
@@ -107,19 +101,15 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
      * @param authorativeConcept the authorative concept to copy from
      */
     public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
-        super(new PfConceptKey());
-        this.fromAuthorative(authorativeConcept);
+        super(authorativeConcept);
     }
 
     @Override
     public ToscaCapabilityAssignment toAuthorative() {
-        ToscaCapabilityAssignment toscaCapabilityAssignment = new ToscaCapabilityAssignment();
+        var toscaCapabilityAssignment = new ToscaCapabilityAssignment();
         super.setToscaEntity(toscaCapabilityAssignment);
         super.toAuthorative();
 
-        toscaCapabilityAssignment.setProperties(
-                PfUtils.mapMap(properties, property -> YAML_JSON_TRANSLATOR.fromYaml(property, Object.class)));
-
         toscaCapabilityAssignment.setAttributes(
                 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
 
@@ -138,8 +128,6 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
     public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
         super.fromAuthorative(toscaCapabilityAssignment);
 
-
-        properties = PfUtils.mapMap(toscaCapabilityAssignment.getProperties(), YAML_JSON_TRANSLATOR::toYaml);
         attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
 
         occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
@@ -152,114 +140,34 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
     }
 
     @Override
-    public void clean() {
-        super.clean();
-
-        properties = PfUtils.mapMap(properties, String::trim);
-        attributes = PfUtils.mapMap(attributes, String::trim);
+    protected Object deserializePropertyValue(String propValue) {
+        return YAML_JSON_TRANSLATOR.fromYaml(propValue, Object.class);
     }
 
     @Override
-    public PfValidationResult validate(final PfValidationResult resultIn) {
-        PfValidationResult result = super.validate(resultIn);
-
-        if (properties != null) {
-            result.append(validateProperties(new PfValidationResult()));
-        }
-
-        if (attributes != null) {
-            result.append(validateAttributes(new PfValidationResult()));
-        }
-
-        if (occurrences != null) {
-            result.append(validateOccurrences(new PfValidationResult()));
-        }
-
-        return result;
+    protected String serializePropertyValue(Object propValue) {
+        return YAML_JSON_TRANSLATOR.toYaml(propValue);
     }
 
-    /**
-     * Validate the properties.
-     *
-     * @param resultIn The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateProperties(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (Entry<String, String> propertyEntry : properties.entrySet()) {
-            if (propertyEntry.getValue() == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "capability assignment property " + propertyEntry.getKey() + " value may not be null"));
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Validate the attributes.
-     *
-     * @param resultIn The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateAttributes(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (Entry<String, String> attributeEntry : attributes.entrySet()) {
-            if (attributeEntry.getValue() == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "capability assignment attribute " + attributeEntry.getKey() + " value may not be null"));
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Validate the occurrences.
-     *
-     * @param resultIn The result of validations up to now
-     * @return the validation result
-     */
-    private PfValidationResult validateOccurrences(final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        for (Integer occurrence : occurrences) {
-            if (occurrence == null) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "capability assignment occurrence value may not be null "));
-            } else if (occurrence < 0 && !occurrence.equals(JPA_UNBOUNDED_VALUE)) {
-                result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "capability assignment occurrence value may not be negative"));
-            }
-        }
+    @Override
+    public void clean() {
+        super.clean();
 
-        return result;
+        attributes = PfUtils.mapMap(attributes, String::trim);
     }
 
     @Override
     public int compareTo(final PfConcept otherConcept) {
-        if (otherConcept == null) {
-            return -1;
-        }
-
         if (this == otherConcept) {
             return 0;
         }
 
-        if (getClass() != otherConcept.getClass()) {
-            return getClass().getName().compareTo(otherConcept.getClass().getName());
-        }
-
-        final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
-        int result = super.compareTo(other);
+        int result = super.compareTo(otherConcept);
         if (result != 0) {
             return result;
         }
 
-        result = PfUtils.compareMaps(properties, other.properties);
-        if (result != 0) {
-            return result;
-        }
+        final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
 
         result = PfUtils.compareMaps(attributes, other.attributes);
         if (result != 0) {