-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailure_policy.py
More file actions
423 lines (364 loc) · 15.6 KB
/
failure_policy.py
File metadata and controls
423 lines (364 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, List, Literal, Optional, Set
from random import random, sample
from collections import defaultdict, deque
@dataclass
class FailureCondition:
"""
A single condition for matching an entity's attribute with an operator and value.
Example usage (YAML):
conditions:
- attr: "capacity"
operator: "<"
value: 100
Attributes:
attr (str):
The name of the attribute to inspect (e.g., "capacity", "region").
operator (str):
The comparison operator: "==", "!=", "<", "<=", ">", ">=",
"contains", "not_contains", "any_value", or "no_value".
value (Any):
The value to compare against (e.g., 100, True, "foo", etc.).
"""
attr: str
operator: str
value: Any
# Supported entity scopes for a rule
EntityScope = Literal["node", "link", "risk_group"]
@dataclass
class FailureRule:
"""
Defines how to match and then select entities for failure.
Attributes:
entity_scope (EntityScope):
The type of entities this rule applies to: "node", "link", or "risk_group".
conditions (List[FailureCondition]):
A list of conditions to filter matching entities.
logic (Literal["and", "or", "any"]):
"and": All conditions must be true for a match.
"or": At least one condition is true for a match.
"any": Skip condition checks and match all.
rule_type (Literal["random", "choice", "all"]):
The selection strategy among the matched set:
- "random": each matched entity is chosen with probability = `probability`.
- "choice": pick exactly `count` items from the matched set (random sample).
- "all": select every matched entity in the matched set.
probability (float):
Probability in [0,1], used if `rule_type="random"`.
count (int):
Number of entities to pick if `rule_type="choice"`.
"""
entity_scope: EntityScope
conditions: List[FailureCondition] = field(default_factory=list)
logic: Literal["and", "or", "any"] = "and"
rule_type: Literal["random", "choice", "all"] = "all"
probability: float = 1.0
count: int = 1
def __post_init__(self) -> None:
if self.rule_type == "random":
if not (0.0 <= self.probability <= 1.0):
raise ValueError(
f"probability={self.probability} must be within [0,1] "
f"for rule_type='random'."
)
@dataclass
class FailurePolicy:
"""
A container for multiple FailureRules plus optional metadata in `attrs`.
The main entry point is `apply_failures`, which:
1) For each rule, gather the relevant entities (node, link, or risk_group).
2) Match them based on rule conditions (or skip if 'logic=any').
3) Apply the selection strategy (all, random, or choice).
4) Collect the union of all failed entities across all rules.
5) Optionally expand failures by shared-risk groups or sub-risks.
Large-scale performance:
- If you set `use_cache=True`, matched sets for each rule are cached,
so repeated calls to `apply_failures` can skip re-matching if the
network hasn't changed. If your network changes between calls,
you should clear the cache or re-initialize the policy.
Attributes:
rules (List[FailureRule]):
A list of FailureRules to apply.
attrs (Dict[str, Any]):
Arbitrary metadata about this policy (e.g. "name", "description").
fail_shared_risk_groups (bool):
If True, after initial selection, expand failures among any
node/link that shares a risk group with a failed entity.
fail_risk_group_children (bool):
If True, and if a risk_group is marked as failed, expand to
children risk_groups recursively.
use_cache (bool):
If True, match results for each rule are cached to speed up
repeated calls. If the network changes, the cached results
may be stale.
"""
rules: List[FailureRule] = field(default_factory=list)
attrs: Dict[str, Any] = field(default_factory=dict)
fail_shared_risk_groups: bool = False
fail_risk_group_children: bool = False
use_cache: bool = False
# Internal cache for matched sets: (rule_index -> set_of_entities)
_match_cache: Dict[int, Set[str]] = field(default_factory=dict, init=False)
def apply_failures(
self,
network_nodes: Dict[str, Any],
network_links: Dict[str, Any],
network_risk_groups: Dict[str, Any] = None,
) -> List[str]:
"""
Identify which entities fail given the defined rules, then optionally
expand by shared-risk groups or nested risk groups.
Args:
network_nodes: {node_id -> node_object_or_dict}, each with top-level attributes
(capacity, disabled, risk_groups, etc.).
network_links: {link_id -> link_object_or_dict}, similarly.
network_risk_groups: {rg_name -> RiskGroup} or dict. If you don't have risk
groups, pass None or {}.
Returns:
A list of IDs that fail (union of all rule matches, possibly expanded).
For risk groups, the ID is the risk group's name.
"""
if network_risk_groups is None:
network_risk_groups = {}
failed_nodes: Set[str] = set()
failed_links: Set[str] = set()
failed_risk_groups: Set[str] = set()
# 1) Collect matched from each rule
for idx, rule in enumerate(self.rules):
matched_ids = self._match_scope(
idx,
rule,
network_nodes,
network_links,
network_risk_groups,
)
# Then select a subset from matched_ids according to rule_type
selected = self._select_entities(matched_ids, rule)
# Add them to the respective fail sets
if rule.entity_scope == "node":
failed_nodes |= set(selected)
elif rule.entity_scope == "link":
failed_links |= set(selected)
elif rule.entity_scope == "risk_group":
failed_risk_groups |= set(selected)
# 2) Optionally expand failures by shared-risk groups
if self.fail_shared_risk_groups:
self._expand_shared_risk_groups(
failed_nodes, failed_links, network_nodes, network_links
)
# 3) Optionally expand risk-group children (if a risk group is failed, recursively fail children)
if self.fail_risk_group_children and failed_risk_groups:
self._expand_failed_risk_group_children(
failed_risk_groups, network_risk_groups
)
# Return union: node IDs, link IDs, and risk_group names
# For the code that uses this, you can interpret them in your manager.
all_failed = set(failed_nodes) | set(failed_links) | set(failed_risk_groups)
return sorted(all_failed)
def _match_scope(
self,
rule_idx: int,
rule: FailureRule,
network_nodes: Dict[str, Any],
network_links: Dict[str, Any],
network_risk_groups: Dict[str, Any],
) -> Set[str]:
"""
Get the set of IDs matched by the given rule, either from cache
or by performing a fresh match over the relevant entity type.
"""
if self.use_cache and rule_idx in self._match_cache:
return self._match_cache[rule_idx]
# Decide which mapping to iterate
if rule.entity_scope == "node":
matched = self._match_entities(network_nodes, rule.conditions, rule.logic)
elif rule.entity_scope == "link":
matched = self._match_entities(network_links, rule.conditions, rule.logic)
else: # risk_group
matched = self._match_entities(
network_risk_groups, rule.conditions, rule.logic
)
if self.use_cache:
self._match_cache[rule_idx] = matched
return matched
def _match_entities(
self,
entity_map: Dict[str, Any],
conditions: List[FailureCondition],
logic: str,
) -> Set[str]:
"""
Return all entity IDs that match the given conditions based on 'and'/'or'/'any' logic.
entity_map is either nodes, links, or risk_groups:
{entity_id -> {top_level_attr: value, ...}}
If logic='any', skip condition checks and return everything.
If no conditions and logic!='any', return empty set.
Returns:
A set of matching entity IDs.
"""
if logic == "any":
return set(entity_map.keys())
if not conditions:
return set()
matched = set()
for entity_id, attrs in entity_map.items():
if self._evaluate_conditions(attrs, conditions, logic):
matched.add(entity_id)
return matched
@staticmethod
def _evaluate_conditions(
attrs: Dict[str, Any],
conditions: List[FailureCondition],
logic: str,
) -> bool:
"""
Evaluate multiple conditions on a single entity. All or any condition(s)
must pass, depending on 'logic'.
"""
if logic == "and":
return all(_evaluate_condition(attrs, c) for c in conditions)
elif logic == "or":
return any(_evaluate_condition(attrs, c) for c in conditions)
else:
raise ValueError(f"Unsupported logic: {logic}")
@staticmethod
def _select_entities(entity_ids: Set[str], rule: FailureRule) -> Set[str]:
"""
From the matched IDs, pick which entities fail under the given rule_type.
"""
if not entity_ids:
return set()
if rule.rule_type == "random":
return {eid for eid in entity_ids if random() < rule.probability}
elif rule.rule_type == "choice":
count = min(rule.count, len(entity_ids))
# sample needs a list
return set(sample(list(entity_ids), k=count))
elif rule.rule_type == "all":
return entity_ids
else:
raise ValueError(f"Unsupported rule_type: {rule.rule_type}")
def _expand_shared_risk_groups(
self,
failed_nodes: Set[str],
failed_links: Set[str],
network_nodes: Dict[str, Any],
network_links: Dict[str, Any],
) -> None:
"""
Expand failures among any node/link that shares a risk group
with a failed entity. BFS until no new failures.
"""
# We'll handle node + link expansions only. (Risk group expansions are separate.)
# Build a map risk_group -> set of node or link IDs
rg_to_entities: Dict[str, Set[str]] = defaultdict(set)
# Gather risk_groups from nodes
for n_id, nd in network_nodes.items():
if "risk_groups" in nd and nd["risk_groups"]:
for rg in nd["risk_groups"]:
rg_to_entities[rg].add(n_id)
# Gather risk_groups from links
for l_id, lk in network_links.items():
if "risk_groups" in lk and lk["risk_groups"]:
for rg in lk["risk_groups"]:
rg_to_entities[rg].add(l_id)
# Combined set of failed node/link IDs
queue = deque(failed_nodes | failed_links)
visited = set(queue) # track which entity IDs we've processed
while queue:
current_id = queue.popleft()
# figure out if current_id is a node or a link by seeing where it appears
current_rgs = []
if current_id in network_nodes:
# node
nd = network_nodes[current_id]
current_rgs = nd.get("risk_groups", [])
elif current_id in network_links:
# link
lk = network_links[current_id]
current_rgs = lk.get("risk_groups", [])
for rg in current_rgs:
# all entity IDs in rg_to_entities[rg] should be failed
for other_id in rg_to_entities[rg]:
if other_id not in visited:
visited.add(other_id)
queue.append(other_id)
if other_id in network_nodes:
failed_nodes.add(other_id)
elif other_id in network_links:
failed_links.add(other_id)
# if other_id in risk_groups => not handled here
def _expand_failed_risk_group_children(
self,
failed_rgs: Set[str],
all_risk_groups: Dict[str, Any],
) -> None:
"""
If we fail a risk_group, also fail its descendants recursively.
We assume each entry in all_risk_groups is something like:
rg_name -> RiskGroup object or { 'name': .., 'children': [...] }
BFS or DFS any children to mark them as failed as well.
"""
queue = deque(failed_rgs)
while queue:
rg_name = queue.popleft()
rg_data = all_risk_groups.get(rg_name)
if not rg_data:
continue
# Suppose the children are in rg_data["children"]
# or if it's an actual RiskGroup object => rg_data.children
child_list = []
if isinstance(rg_data, dict):
child_list = rg_data.get("children", [])
else:
# assume it's a RiskGroup object with a .children
child_list = rg_data.children
for child_obj in child_list:
# child_obj might be a dict or RiskGroup with name
child_name = (
child_obj["name"] if isinstance(child_obj, dict) else child_obj.name
)
if child_name not in failed_rgs:
failed_rgs.add(child_name)
queue.append(child_name)
def _evaluate_condition(entity_attrs: Dict[str, Any], cond: FailureCondition) -> bool:
"""
Evaluate a single FailureCondition against entity attributes.
Operators supported:
==, !=, <, <=, >, >=
contains, not_contains
any_value, no_value
If entity_attrs does not have cond.attr => derived_value=None.
Returns True if condition passes, else False.
"""
has_attr = cond.attr in entity_attrs
derived_value = entity_attrs.get(cond.attr, None)
op = cond.operator
if op == "==":
return derived_value == cond.value
elif op == "!=":
return derived_value != cond.value
elif op == "<":
return (derived_value is not None) and (derived_value < cond.value)
elif op == "<=":
return (derived_value is not None) and (derived_value <= cond.value)
elif op == ">":
return (derived_value is not None) and (derived_value > cond.value)
elif op == ">=":
return (derived_value is not None) and (derived_value >= cond.value)
elif op == "contains":
if derived_value is None:
return False
return cond.value in derived_value
elif op == "not_contains":
if derived_value is None:
return True
return cond.value not in derived_value
elif op == "any_value":
return has_attr # True if attribute key exists
elif op == "no_value":
# Pass if the attribute key is missing or the value is None
return (not has_attr) or (derived_value is None)
else:
raise ValueError(f"Unsupported operator: {op}")