Example #1
0
    def add(self,
            class_id=None,
            managed_object=None,
            prop=None,
            success_value=[],
            failure_value=[],
            transient_value=[],
            poll_sec=None,
            timeout_sec=None,
            call_back=None):
        """
        Adds an event handler.

        An event handler can be added using this method where an user can
        subscribe for the event channel from UCS and can monitor those events
        for any specific success value or failure value for a managed object.

        Args:
            class_id (str) - specifies the class name for which events should be monitored.
            managed_object (object) - specifies a particular managed object that
                user wants to monitor. prop specifies the the property of the
                managed object which will be monitored.
            success_value - specifies the success values of a ManagedObject Prop
            failure_value - specifies the failure values of a ManagedObject Prop
            transient_value - specifies transient values of a ManagedObject Prop 
            poll_sec - specifies the time in seconds for polling event.
            timeout_sec - specifies the time after which method should stop 
                            polling or timeOut.
            call_back - specifies the call Back Method or operation that can be
                            given to this method
        """

        if class_id is not None and managed_object is None:
            if ucscoreutils.find_class_id_in_mo_meta_ignore_case(class_id) \
                    is None:
                raise UcsValidationException(
                    "Invalid ClassId %s specified." % class_id)
        elif managed_object is not None and class_id is None:
            if ucscoreutils.find_class_id_in_mo_meta_ignore_case(
                    managed_object._class_id) is None:
                raise UcsValidationException(
                    "Object of unknown ClassId %s provided." %
                    managed_object._class_id)

            if prop is None:
                raise UcsValidationException(
                    "prop parameter is not provided.")

            mo_property_meta = ucscoreutils.get_mo_property_meta(
                managed_object._class_id, prop)
            if mo_property_meta is None:
                raise UcsValidationException(
                    "Unknown Property %s provided." % prop)

            if not success_value:
                raise UcsValidationException(
                    "success_value parameter is not provided.")
        elif class_id is not None and managed_object is not None:
            raise UcsValidationException(
                "Specify either class_id or managedObject, not both")

        watch_block = None
        param_dict = {'class_id': class_id,
                      'managed_object': managed_object,
                      'prop': prop,
                      'success_value': success_value,
                      'failure_value': failure_value,
                      'transient_value': transient_value,
                      'poll_sec': poll_sec,
                      'timeout_sec': timeout_sec,
                      'call_back': call_back,
                      'start_time': datetime.datetime.now()}
        # log.debug(param_dict)
        if class_id is None and managed_object is None:
            def watch_all_filter(mce):
                """
                Callback method to work on all events.
                """
                return True

            watch_block = self.watch_block_add(
                                    params=param_dict,
                                    filter_callback=watch_all_filter,
                                    callback=call_back)
        elif class_id is not None and managed_object is None:
            def watch__type_filter(mce):
                """
                Callback method to work on events with a specific class_id.
                """
                if mce.mo._class_id.lower() == class_id.lower():
                    return True
                return False

            watch_block = self.watch_block_add(
                                    params=param_dict,
                                    filter_callback=watch__type_filter,
                                    callback=call_back)
        elif class_id is None and managed_object is not None:
            if poll_sec is None:
                def watch_mo_filter(mce):
                    """
                    Callback method to work on events specific to respective
                    managed object.
                    """
                    # log.debug(mce.mo.dn)
                    # log.debug(managed_object.dn)
                    if mce.mo.dn == managed_object.dn:
                        return True
                    return False

                watch_block = self.watch_block_add(
                                    params=param_dict,
                                    filter_callback=watch_mo_filter,
                                    callback=call_back)
            else:
                def watch_none_filter(mce):
                    """
                    Callback method to ignore all events.
                    """
                    return False

                watch_block = self.watch_block_add(
                                    params=param_dict,
                                    filter_callback=watch_none_filter,
                                    callback=call_back)

        if watch_block is None:
            raise UcsValidationException("Error adding WatchBlock...")

        if watch_block is not None and len(self.__wbs) == 1:
            if poll_sec is None:
                self.__thread_enqueue_start()
            self.__thread_dequeue_start()

        return watch_block
Example #2
0
    def query_classid(self, class_id=None, filter_str=None, hierarchy=False,
                      need_response=False):
        """
        Finds an object using it's class id.

        Args:
            class_id (str): class id of the object to be queried for.
            filter_str(str): query objects with specific property with specific value or pattern specifying value.

                      (property_name, "property_value, type="filter_type")\n
                      property_name: Name of the Property\n
                      property_value: Value of the property (str or regular expression)\n
                      filter_type: eq - equal to\n
                                   ne - not equal to\n
                                   ge - greater than or equal to\n
                                   gt - greater than\n
                                   le - less than or equal to\n
                                   lt - less than\n
                                   re - regular expression\n

                      logical filter type: not, and, or\n

                      e.g. '(dn,"org-root/ls-C1_B1", type="eq") or (name, "event", type="re", flag="I")'\n
            hierarchy(bool): if set to True will return all the child
                             hierarchical objects.
            need_response(bool): if set to True will return only response
                                object.


        Returns:
            managedobjectlist or None   by default\n
            managedobjectlist or None   if hierarchy=True\n
            methodresponse              if need_response=True\n

        Example:
            obj = handle.query_classid(class_id="LsServer")\n
            obj = handle.query_classid(class_id="LsServer", hierarchy=True)\n
            obj = handle.query_classid(class_id="LsServer", need_response=True)\n

            filter_str = '(dn,"org-root/ls-C1_B1", type="eq") or (name, "event", type="re", flag="I")'\n
            obj = handle.query_classid(class_id="LsServer", filter_str=filter_str)\n
        """

        # ToDo - How to handle unknown class_id

        from ucsmeta import MO_CLASS_ID
        from ucsfilter import generate_infilter
        from ucsmethodfactory import config_resolve_class

        if not class_id:
            raise ValueError("Provide Parameter class_id")

        meta_class_id = ucscoreutils.find_class_id_in_mo_meta_ignore_case(
                                                                class_id)
        if meta_class_id:
            is_meta_class_id = True
        else:
            meta_class_id = class_id
            is_meta_class_id = False

        if filter_str:
            in_filter = generate_infilter(meta_class_id, filter_str,
                                          is_meta_class_id)
        else:
            in_filter = None

        elem = config_resolve_class(cookie=self.cookie,
                                              class_id=meta_class_id,
                                              in_filter=in_filter,
                                              in_hierarchical=hierarchy)
        response = self.post_elem(elem)
        if response.error_code != 0:
            raise UcsException(response.error_code, response.error_descr)

        if need_response:
            return response

        out_mo_list = ucscoreutils.extract_molist_from_method_response(
                                                                    response,
                                                                    hierarchy)
        return out_mo_list