Merge "Added validation for protocol field while adding flow"
authorGiovanni Meo <gmeo@cisco.com>
Thu, 19 Sep 2013 15:21:04 +0000 (15:21 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 19 Sep 2013 15:21:04 +0000 (15:21 +0000)
opendaylight/forwardingrulesmanager/api/src/main/java/org/opendaylight/controller/forwardingrulesmanager/FlowConfig.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/IPProtocols.java

index ba69c8a3d29f8286f2d4f3157f6547a8f54c717b..62d6855e109f77ac97baf5107fb2c0efbcaa603e 100644 (file)
@@ -655,6 +655,14 @@ public class FlowConfig implements Serializable {
         return ((to >= 0) && (to <= 0xffff));
     }
 
+    public boolean isProtocolValid(String protocol) {
+        int protocol_number = IPProtocols.getProtocolNumberInt(protocol);
+        if (protocol_number < 1 || protocol_number > 255) {
+            return false;
+        }
+        return true;
+    }
+
     private Status conflictWithContainerFlow(IContainer container) {
         // Return true if it's default container
         if (container.getName().equals(GlobalConstants.DEFAULT.toString())) {
@@ -762,6 +770,10 @@ public class FlowConfig implements Serializable {
                 }
             }
 
+            if ((protocol != null) && !isProtocolValid(protocol)) {
+                return new Status(StatusCode.BADREQUEST, String.format("Protocol %s is not valid", protocol));
+            }
+
             if ((tosBits != null) && !isTOSBitsValid(tosBits)) {
                 return new Status(StatusCode.BADREQUEST, String.format("IP ToS bits %s is not in the range 0 - 63",
                         tosBits));
index cdb4463c02acb2636a0991053f9e64951e3f3ba5..66e6e65706fc2b23a1498b589a4e94fafbe3e7bf 100644 (file)
@@ -168,7 +168,8 @@ public enum IPProtocols {
      WESP("WESP",141),
      ROHC("ROHC",142);
      */
-    private static final String regexNumberString = "^[0-9]+$";
+    private static final String regexDecimalString = "^[0-9]{3}$";
+    private static final String regexHexString = "^(0(x|X))[0-9a-fA-F]{2}$";
     private String protocolName;
     private int protocolNumber;
 
@@ -215,7 +216,10 @@ public enum IPProtocols {
     }
 
     public static short getProtocolNumberShort(String name) {
-        if (name.matches(regexNumberString)) {
+        if (name.matches(regexHexString)) {
+            return Short.valueOf(Short.decode(name));
+        }
+        if (name.matches(regexDecimalString)) {
             return Short.valueOf(name);
         }
         for (IPProtocols proto : IPProtocols.values()) {
@@ -227,7 +231,10 @@ public enum IPProtocols {
     }
 
     public static int getProtocolNumberInt(String name) {
-        if (name.matches(regexNumberString)) {
+        if (name.matches(regexHexString)) {
+            return Integer.valueOf(Integer.decode(name));
+        }
+        if (name.matches(regexDecimalString)) {
             return Integer.valueOf(name);
         }
         for (IPProtocols proto : IPProtocols.values()) {
@@ -239,7 +246,10 @@ public enum IPProtocols {
     }
 
     public static byte getProtocolNumberByte(String name) {
-        if (name.matches(regexNumberString)) {
+        if (name.matches(regexHexString)) {
+            return Integer.valueOf(Integer.decode(name)).byteValue();
+        }
+        if (name.matches(regexDecimalString)) {
             return Integer.valueOf(name).byteValue();
         }
         for (IPProtocols proto : IPProtocols.values()) {