Java 17 Upgrade
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpSubGroup.java
index 20e43f0..0d963c8 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-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021, 2023 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.pdp.persistence.concepts;
 
+import jakarta.persistence.CascadeType;
+import jakarta.persistence.Column;
+import jakarta.persistence.ElementCollection;
+import jakarta.persistence.EmbeddedId;
+import jakarta.persistence.Entity;
+import jakarta.persistence.FetchType;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.JoinColumn;
+import jakarta.persistence.JoinTable;
+import jakarta.persistence.OneToMany;
+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.CascadeType;
-import javax.persistence.Column;
-import javax.persistence.ElementCollection;
-import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.JoinColumn;
-import javax.persistence.JoinTable;
-import javax.persistence.OneToMany;
-import javax.persistence.Table;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
-
-import org.onap.policy.common.utils.validation.Assertions;
-import org.onap.policy.common.utils.validation.ParameterValidationUtils;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Min;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.parameters.annotations.Valid;
 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;
 import org.onap.policy.models.base.PfKeyUse;
 import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.PfSearchableKey;
 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.VerifyKey;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
 /**
  * Class to represent a PDP subgroup in the database.
@@ -73,25 +73,32 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi
 @Data
 @EqualsAndHashCode(callSuper = false)
 public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
+    @Serial
     private static final long serialVersionUID = -357224425637789775L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @ElementCollection
-    private List<PfConceptKey> supportedPolicyTypes;
+    @NotNull
+    private List<@NotNull @Valid PfSearchableKey> supportedPolicyTypes;
 
     @ElementCollection
+    @NotNull
     private List<PfConceptKey> policies;
 
     @Column
+    @Min(0)
     private int currentInstanceCount;
 
     @Column
+    @Min(0)
     private int desiredInstanceCount;
 
     @ElementCollection
-    private Map<String, String> properties;
+    private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;
 
     // @formatter:off
     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -104,7 +111,8 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
             }
         )
     // formatter:on
-    private List<JpaPdp> pdpInstances;
+    @NotNull
+    private List<@NotNull @Valid JpaPdp> pdpInstances;
 
     /**
      * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
@@ -130,7 +138,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
      * @param policies policies deployed on this PDP subgroups
      * @param pdpInstances the PDP instances on this PDP subgroups
      */
-    public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfConceptKey> supportedPolicyTypes,
+    public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
             @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
         this.key = key;
         this.supportedPolicyTypes = supportedPolicyTypes;
@@ -145,6 +153,14 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
      */
     public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
         super(copyConcept);
