Use annotations to do validation 90/116490/4
authorJim Hahn <jrh3@att.com>
Sat, 19 Dec 2020 00:01:52 +0000 (19:01 -0500)
committerJim Hahn <jrh3@att.com>
Mon, 21 Dec 2020 15:38:00 +0000 (10:38 -0500)
Modified models to use annotations instead of function calls for most
field validations.  Created a few new validation annotations for use
within models.
Per review comments:
- renamed Key to VerifyKey
- enhanced VerifyKey to imply "@Valid", unless disabled

Issue-ID: POLICY-2648
Change-Id: I2b53f218b0a2ab1ed1f5e278816a3509f1408972
Signed-off-by: Jim Hahn <jrh3@att.com>
43 files changed:
models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java
models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java
models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java
models-base/src/main/java/org/onap/policy/models/base/PfModel.java
models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java
models-base/src/main/java/org/onap/policy/models/base/PfSearchableKey.java
models-base/src/main/java/org/onap/policy/models/base/PfTimestampKey.java
models-base/src/main/java/org/onap/policy/models/base/PfValidator.java [new file with mode: 0644]
models-base/src/main/java/org/onap/policy/models/base/Validated.java
models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfEntries.java [new file with mode: 0644]
models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfItems.java [new file with mode: 0644]
models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java [new file with mode: 0644]
models-base/src/main/java/org/onap/policy/models/base/validation/annotations/VerifyKey.java [new file with mode: 0644]
models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java
models-base/src/test/java/org/onap/policy/models/base/ValidatedTest.java
models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java
models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java
models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java
models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java
models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java
models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java
models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityAssignment.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaCapabilityType.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilter.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaParameter.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRelationshipType.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaRequirement.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaSchemaDefinition.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeInterval.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java

