def set_isolation(self, port): """set_isolation .""" try: client = snmp_client.get_client(self._get_switch_dict(port)) seg_id = port['port']['segmentation_id'] vlan_oid = constants.OID_VLAN_CREATE + '.' + str(seg_id) egress_oid = constants.OID_VLAN_EGRESS_PORT + '.' + str(seg_id) snmp_response = self._snmp_get(client, vlan_oid) no_such_instance_exists = False if snmp_response: for oid, val in snmp_response: value = val.prettyPrint() if constants.SNMP_NO_SUCH_INSTANCE in value: # Fixed for pysnmp versioning issue no_such_instance_exists = True break if not snmp_response or no_such_instance_exists: client.set(vlan_oid, client.get_rfc1902_integer(4)) nibble_byte = self._get_device_nibble_map(client, egress_oid) ifindex = self._get_ifindex_for_port(port) bit_map = client.get_bit_map_for_add(int(ifindex), nibble_byte) bit_list = [] for line in bit_map: bit_list.append(line) set_string = client.get_rfc1902_octet_string(''.join(bit_list)) client.set(egress_oid, set_string) except Exception as e: LOG.error(_LE("Exception in configuring VLAN '%s' "), e) raise exceptions.SNMPFailure(operation="SET", error=e)
def test_set_isolation(self): self.port = self._get_port_payload() self.client = snmp_client.get_client(self.snmp_info) seg_id = 1001 egress_oid = hp_const.OID_VLAN_EGRESS_PORT + '.' + str(seg_id) egress_byte = [] oct_str = rfc1902.OctetString('') with contextlib.nested(mock.patch.object(snmp_client, 'get_client', return_value=self.client), mock.patch.object(snmp_driver.SNMPDriver, '_snmp_get', return_value=None), mock.patch.object(snmp_client.SNMPClient, 'set', return_value=None), mock.patch.object(snmp_driver.SNMPDriver, '_get_device_nibble_map', return_value=None), mock.patch.object(snmp_client.SNMPClient, 'get_bit_map_for_add', return_value=egress_byte)): self.driver.set_isolation(self.port) snmp_client.get_client.called snmp_driver.SNMPDriver._snmp_get.called snmp_client.SNMPClient.set.called snmp_driver.SNMPDriver._get_device_nibble_map.called snmp_client.SNMPClient.get_bit_map_for_add.called snmp_client.SNMPClient.set.assert_called_with(egress_oid, oct_str)
def test__get_device_nibble_map(self): self.client = snmp_client.get_client(self.snmp_info) seg_id = 1001 egrs_oid = hp_const.OID_VLAN_EGRESS_PORT + '.' + str(seg_id) varbinds = [(rfc1902.ObjectName('1.3.6.1.2.1.17.7.1.4.3.1.2.1001'), rfc1902.OctetString('\x80'))] with contextlib.nested(mock.patch.object(snmp_client.SNMPClient, 'get', return_value=varbinds)): egbytes = self.driver._get_device_nibble_map(self.client, egrs_oid) self.assertEqual(egbytes, '\x80')
def get_mac_addr(self, snmp_info): """retrieves switch MAC address.""" client = snmp_client.get_client(snmp_info) oid = constants.OID_MAC_ADDRESS var_binds = client.get(oid) for name, val in var_binds: mac = val.prettyPrint().zfill(12) mac = mac[2:] mac_addr = ':'.join([mac[i:i + 2] for i in range(0, 12, 2)]) return mac_addr
def get_protocol_validation_result(self, credentials): """Get protocol validation result by fetching device MAC.""" client = snmp_client.get_client(credentials) oid = constants.OID_MAC_ADDRESS var_binds = self._snmp_get(client, oid) for name, val in var_binds: mac = val.prettyPrint().zfill(12) mac = mac[2:] mac_addr = ':'.join([mac[i:i + 2] for i in range(0, 12, 2)]) return mac_addr
def test__get_device_nibble_map(self): self.client = snmp_client.get_client(self.snmp_info) seg_id = 1001 egrs_oid = hp_const.OID_VLAN_EGRESS_PORT + '.' + str(seg_id) varbinds = [(rfc1902.ObjectName('1.3.6.1.2.1.17.7.1.4.3.1.2.1001'), rfc1902.OctetString('\x80'))] with contextlib.nested( mock.patch.object(snmp_client.SNMPClient, 'get', return_value=varbinds)): egbytes = self.driver._get_device_nibble_map(self.client, egrs_oid) self.assertEqual(egbytes, '\x80')
def get_ports_status(self, snmp_info): """retrieves port status.""" client = snmp_client.get_client(snmp_info) oids = [constants.OID_IF_INDEX, constants.OID_PORT_STATUS] var_binds = client.get_bulk(*oids) ports_dict = [] for var_bind_table_row in var_binds: if_index = (var_bind_table_row[0][1]).prettyPrint() ports_dict.append( {'ifindex': if_index, 'port_status': var_bind_table_row[1][1].prettyPrint()}) return ports_dict
def test_set_isolation_exception(self): self.port = self._get_port_payload() self.client = snmp_client.get_client(self.snmp_info) with contextlib.nested( mock.patch.object(snmp_client, 'get_client', return_value=self.client), mock.patch.object(prov_driver.SNMPProvisioningDriver, '_snmp_get', return_value=None), mock.patch.object(snmp_client.SNMPClient, 'set', side_effect=exceptions.SNMPFailure)): self.assertRaises(exceptions.SNMPFailure, self.driver.set_isolation, self.port)
def test_delete_isolation(self): self.port = self._get_port_payload() self.client = snmp_client.get_client(self.snmp_info) egress_byte = [] prov_driver_instance = prov_driver.SNMPProvisioningDriver with contextlib.nested(mock.patch.object(snmp_client, 'get_client', return_value=self.client), mock.patch.object(snmp_client.SNMPClient, 'set', return_value=None), mock.patch.object(prov_driver_instance, '_get_device_nibble_map', return_value=None), mock.patch.object(snmp_client.SNMPClient, 'get_bit_map_for_del', return_value=egress_byte)): self.driver.delete_isolation(self.port)
def test_delete_isolation(self): self.port = self._get_port_payload() self.client = snmp_client.get_client(self.snmp_info) egress_byte = [] prov_driver_instance = prov_driver.SNMPProvisioningDriver with contextlib.nested( mock.patch.object(snmp_client, 'get_client', return_value=self.client), mock.patch.object(snmp_client.SNMPClient, 'set', return_value=None), mock.patch.object(prov_driver_instance, '_get_device_nibble_map', return_value=None), mock.patch.object(snmp_client.SNMPClient, 'get_bit_map_for_del', return_value=egress_byte)): self.driver.delete_isolation(self.port)
def delete_isolation(self, port): """delete_isolation deletes the vlan from the physical ports.""" try: client = snmp_client.get_client(self._get_switch_dict(port)) seg_id = port['port']['segmentation_id'] egress_oid = constants.OID_VLAN_EGRESS_PORT + '.' + str(seg_id) nibble_byte = self._get_device_nibble_map(client, egress_oid) ifindex = port['port']['ifindex'] bit_map = client.get_bit_map_for_del(int(ifindex), nibble_byte) bit_list = [] for line in bit_map: bit_list.append(line) set_string = client.get_rfc1902_octet_string(''.join(bit_list)) client.set(egress_oid, set_string) # On port delete removing interface from target vlan, # not deleting global vlan on device except Exception as e: LOG.error(_LE("Exception in deleting VLAN '%s' "), e) raise exceptions.SNMPFailure(operation="SET", error=e)
def _get_ports_info(self, snmp_info): """retrieves switch port information.""" client = snmp_client.get_client(self._get_switch_dict(snmp_info)) oids = [constants.OID_IF_INDEX, constants.OID_PORTS, constants.OID_IF_TYPE, constants.OID_PORT_STATUS] var_binds = client.get_bulk(*oids) ports_list = [] for var_bind_table_row in var_binds: if_index = (var_bind_table_row[0][1]).prettyPrint() port_name = (var_bind_table_row[1][1]).prettyPrint() if_type = (var_bind_table_row[2][1]).prettyPrint() if if_type == constants.PHY_PORT_TYPE: ports_list.append( {'ifindex': if_index, 'interface_name': port_name, 'port_status': var_bind_table_row[3][1].prettyPrint()}) return ports_list
def test_delete_isolation_exception(self): self.port = self._get_port_payload() self.client = snmp_client.get_client(self.snmp_info) egress_byte = [] with contextlib.nested( mock.patch.object(snmp_client, 'get_client', return_value=self.client), mock.patch.object(prov_driver.SNMPProvisioningDriver, '_snmp_get', return_value=None), mock.patch.object(prov_driver.SNMPProvisioningDriver, '_get_device_nibble_map', return_value=None), mock.patch.object(snmp_client.SNMPClient, 'get_bit_map_for_del', return_value=egress_byte), mock.patch.object(snmp_client.SNMPClient, 'set', side_effect=exceptions.SNMPFailure)): self.assertRaises(exceptions.SNMPFailure, self.driver.delete_isolation, self.port)
def _get_ports_info(self, snmp_info): """retrieves switch port information.""" client = snmp_client.get_client(self._get_switch_dict(snmp_info)) oids = [ constants.OID_IF_INDEX, constants.OID_PORTS, constants.OID_IF_TYPE, constants.OID_PORT_STATUS ] var_binds = client.get_bulk(*oids) ports_list = [] for var_bind_table_row in var_binds: if_index = (var_bind_table_row[0][1]).prettyPrint() port_name = (var_bind_table_row[1][1]).prettyPrint() if_type = (var_bind_table_row[2][1]).prettyPrint() if if_type == constants.PHY_PORT_TYPE: ports_list.append({ 'ifindex': if_index, 'interface_name': port_name, 'port_status': var_bind_table_row[3][1].prettyPrint() }) return ports_list
def __init__(self, snmp_info): self.snmp_info = snmp_info self.client = snmp_client.get_client(snmp_info)
def get_sys_name(self, snmp_info): """fetches system name.""" client = snmp_client.get_client(snmp_info) oid = constants.OID_SYS_NAME client.get(oid)