app/gre_tunnel.py: typo has_network -> has_networks

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
network.py: sort out order between event and actual deletion of port

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
network: sort out order between event and actual deletion of network

And make Network.remove_network more robust.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
network.py: exception in Networks.remove_port()

Neutron plugin can call remove_port after network deletion for
automatic delete port like router/dhcp port. So ignore NetworkNotFound
exception.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
gre_tunnel: exception due to race condition

This depends on thread scheduling.
When event handler is invoked, there may not corresponding network_id in
network module.
In fact, Network.remove_network() fires EventNetworkPort(add_del=False)
before EventNetworkDel event so that it is unnecessary to call
_vm_port_mac_handler().

> 127.0.0.1 - - [19/Jun/2013 11:24:25] "DELETE
> /v1.0/networks/8179bb70-a63f-4c74-a82e-a21f3c275c9a HTTP/1.1" 200 115 0.000488
> hub: uncaught exception: Traceback (most recent call last):
>   File "/opt/stack/ryu/ryu/lib/hub.py", line 50, in _launch
>     func(*args, **kwargs)
>   File "/opt/stack/ryu/ryu/base/app_manager.py", line 104, in _event_loop
>     handler(ev)
>   File "/opt/stack/ryu/ryu/app/gre_tunnel.py", line 210, in network_del_handler
>     for (dpid, port_no) in self.nw.list_ports(network_id):
>   File "/opt/stack/ryu/ryu/controller/network.py", line 319, in list_ports
>     return self.networks.list_ports(network_id)
>   File "/opt/stack/ryu/ryu/controller/network.py", line 97, in list_ports
>     raise NetworkNotFound(network_id=network_id)
> NetworkNotFound: no such network id 8179bb70-a63f-4c74-a82e-a21f3c275c9a

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
network: add a helper function to list ports that doesn't raise

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
gre_tunnel.py: race condition

When tunnel key add/del event fires, the corresponding network_id
does not necessarily exists due to race condition.
So mask it.

(15612) accepted ('127.0.0.1', 45714)
127.0.0.1 - - [25/Jun/2013 12:18:18] "DELETE
/v1.0/networks/462be334-3160-48ae-9ef8-a456dc866f8f HTTP/1.1" 200 115
0.000393
(15612) accepted ('127.0.0.1', 45715)
127.0.0.1 - - [25/Jun/2013 12:18:18] "DELETE
/v1.0/tunnels/networks/462be334-3160-48ae-9ef8-a456dc866f8f/key
HTTP/1.1" 200 115 0.000260
hub: uncaught exception: Traceback (most recent call last):
  File "/opt/stack/ryu/ryu/lib/hub.py", line 50, in _launch
    func(*args, **kwargs)
  File "/opt/stack/ryu/ryu/base/app_manager.py", line 104, in _event_loop
    handler(ev)
  File "/opt/stack/ryu/ryu/app/gre_tunnel.py", line 233, in
tunnel_key_del_handler
    for (dpid, port_no) in self.nw.list_ports(network_id):
  File "/opt/stack/ryu/ryu/controller/network.py", line 325, in list_ports
    return self.networks.list_ports(network_id)
  File "/opt/stack/ryu/ryu/controller/network.py", line 103, in list_ports
    raise NetworkNotFound(network_id=network_id)
NetworkNotFound: no such network id 462be334-3160-48ae-9ef8-a456dc866f8f

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
simple_vlan.py: race condition

When tunnel key is registers, network is is not always
registered.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
quantum_adapter: race between ovs port deletion and plugin port deletion

The order between the notification of ovs port deletion via OVSDB protocol and
the notification network id/port deletion via REST from quantum plugin
isn't deterministic.
So when ovs port is deleted, the corresponding network id may or may not
exist.

The code wrongly assumed the order, so resulted in the following exception.
When ovs port is deleted and the corresponding network id isn't find,
just ignore the exception.

> (19257) accepted ('127.0.0.1', 36841)
> 127.0.0.1 - - [19/Jun/2013 11:24:25] "DELETE /v1.0/tunnels/networks/8179bb70-a63f-4c74-a82e-a21f3c275c9a/key HTTP/1.1" 200 115 0.000383
> hub: uncaught exception: Traceback (most recent call last):
>   File "/opt/stack/ryu/ryu/lib/hub.py", line 50, in _launch
>     func(*args, **kwargs)
>   File "/opt/stack/ryu/ryu/base/app_manager.py", line 104, in _event_loop
>     handler(ev)
>   File "/opt/stack/ryu/ryu/app/quantum_adapter.py", line 368, in
> port_del_handler
>     self._port_handler(ev.dp.id, port.port_no, name, False)
>   File "/opt/stack/ryu/ryu/app/quantum_adapter.py", line 336, in _port_handler
>     ovs_switch.update_port(port_no, port_name, add)
>   File "/opt/stack/ryu/ryu/app/quantum_adapter.py", line 279, in update_port
>     self._update_vif_port(old_port, add=False)
>   File "/opt/stack/ryu/ryu/app/quantum_adapter.py", line 194, in
> _update_vif_port
>     self.network_api.remove_port(network_id, self.dpid, port.ofport)
>   File "/opt/stack/ryu/ryu/controller/network.py", line 368, in remove_port
>     self.networks.remove(network_id, dpid, port_no)
>   File "/opt/stack/ryu/ryu/controller/network.py", line 119, in remove
>     raise NetworkNotFound(network_id=network_id)
> NetworkNotFound: no such network id 8179bb70-a63f-4c74-a82e-a21f3c275c9a

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>