index 484632f..4c2cf0c 100644 (file)
@@ -47,6 +47,8 @@ import lombok.NonNull;
 import org.apache.commons.lang3.StringUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 // @formatter:off
 /**
@@ -73,6 +75,8 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
     private static final Pattern KEY_ID_PATTERN = Pattern.compile(PfKey.KEY_ID_REGEXP);
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfConceptKey key;
 
     @ManyToMany(cascade = CascadeType.ALL)
@@ -243,9 +247,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
 
     @Override
     public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
+        BeanValidationResult result = new PfValidator().validateTop(fieldName, this);
         result.addResult(validateConceptMap());
 
         return result;
index 5e3295e..d200a69 100644 (file)
@@ -26,6 +26,7 @@ import javax.persistence.Embeddable;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NonNull;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.common.utils.validation.Assertions;
 
 /**
@@ -43,9 +44,11 @@ public class PfConceptKey extends PfKeyImpl {
     private static final long serialVersionUID = 8932717618579392561L;
 
     @Column(name = NAME_TOKEN, length = 120)
+    @Pattern(regexp = NAME_REGEXP)
     private String name;
 
     @Column(name = VERSION_TOKEN, length = 20)
+    @Pattern(regexp = VERSION_REGEXP)
     private String version;
 
     /**
index 3cbef75..bfeb870 100644 (file)
@@ -26,8 +26,6 @@ import java.util.List;
 import lombok.Getter;
 import lombok.NonNull;
 import lombok.ToString;
-import org.onap.policy.common.parameters.BeanValidationResult;
-import org.onap.policy.common.parameters.ValidationResult;
 import org.onap.policy.common.utils.validation.Assertions;
 
 /**
@@ -241,16 +239,6 @@ public abstract class PfKeyImpl extends PfKey {
         }
     }
 
-    @Override
-    public ValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateRegex(NAME_TOKEN, getName(), getNameRegEx()));
-        result.addResult(validateRegex(VERSION_TOKEN, getVersion(), getVersionRegEx()));
-
-        return result;
-    }
-
     @Override
     public void clean() {
         setName(getName());
index 616284d..b753bb1 100644 (file)
@@ -23,11 +23,12 @@ package org.onap.policy.models.base;
 
 import java.util.List;
 import lombok.EqualsAndHashCode;
+import lombok.Getter;
 import lombok.NonNull;
 import lombok.ToString;
-import org.onap.policy.common.parameters.BeanValidationResult;
-import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.common.utils.validation.Assertions;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 /**
  * This class records a usage of a key in the system. When the list of keys being used by a concept
@@ -42,6 +43,9 @@ import org.onap.policy.common.utils.validation.Assertions;
 public class PfKeyUse extends PfKey {
     private static final long serialVersionUID = 2007147220109881705L;
 
+    @VerifyKey
+    @NotNull
+    @Getter
     private PfKey usedKey;
 
     /**
@@ -134,13 +138,6 @@ public class PfKeyUse extends PfKey {
         usedKey.clean();
     }
 
-    @Override
-    public ValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-        result.addResult(validateKeyNotNull("usedKey", usedKey));
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherObj) {
         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
index c878747..82b8f93 100644 (file)
@@ -35,7 +35,9 @@ import lombok.NonNull;
 import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.common.parameters.ObjectValidationResult;
 import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.common.utils.validation.Assertions;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 /**
  * This class is the base class for all models in the Policy Framework. All model classes inherit
@@ -63,6 +65,8 @@ public abstract class PfModel extends PfConcept {
     private static final long serialVersionUID = -771659065637205430L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfConceptKey key;
 
     /**
@@ -113,9 +117,7 @@ public abstract class PfModel extends PfConcept {
 
     @Override
     public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
+        BeanValidationResult result = new PfValidator().validateTop(fieldName, this);
 
         // Key consistency check
         final Set<PfConceptKey> artifactKeySet = new TreeSet<>();
index 83403ac..9a98648 100644 (file)
@@ -28,8 +28,8 @@ import javax.persistence.Embeddable;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
-import org.onap.policy.common.parameters.BeanValidationResult;
-import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.common.utils.validation.Assertions;
 
 /**
@@ -73,15 +73,23 @@ public class PfReferenceKey extends PfKey {
     private static final int LOCAL_NAME_FIELD = 3;
 
     @Column(name = PARENT_KEY_NAME, length = 120)
+    @NotNull
+    @Pattern(regexp = NAME_REGEXP)
     private String parentKeyName;
 
     @Column(name = PARENT_KEY_VERSION, length = 15)
+    @NotNull
+    @Pattern(regexp = VERSION_REGEXP)
     private String parentKeyVersion;
 
     @Column(name = PARENT_LOCAL_NAME, length = 120)
+    @NotNull
+    @Pattern(regexp = LOCAL_NAME_REGEXP)
     private String parentLocalName;
 
     @Column(name = LOCAL_NAME, length = 120)
+    @NotNull
+    @Pattern(regexp = LOCAL_NAME_REGEXP)
     private String localName;
 
     /**
@@ -339,18 +347,6 @@ public class PfReferenceKey extends PfKey {
         return this.getParentConceptKey().isNewerThan(otherReferenceKey.getParentConceptKey());
     }
 
-    @Override
-    public ValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateRegex(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP));
-        result.addResult(validateRegex(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP));
-        result.addResult(validateRegex(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP));
-        result.addResult(validateRegex(LOCAL_NAME, localName, LOCAL_NAME_REGEXP));
-
-        return result;
-    }
-
     @Override
     public void clean() {
         parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP);
index bed1a35..3ff4160 100644 (file)
@@ -26,6 +26,7 @@ import javax.persistence.Embeddable;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NonNull;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.common.utils.validation.Assertions;
 
 /**
@@ -42,9 +43,11 @@ public class PfSearchableKey extends PfKeyImpl {
     public static final String WILDCARD_NAME_REGEXP = "^[A-Za-z0-9\\-_\\.]+(?:\\.\\*)?$";
 
     @Column(name = NAME_TOKEN, length = 120)
+    @Pattern(regexp = NAME_REGEXP)
     private String name;
 
     @Column(name = VERSION_TOKEN, length = 20)
+    @Pattern(regexp = VERSION_REGEXP)
     private String version;
 
     /**
index 5be9b5f..d12466c 100644 (file)
@@ -29,6 +29,7 @@ import javax.persistence.Embeddable;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NonNull;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.common.utils.validation.Assertions;
 
 @Embeddable
@@ -40,9 +41,11 @@ public class PfTimestampKey extends PfKeyImpl {
     private static final String TIMESTAMP_TOKEN = "timeStamp";
 
     @Column(name = NAME_TOKEN, length = 120)
+    @Pattern(regexp = NAME_REGEXP)
     private String name;
 
     @Column(name = VERSION_TOKEN, length = 20)
+    @Pattern(regexp = VERSION_REGEXP)
     private String version;
 
     @Column(name = TIMESTAMP_TOKEN)
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfValidator.java b/models-base/src/main/java/org/onap/policy/models/base/PfValidator.java
new file mode 100644 (file)
index 0000000..c4e46e0
--- /dev/null
@@ -0,0 +1,160 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.base;
+
+import java.util.Map;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.BeanValidator;
+import org.onap.policy.common.parameters.EntryValidator;
+import org.onap.policy.common.parameters.ObjectValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.parameters.ValueValidator;
+import org.onap.policy.models.base.validation.annotations.PfEntries;
+import org.onap.policy.models.base.validation.annotations.PfItems;
+import org.onap.policy.models.base.validation.annotations.PfMin;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
+
+public class PfValidator extends BeanValidator {
+
+    @Override
+    protected void addValidators(ValueValidator validator) {
+        super.addValidators(validator);
+
+        validator.addAnnotation(PfItems.class, this::verCollection);
+        validator.addAnnotation(PfEntries.class, this::verMap);
+        validator.addAnnotation(VerifyKey.class, this::verKey);
+        validator.addAnnotation(PfMin.class, this::verPfMin);
+    }
+
+    /**
+     * Verifies that the value is >= the minimum value.
+     *
+     * @param result where to add the validation result
+     * @param fieldName field whose value is being verified
+     * @param annot annotation against which the value is being verified
+     * @param value value to be verified
+     * @return {@code true} if the next check should be performed, {@code false} otherwise
+     */
+    public boolean verPfMin(BeanValidationResult result, String fieldName, PfMin annot, Object value) {
+        if (!(value instanceof Number)) {
+            return true;
+        }
+
+        Number num = (Number) value;
+        if (num.longValue() == annot.allowed()) {
+            // this value is always allowed
+            return true;
+        }
+
+        return verMin(result, fieldName, annot.value(), value);
+    }
+
+    /**
+     * Validates the items in a Map.
+     *
+     * @param result where to add the validation result
+     * @param fieldName name of the field containing the collection
+     * @param annot validation annotations for individual entries
+     * @param value value to be verified
+     * @return {@code true} if the next check should be performed, {@code false} otherwise
+     */
+    public boolean verMap(BeanValidationResult result, String fieldName, PfEntries annot, Object value) {
+
+        if (!(value instanceof Map)) {
+            return true;
+        }
+
+        EntryValidator entryValidator = makeEntryValidator(annot.key(), annot.value());
+
+        return verMap(result, fieldName, entryValidator, value);
+    }
+
+    /**
+     * Invokes the value's {@link Validated#validate(String) validate()} method, if the
+     * value is of type {@link Validated}.
+     */
+    @Override
+    public boolean verCascade(BeanValidationResult result, String fieldName, Object value) {
+        if (value instanceof Validated) {
+            ValidationResult result2 = ((Validated) value).validate(fieldName);
+            result.addResult(result2);
+            return result2.isValid();
+        }
+
+        return super.verCascade(result, fieldName, value);
+    }
+
+    /**
+     * Validates a key.
+     *
+     * @param result where to add the validation result
+     * @param fieldName name of the field containing the key
+     * @param annot validation annotations for the key
+     * @param value value to be verified
+     * @return {@code true} if the next check should be performed, {@code false} otherwise
+     */
+    public boolean verKey(BeanValidationResult result, String fieldName, VerifyKey annot, Object value) {
+        if (!(value instanceof PfKey)) {
+            return true;
+        }
+
+        PfKey pfkey = (PfKey) value;
+        if (annot.keyNotNull() && pfkey.isNullKey()) {
+            result.addResult(new ObjectValidationResult(fieldName, xlate(pfkey), ValidationStatus.INVALID,
+                            Validated.IS_A_NULL_KEY));
+            return false;
+        }
+
+        if (annot.valid()) {
+            verCascade(result, fieldName, value);
+        }
+
+        if (!(pfkey instanceof PfKeyImpl)) {
+            return true;
+        }
+
+        BeanValidationResult result2 = new BeanValidationResult(fieldName, value);
+
+        PfKeyImpl keyimpl = (PfKeyImpl) pfkey;
+
+        if (annot.nameNotNull() && keyimpl.isNullName()) {
+            result2.addResult(new ObjectValidationResult("name", pfkey.getName(), ValidationStatus.INVALID,
+                            Validated.IS_NULL));
+        }
+
+        if (annot.versionNotNull() && keyimpl.isNullVersion()) {
+            result2.addResult(new ObjectValidationResult("version", pfkey.getVersion(), ValidationStatus.INVALID,
+                            Validated.IS_NULL));
+        }
+
+        if (!result2.isClean()) {
+            result.addResult(result2);
+        }
+
+        return result2.isValid();
+    }
+
+    @Override
+    public Object xlate(Object value) {
+        return (value instanceof PfKey ? ((PfKey) value).getId() : value);
+    }
+}
index b962d46..2c059ba 100644 (file)
@@ -37,7 +37,7 @@ import org.onap.policy.common.parameters.ValidationStatus;
  * Classes that can be validated. This can be used as a super class or as a stand-alone
  * utility class.
  */
