Skip to content

HL7v2 MLLP Listener #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/containerscan/allowedlist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

general:
vulnerabilities:
- CVE-2018-8292
- CVE-2018-8292
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*.db
*.db-*
src/InformaticsGateway/payloads

.run/
# docfx temp files
_site/

Expand Down Expand Up @@ -559,4 +559,4 @@ FodyWeavers.xsd
### VisualStudio Patch ###
# Additional files built by Visual Studio

# End of https://www.toptal.com/developers/gitignore/api/aspnetcore,dotnetcore,visualstudio,visualstudiocode
# End of https://www.toptal.com/developers/gitignore/api/aspnetcore,dotnetcore,visualstudio,visualstudiocode
71 changes: 71 additions & 0 deletions docker-compose/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2022 MONAI Consortium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: "3.7"
services:
rabbitmq:
image: rabbitmq:3-management
hostname: rabbitmq
ports:
- 5672:5672
- 15672:15672
environment:
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "rabbitmq"
RABBITMQ_DEFAULT_VHOST: "monaideploy"
RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"

healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 15s
timeout: 30s
retries: 3

minio:
image: "minio/minio:latest"
command: server --console-address ":9001" /data
hostname: minio
volumes:
- ./.run/minio/data:/data
- ./.run/minio/config:/root/.minio
ports:
- "9000:9000"
environment:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadmin
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 15s
timeout: 30s
retries: 3

createbuckets:
image: minio/mc
environment:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadmin
BUCKET_NAME: monaideploy
ENDPOINT: http://minio:9000
depends_on:
minio:
condition: service_healthy
entrypoint: >
/bin/sh -c "
until (/usr/bin/mc config host add myminio $$ENDPOINT $$MINIO_ROOT_USER $$MINIO_ROOT_PASSWORD) do echo '...waiting...' && sleep 1; done;
/usr/bin/mc mb myminio/$$BUCKET_NAME;
/usr/bin/mc policy set public myminio/$$BUCKET_NAME;
/usr/bin/mc ls myminio;
# exit 0
"

2 changes: 1 addition & 1 deletion src/Api/Monai.Deploy.InformaticsGateway.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PackageReference Include="Macross.Json.Extensions" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.7" />
<PackageReference Include="Monai.Deploy.Messaging" Version="0.1.3-rc0010" />
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.0-rc0012" />
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.0-rc0014" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

