public void TryGetHandlerTest()
        {
            // Arrange
            var logsProvider                 = Mock.Of <ILogsProvider>();
            var runtimeInfoProvider          = Mock.Of <IRuntimeInfoProvider>();
            var streamRequestHandlerProvider = new StreamRequestHandlerProvider(logsProvider, runtimeInfoProvider);

            // Act
            bool result = streamRequestHandlerProvider.TryGetHandler("Logs", out IStreamRequestHandler handler);

            // Assert
            Assert.True(result);
            Assert.NotNull(handler);

            // Act
            handler = null;
            result  = streamRequestHandlerProvider.TryGetHandler("logs", out handler);

            // Assert
            Assert.True(result);
            Assert.NotNull(handler);

            // Act
            handler = null;
            result  = streamRequestHandlerProvider.TryGetHandler("log", out handler);

            // Assert
            Assert.False(result);
            Assert.Null(handler);

            // Act
            handler = null;
            result  = streamRequestHandlerProvider.TryGetHandler("foo", out handler);

            // Assert
            Assert.False(result);
            Assert.Null(handler);
        }
Пример #2
0
        protected override void Load(ContainerBuilder builder)
        {
            // ILogsUploader
            builder.Register(c => new AzureBlobLogsUploader(this.iotHubHostName, this.deviceId))
            .As <ILogsUploader>()
            .SingleInstance();

            // Task<ILogsProvider>
            builder.Register(
                async c =>
            {
                var logsProcessor = new LogsProcessor(new LogMessageParser(this.iotHubHostName, this.deviceId));
                IRuntimeInfoProvider runtimeInfoProvider = await c.Resolve <Task <IRuntimeInfoProvider> >();
                return(new LogsProvider(runtimeInfoProvider, logsProcessor) as ILogsProvider);
            })
            .As <Task <ILogsProvider> >()
            .SingleInstance();

            // IRequestManager
            builder.Register(
                c =>
            {
                var requestHandlers = new List <IRequestHandler>
                {
                    new PingRequestHandler(),
                    new TaskStatusRequestHandler()
                };
                return(new RequestManager(requestHandlers, this.requestTimeout) as IRequestManager);
            })
            .As <IRequestManager>()
            .SingleInstance();

            if (this.experimentalFeatures.EnableUploadLogs)
            {
                // Task<IRequestHandler> - LogsUploadRequestHandler
                builder.Register(
                    async c =>
                {
                    var logsUploader            = c.Resolve <ILogsUploader>();
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider = await logsProviderTask;
                    return(new LogsUploadRequestHandler(logsUploader, logsProvider, runtimeInfoProvider) as IRequestHandler);
                })
                .As <Task <IRequestHandler> >()
                .SingleInstance();
            }

            if (this.experimentalFeatures.EnableGetLogs)
            {
                // Task<IRequestHandler> - LogsRequestHandler
                builder.Register(
                    async c =>
                {
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider = await logsProviderTask;
                    return(new LogsRequestHandler(logsProvider, runtimeInfoProvider) as IRequestHandler);
                })
                .As <Task <IRequestHandler> >()
                .SingleInstance();
            }

            // Task<IRequestHandler> - RestartRequestHandler
            builder.Register(
                async c =>
            {
                var environmentProviderTask = c.Resolve <Task <IEnvironmentProvider> >();
                var commandFactoryTask      = c.Resolve <Task <ICommandFactory> >();
                var configSourceTask        = c.Resolve <Task <IConfigSource> >();
                IEnvironmentProvider environmentProvider = await environmentProviderTask;
                ICommandFactory commandFactory           = await commandFactoryTask;
                IConfigSource configSource = await configSourceTask;
                return(new RestartRequestHandler(environmentProvider, configSource, commandFactory) as IRequestHandler);
            })
            .As <Task <IRequestHandler> >()
            .SingleInstance();

            // ISdkModuleClientProvider
            builder.Register(c => new SdkModuleClientProvider())
            .As <ISdkModuleClientProvider>()
            .SingleInstance();

            // IEdgeAgentConnection
            builder.Register(
                c =>
            {
                var serde = c.Resolve <ISerde <DeploymentConfig> >();
                var deviceClientprovider = c.Resolve <IModuleClientProvider>();
                var requestManager       = c.Resolve <IRequestManager>();
                var deviceManager        = c.Resolve <IDeviceManager>();
                bool enableSubscriptions = !this.experimentalFeatures.DisableCloudSubscriptions;
                IEdgeAgentConnection edgeAgentConnection = new EdgeAgentConnection(deviceClientprovider, serde, requestManager, deviceManager, enableSubscriptions, this.configRefreshFrequency);
                return(edgeAgentConnection);
            })
            .As <IEdgeAgentConnection>()
            .SingleInstance();

            // Task<IStreamRequestListener>
            builder.Register(
                async c =>
            {
                if (this.enableStreams)
                {
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    var edgeAgentConnection     = c.Resolve <IEdgeAgentConnection>();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider       = await logsProviderTask;
                    var streamRequestHandlerProvider = new StreamRequestHandlerProvider(logsProvider, runtimeInfoProvider);
                    return(new StreamRequestListener(streamRequestHandlerProvider, edgeAgentConnection) as IStreamRequestListener);
                }
                else
                {
                    return(new NullStreamRequestListener() as IStreamRequestListener);
                }
            })
            .As <Task <IStreamRequestListener> >()
            .SingleInstance();

            // Task<IConfigSource>
            builder.Register(
                async c =>
            {
                var edgeAgentConnection          = c.Resolve <IEdgeAgentConnection>();
                var twinConfigSource             = new TwinConfigSource(edgeAgentConnection, this.configuration);
                var backupSourceTask             = c.Resolve <Task <IDeploymentBackupSource> >();
                IConfigSource backupConfigSource = new BackupConfigSource(await backupSourceTask, twinConfigSource);
                return(backupConfigSource);
            })
            .As <Task <IConfigSource> >()
            .SingleInstance();

            // IReporter
            builder.Register(
                c =>
            {
                var runtimeInfoDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(DockerReportedRuntimeInfo),
                    [Constants.Unknown] = typeof(UnknownRuntimeInfo)
                };

                var edgeAgentDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeAgentDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeAgentModule)
                };

                var edgeHubDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeHubDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeHubModule)
                };

                var moduleDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType] = typeof(DockerRuntimeModule)
                };

                var deserializerTypesMap = new Dictionary <Type, IDictionary <string, Type> >
                {
                    { typeof(IRuntimeInfo), runtimeInfoDeserializerTypes },
                    { typeof(IEdgeAgentModule), edgeAgentDeserializerTypes },
                    { typeof(IEdgeHubModule), edgeHubDeserializerTypes },
                    { typeof(IModule), moduleDeserializerTypes }
                };

                var edgeAgentConnection = c.Resolve <IEdgeAgentConnection>();
                return(new IoTHubReporter(
                           edgeAgentConnection,
                           new TypeSpecificSerDe <AgentState>(deserializerTypesMap),
                           this.versionInfo) as IReporter);
            })
            .As <IReporter>()
            .SingleInstance();

            base.Load(builder);
        }