-public abstract class Validated {
+public class Validated {
     public static final String IS_BLANK = "is blank";
     public static final String IS_A_NULL_KEY = "is a null key";
     public static final String IS_NULL = "is null";
@@ -48,12 +48,15 @@ public abstract class Validated {
     public static final String VALUE_TOKEN = "value";
 
     /**
-     * Validates the fields of the object.
+     * Validates the fields of the object. The default method uses a {@link PfValidator}
+     * to validate the object.
      *
      * @param fieldName name of the field containing this
      * @return the result, or {@code null}
      */
-    public abstract ValidationResult validate(String fieldName);
+    public BeanValidationResult validate(@NonNull String fieldName) {
+        return new PfValidator().validateTop(fieldName, this);
+    }
 
     /**
      * Adds a result indicating that a value is invalid.
diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfEntries.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfEntries.java
new file mode 100644 (file)
index 0000000..f372600
--- /dev/null
@@ -0,0 +1,45 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.base.validation.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Validations on entries within a Map.
+ */
+@Retention(RUNTIME)
+@Target(FIELD)
+public @interface PfEntries {
+
+    /**
+     * Validations to perform on each entry's key.
+     */
+    PfItems key();
+
+    /**
+     * Validations to perform on each entry's value.
+     */
+    PfItems value();
+}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfItems.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfItems.java
new file mode 100644 (file)
index 0000000..363cf6f
--- /dev/null
@@ -0,0 +1,82 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.base.validation.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import org.intellij.lang.annotations.Pattern;
+import org.onap.policy.common.parameters.annotations.Max;
+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;
+
+/**
+ * Validations on individual items, typically within a collection.
+ */
+@Retention(RUNTIME)
+@Target(FIELD)
+public @interface PfItems {
+
+    /**
+     * Validates the item is not {@code null}.
+     */
+    NotNull[] notNull() default {};
+
+    /**
+     * Validates the item is not blank.
+     */
+    NotBlank[] notBlank() default {};
+
+    /**
+     * Validates the item matches a regular expression.
+     */
+    Pattern[] pattern() default {};
+
+    /**
+     * Validates the item is not greater than a certain value.
+     */
+    Max[] max() default {};
+
+    /**
+     * Validates the item is not less than a certain value.
+     */
+    Min[] min() default {};
+
+    /**
+     * Validates the item is not less than a certain value.
+     */
+    PfMin[] pfMin() default {};
+
+    /**
+     * Validates the item is valid, using a {@link BeanValidator}.
+     */
+    Valid[] valid() default {};
+
+    /**
+     * Validates a key.
+     */
+    VerifyKey[] key() default {};
+
+}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/PfMin.java
new file mode 100644 (file)
index 0000000..71f0018
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.base.validation.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Same as the "Min" annotation, but allows an extra value that is not in the range.
+ */
+@Retention(RUNTIME)
+@Target(FIELD)
+public @interface PfMin {
+
+    /**
+     * The minimum value allowed.
+     *
+     * @return the minimum value allowed
+     */
+    long value();
+
+    /**
+     * Allowed value.
+     */
+    long allowed();
+}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/VerifyKey.java b/models-base/src/main/java/org/onap/policy/models/base/validation/annotations/VerifyKey.java
new file mode 100644 (file)
index 0000000..cc8c882
--- /dev/null
@@ -0,0 +1,57 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.base.validation.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Validates a key.
+ */
+@Retention(RUNTIME)
+@Target(FIELD)
+public @interface VerifyKey {
+
+    /**
+     * Validates that key.isNullKey() is {@code false}.
+     */
+    boolean keyNotNull() default true;
+
+    /**
+     * Validates that key.isNullName() is {@code false}.
+     */
+    boolean nameNotNull() default true;
+
+    /**
+     * Validates that key.isNullVersion() is {@code false}.
+     */
+    boolean versionNotNull() default false;
+
+    /**
+     * Invokes key.validate(), avoiding the need to include the "Valid" annotation. Note:
+     * if this is {@code true}, then the "Valid" annotation should not be specified, as
+     * that would result in duplicate validation checks.
+     */
+    boolean valid() default true;
+}
index 8d6b1b9..dc69e2e 100644 (file)
@@ -37,6 +37,7 @@ import lombok.NoArgsConstructor;
 import lombok.Setter;
 import org.junit.Test;
 import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.models.base.PfKey.Compatibility;
 import org.onap.policy.models.base.testconcepts.DummyPfKey;
 
@@ -268,10 +269,13 @@ public class PfKeyImplTest {
     @Setter
     @EqualsAndHashCode(callSuper = false)
     @NoArgsConstructor
-    private static class MyKey extends PfKeyImpl {
+    public static class MyKey extends PfKeyImpl {
         private static final long serialVersionUID = 1L;
 
+        @Pattern(regexp = NAME_REGEXP)
         private String name;
+
+        @Pattern(regexp = VERSION_REGEXP)
         private String version;
 
         public MyKey(String name, String version) {
index 8534d82..dab28f6 100644 (file)
@@ -434,12 +434,14 @@ public class ValidatedTest {
         private final String text;
 
         @Override
-        public ValidationResult validate(String fieldName) {
+        public BeanValidationResult validate(String fieldName) {
             if (TEXT.equals(text)) {
                 return null;
             }
 
-            return new ObjectValidationResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME);
+            BeanValidationResult result = new BeanValidationResult(fieldName, this);
+            result.addResult(new ObjectValidationResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME));
+            return result;
         }
     }
 }
index 67a4507..548c262 100644 (file)
@@ -27,20 +27,25 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
-import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
 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.validation.annotations.VerifyKey;
 
 @Data
 @EqualsAndHashCode(callSuper = false)
 public class DummyPfConcept extends PfConcept implements PfAuthorative<DummyAuthorativeConcept> {
     private static final long serialVersionUID = 1L;
+
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfConceptKey key;
 
+    @NotBlank
     private String description;
 
 
@@ -100,16 +105,6 @@ public class DummyPfConcept extends PfConcept implements PfAuthorative<DummyAuth
         description = (description != null ? description.trim() : null);
     }
 
-    @Override
-    public ValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateNotBlank("description", description, false));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index 5a5891a..015d1a9 100644 (file)
@@ -24,7 +24,7 @@ package org.onap.policy.models.base.testconcepts;
 import java.util.Arrays;
 import java.util.List;
 import lombok.NonNull;
-import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfKey;
 
@@ -75,7 +75,7 @@ public class DummyPfKey extends PfKey {
     }
 
     @Override
-    public ValidationResult validate(@NonNull String fieldName) {
+    public BeanValidationResult validate(@NonNull String fieldName) {
         return null;
     }
 
index d231fe7..2e6a2a6 100644 (file)
@@ -36,10 +36,13 @@ import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
@@ -58,15 +61,20 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
     private static final long serialVersionUID = -357224425637789775L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @Column
+    @NotNull
     private PdpState pdpState;
 
     @Column
+    @NotNull
     private PdpHealthStatus healthy;
 
     @Column
+    @NotBlank
     private String message;
 
     /**
@@ -161,19 +169,14 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
 
     @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()));
 
         if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
             addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
         }
 
-        result.addResult(validateNotNull("pdpState", pdpState));
-        result.addResult(validateNotNull("healthy", healthy));
-        result.addResult(validateNotBlank("message", message, false));
-
         return result;
     }
 
index 66d36d8..5e078bc 100644 (file)
@@ -44,14 +44,18 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfReferenceKey;
 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.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
 import org.onap.policy.models.pdp.enums.PdpState;
@@ -70,15 +74,21 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
     private static final long serialVersionUID = -357224425637789775L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfConceptKey key;
 
     @Column
+    @NotBlank
     private String description;
 
     @Column
+    @NotNull
     private PdpState pdpGroupState;
 
     @ElementCollection
+    @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
+                value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
     private Map<String, String> properties;
 
     // @formatter:off
@@ -90,6 +100,8 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
             @JoinColumn(name = "pdpGroupLocalName",        referencedColumnName = "localName")
         })
     // @formatter:on
+    @NotNull
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<JpaPdpSubGroup> pdpSubGroups;
 
     /**
@@ -214,22 +226,6 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
         }
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateNotBlank("description", description, false));
-        result.addResult(validateNotNull("pdpGroupState", pdpGroupState));
-
-        validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotBlank);
-
-        result.addResult(validateNotNull("pdpSubGroups", pdpSubGroups));
-        validateList(result, "pdpSubGroups", pdpSubGroups, Validated::validateNotNull);
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index 49163ee..90cf4e4 100644 (file)
@@ -38,12 +38,13 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.builder.CompareToBuilder;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfTimestampKey;
 import org.onap.policy.models.base.PfUtils;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
 import org.onap.policy.models.pdp.concepts.PdpStatistics;
 
@@ -63,6 +64,8 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
     private static final String NULL_NAME = "NULL";
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfTimestampKey key;
 
     @Column(length = 120)
@@ -201,15 +204,6 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
         return getKey().getKeys();
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-
-        return result;
-    }
-
     @Override
     public void clean() {
         key.clean();
index c3d172a..3474fcc 100644 (file)
@@ -44,6 +44,12 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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,7 +58,7 @@ 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;
@@ -72,21 +78,31 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
     private static final long serialVersionUID = -357224425637789775L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @ElementCollection
+    @NotNull
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<PfSearchableKey> supportedPolicyTypes;
 
     @ElementCollection
+    @NotNull
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<PfConceptKey> policies;
 
     @Column
+    @Min(0)
     private int currentInstanceCount;
 
     @Column
+    @Min(0)
     private int desiredInstanceCount;
 
     @ElementCollection
+    @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
+                value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
     private Map<String, String> properties;
 
     // @formatter:off
@@ -100,6 +116,8 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGro
             }
         )
     // formatter:on
+    @NotNull
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<JpaPdp> pdpInstances;
 
     /**
@@ -278,33 +296,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()));
 
-        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;
     }
 
index b687472..cad1357 100644 (file)
@@ -379,7 +379,7 @@ public class PdpProviderTest {
         assertThatThrownBy(() -> {
             new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
         }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount")
-                        .hasMessageContaining("is negative");
+                        .hasMessageContaining("below the minimum value");
         existingSubGroup.setDesiredInstanceCount(10);
     }
 
index 81c4642..28e4cdc 100644 (file)
@@ -34,13 +34,16 @@ 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.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.Validated;
+import org.onap.policy.models.base.validation.annotations.PfItems;
+import org.onap.policy.models.base.validation.annotations.PfMin;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
 
 /**
@@ -63,13 +66,16 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
     private Map<String, String> properties;
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
     private Map<String, String> attributes;
 
     @ElementCollection
+    @PfItems(notNull = {@NotNull}, pfMin = {@PfMin(value = 0, allowed = -1)})
     private List<Integer> occurrences;
 
     /**
@@ -158,18 +164,6 @@ public class JpaToscaCapabilityAssignment extends JpaToscaEntityType<ToscaCapabi
         attributes = PfUtils.mapMap(attributes, String::trim);
     }
 
-    @Override
-    public BeanValidationResult validate(String fieldName) {
-        BeanValidationResult result = super.validate(fieldName);
-
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-        validateMap(result, "attributes", attributes, Validated::validateEntryValueNotNull);
-
-        validateList(result, "occurrences", occurrences, validateMin(0, JPA_UNBOUNDED_VALUE, true));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index 3a4956c..40c50c0 100644 (file)
@@ -39,13 +39,16 @@ import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.collections4.CollectionUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.utils.ToscaUtils;
@@ -65,6 +68,7 @@ public class JpaToscaCapabilityType extends JpaToscaEntityType<ToscaCapabilityTy
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
     private Map<String, JpaToscaProperty> properties;
 
     /**
@@ -161,8 +165,6 @@ public class JpaToscaCapabilityType extends JpaToscaEntityType<ToscaCapabilityTy
 
         result.addResult(validateKeyVersionNotNull("key", getKey()));
 
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-
         return result;
     }
 
index d80c14a..29daa3a 100644 (file)
@@ -41,14 +41,16 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.collections4.CollectionUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.Validated;
 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;
@@ -68,10 +70,12 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
     private static final long serialVersionUID = -3922690413436539164L;
 
     @ElementCollection
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<JpaToscaConstraint> constraints;
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
     private Map<String, JpaToscaProperty> properties;
 
     /**
@@ -163,16 +167,6 @@ public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implemen
         }
     }
 
-    @Override
-    public BeanValidationResult validate(String fieldName) {
-        BeanValidationResult result = super.validate(fieldName);
-
-        validateList(result, "constraints", constraints, Validated::validateNotNull);
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index 169d7f2..de3279e 100644 (file)
@@ -35,13 +35,16 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
 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.PfUtils;
-import org.onap.policy.models.base.Validated;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
 /**
@@ -54,6 +57,8 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme
     private static final long serialVersionUID = -1330661834220739393L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfConceptKey key;
 
     // @formatter:off
@@ -64,12 +69,16 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme
         @AttributeOverride(name = "version",
                            column = @Column(name = "derived_from_version"))
         })
+    @VerifyKey
     private PfConceptKey derivedFrom;
 
     @ElementCollection
+    @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
+                value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
     private Map<String, String> metadata;
 
     @Column
+    @NotBlank
     private String description;
 
     private transient T toscaEntity;
@@ -185,22 +194,6 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme
         description = (description != null ? description.trim() : null);
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-
-        if (derivedFrom != null) {
-            result.addResult(validateKeyNotNull("derivedFrom", derivedFrom));
-        }
-
-        validateMap(result, "metadata", metadata, Validated::validateEntryNotBlankNotBlank);
-        result.addResult(validateNotBlank("description", description, false));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index f0d7b9c..e879a13 100644 (file)
@@ -34,11 +34,13 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
 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.PfReferenceKey;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 /**
  * Class to represent the EventFilter in TOSCA definition.
@@ -55,15 +57,21 @@ public class JpaToscaEventFilter extends PfConcept {
     private static final long serialVersionUID = 8769020537228210247L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @Column
+    @VerifyKey
+    @NotNull
     private PfConceptKey node;
 
     @Column
+    @NotBlank
     private String requirement;
 
     @Column
+    @NotBlank
     private String capability;
 
     /**
@@ -121,18 +129,6 @@ public class JpaToscaEventFilter extends PfConcept {
         capability = (capability != null ? capability.trim() : capability);
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateKeyNotNull("node", node));
-        result.addResult(validateNotBlank("requirement", requirement, false));
-        result.addResult(validateNotBlank("capability", capability, false));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index c86c34b..cd6656b 100644 (file)
@@ -32,7 +32,7 @@ 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.Valid;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
@@ -55,6 +55,7 @@ public class JpaToscaModel extends PfModel {
     private static final long serialVersionUID = 8800599637708309945L;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
+    @Valid
     private JpaToscaServiceTemplates serviceTemplates;
 
     /**
@@ -116,15 +117,6 @@ public class JpaToscaModel extends PfModel {
         serviceTemplates.clean();
     }
 
-    @Override
-    public BeanValidationResult validate(String fieldName) {
-        BeanValidationResult result = super.validate(fieldName);
-
-        result.addResult(serviceTemplates.validate("serviceTemplates"));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index 05aefa7..c2dcab1 100644 (file)
@@ -42,7 +42,11 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.models.base.PfAuthorative;
@@ -51,7 +55,6 @@ import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 
@@ -70,21 +73,26 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
     private static final StandardCoder STANDARD_CODER = new StandardCoder();
 
     @Column
+    @NotNull
+    @NotBlank
     private String type;
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
     private Map<String, String> properties;
 
     // formatter:off
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
         @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
+    @Valid
     private JpaToscaRequirements requirements;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
     @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
         @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")})
+    @Valid
     private JpaToscaCapabilityAssignments capabilities;
     // @formatter:on
 
@@ -229,19 +237,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
         }
     }
 
-    @Override
-    public BeanValidationResult validate(String fieldName) {
-        BeanValidationResult result = super.validate(fieldName);
-
-        result.addResult(validateNotBlank("type", type, true));
-
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-        validateOptional(result, "requirements", requirements);
-        validateOptional(result, "capabilities", capabilities);
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index f39f9b9..0965d8b 100644 (file)
@@ -45,13 +45,16 @@ import lombok.NonNull;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.utils.ToscaUtils;
@@ -70,6 +73,7 @@ public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implemen
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
     private Map<String, JpaToscaProperty> properties;
 
 
@@ -78,6 +82,7 @@ public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implemen
     @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
         @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
     // @formatter:on
+    @Valid
     private JpaToscaRequirements requirements;
 
     /**
@@ -190,9 +195,6 @@ public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implemen
 
         result.addResult(validateKeyVersionNotNull("key", getKey()));
 
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-        validateOptional(result, "requirements", requirements);
-
         return result;
     }
 
index 19e0e5f..0348bb1 100644 (file)
@@ -32,13 +32,14 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.StringUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+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.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
 
 /**
@@ -53,9 +54,13 @@ public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaP
     private static final long serialVersionUID = 1675770231921107988L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @Column
+    @VerifyKey
+    @NotNull
     private PfConceptKey type;
 
     @Column
@@ -159,16 +164,6 @@ public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaP
         }
     }
 
-    @Override
-    public BeanValidationResult validate(String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateKeyNotNull("type", type));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index ae7b342..83ec403 100644 (file)
@@ -40,6 +40,11 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.models.base.PfAuthorative;
@@ -48,7 +53,7 @@ import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfModelRuntimeException;
 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.tosca.authorative.concepts.ToscaPolicy;
 
 /**
@@ -79,13 +84,17 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
         @AttributeOverride(name = "version",
                            column = @Column(name = "type_version"))
         })
+    @VerifyKey
+    @NotNull
     private PfConceptKey type;
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}), value = @Items(notNull = {@NotNull}))
     private Map<String, String> properties;
 
     @ElementCollection
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<PfConceptKey> targets;
     // @formatter:on
 
@@ -235,10 +244,6 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
         BeanValidationResult result = super.validate(fieldName);
 
         result.addResult(validateKeyVersionNotNull("key", getKey()));
-        result.addResult(validateKeyNotNull("type", type));
-
-        validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotNull);
-        validateList(result, "targets", targets, Validated::validateNotNull);
 
         return result;
     }
index 665c79d..07850f9 100644 (file)
@@ -41,13 +41,16 @@ import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.collections4.CollectionUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.utils.ToscaUtils;
@@ -69,12 +72,15 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
     private Map<String, JpaToscaProperty> properties;
 
     @ElementCollection
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<PfConceptKey> targets;
 
     @ElementCollection
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<JpaToscaTrigger> triggers;
 
     /**
@@ -192,10 +198,6 @@ public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> impl
 
         result.addResult(validateKeyVersionNotNull("key", getKey()));
 
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-        validateList(result, "targets", targets, Validated::validateNotNull);
-        validateList(result, "triggers", triggers, Validated::validateNotNull);
-
         return result;
     }
 
index a7156bb..711fcd2 100644 (file)
@@ -38,7 +38,10 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Items;
+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.common.utils.coder.YamlJsonTranslator;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
@@ -46,7 +49,7 @@ import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
 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.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
 
@@ -65,27 +68,35 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
     private static final long serialVersionUID = 1675770231921107988L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @Column
+    @VerifyKey
+    @NotNull
     private PfConceptKey type;
 
     @Column
+    @NotBlank
     private String description;
 
     @Column
     private boolean required = false;
 
     @Column(name = "default")
+    @NotBlank
     private String defaultValue;
 
     @Column
     private Status status = Status.SUPPORTED;
 
     @ElementCollection
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<JpaToscaConstraint> constraints;
 
     @Column
+    @Valid
     private JpaToscaSchemaDefinition entrySchema;
 
     @ElementCollection
@@ -239,21 +250,6 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
         metadata = PfUtils.mapMap(metadata, String::trim);
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateKeyNotNull("type", type));
-        result.addResult(validateNotBlank("description", description, false));
-        result.addResult(validateNotBlank("defaultValue", defaultValue, false));
-
-        validateList(result, "constraints", constraints, Validated::validateNotNull);
-        validateOptional(result, "entrySchema", entrySchema);
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index 4bbcea4..1bd23b1 100644 (file)
@@ -39,13 +39,16 @@ import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.collections4.CollectionUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
-import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType;
 import org.onap.policy.models.tosca.utils.ToscaUtils;
@@ -65,6 +68,7 @@ public class JpaToscaRelationshipType extends JpaToscaEntityType<ToscaRelationsh
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
     private Map<String, JpaToscaProperty> properties;
 
     /**
@@ -158,8 +162,6 @@ public class JpaToscaRelationshipType extends JpaToscaEntityType<ToscaRelationsh
 
         result.addResult(validateKeyVersionNotNull("key", getKey()));
 
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-
         return result;
     }
 
index bf1249d..b223c24 100644 (file)
@@ -37,13 +37,16 @@ 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.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.Validated;
+import org.onap.policy.models.base.validation.annotations.PfItems;
+import org.onap.policy.models.base.validation.annotations.PfMin;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
 
 /**
@@ -74,10 +77,12 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
     private String relationship;
 
     @ElementCollection
+    @PfItems(notNull = {@NotNull}, pfMin = {@PfMin(value = 0, allowed = -1)})
     private List<Integer> occurrences;
 
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}))
     private Map<String, String> properties;
 
     /**
@@ -195,16 +200,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
         properties = PfUtils.mapMap(properties, String::trim);
     }
 
-    @Override
-    public BeanValidationResult validate(String fieldName) {
-        BeanValidationResult result = super.validate(fieldName);
-
-        validateMap(result, "properties", properties, Validated::validateEntryValueNotNull);
-        validateList(result, "occurrences", occurrences, validateMin(0, JPA_UNBOUNDED_VALUE, true));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index bae3629..1651d1a 100644 (file)
@@ -33,13 +33,17 @@ import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Items;
+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.common.utils.validation.Assertions;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
 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.tosca.authorative.concepts.ToscaConstraint;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
 
@@ -58,12 +62,16 @@ public class JpaToscaSchemaDefinition extends Validated
     private static final long serialVersionUID = 3645882081163287058L;
 
     @Column
+    @VerifyKey
+    @NotNull
     private PfConceptKey type;
 
     @Column
+    @NotBlank
     private String description;
 
     @ElementCollection
+    @Items(notNull = {@NotNull}, valid = {@Valid})
     private List<JpaToscaConstraint> constraints;
 
     /**
@@ -143,18 +151,6 @@ public class JpaToscaSchemaDefinition extends Validated
         description = (description != null ? description.trim() : null);
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("type", type));
-        result.addResult(validateNotBlank("description", description, false));
-
-        validateList(result, "constraints", constraints, Validated::validateNotNull);
-
-        return result;
-    }
-
     @Override
     public int compareTo(final JpaToscaSchemaDefinition other) {
         if (other == null) {
index d4e5184..c0a5cdf 100644 (file)
@@ -42,6 +42,9 @@ import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
 import org.onap.policy.common.parameters.BeanValidationResult;
+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;
@@ -76,6 +79,8 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
     // @formatter:off
     @Column
     @SerializedName("tosca_definitions_version")
+    @NotNull
+    @NotBlank
     private String toscaDefinitionsVersion;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -86,6 +91,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
             }
         )
     @SerializedName("data_types")
+    @Valid
     private JpaToscaDataTypes dataTypes;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -96,6 +102,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
             }
         )
     @SerializedName("capability_types")
+    @Valid
     private JpaToscaCapabilityTypes capabilityTypes;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -106,6 +113,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
             }
         )
     @SerializedName("relationship_types")
+    @Valid
     private JpaToscaRelationshipTypes relationshipTypes;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -116,6 +124,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
             }
         )
     @SerializedName("node_types")
+    @Valid
     private JpaToscaNodeTypes nodeTypes;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -126,6 +135,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
             }
         )
     @SerializedName("policy_types")
+    @Valid
     private JpaToscaPolicyTypes policyTypes;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -138,6 +148,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
             }
         )
     @SerializedName("topology_template")
+    @Valid
     private JpaToscaTopologyTemplate topologyTemplate;
     // @formatter:on
 
@@ -363,15 +374,6 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
     public BeanValidationResult validate(String fieldName) {
         BeanValidationResult result = super.validate(fieldName);
 
-        result.addResult(validateNotBlank("toscaDefinitionsVersion", toscaDefinitionsVersion, true));
-
-        validateOptional(result, "dataTypes", dataTypes);
-        validateOptional(result, "capabilityTypes", capabilityTypes);
-        validateOptional(result, "relationshipTypes", relationshipTypes);
-        validateOptional(result, "nodeTypes", nodeTypes);
-        validateOptional(result, "policyTypes", policyTypes);
-        validateOptional(result, "topologyTemplate", topologyTemplate);
-
         // No point in validating cross references if the structure of the individual parts are not valid
         if (!result.isValid()) {
             return result;
index a8a21c3..6392161 100644 (file)
@@ -35,10 +35,12 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 /**
  * Class to represent the TimeInterval in TOSCA definition.
@@ -56,6 +58,8 @@ public class JpaToscaTimeInterval extends PfConcept {
     private static final long serialVersionUID = 9151467029611969980L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @SerializedName("start_time")
@@ -116,9 +120,7 @@ public class JpaToscaTimeInterval extends PfConcept {
 
     @Override
     public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
+        BeanValidationResult result = super.validate(fieldName);
 
         if (startTime == null || startTime.getTime() == 0) {
             addResult(result, "startTime", startTime, "is null or zero");
index 538680f..7f2920c 100644 (file)
@@ -43,13 +43,17 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.annotations.Entries;
+import org.onap.policy.common.parameters.annotations.Items;
+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.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
 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.tosca.authorative.concepts.ToscaNodeTemplate;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
@@ -71,14 +75,18 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
     public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @Column(name = "description")
+    @NotBlank
     private String description;
 
     // @formatter:off
     @ElementCollection
     @Lob
+    @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
     private Map<String, JpaToscaParameter> inputs;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -89,6 +97,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
         }
     )
     @SerializedName("data_types")
+    @Valid
     private JpaToscaNodeTemplates nodeTemplates;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@@ -99,6 +108,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
             }
         )
     // @formatter:on
+    @Valid
     private JpaToscaPolicies policies;
 
     /**
@@ -239,21 +249,6 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative
         }
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-        result.addResult(validateNotBlank("description", description, false));
-
-        validateMap(result, "inputs", inputs, Validated::validateEntryValueNotNull);
-
-        validateOptional(result, "nodeTemplates", nodeTemplates);
-        validateOptional(result, "policies", policies);
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         int result = compareToWithoutEntities(otherConcept);
index e307540..677354d 100644 (file)
@@ -36,10 +36,14 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-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.PfConcept;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
 /**
  * Class to represent the trigger of policy type in TOSCA definition.
@@ -56,27 +60,36 @@ public class JpaToscaTrigger extends PfConcept {
     private static final long serialVersionUID = -6515211640208986971L;
 
     @EmbeddedId
+    @VerifyKey
+    @NotNull
     private PfReferenceKey key;
 
     @Column
+    @NotBlank
     private String description;
 
     @Column
     @SerializedName("event_type")
+    @NotNull
+    @NotBlank
     private String eventType;
 
     @Column
     @SerializedName("schedule")
+    @Valid
     private JpaToscaTimeInterval schedule;
 
     @Column
     @SerializedName("target_filter")
+    @Valid
     private JpaToscaEventFilter targetFilter;
 
     @Column
+    @Valid
     private JpaToscaConstraint condition;
 
     @Column
+    @Valid
     private JpaToscaConstraint constraint;
 
     @Column
@@ -84,12 +97,16 @@ public class JpaToscaTrigger extends PfConcept {
     private Duration period;
 
     @Column
+    @Min(0)
     private int evaluations = 0;
 
     @Column
+    @NotBlank
     private String method;
 
     @Column
+    @NotNull
+    @NotBlank
     private String action;
 
     /**
@@ -173,28 +190,6 @@ public class JpaToscaTrigger extends PfConcept {
         action = action.trim();
     }
 
-    @Override
-    public BeanValidationResult validate(@NonNull String fieldName) {
-        BeanValidationResult result = new BeanValidationResult(fieldName, this);
-
-        result.addResult(validateKeyNotNull("key", key));
-
-        result.addResult(validateNotBlank("description", description, false));
-        result.addResult(validateNotBlank("eventType", eventType, true));
-
-        validateOptional(result, "schedule", schedule);
-        validateOptional(result, "targetFilter", targetFilter);
-
-        if (evaluations < 0) {
-            addResult(result, "evaluations", evaluations, "is negative");
-        }
-
-        result.addResult(validateNotBlank("method", method, false));
-        result.addResult(validateNotBlank("action", action, true));
-
-        return result;
-    }
-
     @Override
     public int compareTo(final PfConcept otherConcept) {
         if (otherConcept == null) {
index d6266b4..8f366b3 100644 (file)
@@ -141,7 +141,7 @@ public class JpaToscaServiceTemplateTest {
 
         tst.setToscaDefinitionsVersion(null);
         BeanValidationResult result = tst.validate("");
-        assertThat(result.getResult()).contains("toscaDefinitionsVersion").contains(Validated.IS_BLANK);
+        assertThat(result.getResult()).contains("toscaDefinitionsVersion").contains(Validated.IS_NULL);
 
         tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
         tst.setDataTypes(null);