+        this.key = new PfReferenceKey(copyConcept.key);
+        this.supportedPolicyTypes = PfUtils.mapList(copyConcept.supportedPolicyTypes,
+                                        PfSearchableKey::new, new ArrayList<>(0));
+        this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0));
+        this.currentInstanceCount = copyConcept.currentInstanceCount;
+        this.desiredInstanceCount = copyConcept.desiredInstanceCount;
+        this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
+        this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0));
     }
 
     /**
@@ -158,20 +174,20 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
 
     @Override
     public PdpSubGroup toAuthorative() {
-        PdpSubGroup pdpSubgroup = new PdpSubGroup();
+        var pdpSubgroup = new PdpSubGroup();
 
         pdpSubgroup.setPdpType(getKey().getLocalName());
 
         pdpSubgroup.setSupportedPolicyTypes(new ArrayList<>());
-        for (PfConceptKey supportedPolicyTypeKey : supportedPolicyTypes) {
-            ToscaPolicyTypeIdentifier supportedPolicyTypeIdent = new ToscaPolicyTypeIdentifier(
+        for (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
+            var supportedPolicyTypeIdent = new ToscaConceptIdentifier(
                     supportedPolicyTypeKey.getName(), supportedPolicyTypeKey.getVersion());
             pdpSubgroup.getSupportedPolicyTypes().add(supportedPolicyTypeIdent);
         }
 
         pdpSubgroup.setPolicies(new ArrayList<>());
         for (PfConceptKey policyKey : policies) {
-            ToscaPolicyIdentifier toscaPolicyIdentifier = new ToscaPolicyIdentifier();
+            var toscaPolicyIdentifier = new ToscaConceptIdentifier();
             toscaPolicyIdentifier.setName(policyKey.getName());
             toscaPolicyIdentifier.setVersion(policyKey.getVersion());
             pdpSubgroup.getPolicies().add(toscaPolicyIdentifier);
@@ -198,15 +214,15 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
 
         this.supportedPolicyTypes = new ArrayList<>();
         if (pdpSubgroup.getSupportedPolicyTypes() != null) {
-            for (ToscaPolicyTypeIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
+            for (ToscaConceptIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
                 this.supportedPolicyTypes
-                        .add(new PfConceptKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
+                        .add(new PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
             }
         }
 
         this.policies = new ArrayList<>();
         if (pdpSubgroup.getPolicies() != null) {
-            for (ToscaPolicyIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
+            for (ToscaConceptIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
                 this.policies
                         .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
             }
@@ -219,7 +235,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
         this.pdpInstances = new ArrayList<>();
         if (pdpSubgroup.getPdpInstances() != null) {
             for (Pdp pdp : pdpSubgroup.getPdpInstances()) {
-                JpaPdp jpaPdp = new JpaPdp();
+                var jpaPdp = new JpaPdp();
                 jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId()));
                 jpaPdp.fromAuthorative(pdp);
                 this.pdpInstances.add(jpaPdp);
@@ -231,7 +247,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
     public List<PfKey> getKeys() {
         List<PfKey> keyList = getKey().getKeys();
 
-        for (PfConceptKey ptkey : supportedPolicyTypes) {
+        for (PfSearchableKey ptkey : supportedPolicyTypes) {
             keyList.add(new PfKeyUse(ptkey));
         }
 
@@ -251,7 +267,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
     public void clean() {
         key.clean();
 
-        for (PfConceptKey ptkey : supportedPolicyTypes) {
+        for (PfSearchableKey ptkey : supportedPolicyTypes) {
             ptkey.clean();
         }
 
@@ -273,80 +289,13 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
     }
 
     @Override
-    public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
-        PfValidationResult result = resultIn;
-
-        if (key.isNullKey()) {
-            result.addValidationMessage(
-                    new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
-        }
-
-        result = key.validate(result);
-
-        if (key.getParentConceptKey().isNullKey()) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "parent of key is a null key"));
-        }
-
-        if (currentInstanceCount < 0) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "the current instance count of a PDP group may not be negative"));
-        }
-
-        if (desiredInstanceCount < 0) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "the desired instance count of a PDP group may not be negative"));
-        }
-
-        if (properties != null) {
-            for (Entry<String, String> propertyEntry : properties.entrySet()) {
-                if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
-                    result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                            "a property key may not be null or blank"));
-                }
-                if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getValue())) {
-                    result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                            "a property value may not be null or blank"));
-                }
-            }
-        }
-
+    public BeanValidationResult validate(@NonNull String fieldName) {
+        BeanValidationResult result = super.validate(fieldName);
 
-        return validateSubConcepts(result);
-    }
+        validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
 
-    /**
-     * Validate collections of sub concepts.
-     *
-     * @param result the result in which to store the validation result
-     * @return the validation result including the results of this method
-     */
-    private PfValidationResult validateSubConcepts(PfValidationResult result) {
-        if (supportedPolicyTypes == null || supportedPolicyTypes.isEmpty()) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "a PDP subgroup must support at least one policy type"));
-        } else {
-            for (PfConceptKey supportedPolicyType : supportedPolicyTypes) {
-                result = supportedPolicyType.validate(result);
-            }
-        }
-
-        if (policies == null) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "a PDP subgroup must have a list of policies"));
-        } else {
-            for (PfConceptKey policyKey : policies) {
-                result = policyKey.validate(result);
-            }
-        }
-
-        if (pdpInstances == null) {
-            result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "a PDP subgroup must have a list of PDPs"));
-        } else {
-            for (JpaPdp jpaPdp : pdpInstances) {
-                result = jpaPdp.validate(result);
-            }
+        if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
+            addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
         }
 
         return result;
@@ -361,7 +310,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
             return 0;
         }
         if (getClass() != otherConcept.getClass()) {
-            return this.hashCode() - otherConcept.hashCode();
+            return getClass().getName().compareTo(otherConcept.getClass().getName());
         }
 
         final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
@@ -394,21 +343,4 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
 
         return PfUtils.compareObjects(pdpInstances, other.pdpInstances);
     }
-
-    @Override
-    public PfConcept copyTo(@NonNull final PfConcept target) {
-        Assertions.instanceOf(target, JpaPdpSubGroup.class);
-
-        final JpaPdpSubGroup copy = ((JpaPdpSubGroup) target);
-        copy.setKey(new PfReferenceKey(key));
-
-        copy.setSupportedPolicyTypes(PfUtils.mapList(supportedPolicyTypes, PfConceptKey::new));
-        copy.setPolicies(PfUtils.mapList(policies, PfConceptKey::new));
-        copy.setCurrentInstanceCount(currentInstanceCount);
-        copy.setDesiredInstanceCount(desiredInstanceCount);
-        copy.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
-        copy.setPdpInstances(PfUtils.mapList(pdpInstances, JpaPdp::new));
-
-        return copy;
-    }
 }