def metaparser_on_device_alias_context(self, parser, device, alias, context='cli'): '''Call any `metaparser` parser and parse the device using a specific alias with a context (cli, xml, yang, ...) ''' device_handle = self._search_device(device) # Look for the alias. If it doesnt exist, let it crash to the user as # only valid alias should be provided con = device_handle if alias: con = getattr(device_handle, alias) # We dont know which of the python path is the package, # or even if there is an abstract package. It would be easy to fix, but # would need to ask for package name in Robot. It is decided to not # ask, but only support genie.libs.parser.<...> for abstraction, and everything # else would be just straight import package = 'genie.libs.parser' if package in parser: attr_name = parser.replace(package + '.', '') else: # then no abstraction, just load it package = None attr_name = parser # Find the right library with abstraction if package is not None try: cls = load_attribute(package, attr_name, device=device_handle) except Exception as e: if package: msg = "Could not find {p}.'{a}'".format(p=package, a=attr_name) else: msg = "Could not find '{a}'".format(a=attr_name) raise Exception(msg) from e # Instantiate the parser with the connection implementation parser = cls(con) return getattr(parser, context)()
def genie_ops_on_device_alias_context(self, feature, device, alias, context='cli'): '''Learn Ops feature on device using a specific alias with a context (cli, xml, yang, ...) ''' device_handle = self._search_device(device) # Look for the alias. If it doesnt exist, let it crash to the user as # only valid alias should be provided con = device_handle if alias: con = getattr(device_handle, alias) device_handle.mapping[alias] = con # Find the feature for this device # 1) Directory must exists in genie.libs.ops.<feature> # 2) Then abstraction will kick in to find the right one. # 3) The directory syntax is <feature>.<feature.<Feature> # Where the class is capitalized but the directory/files arent. # First import genie.libs for abstraction package = 'genie.libs.ops' try: mod = importlib.import_module(package) except ImportError as e: raise ImportError("package 'genie' and library 'genie.libs' " "are mandatory to have to learn '{f}' ".format( f=feature)) from e # Now find the right library attr_name = '.'.join( [feature.lower(), feature.lower(), feature.title()]) # Find the right library with abstraction if needed # Get context in there added_context = False if hasattr(device_handle, 'custom') and\ 'abstraction' in device_handle.custom and\ 'order' in device_handle.custom['abstraction']: # Add context to it backup_abstraction = deepcopy(device_handle.custom['abstraction']) device_handle.custom['abstraction']['order'].append('context') device_handle.custom['abstraction']['context'] = context added_context = True try: cls = load_attribute(package, attr_name, device=device_handle) except Exception as e: msg = "Could not find {p}.{a} for device {d}"\ .format(p=package, a=attr_name, d=device_handle.name) raise Exception(msg) from e if added_context: device_handle.custom['abstraction'] = backup_abstraction # Call the Ops now ops = cls(device_handle) ops.learn() return ops