def addPort(self, bridge_name, port_name): if bridge_name not in \ [br.name for br in list(self.ovs_state.bridges.values())]: raise OVSExceptions.OVSNoSuchBridge(bridge_name) if port_name in \ [port.name for port in list(self.ovs_state.ports.values())]: raise OVSExceptions.OVSPortExists(port_name) transaction = Transaction(self.cur_id) for instance in list(self.ovs_state.bridges.values()) + list( self.ovs_state.ports.values()): transaction.addOperation(WaitOperation(instance)) switch = self.ovs_state.switch iface = OVSInterface(port_name) port = OVSPort(port_name, [iface]) transaction.addOperation(InsertOperation(iface)) transaction.addOperation(InsertOperation(port)) bridge = None for br in list(self.ovs_state.bridges.values()): if br.name == bridge_name: bridge = br break bridge.ports[port.uuid] = port transaction.addOperation(UpdateOperation(bridge, 'ports')) transaction.addOperation(MutateOperation(switch, 'next_cfg', '+=')) self.sock.send(bytes(json.dumps(transaction), 'utf-8')) del bridge.ports[port.uuid] responses = self.get_responses(self.sock.recv(self.BUFF_SIZE)) for response in responses: res = json.loads(response) transaction.handleResult(res) self.update_notification(res) self.cur_id += 1 return True
def removePort(self, port_name): if port_name not in \ [port.name for port in list(self.ovs_state.ports.values())]: raise OVSExceptions.OVSNoSuchPort(port_name) switch = self.ovs_state.switch transaction = Transaction(self.cur_id) for instance in list(self.ovs_state.bridges.values()) + list( self.ovs_state.ports.values()): transaction.addOperation(WaitOperation(instance)) port = None for p in list(self.ovs_state.ports.values()): if p.name == port_name: port = p break bridge = self.ovs_state.removePort(port.uuid) transaction.addOperation(UpdateOperation(bridge, 'ports')) transaction.addOperation(MutateOperation(switch, 'next_cfg', '+=')) self.sock.send(bytes(json.dumps(transaction), 'utf-8')) responses = self.get_responses(self.sock.recv(self.BUFF_SIZE)) for response in responses: res = json.loads(response) transaction.handleResult(res) self.cur_id += 1 return True
def remove(self, val): isAtom = False if val == 'set': raise OVSExceptions.OVSValueError("scalar value can't be removed") if len(self) == 3 and self[0] == 'set': isAtom = True super(OVSSet, self).remove(val) if isAtom: self = self[1]
def handleResult(self, response): if 'result' not in response: return print(response) result = response['result'] if 'error' in result[-1]: raise OVSExceptions.OVSTransactionFailed(result) for index, param in enumerate(self.params[1:]): param.handleResult(result[index])
def commit(self, transaction): if not isinstance(transaction, Transaction): raise OVSExceptions.OVSValueError("Unexpected Transaction Value") self.sock.send(bytes(json.dumps(transaction)), 'utf-8') responses = self.get_responses(self.sock.recv(self.BUFF_SIZE)) for response in responses: res = json.loads(response) transaction.handleResult(res) self.update_notification(res) self.cur_id += 1
def handleResult(self, response): if 'result' not in response: return False elif response['id'] != self['id']: return False result = response['result'] if len(result) != 0 and 'error' in result[-1]: raise OVSExceptions.OVSTransactionFailed(result) for index, param in enumerate(self.params[1:]): param.handleResult(result[index]) return True
def append(self, atom): if not isinstance(atom, OVSAtom): raise OVSExceptions.OVSValueError("value must be an OVSAtom") if len(self) == 2 and self[0] == 'set': self = [] for val in atom: super(OVSSet, self).append(val) return elif len(self) == 2 and self[0] != 'set': tmp_atom = [self[0], self[1]] self = [] self.append('set') self.append(tmp_atom) self.append(atom) return super(OVSSet, self).append(atom)
def addBridge(self, bridge_name): switch = self.ovs_state.switch if bridge_name in \ [br.name for br in list(self.ovs_state.bridges.values()) if br.name == bridge_name]: raise OVSExceptions.OVSBridgeExists(bridge_name) transaction = Transaction(self.cur_id) # Generate Wait operations for instance in list(self.ovs_state.ports.values()) + list( self.ovs_state.bridges.values()): transaction.addOperation(WaitOperation(instance)) # Build sub-components of a bridge intern_if = OVSInterface(bridge_name) intern_if.type = 'internal' intern_port = OVSPort(bridge_name, [intern_if]) bridge = OVSBridge(bridge_name, [intern_port]) # Generate Insert Operations for built components transaction.addOperation(InsertOperation(intern_if)) transaction.addOperation(InsertOperation(intern_port)) transaction.addOperation(InsertOperation(bridge)) switch.addBridge(bridge) transaction.addOperation(UpdateOperation(switch, ['bridges'])) transaction.addOperation(MutateOperation(switch, 'next_cfg', '+=')) self.sock.send(bytes(json.dumps(transaction), 'utf-8')) del switch.bridges[bridge.uuid] handled = False while not handled: responses = self.get_responses(self.sock.recv(self.BUFF_SIZE)) for response in responses: res = json.loads(response) handled = transaction.handleResult(res) self.update_notification(res) self.cur_id += 1
def removeBridge(self, bridge_name): switch = self.ovs_state.switch bridge = None if bridge_name in \ [br.name for br in list(switch.bridges.values())]: bridge = [ br for br in list(switch.bridges.values()) if br.name == bridge_name ][0] else: raise OVSExceptions.OVSNoSuchBridge(bridge_name) transaction = Transaction(self.cur_id) transaction.addOperation(WaitOperation(switch)) self.ovs_state.removeBridge(bridge.uuid) transaction.addOperation(UpdateOperation(switch, ['bridges'])) transaction.addOperation(MutateOperation(switch, 'next_cfg', '+=')) self.sock.send(bytes(json.dumps(transaction), 'utf-8')) responses = self.get_responses(self.sock.recv(self.BUFF_SIZE)) for response in responses: res = json.loads(response) transaction.handleResult(res) self.cur_id += 1
def __init__(self, l=None): if l and len(l) > 2: raise OVSExceptions.OVSValueError( "Atom shouldn't be more than 2 fields") super(OVSAtom, self).__init__(l)
def pop(self, index): if index == 0: raise OVSExceptions.OVSValueError("scalar value can't be removed") super(OVSSet, self).pop(index)
def insert(self, index, atom): if not isinstance(atom, OVSAtom): raise OVSExceptions.OVSValueError("value must be an OVSAtom") super(OVSSet, self).insert(index, atom)