Esempio n. 1
0
def generic_mo_from_xml_elem(elem):
    """
    create GenericMo object from xml element
    """

    import ucsxmlcodec as xc
    xml_str = xc.to_xml_str(elem)
    gmo = generic_mo_from_xml(xml_str)
    return gmo
Esempio n. 2
0
    def post_elem(self, elem):
        """
        sends the request and receives the response from ucsm server using xml
        element

        Args:
            elem (xml element)

        Returns:
            response xml string

        Example:
            response = post_elem(elem=xml_element)
        """

        import ucsxmlcodec as xc

        dump_xml = self.__dump_xml
        if dump_xml:
            if elem.tag == "aaaLogin":
                elem.attrib['inPassword'] = "*********"
                xml_str = xc.to_xml_str(elem)
                log.debug('%s ====> %s' % (self.__uri, xml_str))
                elem.attrib['inPassword'] = self.__password
                xml_str = xc.to_xml_str(elem)
            else:
                xml_str = xc.to_xml_str(elem)
                log.debug('%s ====> %s' % (self.__uri, xml_str))
        else:
            xml_str = xc.to_xml_str(elem)

        response_str = self.post_xml(xml_str)
        if dump_xml:
            log.debug('%s <==== %s' % (self.__uri, response_str))

        if response_str:
            response = xc.from_xml_str(response_str)
            return response

        return None
Esempio n. 3
0
    def __dequeue_function(self):
        """
        Internal method to dequeue to events.
        """

        while len(self.__wbs):
            # log.debug('waiting to acquire lock on %s' %
            #               self.__dequeue_thread.name)
            self.__condition.acquire()
            # log.debug('condition acquired by %s' %
            #           self.__dequeue_thread.name)

            lowest_timeout = None
            wb_to_remove = []

            for watch_block in self.__wbs:
                poll_sec = watch_block.params["poll_sec"]
                managed_object = watch_block.params["managed_object"]
                timeout_sec = watch_block.params["timeout_sec"]
                transient_value = watch_block.params["transient_value"]
                success_value = watch_block.params["success_value"]
                failure_value = watch_block.params["failure_value"]
                prop = watch_block.params["prop"]
                start_time = watch_block.params["start_time"]

                gmo = None
                pmo = None
                mce = None

                if poll_sec is not None and managed_object is not None:
                    pmo = self.__handle.query_dn(managed_object.dn)
                    if pmo is None:
                        UcsWarning('Mo ' +
                                   managed_object.dn +
                                   ' not found.')
                        continue
                    elem = pmo.to_xml()
                    xml_str = xc.to_xml_str(elem)
                    gmo = ucsmo.generic_mo_from_xml(xml_str)
                else:
                    time_diff = datetime.datetime.now() - start_time
                    timeout_ms = 0
                    if timeout_sec is not None:
                        if time_diff.seconds >= timeout_sec:  # TimeOut
                            wb_to_remove.append(watch_block)
                            continue
                        timeout_ms = (timeout_sec - time_diff.seconds)

                        if lowest_timeout is None:
                            lowest_timeout = timeout_ms
                        else:
                            if lowest_timeout > timeout_ms:
                                lowest_timeout = timeout_ms

                    if timeout_ms > 0:
                        mce = watch_block.dequeue(timeout_ms)
                    else:
                        mce = watch_block.dequeue(2147483647)
                        # print mce
                    if mce is None:
                        continue

                # Means parameter set is not Mo
                if managed_object is None:
                    if watch_block.callback is not None:
                        watch_block.callback(mce)
                    continue

                if mce is not None:
                    elem = mce.mo.to_xml()
                    xml_str = xc.to_xml_str(elem)
                    gmo = ucsmo.generic_mo_from_xml(xml_str)

                attributes = []
                if mce is None:
                    attributes = gmo.properties.keys()
                else:
                    attributes = mce.change_list

                attributes_dict = {}
                for attr in attributes:
                    attributes_dict[
                        ucsgenutils.to_python_propname(attr)] = attr

                nprop = prop.lower()
                if prop in attributes_dict:
                    prop_val = gmo.properties[attributes_dict[nprop]]
                    if mce is None:
                        mce = MoChangeEvent(event_id=0, mo=pmo,
                                            change_list=prop)

            # check if any of the conditional checks match
            # if so, call the callback specified by the application
            # and remove the watch_block (deferred to outside the loop)
                    if (
                        (len(success_value) > 0 and
                                    prop_val in success_value) or
                        (len(failure_value) > 0 and
                                    prop_val in failure_value) or
                        (len(transient_value) > 0 and
                                    prop_val in transient_value)):
                        if watch_block.callback:
                            watch_block.callback(mce)
                        wb_to_remove.append(watch_block)
                        continue

                if poll_sec is not None:
                    poll_ms = poll_sec
                    if timeout_sec is not None:
                        pts = datetime.datetime.now() - start_time
                        if pts.seconds >= timeout_sec:  # TimeOut
                            break
                        timeout_ms = timeout_sec - pts.seconds

                        if timeout_ms < poll_sec:
                            poll_ms = pts.seconds

                    # time.sleep(poll_ms)
                    if lowest_timeout is None:
                        lowest_timeout = poll_ms
                    else:
                        if lowest_timeout > poll_ms:
                            lowest_timeout = poll_ms

            if len(wb_to_remove):
                self.__wbs_lock.acquire()

                for wb in wb_to_remove:
                    self.watch_block_remove(wb)
                wb_to_remove = []

                self.__wbs_lock.release()

            # There is a possibility that all the watchblocks
            # were deleted in the above loop.
            # In that case, no need to wait for more events
            if len(self.__wbs):
                # log.debug('condition wait by %s' %
                #           self.__dequeue_thread.name)
                self.__condition.wait(lowest_timeout)

            # log.debug('condition released by %s' %
            #           self.__dequeue_thread.name)
            self.__condition.release()

        return