Example #1
0
 async def _get_ports(self, connection, protocol):
     try:
         try:
             method, params = ovsdb.monitor('Open_vSwitch', 'ovsdb_port_manager_interfaces_monitor', {
                                                 'Bridge':[ovsdb.monitor_request(["name", "datapath_id", "ports"])],
                                                 'Port':[ovsdb.monitor_request(["interfaces"])]
                                             })
             try:
                 r, _ = await protocol.querywithreply(method, params, connection, self.apiroutine)
             except JsonRPCErrorResultException:
                 # The monitor is already set, cancel it first
                 method2, params2 = ovsdb.monitor_cancel('ovsdb_port_manager_interfaces_monitor')
                 await protocol.querywithreply(method2, params2, connection, self.apiroutine, False)
                 r, _ = await protocol.querywithreply(method, params, connection, self.apiroutine)
         except:
             async def _msg():
                 await self.apiroutine.wait_for_send(OVSDBConnectionPortsSynchronized(connection))
             self.apiroutine.subroutine(_msg(), False)
             raise
         # This is the initial state, it should contains all the ids of ports and interfaces
         self.apiroutine.subroutine(self._update_interfaces(connection, protocol, r, False))
         update_matcher = JsonRPCNotificationEvent.createMatcher('update', connection, connection.connmark,
                                                                 _ismatch = lambda x: x.params[0] == 'ovsdb_port_manager_interfaces_monitor')
         conn_state = protocol.statematcher(connection)
         while True:
             ev, m = await M_(update_matcher, conn_state)
             if m is conn_state:
                 break
             else:
                 self.apiroutine.subroutine(self._update_interfaces(connection, protocol, ev.params[1], True))
     except JsonRPCProtocolException:
         pass
     finally:
         if self.apiroutine.currentroutine in self.monitor_routines:
             self.monitor_routines.remove(self.apiroutine.currentroutine)
Example #2
0
 def _get_ports(self, connection, protocol):
     try:
         try:
             method, params = ovsdb.monitor(
                 'Open_vSwitch', 'ovsdb_port_manager_interfaces_monitor', {
                     'Bridge': [ovsdb.monitor_request(["name", "ports"])],
                     'Port': [ovsdb.monitor_request(["interfaces"])]
                 })
             for m in protocol.querywithreply(method, params, connection,
                                              self.apiroutine):
                 yield m
             if 'error' in self.apiroutine.jsonrpc_result:
                 # The monitor is already set, cancel it first
                 method2, params2 = ovsdb.monitor_cancel(
                     'ovsdb_port_manager_interfaces_monitor')
                 for m in protocol.querywithreply(method2, params2,
                                                  connection,
                                                  self.apiroutine, False):
                     yield m
                 for m in protocol.querywithreply(method, params,
                                                  connection,
                                                  self.apiroutine):
                     yield m
                 if 'error' in self.apiroutine.jsonrpc_result:
                     raise JsonRPCErrorResultException(
                         'OVSDB request failed: ' +
                         repr(self.apiroutine.jsonrpc_result))
             r = self.apiroutine.jsonrpc_result
         except:
             for m in self.apiroutine.waitForSend(
                     OVSDBConnectionPortsSynchronized(connection)):
                 yield m
             raise
         # This is the initial state, it should contains all the ids of ports and interfaces
         self.apiroutine.subroutine(
             self._update_interfaces(connection, protocol, r, False))
         update_matcher = JsonRPCNotificationEvent.createMatcher(
             'update',
             connection,
             connection.connmark,
             _ismatch=lambda x: x.params[
                 0] == 'ovsdb_port_manager_interfaces_monitor')
         conn_state = protocol.statematcher(connection)
         while True:
             yield (update_matcher, conn_state)
             if self.apiroutine.matcher is conn_state:
                 break
             else:
                 self.apiroutine.subroutine(
                     self._update_interfaces(
                         connection, protocol,
                         self.apiroutine.event.params[1], True))
     except JsonRPCProtocolException:
         pass
