AT_BANNER([bridge])

dnl When a port disappears from a datapath, e.g. because an admin used
dnl "ovs-dpctl del-port", the bridge code should be resilient enough to
dnl notice and add it back the next time we reconfigure.  A prior version
dnl of the code failed to do this, so this test guards against regression.
AT_SETUP([bridge - ports that disappear get added back])
OVS_VSWITCHD_START

# Add some ports and make sure that they show up in the datapath.
add_of_ports br0 1 2
AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p1 1/1: (dummy)
    p2 2/2: (dummy)
])

# Delete p1 from the datapath with "ovs-dpctl del-if"
# and check that it disappeared.
AT_CHECK([ovs-appctl dpctl/del-if dummy@ovs-dummy p1])
AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p2 2/2: (dummy)
])

# Force reconfiguration and make sure that p1 got added back.
AT_CHECK([ovs-vsctl del-port p2])
AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p1 1/1: (dummy)
])
OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
OVS_APP_EXIT_AND_WAIT([ovsdb-server])
AT_CLEANUP

dnl When multiple bridges are connected to the same controller, make
dnl sure their status are tracked independently.
AT_SETUP([bridge - multiple bridges share a controller])
OVS_VSWITCHD_START(
   [add-br br1 -- \
    set bridge br1 other-config:hwaddr=aa:66:aa:66:00:00 -- \
    set bridge br1 datapath-type=dummy other-config:datapath-id=1234 ])

dnl Start ovs-testcontroller
AT_CHECK([ovs-testcontroller --detach --no-chdir  punix:controller --pidfile], [0], [ignore])
on_exit 'kill `cat ovs-testcontroller.pid`'
OVS_WAIT_UNTIL([test -e controller])

dnl Add the controller to both bridges, 5 seconds apart.
AT_CHECK([ovs-vsctl set-controller br0 unix:controller])
AT_CHECK([ovs-vsctl set-fail-mode br0 secure])
AT_CHECK([ovs-appctl time/warp 5000], [0], [ignore])
AT_CHECK([ovs-vsctl set-controller br1 unix:controller])
AT_CHECK([ovs-vsctl set-fail-mode br1 secure])

dnl Wait for the controller connectionsi to be up
for i in `seq 0 19`
do
    if ovs-vsctl --columns=is_connected list controller |grep "false"; then
        :
    else
        break
    fi
    ovs-appctl time/warp 1100
done

dnl Make sure the connection status have two records and they are different.
dnl (The exact output contains timing information that are machine dependent.)
AT_CHECK([ovs-vsctl --columns=status list controller | dnl
          grep "status" | sort -u |wc -l], [0], [2
])

OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
OVS_APP_EXIT_AND_WAIT([ovsdb-server])
AT_CLEANUP

AT_SETUP([bridge - add port after stopping controller])
OVS_VSWITCHD_START

dnl Start ovs-testcontroller
AT_CHECK([ovs-testcontroller --no-chdir --detach punix:controller --pidfile], [0], [ignore])
OVS_WAIT_UNTIL([test -e controller])

AT_CHECK([ovs-vsctl set-controller br0 unix:controller])
AT_CHECK([ovs-vsctl add-port br0 p1 -- set Interface p1 type=internal], [0], [ignore])
AT_CHECK([ovs-appctl -t ovs-vswitchd version], [0], [ignore])

# Now kill the ovs-testcontroller
kill `cat ovs-testcontroller.pid`
if test "$IS_WIN32" = "yes"; then
    AT_CHECK([rm controller], [0], [ignore])
fi
OVS_WAIT_UNTIL([! test -e controller])
AT_CHECK([ovs-vsctl --no-wait add-port br0 p2 -- set Interface p2 type=internal], [0], [ignore])
AT_CHECK([ovs-appctl -t ovs-vswitchd version], [0], [ignore])

OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
OVS_APP_EXIT_AND_WAIT([ovsdb-server])
AT_CLEANUP

AT_SETUP([bridge - change ofproto versions])
dnl Start vswitch and add a version test bridge
OVS_VSWITCHD_START(
    [add-br vr_test0 -- \
     set bridge vr_test0 datapath-type=dummy \
                         protocols=OpenFlow10])

dnl set the version to include, say, OpenFlow14
AT_CHECK([ovs-vsctl set bridge vr_test0 protocols=OpenFlow10,OpenFlow14])

dnl now try to use bundle action on a flow
AT_CHECK([ovs-ofctl add-flow vr_test0 --bundle actions=normal])

OVS_APP_EXIT_AND_WAIT([ovs-vswitchd])
OVS_APP_EXIT_AND_WAIT([ovsdb-server])
AT_CLEANUP
