"Fossies" - the Fresh Open Source Software Archive

Member "opensaf-5.21.09/python/pyosaf/utils/immom/agent.py" (1 Jun 2021, 15947 Bytes) of package /linux/misc/opensaf-5.21.09.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "agent.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 5.21.03_vs_5.21.06.

    1 ############################################################################
    2 #
    3 # (C) Copyright 2014 The OpenSAF Foundation
    4 # (C) Copyright 2017 Ericsson AB. All rights reserved.
    5 #
    6 # This program is distributed in the hope that it will be useful, but
    7 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    8 # or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
    9 # under the GNU Lesser General Public License Version 2.1, February 1999.
   10 # The complete license can be accessed from the following location:
   11 # http://opensource.org/licenses/lgpl-license.php
   12 # See the Copying file included with the OpenSAF distribution for full
   13 # licensing terms.
   14 #
   15 # Author(s): Ericsson
   16 #
   17 ############################################################################
   18 """ IMM OM common utilities """
   19 import uuid
   20 from copy import deepcopy
   21 from ctypes import pointer
   22 
   23 from pyosaf import saImmOm, saImm
   24 from pyosaf.saAis import saAis, SaVersionT, SaNameT, SaAisErrorT, \
   25     eSaAisErrorT, eSaBoolT, unmarshalNullArray, SaStringT
   26 from pyosaf.saImm import eSaImmScopeT, SaImmClassNameT, SaImmAttrNameT
   27 from pyosaf.utils import decorate, initialize_decorate, log_err
   28 
   29 # Decorate pure saImmOm* API's with error-handling retry and exception raising
   30 saImmOmInitialize = initialize_decorate(saImmOm.saImmOmInitialize)
   31 saImmOmSelectionObjectGet = decorate(saImmOm.saImmOmSelectionObjectGet)
   32 saImmOmDispatch = decorate(saImmOm.saImmOmDispatch)
   33 saImmOmFinalize = decorate(saImmOm.saImmOmFinalize)
   34 saImmOmClassCreate_2 = decorate(saImmOm.saImmOmClassCreate_2)
   35 saImmOmClassDescriptionGet_2 = decorate(saImmOm.saImmOmClassDescriptionGet_2)
   36 saImmOmClassDescriptionMemoryFree_2 = \
   37     decorate(saImmOm.saImmOmClassDescriptionMemoryFree_2)
   38 saImmOmClassDelete = decorate(saImmOm.saImmOmClassDelete)
   39 saImmOmSearchInitialize_2 = decorate(saImmOm.saImmOmSearchInitialize_2)
   40 saImmOmSearchNext_2 = decorate(saImmOm.saImmOmSearchNext_2)
   41 saImmOmSearchFinalize = decorate(saImmOm.saImmOmSearchFinalize)
   42 saImmOmAccessorInitialize = decorate(saImmOm.saImmOmAccessorInitialize)
   43 saImmOmAccessorGet_2 = decorate(saImmOm.saImmOmAccessorGet_2)
   44 saImmOmAccessorFinalize = decorate(saImmOm.saImmOmAccessorFinalize)
   45 saImmOmAdminOwnerInitialize = decorate(saImmOm.saImmOmAdminOwnerInitialize)
   46 saImmOmAdminOwnerSet = decorate(saImmOm.saImmOmAdminOwnerSet)
   47 saImmOmAdminOwnerRelease = decorate(saImmOm.saImmOmAdminOwnerRelease)
   48 saImmOmAdminOwnerFinalize = decorate(saImmOm.saImmOmAdminOwnerFinalize)
   49 saImmOmAdminOwnerClear = decorate(saImmOm.saImmOmAdminOwnerClear)
   50 saImmOmCcbInitialize = decorate(saImmOm.saImmOmCcbInitialize)
   51 saImmOmCcbObjectCreate_2 = decorate(saImmOm.saImmOmCcbObjectCreate_2)
   52 saImmOmCcbObjectDelete = decorate(saImmOm.saImmOmCcbObjectDelete)
   53 saImmOmCcbObjectModify_2 = decorate(saImmOm.saImmOmCcbObjectModify_2)
   54 saImmOmCcbApply = decorate(saImmOm.saImmOmCcbApply)
   55 saImmOmCcbFinalize = decorate(saImmOm.saImmOmCcbFinalize)
   56 saImmOmCcbGetErrorStrings = decorate(saImmOm.saImmOmCcbGetErrorStrings)
   57 saImmOmAdminOperationInvoke_2 = decorate(saImmOm.saImmOmAdminOperationInvoke_2)
   58 saImmOmAdminOperationInvokeAsync_2 = \
   59     decorate(saImmOm.saImmOmAdminOperationInvokeAsync_2)
   60 saImmOmAdminOperationContinue = decorate(saImmOm.saImmOmAdminOperationContinue)
   61 saImmOmAdminOperationContinueAsync = \
   62     decorate(saImmOm.saImmOmAdminOperationContinueAsync)
   63 saImmOmAdminOperationContinuationClear = \
   64     decorate(saImmOm.saImmOmAdminOperationContinuationClear)
   65 
   66 
   67 class OmAgentManager(object):
   68     """ This class manages the life cycle of an IMM OM agent """
   69     def __init__(self, version=None):
   70         """ Constructor for OmAgentManager class
   71 
   72         Args:
   73             version (SaVersionT): IMM OM API version
   74         """
   75         self.init_version = version if version else SaVersionT('A', 2, 15)
   76         self.version = None
   77         self.handle = None
   78         self.callbacks = None
   79         self.selection_object = None
   80 
   81     def __enter__(self):
   82         """ Enter method for OmAgentManager class """
   83         return self
   84 
   85     def __exit__(self, exception_type, exception_value, traceback):
   86         """ Exit method for OmAgentManager class
   87 
   88         Finalize the IMM OM agent handle
   89         """
   90         if self.handle is not None:
   91             saImmOmFinalize(self.handle)
   92             self.handle = None
   93 
   94     def __del__(self):
   95         """ Destructor for OmAgentManager class
   96 
   97         Finalize the IMM OM agent handle
   98         """
   99         if self.handle is not None:
  100             saImmOm.saImmOmFinalize(self.handle)
  101             self.handle = None
  102 
  103     def initialize(self):
  104         """ Initialize the IMM OM library
  105 
  106         Returns:
  107             SaAisErrorT: Return code of the saImmOmInitialize() API call
  108         """
  109         self.handle = saImmOm.SaImmHandleT()
  110         self.version = deepcopy(self.init_version)
  111         rc = saImmOmInitialize(self.handle, None, self.version)
  112         if rc != eSaAisErrorT.SA_AIS_OK:
  113             log_err("saImmOmInitialize FAILED - %s" % eSaAisErrorT.whatis(rc))
  114 
  115         return rc
  116 
  117     def get_handle(self):
  118         """ Return the IMM OM agent handle successfully initialized
  119 
  120         Returns:
  121             SaImmHandleT: IMM OM agent handle
  122         """
  123         return self.handle
  124 
  125     def dispatch(self, flags):
  126         """ Invoke IMM callbacks for queued events
  127 
  128         Args:
  129             flags (eSaDispatchFlagsT): Flags specifying dispatch mode
  130 
  131         Returns:
  132             SaAisErrorT: Return code of the saImmOmDispatch() API call
  133         """
  134         rc = saImmOmDispatch(self.handle, flags)
  135         if rc != eSaAisErrorT.SA_AIS_OK:
  136             log_err("saImmOmDispatch FAILED - %s" % eSaAisErrorT.whatis(rc))
  137 
  138         return rc
  139 
  140     def finalize(self):
  141         """ Finalize the IMM OM agent handle
  142 
  143         Returns:
  144             SaAisErrorT: Return code of the saImmOmFinalize() API call
  145         """
  146         rc = eSaAisErrorT.SA_AIS_OK
  147         if self.handle:
  148             rc = saImmOmFinalize(self.handle)
  149             if rc != eSaAisErrorT.SA_AIS_OK:
  150                 log_err("saImmOmFinalize FAILED - %s" %
  151                         eSaAisErrorT.whatis(rc))
  152 
  153             if rc == eSaAisErrorT.SA_AIS_OK \
  154                     or rc == eSaAisErrorT.SA_AIS_ERR_BAD_HANDLE:
  155                 # If the Finalize() call returned BAD_HANDLE, the handle should
  156                 # already become stale and invalid, so we reset it anyway.
  157                 self.handle = None
  158         return rc
  159 
  160 
  161 class ImmOmAgent(OmAgentManager):
  162     """ This class acts as a high-level IMM OM agent, providing IMM OM
  163     functions to the users at a higher level, and relieving the users of the
  164     need to manage the life cycle of the IMM OM agent """
  165 
  166     def init(self):
  167         """ Initialize the IMM OM agent
  168 
  169         Returns:
  170             SaAisErrorT: Return code of the corresponding IMM API call(s)
  171         """
  172         # Clean previous resource if any
  173         self.finalize()
  174         return self.initialize()
  175 
  176     def clear_admin_owner(self, obj_name, scope=eSaImmScopeT.SA_IMM_SUBTREE):
  177         """ Clear the admin owner for the set of object identified by the scope
  178         and obj_name parameters
  179 
  180         Args:
  181             obj_name (str): Object name
  182             scope (SaImmScopeT): Scope of the clear operation
  183 
  184         Returns:
  185             SaAisErrorT: Return code of the saImmOmAdminOwnerClear() API call
  186         """
  187         if not obj_name:
  188             rc = eSaAisErrorT.SA_AIS_ERR_NOT_EXIST
  189         else:
  190             obj_name = SaNameT(obj_name)
  191             obj_name = [obj_name]
  192 
  193             rc = saImmOmAdminOwnerClear(self.handle, obj_name, scope)
  194             if rc != eSaAisErrorT.SA_AIS_OK:
  195                 log_err("saImmOmAdminOwnerClear FAILED - %s" %
  196                         eSaAisErrorT.whatis(rc))
  197 
  198         return rc
  199 
  200     def get_class_description(self, class_name):
  201         """ Get class description as a Python list
  202 
  203         Args:
  204             class_name (str): Class name
  205 
  206         Returns:
  207             SaAisErrorT: Return code of the corresponding IMM API call(s)
  208             list: List of class attributes
  209         """
  210         # Perform a deep copy
  211         def attr_def_copy(attr_def):
  212             """ Deep copy attributes """
  213             attr_def_cpy = saImm.SaImmAttrDefinitionT_2()
  214             attr_def_cpy.attrName = SaImmAttrNameT(
  215                 deepcopy(str(attr_def.attrName)))
  216             attr_def_cpy.attrValueType = attr_def.attrValueType
  217             attr_def_cpy.attrFlags = attr_def.attrFlags
  218 
  219             return attr_def_cpy
  220 
  221         class_attrs = []
  222         attr_defs = pointer(pointer(saImm.SaImmAttrDefinitionT_2()))
  223         category = saImm.SaImmClassCategoryT()
  224         c_class_name = class_name if isinstance(
  225             class_name, SaStringT) else SaImmClassNameT(class_name)
  226         rc = saImmOmClassDescriptionGet_2(self.handle,
  227                                           c_class_name,
  228                                           category,
  229                                           attr_defs)
  230         if rc != eSaAisErrorT.SA_AIS_OK:
  231             log_err("saImmOmClassDescriptionGet_2 FAILED - %s" %
  232                     eSaAisErrorT.whatis(rc))
  233         else:
  234             _class_attrs = unmarshalNullArray(attr_defs)
  235 
  236             # Make copy of attr_defs list
  237             class_attrs = [attr_def_copy(item) for item in _class_attrs]
  238 
  239             # Free the original memory
  240             saImmOmClassDescriptionMemoryFree_2(self.handle,
  241                                                 attr_defs.contents)
  242 
  243         return rc, class_attrs
  244 
  245     def get_rdn_attribute_for_class(self, class_name):
  246         """ Return the RDN attribute for the given class
  247         This is safe to call from OI callbacks.
  248 
  249         Args:
  250             class_name (str): Class name
  251 
  252         Returns:
  253             str: RDN attribute of the class
  254         """
  255         rc, class_attrs = self.get_class_description(class_name)
  256         if rc != eSaAisErrorT.SA_AIS_OK:
  257             return ""
  258         else:
  259             for attr_desc in class_attrs:
  260                 if attr_desc.attrFlags & saImm.saImm.SA_IMM_ATTR_RDN:
  261                     return str(attr_desc.attrName)
  262 
  263     def invoke_admin_operation(self, dn, op_id, params=None):
  264         """ Invoke admin op for dn
  265 
  266         Args:
  267             dn (str): Object dn
  268             op_id (str): Operation id
  269             params (list): List of parameters
  270 
  271         Returns:
  272             SaAisErrorT: Return code of the corresponding IMM API call(s)
  273         """
  274         _owner = OmAdminOwner(self.handle)
  275         rc = _owner.init()
  276         if rc == eSaAisErrorT.SA_AIS_OK:
  277             index = dn.rfind(",")
  278             object_rdn = dn[index+1:]
  279             rc = _owner.set_owner(object_rdn)
  280 
  281         if rc == eSaAisErrorT.SA_AIS_OK:
  282             owner_handle = _owner.get_handle()
  283             if params is None:
  284                 params = []
  285 
  286             object_dn = SaNameT(dn)
  287             operation_rc = SaAisErrorT()
  288 
  289             rc = saImmOmAdminOperationInvoke_2(owner_handle, object_dn, 0,
  290                                                op_id, params, operation_rc,
  291                                                saAis.SA_TIME_ONE_SECOND * 10)
  292             if rc != eSaAisErrorT.SA_AIS_OK:
  293                 log_err("saImmOmAdminOperationInvoke_2 FAILED - %s" %
  294                         eSaAisErrorT.whatis(rc))
  295             elif operation_rc.value != eSaAisErrorT.SA_AIS_OK:
  296                 log_err("Administrative operation(%s) on %s FAILED - %s" %
  297                         (op_id, object_dn,
  298                          eSaAisErrorT.whatis(operation_rc.value)))
  299 
  300         return rc
  301 
  302     def get_class_category(self, class_name):
  303         """ Return the category of the given class
  304 
  305         Args:
  306             class_name (str): Class name
  307 
  308         Returns:
  309             SaImmClassCategoryT: Class category
  310         """
  311         c_attr_defs = pointer(pointer(saImmOm.SaImmAttrDefinitionT_2()))
  312         c_category = saImmOm.SaImmClassCategoryT()
  313         c_class_name = saImmOm.SaImmClassNameT(class_name)
  314 
  315         rc = saImmOmClassDescriptionGet_2(self.handle, c_class_name,
  316                                           c_category, c_attr_defs)
  317         if rc != eSaAisErrorT.SA_AIS_OK:
  318             log_err("saImmOmClassDescriptionGet_2 FAILED - %s" %
  319                     eSaAisErrorT.whatis(rc))
  320             return ""
  321         return c_category.value
  322 
  323 
  324 class OmAdminOwner(object):
  325     """ This class encapsulates the ImmOm Admin Owner interface """
  326     def __init__(self, imm_handle, owner_name=""):
  327         """ Constructor for OmAdminOwner class
  328 
  329         Args:
  330             imm_handle (SaImmHandleT): IMM OM agent handle
  331             owner_name (str): Name of the admin owner
  332         """
  333         self.imm_handle = imm_handle
  334         if owner_name:
  335             self.owner_name = owner_name
  336         else:
  337             self.owner_name = "pyosaf_" + str(uuid.uuid4())
  338 
  339         self.owner_name = saImmOm.SaImmAdminOwnerNameT(self.owner_name)
  340         self.owner_handle = None
  341 
  342     def __enter__(self):
  343         """ Enter method for OmAdminOwner class """
  344         return self
  345 
  346     def __exit__(self, exception_type, exception_value, traceback):
  347         """ Exit method for OmAdminOwner class
  348 
  349         Finalize the admin owner handle
  350         """
  351         if self.owner_handle is not None:
  352             saImmOm.saImmOmAdminOwnerFinalize(self.owner_handle)
  353             self.owner_handle = None
  354 
  355     def __del__(self):
  356         """ Destructor for OmAdminOwner class
  357 
  358         Finalize the admin owner handle
  359         """
  360         if self.owner_handle is not None:
  361             saImmOm.saImmOmAdminOwnerFinalize(self.owner_handle)
  362             self.owner_handle = None
  363 
  364     def init(self):
  365         """ Initialize the IMM admin owner interface
  366 
  367         Return:
  368             SaAisErrorT: Return code of the saImmOmAdminOwnerInitialize() call
  369         """
  370         self.owner_handle = saImmOm.SaImmAdminOwnerHandleT()
  371         rc = saImmOmAdminOwnerInitialize(self.imm_handle, self.owner_name,
  372                                          eSaBoolT.SA_TRUE, self.owner_handle)
  373         if rc != eSaAisErrorT.SA_AIS_OK:
  374             log_err("saImmOmAdminOwnerInitialize FAILED - %s" %
  375                     eSaAisErrorT.whatis(rc))
  376         return rc
  377 
  378     def get_handle(self):
  379         """ Return the admin owner handle successfully initialized """
  380         return self.owner_handle
  381 
  382     def set_owner(self, obj_name, scope=eSaImmScopeT.SA_IMM_SUBTREE):
  383         """ Set admin owner for the set of objects identified by the scope and
  384         the obj_name parameters.
  385 
  386         Args:
  387             obj_name (str): Object name
  388             scope (SaImmScopeT): Scope of the admin owner set operation
  389 
  390         Return:
  391             SaAisErrorT: Return code of the saImmOmAdminOwnerSet() API call
  392         """
  393         if not obj_name:
  394             rc = eSaAisErrorT.SA_AIS_ERR_NOT_EXIST
  395         else:
  396             obj_name = SaNameT(obj_name)
  397             obj_name = [obj_name]
  398 
  399             rc = saImmOmAdminOwnerSet(self.owner_handle, obj_name, scope)
  400             if rc != eSaAisErrorT.SA_AIS_OK:
  401                 log_err("saImmOmAdminOwnerSet FAILED - %s" %
  402                         eSaAisErrorT.whatis(rc))
  403 
  404         return rc
  405 
  406     def release_owner(self, obj_name, scope=eSaImmScopeT.SA_IMM_SUBTREE):
  407         """ Release the admin owner for the set of objects identified by the
  408         scope and obj_name parameters
  409 
  410         Args:
  411             obj_name (str): Object name
  412             scope (SaImmScopeT): Scope of the admin owner release operation
  413 
  414         Return:
  415             SaAisErrorT: Return code of the saImmOmAdminOwnerRelease() API call
  416         """
  417         if not obj_name:
  418             rc = eSaAisErrorT.SA_AIS_ERR_NOT_EXIST
  419         else:
  420             obj_name = SaNameT(obj_name)
  421             obj_name = [obj_name]
  422 
  423             rc = saImmOmAdminOwnerRelease(self.owner_handle, obj_name, scope)
  424             if rc != eSaAisErrorT.SA_AIS_OK:
  425                 log_err("saImmOmAdminOwnerRelease FAILED - %s" %
  426                         eSaAisErrorT.whatis(rc))
  427 
  428         return rc