def op_set_connection_args(callback):
    return pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
        client_id=fake_client_id,
        hostname=fake_hostname,
        username=fake_username,
        ca_cert=fake_ca_cert,
        callback=callback,
    )
 def op(self, mocker):
     return pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
         client_id="fake_client_id",
         hostname="fake_hostname",
         username="******",
         ca_cert="fake_ca_cert",
         client_cert="fake_client_cert",
         sas_token="fake_sas_token",
         callback=mocker.MagicMock(),
     )
示例#3
0
def op_set_connection_args(mocker):
    return pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
        client_id=fake_client_id,
        hostname=fake_hostname,
        username=fake_username,
        ca_cert=fake_ca_cert,
        client_cert=fake_certificate,
        sas_token=fake_sas_token,
        callback=mocker.MagicMock(),
    )
    def stage(self, mocker, cls_type, init_kwargs, mock_transport):
        stage = cls_type(**init_kwargs)
        stage.pipeline_root = pipeline_stages_base.PipelineRootStage(
            pipeline_configuration=mocker.MagicMock())
        stage.send_op_down = mocker.MagicMock()
        stage.send_event_up = mocker.MagicMock()

        # Set up the Transport on the stage
        op = pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
            client_id="fake_client_id",
            hostname="fake_hostname",
            username="******",
            ca_cert="fake_ca_cert",
            client_cert="fake_client_cert",
            sas_token="fake_sas_token",
            callback=mocker.MagicMock(),
        )
        stage.run_op(op)
        assert stage.transport is mock_transport.return_value

        return stage
示例#5
0
    def _execute_op(self, op):

        if isinstance(
                op, pipeline_ops_provisioning.
                SetProvisioningClientConnectionArgsOperation):
            # get security client args from above, save some, use some to build topic names,
            # always pass it down because MQTT protocol stage will also want to receive these args.

            client_id = op.registration_id
            query_param_seq = [
                ("api-version", pkg_constant.PROVISIONING_API_VERSION),
                ("ClientVersion", pkg_constant.USER_AGENT),
            ]
            username = "******".format(
                id_scope=op.id_scope,
                registration_id=op.registration_id,
                query_params=urllib.parse.urlencode(query_param_seq),
            )

            hostname = op.provisioning_host

            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
                    client_id=client_id,
                    hostname=hostname,
                    username=username,
                    client_cert=op.client_cert,
                    sas_token=op.sas_token,
                ),
            )

        elif isinstance(
                op,
                pipeline_ops_provisioning.SendRegistrationRequestOperation):
            # Convert Sending the request into MQTT Publish operations
            topic = mqtt_topic.get_topic_for_register(op.request_id)
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic, payload=op.request_payload),
            )

        elif isinstance(op,
                        pipeline_ops_provisioning.SendQueryRequestOperation):
            # Convert Sending the request into MQTT Publish operations
            topic = mqtt_topic.get_topic_for_query(op.request_id,
                                                   op.operation_id)
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic, payload=op.request_payload),
            )

        elif isinstance(op, pipeline_ops_base.EnableFeatureOperation):
            # Enabling for register gets translated into an MQTT subscribe operation
            topic = mqtt_topic.get_topic_for_subscribe()
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTSubscribeOperation(topic=topic),
            )

        elif isinstance(op, pipeline_ops_base.DisableFeatureOperation):
            # Disabling a register response gets turned into an MQTT unsubscribe operation
            topic = mqtt_topic.get_topic_for_subscribe()
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTUnsubscribeOperation(topic=topic),
            )

        else:
            # All other operations get passed down
            operation_flow.pass_op_to_next_stage(self, op)
