| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Core; |
| | 9 | | using Azure.Messaging.ServiceBus.Amqp.Framing; |
| | 10 | | using Azure.Messaging.ServiceBus.Core; |
| | 11 | | using Azure.Messaging.ServiceBus.Management; |
| | 12 | | using Microsoft.Azure.Amqp; |
| | 13 | | using Microsoft.Azure.Amqp.Encoding; |
| | 14 | |
|
| | 15 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 16 | | { |
| | 17 | | #pragma warning disable CA1001 // Types that own disposable fields should be disposable. AmqpRuleManager does not own co |
| | 18 | | internal class AmqpRuleManager : TransportRuleManager |
| | 19 | | #pragma warning restore CA1001 // Types that own disposable fields should be disposable |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// The path of the Service Bus subscription to which the rule manager is bound. |
| | 23 | | /// </summary> |
| | 24 | | /// |
| | 25 | | private readonly string _subscriptionPath; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The policy to use for determining retry behavior for when an operation fails. |
| | 29 | | /// </summary> |
| | 30 | | private readonly ServiceBusRetryPolicy _retryPolicy; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The identifier for the rule manager. |
| | 34 | | /// </summary> |
| | 35 | | private readonly string _identifier; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The AMQP connection scope responsible for managing transport constructs for this instance. |
| | 39 | | /// </summary> |
| | 40 | | /// |
| | 41 | | private readonly AmqpConnectionScope _connectionScope; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Indicates whether or not this instance has been closed. |
| | 45 | | /// </summary> |
| | 46 | | private bool _closed = false; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Indicates whether or not this rule manager has been closed. |
| | 50 | | /// </summary> |
| | 51 | | /// |
| | 52 | | /// <value> |
| | 53 | | /// <c>true</c> if the rule manager is closed; otherwise, <c>false</c>. |
| | 54 | | /// </value> |
| 0 | 55 | | public override bool IsClosed => _closed; |
| | 56 | |
|
| | 57 | | private readonly FaultTolerantAmqpObject<RequestResponseAmqpLink> _managementLink; |
| | 58 | |
|
| | 59 | | static AmqpRuleManager() |
| | 60 | | { |
| 0 | 61 | | AmqpCodec.RegisterKnownTypes(AmqpTrueRuleFilterCodec.Name, AmqpTrueRuleFilterCodec.Code, () => new AmqpTrueR |
| 0 | 62 | | AmqpCodec.RegisterKnownTypes(AmqpFalseRuleFilterCodec.Name, AmqpFalseRuleFilterCodec.Code, () => new AmqpFal |
| 0 | 63 | | AmqpCodec.RegisterKnownTypes(AmqpCorrelationRuleFilterCodec.Name, AmqpCorrelationRuleFilterCodec.Code, () => |
| 0 | 64 | | AmqpCodec.RegisterKnownTypes(AmqpSqlRuleFilterCodec.Name, AmqpSqlRuleFilterCodec.Code, () => new AmqpSqlRule |
| 0 | 65 | | AmqpCodec.RegisterKnownTypes(AmqpEmptyRuleActionCodec.Name, AmqpEmptyRuleActionCodec.Code, () => new AmqpEmp |
| 0 | 66 | | AmqpCodec.RegisterKnownTypes(AmqpSqlRuleActionCodec.Name, AmqpSqlRuleActionCodec.Code, () => new AmqpSqlRule |
| 0 | 67 | | AmqpCodec.RegisterKnownTypes(AmqpRuleDescriptionCodec.Name, AmqpRuleDescriptionCodec.Code, () => new AmqpRul |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Initializes a new instance of the <see cref="AmqpRuleManager"/> class. |
| | 72 | | /// </summary> |
| | 73 | | /// |
| | 74 | | /// <param name="subscriptionPath">The path of the Service Bus subscription to which the rule manager is bound.< |
| | 75 | | /// <param name="connectionScope">The AMQP connection context for operations.</param> |
| | 76 | | /// <param name="retryPolicy">The retry policy to consider when an operation fails.</param> |
| | 77 | | /// <param name="identifier">The identifier for the rule manager.</param> |
| | 78 | | /// |
| | 79 | | /// <remarks> |
| | 80 | | /// As an internal type, this class performs only basic sanity checks against its arguments. It |
| | 81 | | /// is assumed that callers are trusted and have performed deep validation. |
| | 82 | | /// |
| | 83 | | /// Any parameters passed are assumed to be owned by this instance and safe to mutate or dispose; |
| | 84 | | /// creation of clones or otherwise protecting the parameters is assumed to be the purview of the |
| | 85 | | /// caller. |
| | 86 | | /// </remarks> |
| 0 | 87 | | public AmqpRuleManager( |
| 0 | 88 | | string subscriptionPath, |
| 0 | 89 | | AmqpConnectionScope connectionScope, |
| 0 | 90 | | ServiceBusRetryPolicy retryPolicy, |
| 0 | 91 | | string identifier) |
| | 92 | | { |
| 0 | 93 | | Argument.AssertNotNullOrEmpty(subscriptionPath, nameof(subscriptionPath)); |
| 0 | 94 | | Argument.AssertNotNull(connectionScope, nameof(connectionScope)); |
| 0 | 95 | | Argument.AssertNotNull(retryPolicy, nameof(retryPolicy)); |
| | 96 | |
|
| 0 | 97 | | _subscriptionPath = subscriptionPath; |
| 0 | 98 | | _connectionScope = connectionScope; |
| 0 | 99 | | _retryPolicy = retryPolicy; |
| 0 | 100 | | _identifier = identifier; |
| | 101 | |
|
| 0 | 102 | | _managementLink = new FaultTolerantAmqpObject<RequestResponseAmqpLink>( |
| 0 | 103 | | timeout => _connectionScope.OpenManagementLinkAsync( |
| 0 | 104 | | _subscriptionPath, |
| 0 | 105 | | _identifier, |
| 0 | 106 | | timeout, |
| 0 | 107 | | CancellationToken.None), |
| 0 | 108 | | link => |
| 0 | 109 | | { |
| 0 | 110 | | link.Session?.SafeClose(); |
| 0 | 111 | | link.SafeClose(); |
| 0 | 112 | | }); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription. |
| | 117 | | /// </summary> |
| | 118 | | /// |
| | 119 | | /// <param name="description">The rule description that provides the rule to add.</param> |
| | 120 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 121 | | /// |
| | 122 | | /// <remarks> |
| | 123 | | /// You can add rules to the subscription that decides which messages from the topic should reach the subscripti |
| | 124 | | /// A default <see cref="TrueRuleFilter"/> rule named <see cref="RuleProperties.DefaultRuleName"/> is always add |
| | 125 | | /// You can add multiple rules with distinct names to the same subscription. |
| | 126 | | /// Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the messa |
| | 127 | | /// </remarks> |
| | 128 | | /// |
| | 129 | | /// <returns>A task instance that represents the asynchronous add rule operation.</returns> |
| | 130 | | public override async Task AddRuleAsync( |
| | 131 | | RuleProperties description, |
| | 132 | | CancellationToken cancellationToken = default) => |
| 0 | 133 | | await _retryPolicy.RunOperation( |
| 0 | 134 | | async (timeout) => |
| 0 | 135 | | await AddRuleInternalAsync( |
| 0 | 136 | | description, |
| 0 | 137 | | timeout).ConfigureAwait(false), |
| 0 | 138 | | _connectionScope, |
| 0 | 139 | | cancellationToken).ConfigureAwait(false); |
| | 140 | |
|
| | 141 | | /// <summary> |
| | 142 | | /// Adds a rule to the current subscription to filter the messages reaching from topic to the subscription. |
| | 143 | | /// </summary> |
| | 144 | | /// |
| | 145 | | /// <param name="description">The rule description that provides the rule to add.</param> |
| | 146 | | /// <param name="timeout">The per-try timeout specified in the RetryOptions.</param> |
| | 147 | | /// |
| | 148 | | /// <returns>A task instance that represents the asynchronous add rule operation.</returns> |
| | 149 | | private async Task AddRuleInternalAsync( |
| | 150 | | RuleProperties description, |
| | 151 | | TimeSpan timeout) |
| | 152 | | { |
| | 153 | | // Create an AmqpRequest Message to add rule |
| 0 | 154 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest( |
| 0 | 155 | | ManagementConstants.Operations.AddRuleOperation, |
| 0 | 156 | | timeout, |
| 0 | 157 | | null); |
| 0 | 158 | | amqpRequestMessage.Map[ManagementConstants.Properties.RuleName] = description.Name; |
| 0 | 159 | | amqpRequestMessage.Map[ManagementConstants.Properties.RuleDescription] = AmqpMessageConverter.GetRuleDescrip |
| | 160 | |
|
| 0 | 161 | | AmqpResponseMessage response = await ManagementUtilities.ExecuteRequestResponseAsync( |
| 0 | 162 | | _connectionScope, |
| 0 | 163 | | _managementLink, |
| 0 | 164 | | amqpRequestMessage, |
| 0 | 165 | | timeout).ConfigureAwait(false); |
| | 166 | |
|
| 0 | 167 | | if (response.StatusCode != AmqpResponseStatusCode.OK) |
| | 168 | | { |
| 0 | 169 | | throw response.ToMessagingContractException(); |
| | 170 | | } |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Removes the rule on the subscription identified by <paramref name="ruleName" />. |
| | 175 | | /// </summary> |
| | 176 | | /// |
| | 177 | | /// <param name="ruleName">Name of the rule</param> |
| | 178 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 179 | | /// |
| | 180 | | /// <returns>A task instance that represents the asynchronous remove rule operation.</returns> |
| | 181 | | public override async Task RemoveRuleAsync( |
| | 182 | | string ruleName, |
| | 183 | | CancellationToken cancellationToken = default) => |
| 0 | 184 | | await _retryPolicy.RunOperation( |
| 0 | 185 | | async (timeout) => |
| 0 | 186 | | await RemoveRuleInternalAsync( |
| 0 | 187 | | ruleName, |
| 0 | 188 | | timeout).ConfigureAwait(false), |
| 0 | 189 | | _connectionScope, |
| 0 | 190 | | cancellationToken).ConfigureAwait(false); |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Removes the rule on the subscription identified by <paramref name="ruleName" />. |
| | 194 | | /// </summary> |
| | 195 | | /// |
| | 196 | | /// <param name="ruleName">Name of the rule</param> |
| | 197 | | /// <param name="timeout">The per-try timeout specified in the RetryOptions.</param> |
| | 198 | | /// |
| | 199 | | /// <returns>A task instance that represents the asynchronous remove rule operation.</returns> |
| | 200 | | private async Task RemoveRuleInternalAsync( |
| | 201 | | string ruleName, |
| | 202 | | TimeSpan timeout) |
| | 203 | | { |
| | 204 | | // Create an AmqpRequest Message to remove rule |
| 0 | 205 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest( |
| 0 | 206 | | ManagementConstants.Operations.RemoveRuleOperation, |
| 0 | 207 | | timeout, |
| 0 | 208 | | null); |
| 0 | 209 | | amqpRequestMessage.Map[ManagementConstants.Properties.RuleName] = ruleName; |
| | 210 | |
|
| 0 | 211 | | AmqpResponseMessage response = await ManagementUtilities.ExecuteRequestResponseAsync( |
| 0 | 212 | | _connectionScope, |
| 0 | 213 | | _managementLink, |
| 0 | 214 | | amqpRequestMessage, |
| 0 | 215 | | timeout).ConfigureAwait(false); |
| | 216 | |
|
| 0 | 217 | | if (response.StatusCode != AmqpResponseStatusCode.OK) |
| | 218 | | { |
| 0 | 219 | | throw response.ToMessagingContractException(); |
| | 220 | | } |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | /// <summary> |
| | 224 | | /// Get all rules associated with the subscription. |
| | 225 | | /// </summary> |
| | 226 | | /// |
| | 227 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 228 | | /// |
| | 229 | | /// <returns>Returns a list of rules description</returns> |
| | 230 | | public override async Task<IList<RuleProperties>> GetRulesAsync(CancellationToken cancellationToken = default) |
| | 231 | | { |
| 0 | 232 | | IList<RuleProperties> rulesDescription = null; |
| | 233 | |
|
| 0 | 234 | | await _retryPolicy.RunOperation( |
| 0 | 235 | | async (timeout) => |
| 0 | 236 | | rulesDescription = await GetRulesInternalAsync(timeout).ConfigureAwait(false), |
| 0 | 237 | | _connectionScope, |
| 0 | 238 | | cancellationToken).ConfigureAwait(false); |
| | 239 | |
|
| 0 | 240 | | return rulesDescription; |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | /// <summary> |
| | 244 | | /// Get all rules associated with the subscription. |
| | 245 | | /// </summary> |
| | 246 | | /// |
| | 247 | | /// <param name="timeout">The per-try timeout specified in the RetryOptions.</param> |
| | 248 | | /// |
| | 249 | | /// <returns>Returns a list of rules description</returns> |
| | 250 | | private async Task<IList<RuleProperties>> GetRulesInternalAsync(TimeSpan timeout) |
| | 251 | | { |
| 0 | 252 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest( |
| 0 | 253 | | ManagementConstants.Operations.EnumerateRulesOperation, |
| 0 | 254 | | timeout, |
| 0 | 255 | | null); |
| 0 | 256 | | amqpRequestMessage.Map[ManagementConstants.Properties.Top] = int.MaxValue; |
| 0 | 257 | | amqpRequestMessage.Map[ManagementConstants.Properties.Skip] = 0; |
| | 258 | |
|
| 0 | 259 | | var response = await ManagementUtilities.ExecuteRequestResponseAsync( |
| 0 | 260 | | _connectionScope, |
| 0 | 261 | | _managementLink, |
| 0 | 262 | | amqpRequestMessage, |
| 0 | 263 | | timeout).ConfigureAwait(false); |
| 0 | 264 | | var ruleDescriptions = new List<RuleProperties>(); |
| 0 | 265 | | if (response.StatusCode == AmqpResponseStatusCode.OK) |
| | 266 | | { |
| 0 | 267 | | var ruleList = response.GetListValue<AmqpMap>(ManagementConstants.Properties.Rules); |
| 0 | 268 | | foreach (var entry in ruleList) |
| | 269 | | { |
| 0 | 270 | | var amqpRule = (AmqpRuleDescriptionCodec)entry[ManagementConstants.Properties.RuleDescription]; |
| 0 | 271 | | var ruleDescription = AmqpMessageConverter.GetRuleDescription(amqpRule); |
| 0 | 272 | | ruleDescriptions.Add(ruleDescription); |
| | 273 | | } |
| | 274 | | } |
| 0 | 275 | | else if (response.StatusCode == AmqpResponseStatusCode.NoContent) |
| | 276 | | { |
| | 277 | | // Do nothing. Return empty list; |
| | 278 | | } |
| | 279 | | else |
| | 280 | | { |
| 0 | 281 | | throw response.ToMessagingContractException(); |
| | 282 | | } |
| | 283 | |
|
| 0 | 284 | | return ruleDescriptions; |
| 0 | 285 | | } |
| | 286 | |
|
| | 287 | | /// <summary> |
| | 288 | | /// Closes the connection to the transport rule manager instance. |
| | 289 | | /// </summary> |
| | 290 | | /// |
| | 291 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | 292 | | public override async Task CloseAsync(CancellationToken cancellationToken) |
| | 293 | | { |
| 0 | 294 | | if (_closed) |
| | 295 | | { |
| 0 | 296 | | return; |
| | 297 | | } |
| | 298 | |
|
| 0 | 299 | | _closed = true; |
| | 300 | |
|
| 0 | 301 | | if (_managementLink?.TryGetOpenedObject(out var _) == true) |
| | 302 | | { |
| 0 | 303 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 0 | 304 | | await _managementLink.CloseAsync().ConfigureAwait(false); |
| | 305 | | } |
| | 306 | |
|
| 0 | 307 | | _managementLink?.Dispose(); |
| | 308 | |
|
| 0 | 309 | | } |
| | 310 | | } |
| | 311 | | } |