class NetconfConnection(AbstractConnection): ''' Netconf connection class ''' def __init__(self, host, username='******', password='******', logger=None, **kwargs): self.host = host self.username = username self.password = password self.handle = None self.logger = kwargs.get('logger', contrail_logging.getLogger(__name__)) self.config_handle = None self.mode = kwargs.get('mode') self.port = kwargs.get('port',None) def connect(self): if self.port: self.handle = Device(host=self.host, user=self.username, password=self.password, mode=self.mode, port=self.port) else: self.handle = Device(host=self.host, user=self.username, password=self.password, mode=self.mode) try: self.handle.open(gather_facts=False) self.config_handle = Config(self.handle) except (ConnectAuthError,ConnectRefusedError, ConnectTimeoutError, ConnectError) as e: self.logger.exception(e) return self.handle # end connect def disconnect(self): with gevent.Timeout(15.0, False): self.handle.close() def show_version(self): return self.handle.show_version() def zeroize(self): with gevent.Timeout(15.0, False): try: self.handle.zeroize() except Exception as e: pass def get_config(self, mode='set',filter=None): configs = self.handle.rpc.get_config(filter_xml=filter,options={'database' : 'committed', 'format': mode}) return configs.text def get_interfaces(self, terse=True): output = self.handle.rpc.get_interface_information(terse=terse) return EtreeToDict('physical-interface').get_all_entry(output) def get_bgp_peer_count(self): output = self.handle.rpc.get_bgp_summary_information() return EtreeToDict('peer-count').get_all_entry(output) def config(self, stmts=[], commit=True, merge=True, overwrite=False, path=None, ignore_errors=False, timeout=30, url=None): if url: try: self.config_handle.load(url=url,format='text',overwrite=True) self.config_handle.commit(override=True) except RpcTimeoutError as e: self.logger.debug('Exception %s ignored' % (e)) return (True, None) elif path: self.config_handle.load(path=path, overwrite=overwrite, timeout=timeout) else: for stmt in stmts: try: self.config_handle.load(stmt, format='set', merge=True) except ConfigLoadError as e: if ignore_errors: self.logger.debug('Exception %s ignored' % (e)) self.logger.exception(e) else: raise e if commit: try: self.config_handle.commit(timeout=timeout) except CommitError as e: self.logger.exception(e) return (False,e) return (True, None) def configure_interface(self, pi_name, address, mask): stmt = "set interfaces %s unit 0 family inet address %s/%s"%( pi_name, address, mask) self.config([stmt]) def delete_interface(self, pi_name): stmt = "delete interfaces %s unit 0"%pi_name self.config([stmt]) def restart(self, process_name): #TODO Not sure of apis other than cli self.handle.cli('restart %s' % (process_name)) def get_mac_address(self, interface): # Use physical interface interface = interface.split('.')[0] xml_resp = self.handle.rpc.get_interface_information(interface_name=interface) mac_address = xml_resp.findtext( 'physical-interface/current-physical-address') return mac_address.rstrip('\n').lstrip('\n') # end get_mac_address def get_mac_in_arp_table(self, ip_address): # From 'show arp' output, get the MAC address # of a IP xml_resp = self.handle.rpc.get_arp_table_information(no_resolve=True) arp_entries = xml_resp.findall('arp-table-entry') for arp_entry in arp_entries: if arp_entry.find('ip-address').text.strip() == ip_address: mac = arp_entry.find('mac-address').text.strip() self.logger.debug('Found MAC %s for IP %s in arp table of ' '%s' % (mac, ip_address, self.host)) return mac self.logger.warn('IP %s not found in arp table of %s' % ( ip_address, self.host)) return None # end get_mac_in_arp_table def get_assisted_replicated_role(self): output = self.handle.rpc.get_evpn_multicast_snooping_ar_replicators_data() return EtreeToDict('evpn-multicast-snooping-ar-replicators-per-instance').get_all_entry(output) def get_interfaces_vtep(self): output = self.handle.rpc.get_interface_information(interface_name='vtep') return EtreeToDict('physical-interface/logical-interface').get_all_entry(output) def clear_interfaces_statistics(self): return self.handle.rpc.clear_interfaces_statistics_all()
class NetconfConnection(AbstractConnection): ''' Netconf connection class ''' def __init__(self, host, username='******', password='******', logger=None, **kwargs): self.host = host self.username = username self.password = password self.handle = None self.logger = kwargs.get('logger', contrail_logging.getLogger(__name__)) self.config_handle = None self.mode = kwargs.get('mode') def connect(self): self.handle = Device(host=self.host, user=self.username, password=self.password, mode=self.mode) try: self.handle.open(gather_facts=False) self.config_handle = Config(self.handle) except (ConnectAuthError, ConnectRefusedError, ConnectTimeoutError, ConnectError) as e: self.logger.exception(e) return self.handle # end connect def disconnect(self): with gevent.Timeout(15.0, False): self.handle.close() def show_version(self): return self.handle.show_version() def zeroize(self): with gevent.Timeout(15.0, False): try: self.handle.zeroize() except Exception as e: pass def get_config(self, mode='set'): configs = self.handle.rpc.get_config(options={ 'database': 'committed', 'format': mode }) return configs.text def get_interfaces(self, terse=True): output = self.handle.rpc.get_interface_information(terse=terse) return EtreeToDict('physical-interface').get_all_entry(output) def config(self, stmts=[], commit=True, merge=True, overwrite=False, path=None, ignore_errors=False, timeout=30): if path: self.config_handle.load(path=path, overwrite=overwrite, timeout=timeout) else: for stmt in stmts: try: self.config_handle.load(stmt, format='set', merge=True) except ConfigLoadError as e: if ignore_errors: self.logger.debug('Exception %s ignored' % (e)) self.logger.exception(e) else: raise e if commit: try: self.config_handle.commit(timeout=timeout) except CommitError as e: self.logger.exception(e) return (False, e) return (True, None) def restart(self, process_name): #TODO Not sure of apis other than cli self.handle.cli('restart %s' % (process_name)) def get_mac_address(self, interface): # Use physical interface interface = interface.split('.')[0] xml_resp = self.handle.rpc.get_interface_information( interface_name=interface) mac_address = xml_resp.findtext( 'physical-interface/current-physical-address') return mac_address.rstrip('\n').lstrip('\n') # end get_mac_address def get_mac_in_arp_table(self, ip_address): # From 'show arp' output, get the MAC address # of a IP xml_resp = self.handle.rpc.get_arp_table_information(no_resolve=True) arp_entries = xml_resp.findall('arp-table-entry') for arp_entry in arp_entries: if arp_entry.find('ip-address').text.strip() == ip_address: mac = arp_entry.find('mac-address').text.strip() self.logger.debug('Found MAC %s for IP %s in arp table of ' '%s' % (mac, ip_address, self.host)) return mac self.logger.warn('IP %s not found in arp table of %s' % (ip_address, self.host)) return None