Added strict verification of vnode-path-fields. 78/31878/1
authorShigeru Yasuda <s-yasuda@da.jp.nec.com>
Fri, 25 Dec 2015 14:32:58 +0000 (23:32 +0900)
committerShigeru Yasuda <s-yasuda@da.jp.nec.com>
Fri, 25 Dec 2015 14:32:58 +0000 (23:32 +0900)
A vnode-path-fields should be treated as an invalid path if it does not
specify the virtual bridge though it specifies the virtual interface.

Change-Id: I16f6328581fa8ff8e6d478d2974fac65bf0d87d7
Signed-off-by: Shigeru Yasuda <s-yasuda@da.jp.nec.com>
manager/implementation/src/main/java/org/opendaylight/vtn/manager/internal/util/vnode/VNodeIdentifier.java
manager/implementation/src/test/java/org/opendaylight/vtn/manager/internal/util/vnode/VNodeIdentifierTest.java

index c56906d6a80879f4e28a2841a6b15f44b61f9ad5..7ecba2679a6e2fbb8f22886a3ba827b31e2f239a 100644 (file)
@@ -266,7 +266,7 @@ public abstract class VNodeIdentifier<T extends DataObject> {
             } else {
                 String vtname = vpath.getTerminalName();
                 if (vtname == null) {
-                    ident = VTenantIdentifier.create(tname, find);
+                    ident = createTenantIdentifier(tname, iname, find);
                 } else {
                     ident = (iname == null)
                         ? VTerminalIdentifier.create(tname, vtname, find)
@@ -305,6 +305,42 @@ public abstract class VNodeIdentifier<T extends DataObject> {
         return RpcException.getBadArgumentException(msg);
     }
 
+    /**
+     * Create a new identifier for the VTN.
+     *
+     * <p>
+     *   This method is called by {@link #create(VnodePathFields, boolean)}
+     *   if the specified vnode-path-fields does not specify the name of the
+     *   virtual bridge.
+     * </p>
+     *
+     * @param tname  The name of the VTN specified by vnode-path-fields.
+     * @param iname  The name of the virtual interface specified by
+     *               vnode-path-fields.
+     * @param find   {@code true} indicates the given name is used for
+     *               finding existing virtual node.
+     * @return  A {@link VTenantIdentifier} instance that specifies the
+     *          location of the VTN.
+     * @throws RpcException  An error occurred.
+     */
+    private static VTenantIdentifier createTenantIdentifier(
+        String tname, String iname, boolean find) throws RpcException {
+        VTenantIdentifier ident;
+        if (iname == null) {
+            ident = VTenantIdentifier.create(tname, find);
+        } else {
+            // No virtual bridge name is specified though virtual interface
+            // name is specified. This should be treated as an invalid path.
+            if (tname == null) {
+                VNodeType.VTN.checkName(null, find);
+            }
+
+            throw RpcException.getNullArgumentException("Virtual bridge name");
+        }
+
+        return ident;
+    }
+
     /**
      * Default constructor only for JAXB.
      */
index c89251e20bb02a7c04bd3ec57363701f83ec805b..ef5b24048d95a56b668a35761a9f08b58d9f676f 100644 (file)
@@ -381,6 +381,28 @@ public class VNodeIdentifierTest extends TestBase {
             }
         }
 
+        // Virtual bridge name is not specified though VTN and interface name
+        // are specified.
+        tnames = new String[]{null, "vtn", "vtn1"};
+        for (String tname: tnames) {
+            VnodePathFields noBridge = mock(VnodePathFields.class);
+            when(noBridge.getTenantName()).thenReturn(tname);
+            when(noBridge.getInterfaceName()).thenReturn(inames[0]);
+            String msg = (tname == null)
+                ? "VTN name cannot be null"
+                : "Virtual bridge name cannot be null";
+            for (boolean find: bools) {
+                try {
+                    VNodeIdentifier.create(noBridge, find);
+                    unexpected();
+                } catch (RpcException e) {
+                    assertEquals(RpcErrorTag.MISSING_ELEMENT, e.getErrorTag());
+                    assertEquals(VtnErrorTag.BADREQUEST, e.getVtnErrorTag());
+                    assertEquals(msg, e.getMessage());
+                }
+            }
+        }
+
         // Specifying invalid name (read).
         String badName = "bad name";
         Map<VnodePathFields, String> cases = new HashMap<>();