Moti Asayag has uploaded a new change for review.

Change subject: core: Validate host interface labels
......................................................................

core: Validate host interface labels

The host nics labels is a set that each of its elements
should comply to the network labels format.

Change-Id: I37f035816617322c06a7851262177c5f43268081
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/network/VdsNetworkInterface.java
A 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/NetworkLabelFormatConstraint.java
A 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/annotation/ValidNetworkLabelFormat.java
A 
backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/NetworkLabelFormatValidatorTest.java
4 files changed, 125 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/55/22655/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/network/VdsNetworkInterface.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/network/VdsNetworkInterface.java
index ce25e40..be94767 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/network/VdsNetworkInterface.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/network/VdsNetworkInterface.java
@@ -11,6 +11,7 @@
 import org.ovirt.engine.core.common.utils.ObjectUtils;
 import org.ovirt.engine.core.common.utils.ValidationUtils;
 import 
org.ovirt.engine.core.common.validation.annotation.ValidNetworkConfiguration;
+import 
org.ovirt.engine.core.common.validation.annotation.ValidNetworkLabelFormat;
 import org.ovirt.engine.core.compat.Guid;
 
 /**
@@ -42,6 +43,8 @@
     private int mtu;
     private boolean bridged;
     private NetworkImplementationDetails networkImplementationDetails;
+
+    @ValidNetworkLabelFormat(message = "NETWORK_LABEL_FORMAT_INVALID")
     private Set<String> labels;
 
     public VdsNetworkInterface() {
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/NetworkLabelFormatConstraint.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/NetworkLabelFormatConstraint.java
new file mode 100644
index 0000000..a4cce0a
--- /dev/null
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/NetworkLabelFormatConstraint.java
@@ -0,0 +1,33 @@
+package org.ovirt.engine.core.common.validation;
+
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import org.ovirt.engine.core.common.utils.ValidationUtils;
+import 
org.ovirt.engine.core.common.validation.annotation.ValidNetworkLabelFormat;
+
+public class NetworkLabelFormatConstraint implements 
ConstraintValidator<ValidNetworkLabelFormat, Set<String>> {
+
+    @Override
+    public boolean isValid(Set<String> labels, ConstraintValidatorContext 
context) {
+        if (labels == null) {
+            return true;
+        }
+
+        for (String label : labels) {
+            if (!Pattern.matches(ValidationUtils.NO_SPECIAL_CHARACTERS, 
label)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void initialize(ValidNetworkLabelFormat constraintAnnotation) {
+        // Unimplemented method, required for interface ConstraintValidator
+    }
+}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/annotation/ValidNetworkLabelFormat.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/annotation/ValidNetworkLabelFormat.java
new file mode 100644
index 0000000..f4ec8bd
--- /dev/null
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/annotation/ValidNetworkLabelFormat.java
@@ -0,0 +1,28 @@
+package org.ovirt.engine.core.common.validation.annotation;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import org.ovirt.engine.core.common.validation.NetworkLabelFormatConstraint;
+
+/**
+ * The annotated element must not contain two interfaces with the same static 
IP
+ */
+@Target({ METHOD, FIELD, PARAMETER })
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = NetworkLabelFormatConstraint.class)
+public @interface ValidNetworkLabelFormat {
+    String message() default "";
+
+    Class<?>[] groups() default {};
+
+    Class<? extends Payload>[] payload() default {};
+}
diff --git 
a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/NetworkLabelFormatValidatorTest.java
 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/NetworkLabelFormatValidatorTest.java
new file mode 100644
index 0000000..acea43f
--- /dev/null
+++ 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/NetworkLabelFormatValidatorTest.java
@@ -0,0 +1,61 @@
+package org.ovirt.engine.core.common.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import 
org.ovirt.engine.core.common.validation.annotation.ValidNetworkLabelFormat;
+
+@RunWith(Parameterized.class)
+public class NetworkLabelFormatValidatorTest {
+
+    private Validator validator;
+    private boolean expectedResult;
+    private Set<String> labels;
+
+    public NetworkLabelFormatValidatorTest(Set<String> labels, boolean 
expectedResult) {
+        this.labels = labels;
+        this.expectedResult = expectedResult;
+        validator = ValidationUtils.getValidator();
+    }
+
+    @Test
+    public void checkNetworkLabelFormat() {
+        Set<ConstraintViolation<NetworkLabelContainer>> validate =
+                validator.validate(new NetworkLabelContainer(labels));
+        assertEquals(expectedResult, validate.isEmpty());
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> ipAddressParams() {
+        return Arrays.asList(new Object[][] {
+                { new HashSet<String>(), true },
+                { null, true },
+                { new HashSet<String>(Arrays.asList("abc")), true },
+                { new HashSet<String>(Arrays.asList("abc", "xyz")), true },
+                { new HashSet<String>(Arrays.asList("abc-_sc")), true },
+                { new HashSet<String>(Arrays.asList("")), false },
+                { new HashSet<String>(Arrays.asList(" ")), false },
+                { new HashSet<String>(Arrays.asList("abc*")), false },
+                { new HashSet<String>(Arrays.asList("aaa", "abc*")), false }
+        });
+    }
+
+    private class NetworkLabelContainer {
+        @ValidNetworkLabelFormat
+        private Set<String> labels;
+
+        public NetworkLabelContainer(Set<String> labels) {
+            this.labels = labels;
+        }
+    }
+}


-- 
To view, visit http://gerrit.ovirt.org/22655
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I37f035816617322c06a7851262177c5f43268081
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Moti Asayag <masa...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to