def add_bridge(self, name: str): bridge_id, interface_id, port_id = [generate_uuid() for _ in range(3)] bridges = [bridge.uuid for bridge in self.get_bridges()] + [named_uuid(bridge_id)] ops = [ operation.insert("Interface", row={ "name": name, "type": "internal" }, uuid_name=interface_id), operation.insert("Port", row={ "name": name, "interfaces": named_uuid(interface_id) }, uuid_name=port_id), operation.insert("Bridge", row={ "name": name, "ports": named_uuid(port_id) }, uuid_name=bridge_id), operation.update("Open_vSwitch", where=[get_by_uuid(self.get_openvswitch().uuid)], row={"bridges": ["set", bridges]}), ] bridge_raw = self.query.multiple_ops(ops) return self.get_bridge(uuid=bridge_raw["result"][2]["uuid"])
def get_bridge(self, name: str = None, uuid: str = None): conds = [get_by_uuid(uuid)] if uuid else [get_by_name(name)] bridge_raw = self.query.select_from_table( "Bridge", where=conds)["result"][0]["rows"] if not bridge_raw: raise OvsdbResourceNotFoundException return OvsBridge(bridge_raw[0], self)
def set_fail_mode(self, mode: FailMode): """ Sets the fail mode of the bridge :param mode: the mode :return: """ self.api.query.update_table("Bridge", row={"fail_mode": mode.value}, where=[get_by_uuid(self.uuid)]) self._update_bridge_object()
def set_rstp(self, enabled: bool): """ Sets the RSTP parameter of the bridge. :param enabled: boolean that represents the rstp state. :return: """ self.api.query.update_table("Bridge", row={"rstp_enable": enabled}, where=[get_by_uuid(self.uuid)]) self._update_bridge_object()
def set_protocols(self, protocols: List): """ Sets the supported protocols of the bridge :param protocols: list of supported protocols :return: """ self.api.query.update_table("Bridge", where=[get_by_uuid(self.uuid)], row={"protocols": ["set", protocols]}) self._update_bridge_object()
def del_bridge(self, bridge: OvsBridge): if not bridge: raise OvsdbQueryException("Please provide a bridge") other_bridges = [ br.uuid for br in self.get_bridges() if br.uuid != bridge.uuid ] self.query.update_table( "Open_vSwitch", row={"bridges": ["set", other_bridges]}, where=[get_by_uuid(self.get_openvswitch().uuid)])
def set_connection_mode(self, mode: ConnectionMode): """ Sets the connection mode to the controller :param mode: the mode :return: """ self.api.query.update_table("Controller", row={"connection_mode": mode.value}, where=[get_by_uuid(self.uuid)]) self._update_controller_object()
def del_ports(self): """ Deletes all ports of a bridge :return: """ local_port = [ p.uuid for p in self.get_ports() if getattr(p, "name") == getattr(self, "name") ] self.api.query.update_table("Bridge", row={"ports": ["set", local_port]}, where=[get_by_uuid(self.uuid)]) self._update_bridge_object()
def del_port(self, port: OvsPort): """ Deletes a port :param port: the port to delete :return: """ if not port: raise OvsdbQueryException("Please provide a port") new_ports = [p.uuid for p in self.get_ports() if p.uuid != port.uuid] self.api.query.update_table("Bridge", row={"ports": ["set", new_ports]}, where=[get_by_uuid(self.uuid)]) self._update_bridge_object()
def add_port(self, port: str, patch_peer: str = None, *, may_exist=False): """ Adds a port to the bridge :param port: name of the interface to attach :param patch_peer: if the port connects with another bridge, name of the patch port of the other bridge. :param may_exist: asdasdd :return: query response """ if may_exist: try: self.get_port(port) return except OvsdbResourceNotFoundException: pass port_id, interface_id = [generate_uuid() for _ in range(2)] all_ports_uuid = [p.uuid for p in self.get_ports()] + [named_uuid(port_id)] interface = {"name": port} if patch_peer: interface["type"] = "patch" interface["options"] = ["map", [["peer", patch_peer]]] ops = [ operation.insert("Interface", row=interface, uuid_name=interface_id), operation.insert("Port", row={ "name": port, "interfaces": named_uuid(interface_id) }, uuid_name=port_id), operation.update("Bridge", where=[get_by_uuid(self.uuid)], row={"ports": ["set", all_ports_uuid]}) ] response = self.api.query.multiple_ops(ops) self._update_bridge_object() return response
def set_controller(self, target: str) -> OvsController: """ Sets the controller of the bridge :param target: address where the controller is (e.g. tcp:HOST:PORT) :return:OvsController """ controller_id = generate_uuid() ops = [ operation.insert("Controller", row={ "role": "other", "target": target }, uuid_name=controller_id), operation.update( "Bridge", where=[get_by_uuid(self.uuid)], row={"controller": ["set", [named_uuid(controller_id)]]}) ] self.api.query.multiple_ops(ops) self._update_bridge_object() return self.get_controller()
def get_port(self, uuid: str = None, name: str = None): conds = [get_by_uuid(uuid)] if uuid else [get_by_name(name)] port_raw = self.query.select_from_table("Port", where=conds) return OvsPort(port_raw["result"][0]["rows"][0], self)
def get_interface(self, uuid: str): interface_raw = self.query.select_from_table("Interface", where=[get_by_uuid(uuid)]) return OvsInterface(interface_raw["result"][0]["rows"][0], self)
def get_controller(self, uuid: str): controller_raw = self.query.select_from_table( "Controller", where=[get_by_uuid(uuid)]) return OvsController(controller_raw["result"][0]["rows"][0], self)
def del_bridges(self): self.query.update_table( "Open_vSwitch", row={"bridges": ["set", []]}, where=[get_by_uuid(self.get_openvswitch().uuid)])