-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
178 lines (143 loc) · 5.65 KB
/
utils.py
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
""" Support function for main.py """
from typing import Optional
from napps.kytos.telemetry_int import settings
from .exceptions import FlowsNotFound, PriorityOverflow
from .kytos_api_helper import get_stored_flows as _get_stored_flows
async def get_found_stored_flows(cookies: list[int] = None) -> dict[int, list[dict]]:
"""Get stored flows ensuring that flows are found."""
cookies = cookies or []
stored_flows = await _get_stored_flows(cookies)
for cookie, flows in stored_flows.items():
if not flows:
raise FlowsNotFound(get_id_from_cookie(cookie))
return stored_flows
def has_int_enabled(evc: dict) -> bool:
"""Check if evc has telemetry."""
return (
"metadata" in evc
and "telemetry" in evc["metadata"]
and isinstance(evc["metadata"]["telemetry"], dict)
and "enabled" in evc["metadata"]["telemetry"]
and evc["metadata"]["telemetry"]["enabled"]
)
def get_evc_unis(evc: dict) -> tuple[dict, dict]:
"""Parse evc for unis."""
uni_a_split = evc["uni_a"]["interface_id"].split(":")
uni_z_split = evc["uni_z"]["interface_id"].split(":")
return (
{
"interface_id": evc["uni_a"]["interface_id"],
"tag": evc["uni_a"].get("tag", {}),
"port_number": int(uni_a_split[-1]),
"switch": ":".join(uni_a_split[:-1]),
},
{
"interface_id": evc["uni_z"]["interface_id"],
"tag": evc["uni_z"].get("tag", {}),
"port_number": int(uni_z_split[-1]),
"switch": ":".join(uni_z_split[:-1]),
},
)
def add_to_apply_actions(
instructions: list[dict], new_instruction: dict, position: int
):
"""Create the actions list"""
for instruction in instructions:
if instruction["instruction_type"] == "apply_actions":
instruction["actions"].insert(position, new_instruction)
return instructions
def has_instruction_and_action_type(
instructions: list[dict], instruction_type: str, action_type: str
) -> bool:
"""Check if any of the instructions has a given type and action type."""
for instruction in instructions:
if (
instruction["instruction_type"] != instruction_type
or "actions" not in instruction
):
continue
for action in instruction["actions"]:
if "action_type" in action and action["action_type"] == action_type:
return True
return False
def get_cookie(evc_id: str, cookie_prefix: int) -> int:
"""Return the cookie integer from evc id.
cookie_prefix is supposed to be the reserved byte value that
mef_eline or telemetry_int uses.
"""
return int(evc_id, 16) + (cookie_prefix << 56)
def get_id_from_cookie(cookie: int) -> str:
"""Return the evc id given a cookie value."""
evc_id = cookie & 0xFFFFFFFFFFFFFF
return f"{evc_id:x}"
def is_intra_switch_evc(evc):
"""Returns if EVC is intra-switch (two UNIs on the same switch)"""
uni_a, uni_z = get_evc_unis(evc)
if uni_a["switch"] == uni_z["switch"]:
return True
return False
def modify_actions(actions: list[dict], actions_to_change: list[str], remove=True):
"""Change the current actions
If remove == True, remove actions_to_change from actions.
If remove == False, keep actions_to_change, remove everything else
Args:
actions = current list of actions on a flow
actions_to_change = list of actions as strings
remove = boolean
Return
actions
"""
del_indexes = set()
for index, action in enumerate(actions):
if remove:
if action["action_type"] in actions_to_change:
del_indexes.add(index)
else:
if action["action_type"] not in actions_to_change:
del_indexes.add(index)
return [action for i, action in enumerate(actions) if i not in del_indexes]
def set_priority(flow: dict, evc_id: str = "") -> dict:
"""Find a suitable priority number. EP031 describes 100 as the addition."""
if flow["flow"]["priority"] + 100 < (2**16 - 2):
flow["flow"]["priority"] += 100
elif flow["flow"]["priority"] + 1 < (2**16 - 2):
flow["flow"]["priority"] += 1
else:
raise PriorityOverflow(evc_id, f"Flow {flow} would overflow max priority")
return flow
def set_owner(flow: dict) -> dict:
"""Set flow owner."""
flow["flow"]["owner"] = "telemetry_int"
return flow
def get_new_cookie(cookie: int, cookie_prefix=settings.INT_COOKIE_PREFIX) -> int:
"""Convert from mef-eline cookie by replacing the most significant byte."""
return (cookie & 0xFFFFFFFFFFFFFF) + (cookie_prefix << 56)
def set_new_cookie(flow: dict) -> dict:
"""Set new cookie."""
flow["flow"]["cookie"] = get_new_cookie(
flow["flow"]["cookie"], cookie_prefix=settings.INT_COOKIE_PREFIX
)
return flow
def set_instructions_from_actions(flow: dict) -> dict:
"""Get intructions or convert from actions."""
if "instructions" in flow["flow"]:
return flow
instructions = [
{
"instruction_type": "apply_actions",
"actions": flow["flow"].get("actions", []),
}
]
flow["flow"].pop("actions", None)
flow["flow"]["instructions"] = instructions
return flow
def get_svlan_dpid_link(link: dict, dpid: str) -> Optional[int]:
"""Try to get svlan of a link if a dpid matches one of the endpoints."""
if any(
(
link["endpoint_a"]["switch"] == dpid and "s_vlan" in link["metadata"],
link["endpoint_b"]["switch"] == dpid and "s_vlan" in link["metadata"],
)
):
return link["metadata"]["s_vlan"]["value"]
return None