示例#6
0
    def _execute_op(self, op):

        if isinstance(op,
                      pipeline_ops_iothub.SetIoTHubConnectionArgsOperation):
            self.device_id = op.device_id
            self.module_id = op.module_id

            # if we get auth provider args from above, we save some, use some to build topic names,
            # and always pass it down because we know that the MQTT protocol stage will also want
            # to receive these args.
            self._set_topic_names(device_id=op.device_id,
                                  module_id=op.module_id)

            if op.module_id:
                client_id = "{}/{}".format(op.device_id, op.module_id)
            else:
                client_id = op.device_id

            query_param_seq = [
                ("api-version", pkg_constant.IOTHUB_API_VERSION),
                ("DeviceClientType", pkg_constant.USER_AGENT),
            ]
            username = "******".format(
                hostname=op.hostname,
                client_id=client_id,
                query_params=urllib.parse.urlencode(query_param_seq),
            )

            if op.gateway_hostname:
                hostname = op.gateway_hostname
            else:
                hostname = op.hostname

            # TODO: test to make sure client_cert and sas_token travel down correctly
            self.send_worker_op_down(
                worker_op=pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
                    client_id=client_id,
                    hostname=hostname,
                    username=username,
                    ca_cert=op.ca_cert,
                    client_cert=op.client_cert,
                    sas_token=op.sas_token,
                    callback=op.callback,
                ),
                op=op,
            )

        elif (isinstance(op, pipeline_ops_base.UpdateSasTokenOperation)
              and self.pipeline_root.connected):
            logger.debug(
                "{}({}): Connected.  Passing op down and reconnecting after token is updated."
                .format(self.name, op.name))

            # make a callback that can call the user's callback after the reconnect is complete
            def on_reconnect_complete(reconnect_op, error):
                if error:
                    logger.error(
                        "{}({}) reconnection failed.  returning error {}".
                        format(self.name, op.name, error))
                    self.send_completed_op_up(op, error=error)
                else:
                    logger.debug(
                        "{}({}) reconnection succeeded.  returning success.".
                        format(self.name, op.name))
                    self.send_completed_op_up(op)

            # save the old user callback so we can call it later.
            old_callback = op.callback

            # make a callback that either fails the UpdateSasTokenOperation (if the lower level failed it),
            # or issues a ReconnectOperation (if the lower level returned success for the UpdateSasTokenOperation)
            def on_token_update_complete(op, error):
                op.callback = old_callback
                if error:
                    logger.error(
                        "{}({}) token update failed.  returning failure {}".
                        format(self.name, op.name, error))
                    self.send_completed_op_up(op, error=error)
                else:
                    logger.debug(
                        "{}({}) token update succeeded.  reconnecting".format(
                            self.name, op.name))

                    self.send_op_down(
                        pipeline_ops_base.ReconnectOperation(
                            callback=on_reconnect_complete))

                logger.debug(
                    "{}({}): passing to next stage with updated callback.".
                    format(self.name, op.name))

            # now, pass the UpdateSasTokenOperation down with our new callback.
            op.callback = on_token_update_complete
            self.send_op_down(op)

        elif isinstance(
                op, pipeline_ops_iothub.SendD2CMessageOperation) or isinstance(
                    op, pipeline_ops_iothub.SendOutputEventOperation):
            # Convert SendTelementry and SendOutputEventOperation operations into MQTT Publish operations
            topic = mqtt_topic_iothub.encode_properties(
                op.message, self.telemetry_topic)
            self.send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic, payload=op.message.data,
                    callback=op.callback),
                op=op,
            )

        elif isinstance(op, pipeline_ops_iothub.SendMethodResponseOperation):
            # Sending a Method Response gets translated into an MQTT Publish operation
            topic = mqtt_topic_iothub.get_method_topic_for_publish(
                op.method_response.request_id, str(op.method_response.status))
            payload = json.dumps(op.method_response.payload)
            self.send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic, payload=payload, callback=op.callback),
                op=op,
            )

        elif isinstance(op, pipeline_ops_base.EnableFeatureOperation):
            # Enabling a feature gets translated into an MQTT subscribe operation
            topic = self.feature_to_topic[op.feature_name]
            self.send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTSubscribeOperation(
                    topic=topic, callback=op.callback),
                op=op,
            )

        elif isinstance(op, pipeline_ops_base.DisableFeatureOperation):
            # Disabling a feature gets turned into an MQTT unsubscribe operation
            topic = self.feature_to_topic[op.feature_name]
            self.send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTUnsubscribeOperation(
                    topic=topic, callback=op.callback),
                op=op,
            )

        elif isinstance(op, pipeline_ops_base.SendIotRequestOperation):
            if op.request_type == pipeline_constant.TWIN:
                topic = mqtt_topic_iothub.get_twin_topic_for_publish(
                    method=op.method,
                    resource_location=op.resource_location,
                    request_id=op.request_id,
                )
                self.send_worker_op_down(
                    worker_op=pipeline_ops_mqtt.MQTTPublishOperation(
                        topic=topic,
                        payload=op.request_body,
                        callback=op.callback),
                    op=op,
                )
            else:
                raise pipeline_exceptions.OperationError(
                    "SendIotRequestOperation request_type {} not supported".
                    format(op.request_type))

        else:
            # All other operations get passed down
            self.send_op_down(op)
    def _execute_op(self, op):

        if isinstance(
                op, pipeline_ops_provisioning.
                SetProvisioningClientConnectionArgsOperation):
            # get security client args from above, save some, use some to build topic names,
            # always pass it down because MQTT protocol stage will also want to receive these args.

            client_id = op.registration_id
            query_param_seq = [
                ("api-version", pkg_constant.PROVISIONING_API_VERSION),
                ("ClientVersion", pkg_constant.USER_AGENT),
            ]
            username = "******".format(
                id_scope=op.id_scope,
                registration_id=op.registration_id,
                query_params=urllib.parse.urlencode(query_param_seq),
            )

            hostname = op.provisioning_host

            self._send_worker_op_down(
                worker_op=pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
                    client_id=client_id,
                    hostname=hostname,
                    username=username,
                    client_cert=op.client_cert,
                    sas_token=op.sas_token,
                    callback=op.callback,
                ),
                op=op,
            )

        elif isinstance(
                op,
                pipeline_ops_provisioning.SendRegistrationRequestOperation):
            # Convert Sending the request into MQTT Publish operations
            topic = mqtt_topic.get_topic_for_register(op.request_id)

            # This is an easier way to get the json eventually
            # rather than formatting strings without if else conditions
            registration_payload = DeviceRegistrationPayload(
                registration_id=op.registration_id,
                custom_payload=op.request_payload)

            self._send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic,
                    payload=registration_payload.get_json_string(),
                    callback=op.callback,
                ),
                op=op,
            )

        elif isinstance(op,
                        pipeline_ops_provisioning.SendQueryRequestOperation):
            # Convert Sending the request into MQTT Publish operations
            topic = mqtt_topic.get_topic_for_query(op.request_id,
                                                   op.operation_id)
            self._send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic,
                    payload=op.request_payload,
                    callback=op.callback),
                op=op,
            )

        elif isinstance(op, pipeline_ops_base.EnableFeatureOperation):
            # Enabling for register gets translated into an MQTT subscribe operation
            topic = mqtt_topic.get_topic_for_subscribe()
            self._send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTSubscribeOperation(
                    topic=topic, callback=op.callback),
                op=op,
            )

        elif isinstance(op, pipeline_ops_base.DisableFeatureOperation):
            # Disabling a register response gets turned into an MQTT unsubscribe operation
            topic = mqtt_topic.get_topic_for_subscribe()
            self._send_worker_op_down(
                worker_op=pipeline_ops_mqtt.MQTTUnsubscribeOperation(
                    topic=topic, callback=op.callback),
                op=op,
            )

        else:
            # All other operations get passed down
            self._send_op_down(op)
