def _update_attrs(cls, node_data): node_db = db().query(Node).get(node_data['id']) is_ether = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.ether is_bond = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.bond interfaces = filter(is_ether, node_data['interfaces']) bond_interfaces = filter(is_bond, node_data['interfaces']) interfaces_db = node_db.nic_interfaces bond_interfaces_db = node_db.bond_interfaces for iface in interfaces: current_iface = filter(lambda i: i.id == iface['id'], interfaces_db)[0] # Remove all old network's assignment for this interface. db().query(NetworkNICAssignment).filter_by( interface_id=current_iface.id).delete() for net in iface['assigned_networks']: net_assignment = NetworkNICAssignment() net_assignment.network_id = net['id'] net_assignment.interface_id = current_iface.id db().add(net_assignment) if 'interface_properties' in iface: current_iface.interface_properties = iface[ 'interface_properties'] map(db().delete, bond_interfaces_db) db().commit() for bond in bond_interfaces: bond_db = NodeBondInterface() bond_db.node_id = node_db.id db().add(bond_db) bond_db.name = bond['name'] #bond_db.mode = bond['mode'] bond_db.mac = bond.get('mac') #bond_db.flags = bond.get('flags', {}) if bond.get('bond_properties', {}).get('mode'): bond_db.mode = bond['bond_properties']['mode'] else: bond_db.mode = bond['mode'] #bond_db.mode = consts.OVS_BOND_MODES[0] bond_db.bond_properties = bond.get('bond_properties', {}) bond_db.interface_properties = bond.get('interface_properties', {}) db().commit() db().refresh(bond_db) # Add new network assignment. map(bond_db.assigned_networks_list.append, [ db().query(NetworkGroup).get(ng['id']) for ng in bond['assigned_networks'] ]) # Add new slaves. for nic in bond['slaves']: bond_db.slaves.append(db().query(NodeNICInterface).filter_by( name=nic['name']).filter_by(node_id=node_db.id).first()) db().commit() return node_db.id
def test_update_offloading_modes_for_bond_interface(self): different_modes = [ [{ 'name': 'mode_for_nic1', 'state': None, 'sub': [ { 'name': 'sub_mode_for_nic1', 'state': None, 'sub': [] } ] }], [{ 'name': 'mode_for_nic2', 'state': None, 'sub': [] }], ] nics = [] for i in xrange(2): nic_data = copy.deepcopy(self.sample_nic_interface_data) nic_data['offloading_modes'] = \ self.unchanged_modes + different_modes[i] nics.append(NodeNICInterface(**nic_data)) sample_bond_data = { 'node_id': 1, 'name': 'test_bond_interface', 'mode': 'active-backup', 'bond_properties': jsonutils.dumps( {'test_property': 'test_value'}) } bond = NodeBondInterface(**sample_bond_data) bond.slaves = nics bond_modes = bond.offloading_modes self.assertListEqual(self.unchanged_modes, bond_modes) bond.offloading_modes = self.changed_modes bond_modes = bond.offloading_modes self.assertListEqual(self.changed_modes, bond_modes) for i in xrange(2): self.assertListEqual(self.changed_modes + different_modes[i], nics[i].offloading_modes)
def _update_attrs(cls, node_data): node_db = db().query(Node).get(node_data['id']) is_ether = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.ether is_bond = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.bond interfaces = filter(is_ether, node_data['interfaces']) bond_interfaces = filter(is_bond, node_data['interfaces']) interfaces_db = node_db.nic_interfaces bond_interfaces_db = node_db.bond_interfaces for iface in interfaces: current_iface = filter( lambda i: i.id == iface['id'], interfaces_db )[0] # Remove all old network's assignment for this interface. db().query(NetworkNICAssignment).filter_by( interface_id=current_iface.id ).delete() for net in iface['assigned_networks']: net_assignment = NetworkNICAssignment() net_assignment.network_id = net['id'] net_assignment.interface_id = current_iface.id db().add(net_assignment) if 'interface_properties' in iface: current_iface.interface_properties = \ iface['interface_properties'] map(db().delete, bond_interfaces_db) db().commit() for bond in bond_interfaces: bond_db = NodeBondInterface() bond_db.node_id = node_db.id db().add(bond_db) bond_db.name = bond['name'] if bond.get('bond_properties', {}).get('mode'): bond_db.mode = bond['bond_properties']['mode'] else: bond_db.mode = bond['mode'] bond_db.mac = bond.get('mac') bond_db.bond_properties = bond.get('bond_properties', {}) bond_db.interface_properties = bond.get('interface_properties', {}) db().commit() db().refresh(bond_db) # Add new network assignment. map(bond_db.assigned_networks_list.append, [db().query(NetworkGroup).get(ng['id']) for ng in bond['assigned_networks']]) # Add new slaves. for nic in bond['slaves']: bond_db.slaves.append( db().query(NodeNICInterface).filter_by( name=nic['name'] ).filter_by( node_id=node_db.id ).first() ) db().commit() return node_db.id
def test_delete_bond_and_networks_state_on_unassigmnet(self): """Test verifies that 1. bond configuration will be deleted 2. network unassigned from node interfaces when node unnasigned from cluster """ cluster = self.env.create( nodes_kwargs=[{}] ) node = self.env.nodes[0] node.bond_interfaces.append( NodeBondInterface(name='ovs-bond0', slaves=node.nic_interfaces)) self.db.flush() resp = self.app.post( reverse( 'NodeUnassignmentHandler', kwargs={'cluster_id': cluster['id']} ), jsonutils.dumps([{'id': node.id}]), headers=self.default_headers ) self.assertEqual(resp.status_code, 200) self.assertEqual(node.bond_interfaces, []) for interface in node.interfaces: self.assertEqual(interface.assigned_networks_list, [])
def test_get_bond_interfaces_for_all_nodes(self): node = self.env.nodes[0] node.bond_interfaces.append( NodeBondInterface(name='ovs-bond0', slaves=node.nic_interfaces)) self.db.flush() bond_interfaces = objects.Cluster.get_bond_interfaces_for_all_nodes( self.env.clusters[0]) self.assertEqual(len(bond_interfaces), 1)
def _update_attrs(cls, node_data): node_db = db().query(Node).get(node_data['id']) is_ether = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.ether is_bond = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.bond interfaces = filter(is_ether, node_data['interfaces']) bond_interfaces = filter(is_bond, node_data['interfaces']) interfaces_db = node_db.nic_interfaces bond_interfaces_db = node_db.bond_interfaces for iface in interfaces: current_iface = filter( lambda i: i.id == iface['id'], interfaces_db )[0] # Remove all old network's assignment for this interface. db().query(NetworkNICAssignment).filter_by( interface_id=current_iface.id ).delete() for net in iface['assigned_networks']: net_assignment = NetworkNICAssignment() net_assignment.network_id = net['id'] net_assignment.interface_id = current_iface.id db().add(net_assignment) db().commit() # Remove bonds from DB if they are not in a received data. received_bond_ids = [x['id'] for x in bond_interfaces if 'id' in x] unused_bonds = filter(lambda x: x.id not in received_bond_ids, bond_interfaces_db) map(db().delete, unused_bonds) db().commit() for bond in bond_interfaces: if 'id' in bond: bond_db = filter( lambda i: i.id == bond['id'], bond_interfaces_db )[0] # Clear all previous assigned networks. map(bond_db.assigned_networks_list.remove, list(bond_db.assigned_networks_list)) # Clear all previous assigned slaves. map(bond_db.slaves.remove, list(bond_db.slaves)) else: # Create a bond if not exists. bond_db = NodeBondInterface() bond_db.node_id = node_db.id db().add(bond_db) bond_db.name = bond['name'] if 'mode' in bond: bond_db.mode = bond['mode'] bond_db.mac = bond.get('mac') bond_db.flags = bond.get('flags', {}) db().commit() db().refresh(bond_db) # Add new network assignment. map(bond_db.assigned_networks_list.append, [db().query(NetworkGroup).get(ng['id']) for ng in bond['assigned_networks']]) # Add new slaves. for nic in bond['slaves']: bond_db.slaves.append( db().query(NodeNICInterface).filter_by( name=nic['name'] ).filter_by( node_id=node_db.id ).first() ) db().commit() return node_db.id
def _update_attrs(cls, node_data): node_db = db().query(Node).get(node_data['id']) is_ether = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.ether is_bond = lambda x: x['type'] == consts.NETWORK_INTERFACE_TYPES.bond interfaces = filter(is_ether, node_data['interfaces']) bond_interfaces = filter(is_bond, node_data['interfaces']) interfaces_db = node_db.nic_interfaces bond_interfaces_db = node_db.bond_interfaces for iface in interfaces: current_iface = filter(lambda i: i.id == iface['id'], interfaces_db)[0] # Remove all old network's assignment for this interface. db().query(NetworkNICAssignment).filter_by( interface_id=current_iface.id).delete() for net in iface['assigned_networks']: net_assignment = NetworkNICAssignment() net_assignment.network_id = net['id'] net_assignment.interface_id = current_iface.id db().add(net_assignment) db().commit() # Remove bonds from DB if they are not in a received data. received_bond_ids = [x['id'] for x in bond_interfaces if 'id' in x] unused_bonds = filter(lambda x: x.id not in received_bond_ids, bond_interfaces_db) map(db().delete, unused_bonds) db().commit() for bond in bond_interfaces: if 'id' in bond: bond_db = filter(lambda i: i.id == bond['id'], bond_interfaces_db)[0] # Clear all previous assigned networks. map(bond_db.assigned_networks_list.remove, list(bond_db.assigned_networks_list)) # Clear all previous assigned slaves. map(bond_db.slaves.remove, list(bond_db.slaves)) else: # Create a bond if not exists. bond_db = NodeBondInterface() bond_db.node_id = node_db.id db().add(bond_db) bond_db.name = bond['name'] if 'mode' in bond: bond_db.mode = bond['mode'] bond_db.mac = bond.get('mac') bond_db.flags = bond.get('flags', {}) db().commit() db().refresh(bond_db) # Add new network assignment. map(bond_db.assigned_networks_list.append, [ db().query(NetworkGroup).get(ng['id']) for ng in bond['assigned_networks'] ]) # Add new slaves. for nic in bond['slaves']: bond_db.slaves.append(db().query(NodeNICInterface).filter_by( name=nic['name']).filter_by(node_id=node_db.id).first()) db().commit() return node_db.id