Java 17 Upgrade
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpSubGroup.java
index c3d172a..0d963c8 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Model
  * ================================================================================
- * Copyright (C) 2019-2020 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.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;
@@ -52,11 +57,10 @@ 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.Validated;
+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.
@@ -69,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<PfSearchableKey> 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)
@@ -100,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.
@@ -162,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 (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
-            ToscaPolicyTypeIdentifier supportedPolicyTypeIdent = new ToscaPolicyTypeIdentifier(
+            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);
@@ -202,7 +214,7 @@ 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 PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
             }
@@ -210,7 +222,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
 
         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()));
             }
@@ -223,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);
@@ -278,33 +290,14 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
 
     @Override
     public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
+        BeanValidationResult result = super.validate(fieldName);
 
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey()));
+        validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
 
-        if (currentInstanceCount < 0) {
-            addResult(result, "currentInstanceCount", currentInstanceCount, "is negative");
-        }
-
-        if (desiredInstanceCount < 0) {
-            addResult(result, "desiredInstanceCount", desiredInstanceCount, "is negative");
-        }
-
-        validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotBlank);
-
-        if (supportedPolicyTypes == null || supportedPolicyTypes.isEmpty()) {
+        if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
             addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
-        } else {
-            validateList(result, "supportedPolicyTypes", supportedPolicyTypes, Validated::validateNotNull);
         }
 
-        result.validateNotNull("policies", policies);
-        validateList(result, "policies", policies, Validated::validateNotNull);
-
-        result.validateNotNull("pdpInstances", pdpInstances);
-        validateList(result, "pdpInstances", pdpInstances, Validated::validateNotNull);
-
         return result;
     }