示例#8
0
    def _run_op(self, op):

        if isinstance(op, pipeline_ops_iothub.SetAuthProviderArgsOperation):
            self.device_id = op.device_id
            self.module_id = op.module_id

            # if we get auth provider args from above, we save some, use some to build topic names,
            # and always pass it down because we know that the MQTT protocol stage will also want
            # to receive these args.
            self._set_topic_names(device_id=op.device_id,
                                  module_id=op.module_id)

            if op.module_id:
                client_id = "{}/{}".format(op.device_id, op.module_id)
            else:
                client_id = op.device_id

            username = "******".format(
                hostname=op.hostname, client_id=client_id)

            if op.gateway_hostname:
                hostname = op.gateway_hostname
            else:
                hostname = op.hostname

            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.SetMQTTConnectionArgsOperation(
                    client_id=client_id,
                    hostname=hostname,
                    username=username,
                    ca_cert=op.ca_cert),
            )

        elif isinstance(
                op, pipeline_ops_iothub.SendD2CMessageOperation) or isinstance(
                    op, pipeline_ops_iothub.SendOutputEventOperation):
            # Convert SendTelementry and SendOutputEventOperation operations into MQTT Publish operations
            topic = mqtt_topic_iothub.encode_properties(
                op.message, self.telemetry_topic)
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTPublishOperation(
                    topic=topic, payload=op.message.data),
            )

        elif isinstance(op, pipeline_ops_iothub.SendMethodResponseOperation):
            # Sending a Method Response gets translated into an MQTT Publish operation
            topic = mqtt_topic_iothub.get_method_topic_for_publish(
                op.method_response.request_id, str(op.method_response.status))
            payload = json.dumps(op.method_response.payload)
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTPublishOperation(topic=topic,
                                                              payload=payload),
            )

        elif isinstance(op, pipeline_ops_base.EnableFeatureOperation):
            # Enabling a feature gets translated into an MQTT subscribe operation
            topic = self.feature_to_topic[op.feature_name]
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTSubscribeOperation(topic=topic),
            )

        elif isinstance(op, pipeline_ops_base.DisableFeatureOperation):
            # Disabling a feature gets turned into an MQTT unsubscribe operation
            topic = self.feature_to_topic[op.feature_name]
            operation_flow.delegate_to_different_op(
                stage=self,
                original_op=op,
                new_op=pipeline_ops_mqtt.MQTTUnsubscribeOperation(topic=topic),
            )

        elif isinstance(op, pipeline_ops_base.SendIotRequestOperation):
            if op.request_type == constant.TWIN:
                topic = mqtt_topic_iothub.get_twin_topic_for_publish(
                    method=op.method,
                    resource_location=op.resource_location,
                    request_id=op.request_id,
                )
                operation_flow.delegate_to_different_op(
                    stage=self,
                    original_op=op,
                    new_op=pipeline_ops_mqtt.MQTTPublishOperation(
                        topic=topic, payload=op.request_body),
                )
            else:
                raise NotImplementedError(
                    "SendIotRequestOperation request_type {} not supported".
                    format(op.request_type))

        else:
            # All other operations get passed down
            operation_flow.pass_op_to_next_stage(self, op)