Refactoring SubnetConfig: 91/91/1
authorKalvin Hom <kahom@cisco.com>
Fri, 29 Mar 2013 20:40:31 +0000 (13:40 -0700)
committerKalvin Hom <kahom@cisco.com>
Fri, 29 Mar 2013 20:40:31 +0000 (13:40 -0700)
-Use NetUtils IP validation methods
instead of own regex and duplicate method
-Remove unused method getFieldsNames()

Change-Id: I6efc16a0fad39c622411e93cf5a3e4d377678de7
Signed-off-by: Kalvin Hom <kahom@cisco.com>
opendaylight/switchmanager/src/main/java/org/opendaylight/controller/switchmanager/SubnetConfig.java

index 1e974af3f040e1ec4cd96fe60180d99cf661ae04..3624dd775477c307150990e8ad1117ba2bf7dadb 100644 (file)
@@ -11,7 +11,6 @@ package org.opendaylight.controller.switchmanager;
 
 import java.io.Serializable;
 import java.lang.reflect.Field;
 
 import java.io.Serializable;
 import java.lang.reflect.Field;
-import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
@@ -26,6 +25,7 @@ import javax.xml.bind.annotation.XmlAttribute;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.utils.GUIField;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.utils.GUIField;
+import org.opendaylight.controller.sal.utils.NetUtils;
 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
 
 /**
 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
 
 /**
@@ -36,10 +36,6 @@ import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
 public class SubnetConfig implements Serializable {
     //static fields are by default excluded by Gson parser
     private static final long serialVersionUID = 1L;
 public class SubnetConfig implements Serializable {
     //static fields are by default excluded by Gson parser
     private static final long serialVersionUID = 1L;
-    private static final String regexIP = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
-            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
-            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
-            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])[/](\\d|[12]\\d|3[0-2])$";
     private static final String prettyFields[] = { GUIField.NAME.toString(),
             GUIField.GATEWAYIP.toString(), GUIField.NODEPORTS.toString() };
 
     private static final String prettyFields[] = { GUIField.NAME.toString(),
             GUIField.GATEWAYIP.toString(), GUIField.NODEPORTS.toString() };
 
@@ -94,28 +90,6 @@ public class SubnetConfig implements Serializable {
         return maskLen;
     }
 
         return maskLen;
     }
 
-    public boolean isIPv6AddressValid() {
-        if (subnet == null)
-            return false;
-        String values[] = subnet.split("/");
-        try {
-            //when given an IP address, InetAddress.getByName validates the ip address
-            InetAddress addr = InetAddress.getByName(values[0]);
-            if (!(addr instanceof Inet6Address)) {
-                return false;
-            }
-        } catch (UnknownHostException ex) {
-            return false;
-        }
-        if (values.length >= 2) {
-            int prefix = Integer.valueOf(values[1]);
-            if ((prefix < 0) || (prefix > 128)) {
-                return false;
-            }
-        }
-        return true;
-    }
-
     private Set<Short> getPortList(String ports) {
         /*
          * example:
     private Set<Short> getPortList(String ports) {
         /*
          * example:
@@ -142,9 +116,9 @@ public class SubnetConfig implements Serializable {
 
     private boolean hasValidIP() {
         if (subnet != null) {
 
     private boolean hasValidIP() {
         if (subnet != null) {
-            if (subnet.matches(regexIP)) {
+            if (NetUtils.isIPv4AddressValid(subnet)) {
                 return true;
                 return true;
-            } else if (isIPv6AddressValid()) {
+            } else if (NetUtils.isIPv6AddressValid(subnet)) {
                 return true;
             }
         }
                 return true;
             }
         }
@@ -192,18 +166,6 @@ public class SubnetConfig implements Serializable {
         return false;
     }
 
         return false;
     }
 
-    public static List<String> getFieldsNames() {
-        List<String> fieldList = new ArrayList<String>();
-        for (Field fld : SubnetConfig.class.getDeclaredFields()) {
-            fieldList.add(fld.getName());
-        }
-        //remove the four static fields
-        for (short i = 0; i < 3; i++) {
-            fieldList.remove(0);
-        }
-        return fieldList;
-    }
-
     public static List<String> getGuiFieldsNames() {
         List<String> fieldList = new ArrayList<String>();
         for (String str : prettyFields) {
     public static List<String> getGuiFieldsNames() {
         List<String> fieldList = new ArrayList<String>();
         for (String str : prettyFields) {