Expand Down
9 changes: 9 additions & 0 deletions src/Configuration/ConfigurationValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public ValidateOptionsResult Validate(string name, InformaticsGatewayConfigurati
valid &= IsDicomWebValid(options.DicomWeb);
valid &= IsFhirValid(options.Fhir);
valid &= IsStorageValid(options.Storage);
valid &= IsHl7Valid(options.Hl7);

#pragma warning disable CA2254 // Template should be a static expression
_validationErrors.ForEach(p => _logger.Log(LogLevel.Error, p));
Expand All @@ -62,6 +63,14 @@ public ValidateOptionsResult Validate(string name, InformaticsGatewayConfigurati
return valid ? ValidateOptionsResult.Success : ValidateOptionsResult.Fail(string.Join(Environment.NewLine, _validationErrors));
}

private bool IsHl7Valid(Hl7Configuration hl7)
{
var valid = true;

valid &= ValidationExtensions.IsPortValid("InformaticsGateway>hl7>port", hl7.Port, _validationErrors);
return valid;
}

private bool IsStorageValid(StorageConfiguration storage)
{
var valid = true;
Expand Down
46 changes: 46 additions & 0 deletions src/Configuration/Hl7Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
// SPDX-License-Identifier: Apache License 2.0

using Microsoft.Extensions.Configuration;

namespace Monai.Deploy.InformaticsGateway.Configuration
{
public class Hl7Configuration
{
public static readonly int DefaultClientTimeout = 300000;
public const int DefaultMaximumNumberOfConnections = 10;

/// <summary>
/// Gets or sets the client connection timeout in milliseconds.
/// </summary>
[ConfigurationKeyName("clientTimeout")]
public int ClientTimeoutMilliseconds { get; set; } = DefaultClientTimeout;

/// <summary>
/// Gets or sets maximum number of concurrent connections for the HL7 service.
/// Defaults to 10.
/// </summary>
[ConfigurationKeyName("maximumNumberOfConnections")]
public int MaximumNumberOfConnections { get; set; } = DefaultMaximumNumberOfConnections;

/// <summary>
/// Gets or sets the MLLP listening port.
/// Defaults to 2575.
/// </summary>
[ConfigurationKeyName("clientTimeout")]
public int Port { get; set; } = 2575;

/// <summary>
/// Gets or sets wether to respond with an ack/nack message.
/// Defaults to true.
/// </summary>
[ConfigurationKeyName("sendAck")]
public bool SendAcknowledgment { get; set; } = true;

public uint BufferSize { get; set; } = 10240;

public Hl7Configuration()
{
}
}
}
8 changes: 8 additions & 0 deletions src/Configuration/InformaticsGatewayConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public class InformaticsGatewayConfiguration
[ConfigurationKeyName("fhir")]
public FhirConfiguration Fhir { get; set; }

/// <summary>
/// Represents the <c>hl7</c> section of the configuration file.
/// </summary>
/// <value></value>
[ConfigurationKeyName("hl7")]
public Hl7Configuration Hl7 { get; set; }

/// <summary>
/// Represents the <c>export</c> section of the configuration file.
/// </summary>
Expand Down Expand Up @@ -83,6 +90,7 @@ public InformaticsGatewayConfiguration()
Export = new DataExportConfiguration();
Messaging = new MessageBrokerConfiguration();
Database = new DatabaseConfiguration();
Hl7 = new Hl7Configuration();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Monai.Deploy.Messaging" Version="0.1.3-rc0010" />
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.0-rc0012" />
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.0-rc0014" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.24" />
</ItemGroup>

Expand Down
35 changes: 35 additions & 0 deletions src/InformaticsGateway/Logging/Log.800.Hl7Service.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
// SPDX-License-Identifier: Apache License 2.0

using System;
using Microsoft.Extensions.Logging;

namespace Monai.Deploy.InformaticsGateway.Logging
{
public static partial class Log
{
[LoggerMessage(EventId = 800, Level = LogLevel.Information, Message = "New HL7 client connected.")]
public static partial void ClientConnected(this ILogger logger);

[LoggerMessage(EventId = 801, Level = LogLevel.Error, Message = "Error reading data, connection may be dropped.")]
public static partial void ExceptionReadingClientStream(this ILogger logger, Exception ex);

[LoggerMessage(EventId = 802, Level = LogLevel.Error, Message = "Error parsing HL7 message.")]
public static partial void ErrorParsingHl7Message(this ILogger logger, Exception ex);

[LoggerMessage(EventId = 803, Level = LogLevel.Warning, Message = "Unable to locate {segment} field {field} in the HL7 message.")]
public static partial void MissingFieldInHL7Message(this ILogger logger, string segment, int field, Exception ex);

[LoggerMessage(EventId = 804, Level = LogLevel.Error, Message = "Error sending HL7 acknowledgment.")]
public static partial void ErrorSendingHl7Acknowledgment(this ILogger logger, Exception ex);

[LoggerMessage(EventId = 805, Level = LogLevel.Information, Message = "Maximum number {maximumAllowedConcurrentConnections} of clients reached.")]
public static partial void MaxedOutHl7Connections(this ILogger logger, int maximumAllowedConcurrentConnections);

[LoggerMessage(EventId = 806, Level = LogLevel.Information, Message = "HL7 listening on port: {port}.")]
public static partial void Hl7ListeningOnPort(this ILogger logger, int port);

[LoggerMessage(EventId = 807, Level = LogLevel.Critical, Message = "Socket error: {error}")]
public static partial void Hl7SocketException(this ILogger logger, string error);
}
}
23 changes: 21 additions & 2 deletions src/InformaticsGateway/Monai.Deploy.InformaticsGateway.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--
<!--
~ Copyright 2022 MONAI Consortium
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -36,6 +36,7 @@
<PackageReference Include="DotNext.Threading" Version="4.6.1" />
<PackageReference Include="fo-dicom" Version="5.0.2" />
<PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.3.1" />
<PackageReference Include="HL7-dotnetcore" Version="2.29.0" />
<PackageReference Include="GitVersion.MsBuild" Version="5.10.3">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
Expand All @@ -45,7 +46,9 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.0-rc0012" />
<PackageReference Include="Monai.Deploy.Messaging.RabbitMQ" Version="0.1.3-rc0010" />
<PackageReference Include="Monai.Deploy.Storage" Version="0.2.0-rc0014" />
<PackageReference Include="Monai.Deploy.Storage.MinIO" Version="0.2.0-rc0014" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
Expand Down Expand Up @@ -78,4 +81,20 @@
<None Include="./appsettings.json" CopyToOutputDirectory="Always" />
<None Include="./appsettings.Development.json" CopyToOutputDirectory="Always" />
</ItemGroup>

<Target Name="CopyPlugins" AfterTargets="AfterPublish">
<ItemGroup>
<PluginDlls Include="$(PublishDir)Monai.Deploy.Messaging.RabbitMQ.dll;$(PublishDir)Monai.Deploy.Storage.MinIO.dll;$(PublishDir)Minio.dll" />
</ItemGroup>
<Copy SourceFiles="@(PluginDlls)" DestinationFolder="$(PublishDir)\plug-ins\" SkipUnchangedFiles="true" />
<Message Text="Files copied successfully to $(PublishDir)\plug-ins\." Importance="high" />
</Target>

<Target Name="CopyPluginsBuild" AfterTargets="Build">
<ItemGroup>
<PluginDlls Include="$(OutDir)Monai.Deploy.Messaging.RabbitMQ.dll;$(OutDir)Monai.Deploy.Storage.MinIO.dll;$(OutDir)Minio.dll" />
</ItemGroup>
<Copy SourceFiles="@(PluginDlls)" DestinationFolder="$(OutDir)\plug-ins\" SkipUnchangedFiles="true" />
<Message Text="Files copied successfully to $(OutDir)\plug-ins\." Importance="high" />
</Target>
</Project>
6 changes: 6 additions & 0 deletions src/InformaticsGateway/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
using Monai.Deploy.InformaticsGateway.Configuration;
using Monai.Deploy.InformaticsGateway.Database;
using Monai.Deploy.InformaticsGateway.Repositories;
using Monai.Deploy.InformaticsGateway.Services.Common;
using Monai.Deploy.InformaticsGateway.Services.Connectors;
using Monai.Deploy.InformaticsGateway.Services.DicomWeb;
using Monai.Deploy.InformaticsGateway.Services.Export;
using Monai.Deploy.InformaticsGateway.Services.HealthLevel7;
using Monai.Deploy.InformaticsGateway.Services.Http;
using Monai.Deploy.InformaticsGateway.Services.Scp;
using Monai.Deploy.InformaticsGateway.Services.Storage;
Expand Down Expand Up @@ -120,13 +122,16 @@ internal static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddSingleton<IMonaiServiceLocator, MonaiServiceLocator>();
services.AddSingleton<IStorageInfoProvider, StorageInfoProvider>();
services.AddSingleton<IMonaiAeChangedNotificationService, MonaiAeChangedNotificationService>();
services.AddSingleton<ITcpListenerFactory, TcpListenerFactory>();
services.AddSingleton<IMllpClientFactory, MllpClientFactory>();
services.AddSingleton<IApplicationEntityManager, ApplicationEntityManager>();
services.AddSingleton<SpaceReclaimerService>();
services.AddSingleton<ScpService>();
services.AddSingleton<ScuExportService>();
services.AddSingleton<DicomWebExportService>();
services.AddSingleton<DataRetrievalService>();
services.AddSingleton<PayloadNotificationService>();
services.AddSingleton<MllpService>();

var timeout = TimeSpan.FromSeconds(hostContext.Configuration.GetValue("InformaticsGateway:dicomWeb:clientTimeout", DicomWebConfiguration.DefaultClientTimeout));
services
Expand All @@ -149,6 +154,7 @@ internal static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddHostedService<ScuExportService>(p => p.GetService<ScuExportService>());
services.AddHostedService<DicomWebExportService>(p => p.GetService<DicomWebExportService>());
services.AddHostedService<PayloadNotificationService>(p => p.GetService<PayloadNotificationService>());
services.AddHostedService<MllpService>(p => p.GetService<MllpService>());
})
.ConfigureWebHostDefaults(webBuilder =>
{
Expand Down
21 changes: 21 additions & 0 deletions src/InformaticsGateway/Services/Common/INetworkStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
// SPDX-License-Identifier: Apache License 2.0

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Monai.Deploy.InformaticsGateway.Services.Common
{
internal interface INetworkStream : IDisposable
{
int ReadTimeout { get; set; }
int WriteTimeout { get; set; }

ValueTask WriteAsync(ReadOnlyMemory<byte> ackData, CancellationToken cancellationToken = default);

Task FlushAsync(CancellationToken cancellationToken = default);

ValueTask<int> ReadAsync(Memory<byte> messageBuffer, CancellationToken cancellationToken = default);
}
}
15 changes: 15 additions & 0 deletions src/InformaticsGateway/Services/Common/ITcpClientAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
// SPDX-License-Identifier: Apache License 2.0

using System;
using System.Net;

namespace Monai.Deploy.InformaticsGateway.Services.Common
{
internal interface ITcpClientAdapter : IDisposable
{
EndPoint RemoteEndPoint { get; }

INetworkStream GetStream();
}
}
Loading