Source code for ixnetwork.api.ixn_python
"""
:author: yoram@ignissoft.com
"""
from sys import platform
import logging
import itertools
import posixpath
import imp
if platform == 'win32':
py_tail = 'API/Python/IxNetwork.py'
else:
py_tail = 'lib/PythonApi/IxNetwork.py'
[docs]class IxnPythonWrapper(object):
def __init__(self, logger, ixn_install_dir):
""" Init IXN Python package.
:param looger: application logger, if stream handler and log level is DEBUG -> enable IXN Python debug.
:param ixn_install_dir: full path to IXN installation directory up to (including) version number.
:todo: Add logger to log IXN Python package commands only to create a clean Python script for debug.
"""
super(self.__class__, self).__init__()
ixn_python_module = posixpath.sep.join([ixn_install_dir, py_tail])
self.ixn = imp.load_source('IxNet', ixn_python_module).IxNet()
stream_handler = None
for handler in logger.handlers:
if isinstance(handler, logging.StreamHandler):
stream_handler = handler
if stream_handler and logger.getEffectiveLevel() == 10:
self.ixn.setDebug(True)
[docs] def getVersion(self):
return self.ixn.getVersion()
[docs] def connect(self, ip, port):
return self.ixn.connect(ip, '-port', port, '-version', self.getVersion())
[docs] def getRoot(self):
return self.ixn.getRoot()
[docs] def commit(self):
self.ixn.commit()
[docs] def execute(self, command, *arguments):
return self.ixn.execute(command, *arguments)
[docs] def newConfig(self):
self.execute('newConfig')
[docs] def loadConfig(self, configFileName):
self.execute('loadConfig', self.ixn.readFrom(configFileName.replace('\\', '/')))
[docs] def saveConfig(self, configFileName):
self.execute('saveConfig', self.ixn.writeTo(configFileName.replace('\\', '/')))
[docs] def getList(self, objRef, childList):
return self.ixn.getList(objRef, childList)
[docs] def getAttribute(self, objRef, attribute):
return self.ixn.getAttribute(objRef, '-' + attribute)
[docs] def getListAttribute(self, objRef, attribute):
value = self.getAttribute(objRef, attribute)
return [v[0] for v in value] if type(value[0]) is list else value
[docs] def help(self, objRef):
return self.ixn.help(objRef)
[docs] def add(self, parent, obj_type, **attributes):
""" IXN API add command
@param parent: object parent - object will be created under this parent.
@param object_type: object type.
@param attributes: additional attributes.
@return: STC object reference.
"""
return self.ixn.add(parent.obj_ref(), obj_type, *self._get_args_list(**attributes))
[docs] def setAttributes(self, objRef, **attributes):
self.ixn.setMultiAttribute(objRef, *self._get_args_list(**attributes))
[docs] def remapIds(self, objRef):
return self.ixn.remapIds(objRef)[0]
def _get_args_list(self, **attributes):
keys = ['-' + attribute for attribute in attributes.keys()]
return itertools.chain.from_iterable(zip(keys, attributes.values()))