"Fossies" - the Fresh Open Source Software Archive

Member "opensaf-5.21.09/python/pyosaf/utils/ntf/agent.py" (31 May 2021, 20356 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.

    1 ############################################################################
    2 #
    3 # (C) Copyright 2017 Ericsson AB. All rights reserved.
    4 #
    5 # This program is distributed in the hope that it will be useful, but
    6 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    7 # or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
    8 # under the GNU Lesser General Public License Version 2.1, February 1999.
    9 # The complete license can be accessed from the following location:
   10 # http://opensource.org/licenses/lgpl-license.php
   11 # See the Copying file included with the OpenSAF distribution for full
   12 # licensing terms.
   13 #
   14 # Author(s): Ericsson
   15 #
   16 ############################################################################
   17 # pylint: disable=unused-argument,too-few-public-methods
   18 """ NTF utils common data structures and functions """
   19 from copy import deepcopy
   20 
   21 from pyosaf.saAis import saAis, SaVersionT, SaSelectionObjectT, eSaAisErrorT
   22 from pyosaf import saNtf
   23 from pyosaf.utils import decorate, initialize_decorate, log_err
   24 
   25 # Decorate pure saNtf* API's with error-handling retry and exception raising
   26 # Library Life Cycle API's
   27 saNtfInitialize = initialize_decorate(saNtf.saNtfInitialize)
   28 saNtfInitialize_2 = initialize_decorate(saNtf.saNtfInitialize_2)
   29 saNtfInitialize_3 = initialize_decorate(saNtf.saNtfInitialize_3)
   30 saNtfSelectionObjectGet = decorate(saNtf.saNtfSelectionObjectGet)
   31 saNtfDispatch = decorate(saNtf.saNtfDispatch)
   32 saNtfFinalize = decorate(saNtf.saNtfFinalize)
   33 # Producer API's
   34 saNtfObjectCreateDeleteNotificationAllocate = \
   35     decorate(saNtf.saNtfObjectCreateDeleteNotificationAllocate)
   36 saNtfAttributeChangeNotificationAllocate = \
   37     decorate(saNtf.saNtfAttributeChangeNotificationAllocate)
   38 saNtfStateChangeNotificationAllocate = \
   39     decorate(saNtf.saNtfStateChangeNotificationAllocate)
   40 saNtfStateChangeNotificationAllocate_3 = \
   41     decorate(saNtf.saNtfStateChangeNotificationAllocate_3)
   42 saNtfAlarmNotificationAllocate = decorate(saNtf.saNtfAlarmNotificationAllocate)
   43 saNtfSecurityAlarmNotificationAllocate = \
   44     decorate(saNtf.saNtfSecurityAlarmNotificationAllocate)
   45 saNtfMiscellaneousNotificationAllocate = \
   46     decorate(saNtf.saNtfMiscellaneousNotificationAllocate)
   47 saNtfPtrValAllocate = decorate(saNtf.saNtfPtrValAllocate)
   48 saNtfArrayValAllocate = decorate(saNtf.saNtfArrayValAllocate)
   49 saNtfIdentifierAllocate = decorate(saNtf.saNtfIdentifierAllocate)
   50 saNtfNotificationSend = decorate(saNtf.saNtfNotificationSend)
   51 saNtfNotificationSendWithId = decorate(saNtf.saNtfNotificationSendWithId)
   52 saNtfNotificationFree = decorate(saNtf.saNtfNotificationFree)
   53 saNtfVariableDataSizeGet = decorate(saNtf.saNtfVariableDataSizeGet)
   54 # Consumer API's
   55 saNtfLocalizedMessageGet = decorate(saNtf.saNtfLocalizedMessageGet)
   56 saNtfLocalizedMessageFree = decorate(saNtf.saNtfLocalizedMessageFree)
   57 saNtfLocalizedMessageFree_2 = decorate(saNtf.saNtfLocalizedMessageFree_2)
   58 saNtfPtrValGet = decorate(saNtf.saNtfPtrValGet)
   59 saNtfArrayValGet = decorate(saNtf.saNtfArrayValGet)
   60 saNtfObjectCreateDeleteNotificationFilterAllocate = \
   61     decorate(saNtf.saNtfObjectCreateDeleteNotificationFilterAllocate)
   62 saNtfAttributeChangeNotificationFilterAllocate = \
   63     decorate(saNtf.saNtfAttributeChangeNotificationFilterAllocate)
   64 saNtfStateChangeNotificationFilterAllocate = \
   65     decorate(saNtf.saNtfStateChangeNotificationFilterAllocate)
   66 saNtfStateChangeNotificationFilterAllocate_2 = \
   67     decorate(saNtf.saNtfStateChangeNotificationFilterAllocate_2)
   68 saNtfAlarmNotificationFilterAllocate = \
   69     decorate(saNtf.saNtfAlarmNotificationFilterAllocate)
   70 saNtfSecurityAlarmNotificationFilterAllocate = \
   71     decorate(saNtf.saNtfSecurityAlarmNotificationFilterAllocate)
   72 saNtfNotificationFilterFree = decorate(saNtf.saNtfNotificationFilterFree)
   73 # Subscriber API's
   74 saNtfNotificationSubscribe = decorate(saNtf.saNtfNotificationSubscribe)
   75 saNtfNotificationSubscribe_3 = decorate(saNtf.saNtfNotificationSubscribe_3)
   76 saNtfNotificationUnsubscribe = decorate(saNtf.saNtfNotificationUnsubscribe)
   77 saNtfNotificationUnsubscribe_2 = decorate(saNtf.saNtfNotificationUnsubscribe_2)
   78 # Reader API's
   79 saNtfNotificationReadInitialize = \
   80     decorate(saNtf.saNtfNotificationReadInitialize)
   81 saNtfNotificationReadInitialize_2 = \
   82     decorate(saNtf.saNtfNotificationReadInitialize_2)
   83 saNtfNotificationReadInitialize_3 = \
   84     decorate(saNtf.saNtfNotificationReadInitialize_3)
   85 saNtfNotificationReadNext = decorate(saNtf.saNtfNotificationReadNext)
   86 saNtfNotificationReadNext_3 = decorate(saNtf.saNtfNotificationReadNext_3)
   87 saNtfNotificationReadFinalize = decorate(saNtf.saNtfNotificationReadFinalize)
   88 
   89 
   90 class NotificationInfo(object):
   91     """ This class encapsulates data structures for use by each specific
   92     notification type """
   93     def __init__(self):
   94         # Header info
   95         self.event_type = 0
   96         self.notification_object = ""
   97         self.notifying_object = ""
   98         self.ntf_class_id = saNtf.SaNtfClassIdT(0, 0, 0)
   99         self.event_time = saAis.SA_TIME_UNKNOWN
  100         self.notification_id = None
  101         self.additional_text = ""
  102         self.additional_info = []
  103         # Object create/delete notification info
  104         self.object_attributes = []
  105         self.source_indicator = \
  106             saNtf.eSaNtfSourceIndicatorT.SA_NTF_UNKNOWN_OPERATION
  107         # Attribute change notification info
  108         self.changed_attributes = []
  109         # State change notification info
  110         self.state_changes = []
  111         # Alarm info
  112         self.probable_cause = \
  113             saNtf.eSaNtfProbableCauseT.SA_NTF_UNSPECIFIED_REASON
  114         self.specific_problems = []
  115         self.perceived_severity = \
  116             saNtf.eSaNtfSeverityT.SA_NTF_SEVERITY_INDETERMINATE
  117         self.trend = saNtf.eSaNtfSeverityTrendT.SA_NTF_TREND_NO_CHANGE
  118         self.threshold_information = None
  119         self.monitored_attrs = []
  120         self.proposed_repair_actions = []
  121         # Security alarm info
  122         self.severity = saNtf.eSaNtfSeverityT.SA_NTF_SEVERITY_INDETERMINATE
  123         self.security_alarm_detector = None
  124         self.service_user = None
  125         self.service_provider = None
  126 
  127 
  128 class NotificationFilterInfo(object):
  129     """ This class encapsulates the notification filter data structure for use
  130     by each specific notification type """
  131     def __init__(self):
  132         # Header info
  133         self.obj_create_del_evt_list = []
  134         self.attr_change_evt_list = []
  135         self.state_change_evt_list = []
  136         self.alarm_evt_list = []
  137         self.sec_alarm_evt_list = []
  138         self.notification_object_list = []
  139         self.notifying_objects_list = []
  140         self.ntf_class_id_list = []
  141         # Filter info
  142         self.source_indicator_list = []
  143         self.changed_state_list = []
  144         self.probable_cause_list = []
  145         self.perceived_severity_list = []
  146         self.trend_list = []
  147         self.severity_list = []
  148 
  149 
  150 class AdditionalInfo(object):
  151     """ This class contains a piece of additional information to be included
  152     in a notification """
  153     def __init__(self, info_id=None, info_type=None, info_value=None):
  154         """ Constructor for AdditionalInfo class
  155 
  156         Args:
  157             info_id (SaNtfElementIdT): infoId field of SaNtfAdditionalInfoT
  158             info_type (SaNtfValueTypeT): infoType field of SaNtfAdditionalInfoT
  159             info_value (Any type of eSaNtfValueTypeT): infoValue field of
  160                 SaNtfAdditionalInfoT
  161         """
  162         self.info_id = info_id
  163         self.info_type = info_type
  164         self.info_value = info_value
  165 
  166 
  167 class StateChange(object):
  168     """ This class contains information about a state change event """
  169     def __init__(self, state_id=None, new_state=None, old_state_present=False,
  170                  old_state=None):
  171         """ Constructor for StateChange class
  172 
  173         Args:
  174             state_id (SaNtfElementIdT): stateId field of SaNtfStateChangeT
  175             new_state (int): newState field of SaNtfStateChangeT
  176             old_state_present (bool): oldStatePresent field of
  177                 SaNtfStateChangeT
  178             old_state (int): oldState field of SaNtfStateChangeT
  179         """
  180         self.state_id = state_id
  181         self.new_state = new_state
  182         self.old_state_present = old_state_present
  183         self.old_state = old_state
  184 
  185 
  186 class AttributeChange(object):
  187     """ This class contains information about an object's attribute change """
  188     def __init__(self, attribute_id=None, attribute_type=None,
  189                  new_attribute_value=None, old_attribute_present=False,
  190                  old_attribute_value=None):
  191         """ Constructor for AttributeChange class
  192 
  193         Args:
  194             attribute_id (SaNtfElementIdT): attributeId field of
  195                 SaNtfAttributeChangeT
  196             attribute_type (SaNtfValueTypeT): attributeType field of
  197                 SaNtfAttributeChangeT
  198             new_attribute_value (Any type of eSaNtfValueTypeT):
  199                 newAttributeValue field of SaNtfAttributeChangeT
  200             old_attribute_present (bool): oldAttributePresent field of
  201                 SaNtfAttributeChangeT
  202             old_attribute_value (Any type of eSaNtfValueTypeT):
  203                 oldAttributeValue field of SaNtfAttributeChangeT
  204         """
  205         self.attribute_id = attribute_id
  206         self.attribute_type = attribute_type
  207         self.new_attribute_value = new_attribute_value
  208         self.old_attribute_present = old_attribute_present
  209         self.old_attribute_value = old_attribute_value
  210 
  211 
  212 class Attribute(object):
  213     """ This class contains information about an object's attribute """
  214     def __init__(self, attribute_id=None, attribute_type=None,
  215                  attribute_value=None):
  216         """ Constructor for Attribute class
  217 
  218         Args:
  219             attribute_id (SaNtfElementIdT): attributeId field of
  220                 SaNtfAttributeT
  221             attribute_type (SaNtfValueTypeT): attributeType field of
  222                 SaNtfAttributeT
  223             attribute_value (Any type of eSaNtfValueTypeT): attributeValue
  224                 field of SaNtfAttributeT
  225         """
  226         self.attribute_id = attribute_id
  227         self.attribute_type = attribute_type
  228         self.attribute_value = attribute_value
  229 
  230 
  231 class SecurityAlarmDetector(object):
  232     """ This class contains information about a security alarm detector """
  233     def __init__(self, value=None, value_type=None):
  234         """ Constructor for SecurityAlarmDetector class
  235 
  236         Args:
  237             value (Any type of eSaNtfValueTypeT): valueType field of
  238                 SaNtfSecurityAlarmDetectorT
  239             value_type (SaNtfValueTypeT): value field of
  240                 SaNtfSecurityAlarmDetectorT
  241         """
  242         self.value = value
  243         self.value_type = value_type
  244 
  245 
  246 class ServiceUser(object):
  247     """ This class contains information about a service user """
  248     def __init__(self, value=None, value_type=None):
  249         """ Constructor for ServiceUser class
  250 
  251         Args:
  252             value (Any type of eSaNtfValueTypeT): valueType field of
  253                 SaNtfServiceUserT
  254             value_type (SaNtfValueTypeT): value field of SaNtfServiceUserT
  255         """
  256         self.value = value
  257         self.value_type = value_type
  258 
  259 
  260 class ServiceProvider(ServiceUser):
  261     """ This class contains information about a service provider """
  262     pass
  263 
  264 
  265 class ThresholdInformation(object):
  266     """ This class contains information about a threshold """
  267     def __init__(self, threshold_id=None, threshold_value_type=None,
  268                  threshold_value=None, threshold_hysteresis=None,
  269                  observed_value=None, arm_time=None):
  270         """ Constructor for ThresholdInformation class
  271 
  272         Args:
  273             threshold_id (SaNtfElementIdT): thresholdId field of
  274                 SaNtfThresholdInformationT
  275             threshold_value_type (SaNtfValueTypeT): thresholdValueType field of
  276                 SaNtfThresholdInformationT
  277             threshold_value (Any type of eSaNtfValueTypeT): thresholdValue
  278                  field of SaNtfThresholdInformationT
  279             threshold_hysteresis (SaNtfValueTypeT): thresholdHysteresis
  280                 field of SaNtfThresholdInformationT
  281             observed_value (Any type of eSaNtfValueTypeT): observedValue field
  282                 of SaNtfThresholdInformationT
  283             arm_time (SaTimeT): armTime field of SaNtfThresholdInformationT
  284         """
  285         self.threshold_id = threshold_id
  286         self.threshold_value_type = threshold_value_type
  287         self.threshold_value = threshold_value
  288         self.threshold_hysteresis = threshold_hysteresis
  289         self.observed_value = observed_value
  290         self.arm_time = arm_time
  291 
  292 
  293 class SpecificProblem(object):
  294     """ This class contains information about a specific problem """
  295     def __init__(self, problem_id=None, problem_class_id=None,
  296                  problem_type=None, problem_value=None):
  297         """ Constructor for SpecificProblem class
  298 
  299         Args:
  300             problem_id (SaNtfElementIdT): problemId field of
  301                 SaNtfSpecificProblemT
  302             problem_class_id (SaNtfClassIdT): problemClassId field of
  303                 SaNtfSpecificProblemT
  304             problem_type (SaNtfValueTypeT): problemType field of
  305                 SaNtfSpecificProblemT
  306             problem_value (Any type of eSaNtfValueTypeT): problemValue field of
  307                 SaNtfSpecificProblemT
  308         """
  309         self.problem_id = problem_id
  310         self.problem_class_id = problem_class_id
  311         self.problem_type = problem_type
  312         self.problem_value = problem_value
  313 
  314 
  315 class ProposedRepairAction(object):
  316     """ This class contains information about a proposed repair action """
  317     def __init__(self, action_id=None, action_value_type=None,
  318                  action_value=None):
  319         """ Constructor for ProposedRepairAction class
  320 
  321         Args:
  322             action_id (SaNtfElementIdT): actionId field of
  323                 SaNtfProposedRepairActionT
  324             action_value_type (SaNtfValueTypeT): actionValueType field of
  325                 SaNtfProposedRepairActionT
  326             action_value (Any type of eSaNtfValueTypeT): actionValue field of
  327                 SaNtfProposedRepairActionT
  328         """
  329         self.action_id = action_id
  330         self.action_value_type = action_value_type
  331         self.action_value = action_value
  332 
  333 
  334 class NtfAgent(object):
  335     """ This class manages the life cycle of an NTF agent """
  336 
  337     def __init__(self, version=None):
  338         """ Constructor for NtfAgent class
  339 
  340         Args:
  341             version (SaVersionT): NTF API version
  342         """
  343         self.init_version = version if version is not None \
  344             else SaVersionT('A', 1, 1)
  345         self.version = None
  346         self.handle = None
  347         self.callbacks = None
  348         self.sel_obj = SaSelectionObjectT()
  349         self.ntf_notif_function = None
  350         self.ntf_notif_discarded_function = None
  351 
  352     def __enter__(self):
  353         """ Enter method for NtfAgent class """
  354         return self
  355 
  356     def __exit__(self, exception_type, exception_value, traceback):
  357         """ Exit method for NtfAgent class
  358 
  359         Finalize the NTF agent handle
  360         """
  361         if self.handle is not None:
  362             saNtfFinalize(self.handle)
  363             self.handle = None
  364 
  365     def __del__(self):
  366         """ Destructor for NtfAgent class
  367 
  368         Finalize the NTF agent handle
  369         """
  370         if self.handle is not None:
  371             saNtf.saNtfFinalize(self.handle)
  372             self.handle = None
  373 
  374     def _ntf_notif_callback(self, c_subscription_id, c_notif):
  375         """ This callback is invoked by NTF to deliver a notification to a
  376         subscriber of that notification type.
  377 
  378         Args:
  379             c_subscription_id (SaNtfSubscriptionIdT): The subscription id
  380                 previously provided by the subscriber when subscribing for this
  381                 type of notification
  382             c_notif (SaNtfNotificationsT): The notification delivered by this
  383                 callback
  384         """
  385         pass
  386 
  387     def _ntf_notif_discarded_callback(self, c_subscription_id,
  388                                       c_notification_type, c_number_discarded,
  389                                       c_discarded_notification_identifiers):
  390         """ This callback is invoked by NTF to notify a subscriber of a
  391         particular notification type that one or more notifications of that
  392         type have been discarded.
  393 
  394         Args:
  395             c_subscription_id (SaNtfSubscriptionIdT): The subscription id
  396                 previously provided by the subscriber when subscribing for
  397                 discarded notifications
  398             c_notification_type (SaNtfNotificationTypeT): The notification type
  399                 of the discarded notifications
  400             c_number_discarded (SaUint32T): The number of discarded
  401                 notifications
  402             c_discarded_notification_identifiers (SaNtfIdentifierT): The list
  403                 of notification identifiers of the discarded notifications
  404         """
  405         pass
  406 
  407     def initialize(self, ntf_notif_func=None, ntf_notif_discarded_func=None):
  408         """ Initialize the NTF agent library
  409 
  410         Args:
  411             ntf_notif_func (callback): Callback function for subscribed
  412                 notifications
  413             ntf_notif_discarded_func (callback): Callback function for
  414                 discarded notifications
  415 
  416         Returns:
  417             SaAisErrorT: Return code of the saNtfInitialize() API call
  418         """
  419         self.callbacks = None
  420         # Set up callbacks if any
  421         if ntf_notif_func is not None or ntf_notif_discarded_func is not None:
  422             self.ntf_notif_function = ntf_notif_func
  423             self.ntf_notif_discarded_function = ntf_notif_discarded_func
  424 
  425             self.callbacks = saNtf.SaNtfCallbacksT()
  426             self.callbacks.saNtfNotificationCallback = \
  427                 saNtf.SaNtfNotificationCallbackT(self._ntf_notif_callback)
  428             self.callbacks.saNtfNotificationDiscardedCallback = \
  429                 saNtf.SaNtfNotificationDiscardedCallbackT(
  430                     self._ntf_notif_discarded_callback)
  431 
  432         self.handle = saNtf.SaNtfHandleT()
  433         self.version = deepcopy(self.init_version)
  434         rc = saNtfInitialize(self.handle, self.callbacks, self.version)
  435         if rc != eSaAisErrorT.SA_AIS_OK:
  436             log_err("saNtfInitialize FAILED - %s" % eSaAisErrorT.whatis(rc))
  437 
  438         return rc
  439 
  440     def get_handle(self):
  441         """ Return the NTF agent handle successfully initialized
  442 
  443         Returns:
  444             SaNtfHandleT: NTF agent handle
  445         """
  446         return self.handle
  447 
  448     def _fetch_sel_obj(self):
  449         """ Obtain a selection object (OS file descriptor)
  450 
  451         Returns:
  452             SaAisErrorT: Return code of the saNtfSelectionObjectGet() API call
  453         """
  454         rc = saNtfSelectionObjectGet(self.handle, self.sel_obj)
  455         if rc != eSaAisErrorT.SA_AIS_OK:
  456             log_err("saNtfSelectionObjectGet FAILED - %s" %
  457                     eSaAisErrorT.whatis(rc))
  458 
  459         return rc
  460 
  461     def get_selection_object(self):
  462         """ Return the selection object associated with the NTF handle
  463 
  464         Returns:
  465             SaSelectionObjectT: Selection object associated with the NTF handle
  466         """
  467         return self.sel_obj
  468 
  469     def dispatch(self, flags):
  470         """ Invoke NTF callbacks for queued events
  471 
  472         Args:
  473             flags (eSaDispatchFlagsT): Flags specifying dispatch mode
  474 
  475         Returns:
  476             SaAisErrorT: Return code of the saNtfDispatch() API call
  477         """
  478         rc = saNtfDispatch(self.handle, flags)
  479         if rc != eSaAisErrorT.SA_AIS_OK:
  480             log_err("saNtfDispatch FAILED - %s" % eSaAisErrorT.whatis(rc))
  481 
  482         return rc
  483 
  484     def finalize(self):
  485         """ Finalize the NTF agent handle
  486 
  487         Returns:
  488             SaAisErrorT: Return code of the saNtfFinalize() API call
  489         """
  490         rc = eSaAisErrorT.SA_AIS_OK
  491         if self.handle:
  492             rc = saNtfFinalize(self.handle)
  493             if rc != eSaAisErrorT.SA_AIS_OK:
  494                 log_err("saNtfFinalize FAILED - %s" % eSaAisErrorT.whatis(rc))
  495             if rc == eSaAisErrorT.SA_AIS_OK \
  496                     or rc == eSaAisErrorT.SA_AIS_ERR_BAD_HANDLE:
  497                 # If the Finalize() call returned BAD_HANDLE, the handle should
  498                 # already become stale and invalid, so we reset it anyway
  499                 self.handle = None
  500 
  501         return rc