Example #3
0
 def _get_ports(self, connection, protocol):
     try:
         try:
             method, params = ovsdb.monitor('Open_vSwitch', 'ovsdb_port_manager_interfaces_monitor', {
                                                 'Bridge':[ovsdb.monitor_request(["name", "datapath_id", "ports"])],
                                                 'Port':[ovsdb.monitor_request(["interfaces"])]
                                             })
             for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                 yield m
             if 'error' in self.apiroutine.jsonrpc_result:
                 # The monitor is already set, cancel it first
                 method2, params2 = ovsdb.monitor_cancel('ovsdb_port_manager_interfaces_monitor')
                 for m in protocol.querywithreply(method2, params2, connection, self.apiroutine, False):
                     yield m
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                     yield m
                 if 'error' in self.apiroutine.jsonrpc_result:
                     raise JsonRPCErrorResultException('OVSDB request failed: ' + repr(self.apiroutine.jsonrpc_result))
             r = self.apiroutine.jsonrpc_result
         except:
             for m in self.apiroutine.waitForSend(OVSDBConnectionPortsSynchronized(connection)):
                 yield m
             raise
         # This is the initial state, it should contains all the ids of ports and interfaces
         self.apiroutine.subroutine(self._update_interfaces(connection, protocol, r, False))
         update_matcher = JsonRPCNotificationEvent.createMatcher('update', connection, connection.connmark,
                                                                 _ismatch = lambda x: x.params[0] == 'ovsdb_port_manager_interfaces_monitor')
         conn_state = protocol.statematcher(connection)
         while True:
             yield (update_matcher, conn_state)
             if self.apiroutine.matcher is conn_state:
                 break
             else:
                 self.apiroutine.subroutine(self._update_interfaces(connection, protocol, self.apiroutine.event.params[1], True))
     except JsonRPCProtocolException:
         pass
     finally:
         if self.apiroutine.currentroutine in self.monitor_routines:
             self.monitor_routines.remove(self.apiroutine.currentroutine)