Пример #3
0
        protected override void Load(ContainerBuilder builder)
        {
            // ILogsUploader
            builder.Register(c => new AzureBlobLogsUploader(this.iotHubHostName, this.deviceId))
            .As <ILogsUploader>()
            .SingleInstance();

            // Task<ILogsProvider>
            builder.Register(
                async c =>
            {
                var logsProcessor = new LogsProcessor(new LogMessageParser(this.iotHubHostName, this.deviceId));
                IRuntimeInfoProvider runtimeInfoProvider = await c.Resolve <Task <IRuntimeInfoProvider> >();
                return(new LogsProvider(runtimeInfoProvider, logsProcessor) as ILogsProvider);
            })
            .As <Task <ILogsProvider> >()
            .SingleInstance();

            // Task<IStreamRequestListener>
            builder.Register(
                async c =>
            {
                if (this.enableStreams)
                {
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider       = await logsProviderTask;
                    var streamRequestHandlerProvider = new StreamRequestHandlerProvider(logsProvider, runtimeInfoProvider);
                    return(new StreamRequestListener(streamRequestHandlerProvider) as IStreamRequestListener);
                }
                else
                {
                    return(new NullStreamRequestListener() as IStreamRequestListener);
                }
            })
            .As <Task <IStreamRequestListener> >()
            .SingleInstance();

            // Task<IRequestManager>
            builder.Register(
                async c =>
            {
                var logsUploader            = c.Resolve <ILogsUploader>();
                var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                ILogsProvider logsProvider = await logsProviderTask;
                var requestHandlers        = new List <IRequestHandler>
                {
                    new PingRequestHandler(),
                    new LogsUploadRequestHandler(logsUploader, logsProvider, runtimeInfoProvider)
                };
                return(new RequestManager(requestHandlers, this.requestTimeout) as IRequestManager);
            })
            .As <Task <IRequestManager> >()
            .SingleInstance();

            // Task<IEdgeAgentConnection>
            builder.Register(
                async c =>
            {
                var serde = c.Resolve <ISerde <DeploymentConfig> >();
                var deviceClientprovider      = c.Resolve <IModuleClientProvider>();
                var streamRequestListenerTask = c.Resolve <Task <IStreamRequestListener> >();
                var requestManagerTask        = c.Resolve <Task <IRequestManager> >();
                IStreamRequestListener streamRequestListener = await streamRequestListenerTask;
                IRequestManager requestManager           = await requestManagerTask;
                IEdgeAgentConnection edgeAgentConnection = new EdgeAgentConnection(deviceClientprovider, serde, requestManager, streamRequestListener, this.configRefreshFrequency);
                return(edgeAgentConnection);
            })
            .As <Task <IEdgeAgentConnection> >()
            .SingleInstance();

            // Task<IConfigSource>
            builder.Register(
                async c =>
            {
                var serde = c.Resolve <ISerde <DeploymentConfigInfo> >();
                var edgeAgentConnectionTask              = c.Resolve <Task <IEdgeAgentConnection> >();
                IEncryptionProvider encryptionProvider   = await c.Resolve <Task <IEncryptionProvider> >();
                IEdgeAgentConnection edgeAgentConnection = await edgeAgentConnectionTask;
                var twinConfigSource             = new TwinConfigSource(edgeAgentConnection, this.configuration);
                IConfigSource backupConfigSource = new FileBackupConfigSource(this.backupConfigFilePath, twinConfigSource, serde, encryptionProvider);
                return(backupConfigSource);
            })
            .As <Task <IConfigSource> >()
            .SingleInstance();

            // Task<IReporter>
            builder.Register(
                async c =>
            {
                var runtimeInfoDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(DockerReportedRuntimeInfo),
                    [Constants.Unknown] = typeof(UnknownRuntimeInfo)
                };

                var edgeAgentDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeAgentDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeAgentModule)
                };

                var edgeHubDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeHubDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeHubModule)
                };

                var moduleDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType] = typeof(DockerRuntimeModule)
                };

                var deserializerTypesMap = new Dictionary <Type, IDictionary <string, Type> >
                {
                    { typeof(IRuntimeInfo), runtimeInfoDeserializerTypes },
                    { typeof(IEdgeAgentModule), edgeAgentDeserializerTypes },
                    { typeof(IEdgeHubModule), edgeHubDeserializerTypes },
                    { typeof(IModule), moduleDeserializerTypes }
                };

                var edgeAgentConnectionTask = c.Resolve <Task <IEdgeAgentConnection> >();
                IEdgeAgentConnection edgeAgentConnection = await edgeAgentConnectionTask;

                return(new IoTHubReporter(
                           edgeAgentConnection,
                           new TypeSpecificSerDe <AgentState>(deserializerTypesMap),
                           this.versionInfo) as IReporter);
            })
            .As <Task <IReporter> >()
            .SingleInstance();

            base.Load(builder);
        }