-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolver.py
More file actions
382 lines (328 loc) · 15.6 KB
/
resolver.py
File metadata and controls
382 lines (328 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""Binding resolution with comprehensive pattern matching"""
import re
from typing import Any
from asyncapi_python.kernel.document.bindings import AmqpChannelBinding
from asyncapi_python.kernel.document.channel import Channel
from asyncapi_python.kernel.wire import EndpointParams
from asyncapi_python.kernel.wire.utils import substitute_parameters
from .config import AmqpBindingType, AmqpConfig
def _validate_no_wildcards_in_queue(param_values: dict[str, str]) -> None:
"""Validate that parameter values don't contain AMQP wildcards when using queue bindings.
AMQP queue names are literal - they don't support pattern matching.
Only topic exchange routing keys support wildcards (* and #).
Args:
param_values: Dictionary of parameter values to check
Raises:
ValueError: If any parameter value contains wildcard characters
"""
wildcards_found: list[str] = []
for param_name, param_value in param_values.items():
if "*" in param_value or "#" in param_value:
wildcards_found.append(f"{param_name}={param_value}")
if wildcards_found:
raise ValueError(
f"AMQP queue bindings do not support wildcard patterns ('*' or '#'). "
f"Found wildcards in parameters: {', '.join(wildcards_found)}. "
f"Use 'is: routingKey' with a topic exchange for pattern matching, "
f"or provide concrete parameter values for queue bindings."
)
def _substitute_routing_key_with_wildcards(
template: str, param_values: dict[str, str]
) -> str:
"""
Substitute parameters in routing key template, using wildcards for missing parameters.
For topic exchange bindings, missing parameters are replaced with '*' (single-word wildcard).
If no parameters are provided and template has placeholders, all are replaced with '*'.
Parameter values can also explicitly contain wildcards ('*' or '#').
Args:
template: Template string with {param} placeholders (e.g., "weather.{location}.{severity}")
param_values: Dictionary of parameter values (can be empty, partial, or contain wildcards)
Returns:
Resolved routing key with wildcards for missing parameters
Examples:
- template="weather.{location}.{severity}", params={} → "weather.*.*"
- template="weather.{location}.{severity}", params={"location": "NYC"} → "weather.NYC.*"
- template="weather.{location}.{severity}", params={"severity": "#"} → "weather.*.#"
"""
# Find all {param} placeholders
placeholders = re.findall(r"\{(\w+)\}", template)
# Build substitution dict - use provided value or '*' wildcard for missing params
substitutions = {p: param_values.get(p, "*") for p in placeholders}
# Perform substitution
result = template
for key, value in substitutions.items():
result = result.replace(f"{{{key}}}", value)
return result
def resolve_amqp_config(
params: EndpointParams, operation_name: str, app_id: str
) -> AmqpConfig:
"""
Resolve AMQP configuration using comprehensive pattern matching for precedence rules.
Precedence (highest to lowest):
1. Reply channel special case
2. Channel AMQP binding (queue/routingKey/exchange)
3. Channel address (with parameter substitution)
4. Operation name
5. REJECT if none available
"""
channel = params["channel"]
param_values = params["parameters"] or {}
is_reply = params["is_reply"]
# Extract AMQP binding if present (validation will be done later based on binding type)
amqp_binding = None
if channel.bindings and hasattr(channel.bindings, "amqp") and channel.bindings.amqp:
amqp_binding = channel.bindings.amqp
# Comprehensive pattern matching for precedence
match (
is_reply,
amqp_binding,
channel.address,
operation_name,
):
# Reply channel pattern - anonymous queue (no address, no binding)
case (True, None, None, _):
# Anonymous reply queue: exclusive and temporary (deleted on connection loss)
return AmqpConfig(
queue_name=f"reply-{app_id}", # App-specific reply queue
exchange_name="", # Default exchange for reply
routing_key=f"reply-{app_id}", # Direct routing to the reply queue
binding_type=AmqpBindingType.REPLY,
queue_properties={
"durable": False,
"exclusive": True,
"auto_delete": True,
},
)
# Reply channel with explicit address - check if direct queue or topic exchange
case (True, _, address, _) if address:
resolved_address = substitute_parameters(address, param_values)
# If address starts with "reply-", treat it as a direct queue name (RPC pattern)
if resolved_address.startswith("reply-"):
return AmqpConfig(
queue_name=resolved_address, # Use address as queue name
exchange_name="", # Default exchange for direct routing
routing_key=resolved_address, # Route directly to queue
binding_type=AmqpBindingType.REPLY,
queue_properties={
"durable": False,
"exclusive": True,
"auto_delete": True,
},
)
else:
# Topic-based reply pattern - shared exchange with filtering
return AmqpConfig(
queue_name=f"reply-{app_id}", # App-specific reply queue
exchange_name=resolved_address, # Shared exchange for replies
exchange_type="topic", # Enable pattern matching for filtering
routing_key=app_id, # Filter messages by app_id
binding_type=AmqpBindingType.REPLY,
queue_properties={"durable": True, "exclusive": False},
)
# Reply channel with binding - defer to binding resolution
case (True, binding, _, _) if binding and binding.type == "queue":
config = resolve_queue_binding(
binding, param_values, channel, operation_name
)
# Override queue name with reply- prefix for reply queues
config.queue_name = f"reply-{app_id}-{config.queue_name}"
config.routing_key = config.queue_name
config.binding_type = AmqpBindingType.REPLY
return config
case (True, binding, _, _) if binding and binding.type == "routingKey":
config = resolve_routing_key_binding(
binding, param_values, channel, operation_name
)
# For reply with routing key binding, create a prefixed queue
config.queue_name = f"reply-{app_id}"
config.binding_type = AmqpBindingType.REPLY
return config
# AMQP queue binding pattern (dataclass only)
case (False, binding, _, _) if binding and binding.type == "queue":
return resolve_queue_binding(binding, param_values, channel, operation_name)
# AMQP routing key binding pattern (dataclass only)
case (False, binding, _, _) if binding and binding.type == "routingKey":
return resolve_routing_key_binding(
binding, param_values, channel, operation_name
)
# AMQP exchange binding pattern (dataclass only)
case (False, binding, _, _) if binding and binding.exchange:
return resolve_exchange_binding(
binding, param_values, channel, operation_name, channel.key
)
# Channel address pattern (with parameter substitution)
case (False, None, address, _) if address:
# Validate no wildcards for implicit queue binding
_validate_no_wildcards_in_queue(param_values)
resolved_address = substitute_parameters(address, param_values)
return AmqpConfig(
queue_name=resolved_address,
exchange_name="", # Default exchange
routing_key=resolved_address,
binding_type=AmqpBindingType.QUEUE,
queue_properties={"durable": True, "exclusive": False},
)
# Operation name pattern (fallback)
case (False, None, None, op_name) if op_name:
# Validate no wildcards for implicit queue binding
_validate_no_wildcards_in_queue(param_values)
return AmqpConfig(
queue_name=op_name,
exchange_name="", # Default exchange
routing_key=op_name,
binding_type=AmqpBindingType.QUEUE,
queue_properties={"durable": True, "exclusive": False},
)
# No match - reject creation
case _:
raise ValueError(
f"Cannot resolve AMQP binding: no valid configuration found. "
f"Channel: {channel.address}, Binding: {amqp_binding}, Operation: {operation_name}"
)
def resolve_queue_binding(
binding: AmqpChannelBinding,
param_values: dict[str, str],
channel: Channel,
operation_name: str,
) -> AmqpConfig:
"""Resolve AMQP queue binding configuration
Queue bindings require:
- No wildcards allowed in parameter values
"""
# Validate no wildcards in queue binding parameters
_validate_no_wildcards_in_queue(param_values)
# Determine queue name with precedence
match (getattr(binding, "queue", None), channel.address, operation_name):
case (queue_config, _, _) if queue_config and getattr(
queue_config, "name", None
):
queue_name = substitute_parameters(queue_config.name, param_values)
case (_, address, _) if address:
queue_name = substitute_parameters(address, param_values)
case (_, _, op_name) if op_name:
queue_name = op_name
case _:
raise ValueError("Cannot determine queue name for queue binding")
# Extract queue properties
queue_config = getattr(binding, "queue", None)
queue_properties = {"durable": True, "exclusive": False} # Defaults
if queue_config:
if hasattr(queue_config, "durable"):
queue_properties["durable"] = queue_config.durable
if hasattr(queue_config, "exclusive"):
queue_properties["exclusive"] = queue_config.exclusive
if hasattr(queue_config, "auto_delete"):
queue_properties["auto_delete"] = queue_config.auto_delete
return AmqpConfig(
queue_name=queue_name,
exchange_name="", # Queue bindings use default exchange
routing_key=queue_name, # For default exchange, routing_key = queue_name
binding_type=AmqpBindingType.QUEUE,
queue_properties=queue_properties,
)
def resolve_routing_key_binding(
binding: AmqpChannelBinding,
param_values: dict[str, str],
channel: Channel,
operation_name: str,
) -> AmqpConfig:
"""Resolve AMQP routing key binding configuration for pub/sub patterns
For routing key bindings:
- Parameter values can explicitly contain wildcards ('*' or '#')
- Wildcards are allowed for topic exchange pattern matching
"""
# Determine exchange name and type
# For exchange name, we need concrete values (no wildcards)
# If param_values has placeholders, use them; otherwise use literal exchange name
exchange_config = getattr(binding, "exchange", None)
match (
exchange_config and getattr(exchange_config, "name", None),
channel.address,
operation_name,
):
case (exchange_name, _, _) if exchange_name:
# Exchange name should be literal (no parameter substitution for exchange names)
resolved_exchange = exchange_name
case (None, address, _) if address:
# If address is used for exchange, check if it has parameters
# If it does, use wildcards; if not, use as-is
resolved_exchange = _substitute_routing_key_with_wildcards(
address, param_values
)
case (None, None, op_name) if op_name:
resolved_exchange = op_name
case _:
raise ValueError("Cannot determine exchange name for routing key binding")
# Determine exchange type
exchange_type = "topic" # Default for routing key bindings
if exchange_config and hasattr(exchange_config, "type"):
exchange_type = exchange_config.type
# Determine routing key - this is where wildcards are allowed
match (getattr(binding, "routingKey", None), channel.address, operation_name):
case (routing_key, _, _) if routing_key:
# Use wildcard substitution for routing keys
resolved_routing_key = _substitute_routing_key_with_wildcards(
routing_key, param_values
)
case (None, address, _) if address:
# Use wildcard substitution for routing keys from address
resolved_routing_key = _substitute_routing_key_with_wildcards(
address, param_values
)
case (None, None, op_name) if op_name:
resolved_routing_key = op_name
case _:
raise ValueError("Cannot determine routing key for routing key binding")
return AmqpConfig(
queue_name="", # Auto-generated exclusive queue for pub/sub
exchange_name=resolved_exchange,
exchange_type=exchange_type,
routing_key=resolved_routing_key,
binding_type=AmqpBindingType.ROUTING_KEY,
queue_properties={"durable": False, "exclusive": True, "auto_delete": True},
)
def resolve_exchange_binding(
binding: AmqpChannelBinding,
param_values: dict[str, str],
channel: Channel,
operation_name: str,
channel_key: str = "",
) -> AmqpConfig:
"""Resolve AMQP exchange binding configuration for advanced pub/sub"""
# Get exchange config from dataclass binding
exchange_config = getattr(binding, "exchange", None)
exchange_name = getattr(exchange_config, "name", None) if exchange_config else None
match (
exchange_name,
channel.address,
channel_key,
operation_name,
):
case (exchange_name, _, _, _) if exchange_name:
resolved_exchange = substitute_parameters(exchange_name, param_values)
case (None, address, _, _) if address:
resolved_exchange = substitute_parameters(address, param_values)
case (None, None, ch_key, _) if ch_key:
# Use channel key as fallback when address is null
resolved_exchange = ch_key.lstrip("/") # Remove leading slash
case (None, None, "", op_name) if op_name:
resolved_exchange = op_name
case _:
raise ValueError("Cannot determine exchange name for exchange binding")
# Determine exchange type from dataclass
exchange_type = "fanout" # Default for exchange bindings
if exchange_config and hasattr(exchange_config, "type"):
exchange_type = exchange_config.type
# Extract binding arguments for headers exchange from dataclass
binding_args: dict[str, Any] = {}
# Note: bindingKeys is not part of AmqpChannelBinding spec
# This would be handled by operation-level bindings if needed
return AmqpConfig(
queue_name="", # Auto-generated exclusive queue
exchange_name=resolved_exchange,
exchange_type=exchange_type,
routing_key="", # No routing key for fanout/headers exchanges
binding_type=AmqpBindingType.EXCHANGE,
queue_properties={"durable": False, "exclusive": True, "auto_delete": True},
binding_arguments=binding_args,
)