Example #4
0
    def _get_bridges(self, connection, protocol):
        try:
            try:
                vhost = protocol.vhost
                if not hasattr(connection, 'ovsdb_systemid'):
                    method, params = ovsdb.transact(
                        'Open_vSwitch',
                        ovsdb.select('Open_vSwitch', [], ['external_ids']))
                    for m in protocol.querywithreply(method, params,
                                                     connection,
                                                     self.apiroutine):
                        yield m
                    result = self.apiroutine.jsonrpc_result[0]
                    system_id = ovsdb.omap_getvalue(
                        result['rows'][0]['external_ids'], 'system-id')
                    connection.ovsdb_systemid = system_id
                else:
                    system_id = connection.ovsdb_systemid
                if (vhost, system_id) in self.managed_systemids:
                    oc = self.managed_systemids[(vhost, system_id)]
                    ep = _get_endpoint(oc)
                    econns = self.endpoint_conns.get((vhost, ep))
                    if econns:
                        try:
                            econns.remove(oc)
                        except ValueError:
                            pass
                    del self.managed_systemids[(vhost, system_id)]
                self.managed_systemids[(vhost, system_id)] = connection
                self.managed_bridges[connection] = []
                ep = _get_endpoint(connection)
                self.endpoint_conns.setdefault((vhost, ep),
                                               []).append(connection)
                method, params = ovsdb.monitor(
                    'Open_vSwitch', 'ovsdb_manager_bridges_monitor',
                    {'Bridge': ovsdb.monitor_request(['name', 'datapath_id'])})
                try:
                    for m in protocol.querywithreply(method, params,
                                                     connection,
                                                     self.apiroutine):
                        yield m
                except JsonRPCErrorResultException:
                    # The monitor is already set, cancel it first
                    method, params = ovsdb.monitor_cancel(
                        'ovsdb_manager_bridges_monitor')
                    for m in protocol.querywithreply(method, params,
                                                     connection,
                                                     self.apiroutine, False):
                        yield m
                    method, params = ovsdb.monitor(
                        'Open_vSwitch', 'ovsdb_manager_bridges_monitor', {
                            'Bridge':
                            ovsdb.monitor_request(['name', 'datapath_id'])
                        })
                    for m in protocol.querywithreply(method, params,
                                                     connection,
                                                     self.apiroutine):
                        yield m
            except Exception:
                for m in self.apiroutine.waitForSend(
                        OVSDBConnectionSetup(system_id, connection,
                                             connection.connmark, vhost)):
                    yield m
                raise
            else:
                # Process initial bridges
                init_subprocesses = []
                if self.apiroutine.jsonrpc_result and 'Bridge' in self.apiroutine.jsonrpc_result:
                    init_subprocesses = [
                        self._update_bridge(connection, protocol, buuid, vhost)
                        for buuid in
                        self.apiroutine.jsonrpc_result['Bridge'].keys()
                    ]

                def init_process():
                    try:
                        with closing(
                                self.apiroutine.executeAll(init_subprocesses,
                                                           retnames=())) as g:
                            for m in g:
                                yield m
                    except Exception:
                        for m in self.apiroutine.waitForSend(
                                OVSDBConnectionSetup(system_id, connection,
                                                     connection.connmark,
                                                     vhost)):
                            yield m
                        raise
                    else:
                        for m in self.apiroutine.waitForSend(
                                OVSDBConnectionSetup(system_id, connection,
                                                     connection.connmark,
                                                     vhost)):
                            yield m

                self.apiroutine.subroutine(init_process())
            # Wait for notify
            notification = JsonRPCNotificationEvent.createMatcher(
                'update',
                connection,
                connection.connmark,
                _ismatch=lambda x: x.params[
                    0] == 'ovsdb_manager_bridges_monitor')
            conn_down = protocol.statematcher(connection)
            while True:
                yield (conn_down, notification)
                if self.apiroutine.matcher is conn_down:
                    break
                else:
                    for buuid, v in self.apiroutine.event.params[1][
                            'Bridge'].items():
                        # If a bridge's name or datapath-id is changed, we remove this bridge and add it again
                        if 'old' in v:
                            # A bridge is deleted
                            bridges = self.managed_bridges[connection]
                            for i in range(0, len(bridges)):
                                if buuid == bridges[i][3]:
                                    self.scheduler.emergesend(
                                        OVSDBBridgeSetup(
                                            OVSDBBridgeSetup.DOWN,
                                            bridges[i][1],
                                            system_id,
                                            bridges[i][2],
                                            connection,
                                            connection.connmark,
                                            vhost,
                                            bridges[i][3],
                                            new_datapath_id=int(
                                                v['new']['datapath_id'], 16)
                                            if 'new' in v
                                            and 'datapath_id' in v['new'] else
                                            None))
                                    del self.managed_conns[(vhost,
                                                            bridges[i][1])]
                                    del bridges[i]
                                    break
                        if 'new' in v:
                            # A bridge is added
                            self.apiroutine.subroutine(
                                self._update_bridge(connection, protocol,
                                                    buuid, vhost))
        except JsonRPCProtocolException:
            pass
        finally:
            del connection._ovsdb_manager_get_bridges
