-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolver.py
More file actions
293 lines (256 loc) · 11.9 KB
/
resolver.py
File metadata and controls
293 lines (256 loc) · 11.9 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
"""Binding resolution with comprehensive pattern matching"""
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 .config import AmqpBindingType, AmqpConfig
from .utils import substitute_parameters, validate_parameters_strict
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"]
# Strict parameter validation first
validate_parameters_strict(channel, param_values)
# Extract AMQP binding if present
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:
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:
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"""
# 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"""
# Determine exchange name and type
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:
resolved_exchange = substitute_parameters(exchange_name, param_values)
case (None, address, _) if address:
resolved_exchange = substitute_parameters(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
match (getattr(binding, "routingKey", None), channel.address, operation_name):
case (routing_key, _, _) if routing_key:
resolved_routing_key = substitute_parameters(routing_key, param_values)
case (None, address, _) if address:
resolved_routing_key = substitute_parameters(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,
)