示例#1
0
    def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        super(RequestHeader, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        self.protocol_version = contents.ProtocolVersion()
        self.protocol_version.read(tstream, kmip_version=kmip_version)

        kmip_version = contents.protocol_version_to_kmip_version(
            self.protocol_version
        )

        # Read the maximum response size if it is present
        if self.is_tag_next(Tags.MAXIMUM_RESPONSE_SIZE, tstream):
            self.maximum_response_size = contents.MaximumResponseSize()
            self.maximum_response_size.read(tstream, kmip_version=kmip_version)

        # Read the asynchronous indicator if it is present
        if self.is_tag_next(Tags.ASYNCHRONOUS_INDICATOR, tstream):
            self.asynchronous_indicator = contents.AsynchronousIndicator()
            self.asynchronous_indicator.read(
                tstream,
                kmip_version=kmip_version
            )

        # Read the authentication if it is present
        if self.is_tag_next(Tags.AUTHENTICATION, tstream):
            self.authentication = contents.Authentication()
            self.authentication.read(tstream, kmip_version=kmip_version)

        # Read the batch error continuation option if it is present
        if self.is_tag_next(Tags.BATCH_ERROR_CONTINUATION_OPTION, tstream):
            self.batch_error_cont_option = BatchErrorContinuationOption()
            self.batch_error_cont_option.read(
                tstream,
                kmip_version=kmip_version
            )

        # Read the batch order option if it is present
        if self.is_tag_next(Tags.BATCH_ORDER_OPTION, tstream):
            self.batch_order_option = contents.BatchOrderOption()
            self.batch_order_option.read(tstream, kmip_version=kmip_version)

        # Read the time stamp if it is present
        if self.is_tag_next(Tags.TIME_STAMP, tstream):
            self.time_stamp = contents.TimeStamp()
            self.time_stamp.read(tstream, kmip_version=kmip_version)

        self.batch_count = contents.BatchCount()
        self.batch_count.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)
示例#2
0
    def _process_request(self, message):
        header = message.request_header

        protocol_version = header.protocol_version
        #        maximum_response_size = header.maximum_response_size
        asynchronous_indicator = header.asynchronous_indicator
        #        authentication = header.authentication
        batch_error_cont_option = header.batch_error_cont_option
        #        batch_order_option = header.batch_order_option
        #        time_stamp = header.time_stamp
        request_batch_count = header.batch_count.value

        # TODO (peter-hamilton) Log receipt of message with time stamp

        if asynchronous_indicator is None:
            asynchronous_indicator = AsynchronousIndicator(False)

        if batch_error_cont_option is None:
            batch_error_cont_option = BatchErrorContinuationOption(BECO.STOP)

        request_batch_items = message.batch_items
        response_batch_items = []

        for i in range(request_batch_count):
            request_batch_item = request_batch_items[i]
            failure_occurred = False

            operation = request_batch_item.operation
            ubi_id = request_batch_item.unique_batch_item_id
            payload = request_batch_item.request_payload
            message_extension = request_batch_item.message_extension

            result = self._process_operation(operation, payload)

            result_status = result[0]
            result_reason = result[1]
            result_message = result[2]
            asyn_cv = None
            response_payload = None
            message_extension = None

            if result_status.value is RS.SUCCESS:
                response_payload = result[3]
            elif result_status.value is RS.OPERATION_FAILED:
                failure_occurred = True
                result_reason = result[1]
            elif result_status.value is RS.OPERATION_PENDING:
                # TODO (peter-hamilton) Need to add a way to track async
                # TODO (peter-hamilton) operations.
                asyn_cv = b'\x00'
            elif result_status.value is RS.OPERATION_UNDONE:
                result_reason = result[1]
            else:
                msg = 'Unrecognized operation result status: {0}'
                raise RuntimeError(msg.format(result_status))

            resp_bi = ResponseBatchItem(operation=operation,
                                        unique_batch_item_id=ubi_id,
                                        result_status=result_status,
                                        result_reason=result_reason,
                                        result_message=result_message,
                                        async_correlation_value=asyn_cv,
                                        response_payload=response_payload,
                                        message_extension=message_extension)
            response_batch_items.append(resp_bi)

            if failure_occurred:
                if batch_error_cont_option.value is BECO.STOP:
                    break
                elif batch_error_cont_option.value is BECO.UNDO:
                    # TODO (peter-hamilton) Tell client to undo operations.
                    # TODO (peter-hamilton) Unclear what response should be.
                    break
                elif batch_error_cont_option.value is BECO.CONTINUE:
                    continue
                else:
                    msg = 'Unrecognized batch error continuation option: {0}'
                    raise RuntimeError(msg.format(batch_error_cont_option))

        response_batch_count = BatchCount(len(response_batch_items))
        response_time_stamp = TimeStamp(int(time.time()))
        response_header = ResponseHeader(protocol_version=protocol_version,
                                         time_stamp=response_time_stamp,
                                         batch_count=response_batch_count)

        response_message = ResponseMessage(response_header=response_header,
                                           batch_items=response_batch_items)
        return response_message