[ONOS-6661] MacAddress validation in not valid
Change-Id: Ib4e6a59ca4e158b79376b7fc0a671b7f8c33ba9a
diff --git a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
index 1027708..7908d71 100644
--- a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
+++ b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
@@ -18,12 +18,15 @@
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* The class representing MAC address.
*/
public class MacAddress {
+ private static final Pattern MAC_PATTERN = Pattern.compile("^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$");
/**
* First MAC address in ONOS OUI range.
*/
@@ -77,13 +80,12 @@
* @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
*/
public static MacAddress valueOf(final String address) {
- final String[] elements = address.split(":");
- if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
+ if (!isValid(address)) {
throw new IllegalArgumentException(
"Specified MAC Address must contain 12 hex digits"
+ " separated pairwise by :'s.");
}
-
+ final String[] elements = address.split(":");
final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
final String element = elements[i];
@@ -273,4 +275,9 @@
}
return builder.toString();
}
+
+ private static boolean isValid(final String mac) {
+ Matcher matcher = MAC_PATTERN.matcher(mac);
+ return matcher.matches();
+ }
}