Example #5
0
 def _get_bridges(self, connection, protocol):
     try:
         try:
             vhost = protocol.vhost
             if not hasattr(connection, 'ovsdb_systemid'):
                 method, params = ovsdb.transact('Open_vSwitch', ovsdb.select('Open_vSwitch', [], ['external_ids']))
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                     yield m
                 result = self.apiroutine.jsonrpc_result[0]
                 system_id = ovsdb.omap_getvalue(result['rows'][0]['external_ids'], 'system-id')
                 connection.ovsdb_systemid = system_id
             else:
                 system_id = connection.ovsdb_systemid
             if (vhost, system_id) in self.managed_systemids:
                 oc = self.managed_systemids[(vhost, system_id)]
                 ep = _get_endpoint(oc)
                 econns = self.endpoint_conns.get((vhost, ep))
                 if econns:
                     try:
                         econns.remove(oc)
                     except ValueError:
                         pass
                 del self.managed_systemids[(vhost, system_id)]
             self.managed_systemids[(vhost, system_id)] = connection
             self.managed_bridges[connection] = []
             ep = _get_endpoint(connection)
             self.endpoint_conns.setdefault((vhost, ep), []).append(connection)
             method, params = ovsdb.monitor('Open_vSwitch', 'ovsdb_manager_bridges_monitor', {'Open_vSwitch':ovsdb.monitor_request(['bridges'])})
             for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                 yield m
             if 'error' in self.apiroutine.jsonrpc_result:
                 # The monitor is already set, cancel it first
                 method, params = ovsdb.monitor_cancel('ovsdb_manager_bridges_monitor')
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine, False):
                     yield m
                 method, params = ovsdb.monitor('Open_vSwitch', 'ovsdb_manager_bridges_monitor', {'Open_vSwitch':ovsdb.monitor_request(['bridges'], True, False, False, True)})
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                     yield m
                 if 'error' in self.apiroutine.jsonrpc_result:
                     raise JsonRPCErrorResultException('OVSDB request failed: ' + repr(self.apiroutine.jsonrpc_result))
         except Exception:
             for m in self.apiroutine.waitForSend(OVSDBConnectionSetup(system_id, connection, connection.connmark, vhost)):
                 yield m
             raise
         else:
             # Process initial bridges
             init_subprocesses = [self._update_bridge(connection, protocol, buuid, vhost)
                                 for v in self.apiroutine.jsonrpc_result['Open_vSwitch'].values()
                                 for _, buuid in ovsdb.getlist(v['new']['bridges'])]
             def init_process():
                 try:
                     for m in self.apiroutine.executeAll(init_subprocesses, retnames = ()):
                         yield m
                 except Exception:
                     for m in self.apiroutine.waitForSend(OVSDBConnectionSetup(system_id, connection, connection.connmark, vhost)):
                         yield m
                     raise
                 else:
                     for m in self.apiroutine.waitForSend(OVSDBConnectionSetup(system_id, connection, connection.connmark, vhost)):
                         yield m
             self.apiroutine.subroutine(init_process())
         # Wait for notify
         notification = JsonRPCNotificationEvent.createMatcher('update', connection, connection.connmark, _ismatch = lambda x: x.params[0] == 'ovsdb_manager_bridges_monitor')
         conn_down = protocol.statematcher(connection)
         while True:
             yield (conn_down, notification)
             if self.apiroutine.matcher is conn_down:
                 break
             else:
                 for v in self.apiroutine.event.params[1]['Open_vSwitch'].values():
                     if 'old' not in v:
                         old = set()
                     else:
                         old = set((oid for _, oid in ovsdb.getlist(v['old']['bridges'])))
                     if 'new' not in v:
                         new = set()
                     else:
                         new = set((oid for _, oid in ovsdb.getlist(v['new']['bridges'])))
                     for buuid in (new - old):
                         self.apiroutine.subroutine(self._update_bridge(connection, protocol, buuid, vhost))
                     for buuid in (old - new):
                         bridges = self.managed_bridges[connection]
                         for i in range(0, len(bridges)):
                             if buuid == bridges[i][3]:
                                 self.scheduler.emergesend(OVSDBBridgeSetup(OVSDBBridgeSetup.DOWN,
                                                                            bridges[i][1],
                                                                            system_id,
                                                                            bridges[i][2],
                                                                            connection,
                                                                            connection.connmark,
                                                                            vhost,
                                                                            bridges[i][3]))
                                 del self.managed_conns[(vhost, bridges[i][1])]
                                 del bridges[i]
                                 break
     except JsonRPCProtocolException:
         pass
     finally:
         del connection._ovsdb_manager_get_bridges
