|
| 1 | +// Copyright 2022 MONAI Consortium |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// Unless required by applicable law or agreed to in writing, software |
| 7 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +// See the License for the specific language governing permissions and |
| 10 | +// limitations under the License. |
| 11 | + |
| 12 | +using System.Configuration; |
| 13 | +using System.Reflection; |
| 14 | +using Microsoft.Extensions.Configuration; |
| 15 | +using Newtonsoft.Json; |
| 16 | +using TechTalk.SpecFlow.Infrastructure; |
| 17 | + |
| 18 | +namespace Monai.Deploy.InformaticsGateway.Integration.Test.Drivers |
| 19 | +{ |
| 20 | + public sealed class Configurations |
| 21 | + { |
| 22 | + private readonly IConfiguration _config; |
| 23 | + private readonly ISpecFlowOutputHelper _outputHelper; |
| 24 | + |
| 25 | + public TestRunnerSettings TestRunnerOptions { get; private set; } |
| 26 | + public InformaticsGatewaySettings InformaticsGatewayOptions { get; private set; } |
| 27 | + public MessageBrokerSettings MessageBrokerOptions { get; private set; } |
| 28 | + public Dictionary<string, StudySpec> StudySpecs { get; private set; } |
| 29 | + public StorageServiceSettings StorageServiceOptions { get; private set; } |
| 30 | + |
| 31 | + public Configurations(ISpecFlowOutputHelper outputHelper) |
| 32 | + { |
| 33 | + TestRunnerOptions = new TestRunnerSettings(); |
| 34 | + InformaticsGatewayOptions = new InformaticsGatewaySettings(); |
| 35 | + MessageBrokerOptions = new MessageBrokerSettings(); |
| 36 | + StorageServiceOptions = new StorageServiceSettings(); |
| 37 | + _outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper)); |
| 38 | + StudySpecs = LoadStudySpecs() ?? throw new NullReferenceException("study.json not found or empty."); |
| 39 | + _config = new ConfigurationBuilder() |
| 40 | + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
| 41 | + .Build(); |
| 42 | + |
| 43 | + LoadConfiguration(); |
| 44 | + } |
| 45 | + |
| 46 | + private Dictionary<string, StudySpec> LoadStudySpecs() |
| 47 | + { |
| 48 | + var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| 49 | + var studyJsonPath = Path.Combine(assemblyPath ?? string.Empty, "study.json"); |
| 50 | + |
| 51 | + if (!File.Exists(studyJsonPath)) |
| 52 | + { |
| 53 | + throw new FileNotFoundException($"study.json not found in {studyJsonPath}"); |
| 54 | + } |
| 55 | + |
| 56 | + var studyJson = File.ReadAllText(studyJsonPath); |
| 57 | + return JsonConvert.DeserializeObject<Dictionary<string, StudySpec>>(studyJson); |
| 58 | + } |
| 59 | + |
| 60 | + private void LoadConfiguration() |
| 61 | + { |
| 62 | + _config.GetSection(nameof(TestRunnerSettings)).Bind(TestRunnerOptions); |
| 63 | + _config.GetSection(nameof(InformaticsGatewaySettings)).Bind(InformaticsGatewayOptions); |
| 64 | + _config.GetSection(nameof(MessageBrokerSettings)).Bind(MessageBrokerOptions); |
| 65 | + _config.GetSection(nameof(StorageServiceSettings)).Bind(StorageServiceOptions); |
| 66 | + |
| 67 | + if (InformaticsGatewayOptions.TemporaryDataStore == "$DATA_PATH") |
| 68 | + { |
| 69 | + InformaticsGatewayOptions.TemporaryDataStore = Environment.GetEnvironmentVariable("DATA_PATH") ?? throw new ConfigurationErrorsException("Environment variable 'DATA_PATH' is undefined."); |
| 70 | + } |
| 71 | + |
| 72 | + _outputHelper.WriteLine("Informatics Gateway data path = {0}", InformaticsGatewayOptions.TemporaryDataStore); |
| 73 | + if (TestRunnerOptions.HostIp == "$HOST_IP") |
| 74 | + { |
| 75 | + TestRunnerOptions.HostIp = Environment.GetEnvironmentVariable("HOST_IP") ?? throw new ConfigurationErrorsException("Environment variable 'HOST_IP' is undefined."); |
| 76 | + } |
| 77 | + _outputHelper.WriteLine("Test Runner Host/IP = {0}", TestRunnerOptions.HostIp); |
| 78 | + if (InformaticsGatewayOptions.Host == "$HOST_IP") |
| 79 | + { |
| 80 | + InformaticsGatewayOptions.Host = Environment.GetEnvironmentVariable("HOST_IP") ?? throw new ConfigurationErrorsException("Environment variable 'HOST_IP' is undefined."); |
| 81 | + } |
| 82 | + _outputHelper.WriteLine("Informatics Gateway Host/IP = {0}", TestRunnerOptions.HostIp); |
| 83 | + if (MessageBrokerOptions.Endpoint == "$HOST_IP") |
| 84 | + { |
| 85 | + MessageBrokerOptions.Endpoint = Environment.GetEnvironmentVariable("HOST_IP") ?? throw new ConfigurationErrorsException("Environment variable 'HOST_IP' is undefined."); |
| 86 | + } |
| 87 | + _outputHelper.WriteLine("Message Broker Host/IP = {0}", TestRunnerOptions.HostIp); |
| 88 | + if (StorageServiceOptions.Host == "$HOST_IP") |
| 89 | + { |
| 90 | + StorageServiceOptions.Host = Environment.GetEnvironmentVariable("HOST_IP") ?? throw new ConfigurationErrorsException("Environment variable 'HOST_IP' is undefined."); |
| 91 | + } |
| 92 | + _outputHelper.WriteLine("Storage Service Host/IP = {0}", TestRunnerOptions.HostIp); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public class StorageServiceSettings |
| 97 | + { |
| 98 | + public string Host { get; set; } |
| 99 | + public int Port { get; set; } |
| 100 | + public string AccessToken { get; set; } |
| 101 | + public string AccessKey { get; set; } |
| 102 | + |
| 103 | + public string Endpoint => $"{Host}:{Port}"; |
| 104 | + } |
| 105 | + |
| 106 | + public class TestRunnerSettings |
| 107 | + { |
| 108 | + /// <summary> |
| 109 | + /// Gets or sets the Host/IP Address used when createing a DICOM source. |
| 110 | + /// If not specified, the test runner would query and use first available IPv4 IP Address. |
| 111 | + /// </summary> |
| 112 | + /// <value></value> |
| 113 | + public string HostIp { get; set; } |
| 114 | + } |
| 115 | + |
| 116 | + public class InformaticsGatewaySettings |
| 117 | + { |
| 118 | + /// <summary> |
| 119 | + /// Gets or set the path where the temporary payloads are stored. |
| 120 | + /// </summary> |
| 121 | + public string TemporaryDataStore { get; set; } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Gets or set host name or IP address of the Informatics Gateway. |
| 125 | + /// </summary> |
| 126 | + public string Host { get; set; } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Gets or sets the DIMSE port of the Informatics Gateway. |
| 130 | + /// </summary> |
| 131 | + public int DimsePort { get; set; } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Gets or sets the RESTful API port of the Informatics Gateway. |
| 135 | + /// </summary> |
| 136 | + public int ApiPort { get; set; } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Gets or sets the name of the bucket used by the storage service. |
| 140 | + /// </summary> |
| 141 | + public string StorageServiceBucketName { get; set; } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Gets the API endpoint of the Informatics Gateway. |
| 145 | + /// </summary> |
| 146 | + public Uri ApiEndpoint => new Uri($"http://{Host}:{ApiPort}"); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Maps modality type specs from study.json |
| 151 | + /// </summary> |
| 152 | + public class StudySpec |
| 153 | + { |
| 154 | + private const int OneMiB = 1048576; |
| 155 | + public int SeriesMin { get; set; } |
| 156 | + public int SeriesMax { get; set; } |
| 157 | + public int InstanceMin { get; set; } |
| 158 | + public int InstanceMax { get; set; } |
| 159 | + public float SizeMin { get; set; } |
| 160 | + public float SizeMax { get; set; } |
| 161 | + |
| 162 | + public long SizeMinBytes |
| 163 | + { |
| 164 | + get { return (long)(SizeMin * OneMiB); } |
| 165 | + } |
| 166 | + |
| 167 | + public long SizeMaxBytes |
| 168 | + { |
| 169 | + get { return (long)(SizeMax * OneMiB); } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public class MessageBrokerSettings |
| 174 | + { |
| 175 | + public string Endpoint { get; set; } |
| 176 | + public string Username { get; set; } |
| 177 | + public string Password { get; set; } |
| 178 | + public string VirtualHost { get; set; } |
| 179 | + public string Exchange { get; set; } |
| 180 | + } |
| 181 | +} |
0 commit comments