"Fossies" - the Fresh Open Source Software Archive

Member "opensaf-5.21.09/python/pyosaf/utils/immom/accessor.py" (31 May 2021, 5642 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 "accessor.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 5.20.05_vs_5.20.08.

    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 from ctypes import pointer
   20 
   21 from pyosaf.saAis import SaNameT, eSaAisErrorT, unmarshalNullArray
   22 from pyosaf.saImm import unmarshalSaImmValue, SaImmAttrNameT, \
   23     SaImmAttrValuesT_2
   24 from pyosaf import saImmOm
   25 
   26 from pyosaf.utils import bad_handle_retry, log_err
   27 from pyosaf.utils.immom import agent
   28 from pyosaf.utils.immom.object import ImmObject
   29 
   30 
   31 class ImmOmAccessor(agent.OmAgentManager):
   32     """ This class provides functions of the ImmOmAccessor interface """
   33     def __init__(self, version=None):
   34         """ Constructor for ImmOmAccessor class
   35 
   36         Args:
   37             version (SaVersionT): IMM OM version
   38         """
   39         super(ImmOmAccessor, self).__init__(version)
   40         self.accessor_handle = None
   41 
   42     def __enter__(self):
   43         """ Enter method for ImmOmAccessor class """
   44         return self
   45 
   46     def __exit__(self, exception_type, exception_value, traceback):
   47         """ Exit method for ImmOmAccessor class
   48 
   49         Finalize the accessor handle and the IMM OM agent handle
   50         """
   51         if self.accessor_handle is not None:
   52             agent.saImmOmAccessorFinalize(self.accessor_handle)
   53             self.accessor_handle = None
   54         if self.handle is not None:
   55             agent.saImmOmFinalize(self.handle)
   56             self.handle = None
   57 
   58     def __del__(self):
   59         """ Destructor for ImmOmAccessor class
   60 
   61         Finalize the accessor handle and the IMM OM agent handle
   62         """
   63         if self.accessor_handle is not None:
   64             saImmOm.saImmOmAccessorFinalize(self.accessor_handle)
   65             self.accessor_handle = None
   66         if self.handle is not None:
   67             saImmOm.saImmOmFinalize(self.handle)
   68             self.handle = None
   69 
   70     @bad_handle_retry
   71     def init(self):
   72         """ Initialize the accessor handle
   73 
   74         Returns:
   75             SaAisErrorT: Return code of the saImmOmAccessorInitialize() call
   76         """
   77         self.finalize()
   78         rc = self.initialize()
   79         if rc == eSaAisErrorT.SA_AIS_OK:
   80             self.accessor_handle = saImmOm.SaImmAccessorHandleT()
   81             # Initialize ImmOmAccessor Handle
   82             rc = agent.saImmOmAccessorInitialize(self.handle,
   83                                                  self.accessor_handle)
   84             if rc != eSaAisErrorT.SA_AIS_OK:
   85                 log_err("saImmOmAccessorInitialize FAILED - %s" %
   86                         eSaAisErrorT.whatis(rc))
   87         return rc
   88 
   89     @bad_handle_retry
   90     def get(self, object_name, attr_name_list=None, class_name=None):
   91         """ Obtain values of some attributes of the given object
   92 
   93         Args:
   94             object_name (str): Object name
   95             attr_name_list (list): List of attributes
   96             class_name (str): Class name
   97 
   98         Returns:
   99             SaAisErrorT: Return code of the corresponding IMM API call(s)
  100             ImmObject: Imm object
  101         """
  102         imm_obj = None
  103         # Always request the SaImmAttrClassName attribute if needed
  104         if attr_name_list and not class_name \
  105                 and 'SaImmAttrClassName' not in attr_name_list \
  106                 and not attr_name_list == \
  107                     ['SA_IMM_SEARCH_GET_CONFIG_ATTR']:
  108             attr_name_list.append('SaImmAttrClassName')
  109 
  110         attr_names = [SaImmAttrNameT(attr) for attr in attr_name_list] \
  111             if attr_name_list else None
  112 
  113         attributes = pointer(pointer(SaImmAttrValuesT_2()))
  114 
  115         rc = agent.saImmOmAccessorGet_2(self.accessor_handle,
  116                                         SaNameT(object_name),
  117                                         attr_names, attributes)
  118 
  119         if rc == eSaAisErrorT.SA_AIS_OK:
  120             attrs = {}
  121             attr_list = unmarshalNullArray(attributes)
  122             for attr in attr_list:
  123                 attr_range = list(range(attr.attrValuesNumber))
  124                 attrs[str(attr.attrName)] = [
  125                     attr.attrValueType,
  126                     [unmarshalSaImmValue(attr.attrValues[val],
  127                                          attr.attrValueType)
  128                      for val in attr_range]]
  129             if 'SaImmAttrClassName' not in attrs and class_name:
  130                 attrs['SaImmAttrClassName'] = class_name
  131             imm_obj = ImmObject(dn=object_name, attributes=attrs)
  132 
  133         if rc == eSaAisErrorT.SA_AIS_ERR_BAD_HANDLE:
  134             init_rc = self.init()
  135             # If the re-initialization of agent handle succeeds, we still need
  136             # to return BAD_HANDLE to the users, so that they would re-try the
  137             # failed operation. Otherwise, the true error code is returned
  138             # to the user to decide further actions.
  139             if init_rc != eSaAisErrorT.SA_AIS_OK:
  140                 log_err("saImmOmAccessorGet_2 FAILED - %s" %
  141                         eSaAisErrorT.whatis(rc))
  142                 rc = init_rc
  143 
  144         return rc, imm_obj