Example #6
0
 def _get_bridges(self, connection, protocol):
     try:
         try:
             vhost = protocol.vhost
             if not hasattr(connection, 'ovsdb_systemid'):
                 method, params = ovsdb.transact('Open_vSwitch', ovsdb.select('Open_vSwitch', [], ['external_ids']))
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                     yield m
                 result = self.apiroutine.jsonrpc_result[0]
                 system_id = ovsdb.omap_getvalue(result['rows'][0]['external_ids'], 'system-id')
                 connection.ovsdb_systemid = system_id
             else:
                 system_id = connection.ovsdb_systemid
             if (vhost, system_id) in self.managed_systemids:
                 oc = self.managed_systemids[(vhost, system_id)]
                 ep = _get_endpoint(oc)
                 econns = self.endpoint_conns.get((vhost, ep))
                 if econns:
                     try:
                         econns.remove(oc)
                     except ValueError:
                         pass
                 del self.managed_systemids[(vhost, system_id)]
             self.managed_systemids[(vhost, system_id)] = connection
             self.managed_bridges[connection] = []
             ep = _get_endpoint(connection)
             self.endpoint_conns.setdefault((vhost, ep), []).append(connection)
             method, params = ovsdb.monitor('Open_vSwitch', 'ovsdb_manager_bridges_monitor', {'Bridge':ovsdb.monitor_request(['name', 'datapath_id'])})
             for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                 yield m
             if 'error' in self.apiroutine.jsonrpc_result:
                 # The monitor is already set, cancel it first
                 method, params = ovsdb.monitor_cancel('ovsdb_manager_bridges_monitor')
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine, False):
                     yield m
                 method, params = ovsdb.monitor('Open_vSwitch', 'ovsdb_manager_bridges_monitor', {'Bridge':ovsdb.monitor_request(['name', 'datapath_id'])})
                 for m in protocol.querywithreply(method, params, connection, self.apiroutine):
                     yield m
                 if 'error' in self.apiroutine.jsonrpc_result:
                     raise JsonRPCErrorResultException('OVSDB request failed: ' + repr(self.apiroutine.jsonrpc_result))
         except Exception:
             for m in self.apiroutine.waitForSend(OVSDBConnectionSetup(system_id, connection, connection.connmark, vhost)):
                 yield m
             raise
         else:
             # Process initial bridges
             init_subprocesses = [self._update_bridge(connection, protocol, buuid, vhost)
                                 for buuid in self.apiroutine.jsonrpc_result['Bridge'].keys()]
             def init_process():
                 try:
                     with closing(self.apiroutine.executeAll(init_subprocesses, retnames = ())) as g:
                         for m in g:
                             yield m
                 except Exception:
                     for m in self.apiroutine.waitForSend(OVSDBConnectionSetup(system_id, connection, connection.connmark, vhost)):
                         yield m
                     raise
                 else:
                     for m in self.apiroutine.waitForSend(OVSDBConnectionSetup(system_id, connection, connection.connmark, vhost)):
                         yield m
             self.apiroutine.subroutine(init_process())
         # Wait for notify
         notification = JsonRPCNotificationEvent.createMatcher('update', connection, connection.connmark, _ismatch = lambda x: x.params[0] == 'ovsdb_manager_bridges_monitor')
         conn_down = protocol.statematcher(connection)
         while True:
             yield (conn_down, notification)
             if self.apiroutine.matcher is conn_down:
                 break
             else:
                 for buuid, v in self.apiroutine.event.params[1]['Bridge'].items():
                     # If a bridge's name or datapath-id is changed, we remove this bridge and add it again
                     if 'old' in v:
                         # A bridge is deleted
                         bridges = self.managed_bridges[connection]
                         for i in range(0, len(bridges)):
                             if buuid == bridges[i][3]:
                                 self.scheduler.emergesend(OVSDBBridgeSetup(OVSDBBridgeSetup.DOWN,
                                                                            bridges[i][1],
                                                                            system_id,
                                                                            bridges[i][2],
                                                                            connection,
                                                                            connection.connmark,
                                                                            vhost,
                                                                            bridges[i][3],
                                                                            new_datapath_id =
                                                                             int(v['new']['datapath_id'], 16) if 'new' in v and 'datapath_id' in v['new']
                                                                             else None))
                                 del self.managed_conns[(vhost, bridges[i][1])]
                                 del bridges[i]
                                 break
                     if 'new' in v:
                         # A bridge is added
                         self.apiroutine.subroutine(self._update_bridge(connection, protocol, buuid, vhost))
     except JsonRPCProtocolException:
         pass
     finally:
         del connection._ovsdb_manager_get_bridges