1
|
|
|
"""Module Graph of kytos/pathfinder Kytos Network Application.""" |
2
|
|
|
|
3
|
1 |
|
from itertools import combinations |
4
|
|
|
|
5
|
1 |
|
from kytos.core import log |
6
|
1 |
|
|
7
|
1 |
|
try: |
8
|
|
|
import networkx as nx |
9
|
|
|
from networkx.exception import NodeNotFound, NetworkXNoPath |
10
|
|
|
except ImportError: |
11
|
|
|
PACKAGE = 'networkx>=2.2' |
12
|
|
|
log.error(f"Package {PACKAGE} not found. Please 'pip install {PACKAGE}'") |
13
|
1 |
|
|
14
|
|
|
|
15
|
|
|
class Filter: |
16
|
1 |
|
"""Class responsible for removing items with disqualifying values.""" |
17
|
1 |
|
|
18
|
|
|
def __init__(self, filter_type, filter_function): |
19
|
1 |
|
self._filter_type = filter_type |
20
|
|
|
self._filter_function = filter_function |
21
|
1 |
|
|
22
|
|
|
def run(self, value, items): |
23
|
1 |
|
"""Filter out items. Filter chosen is picked at runtime.""" |
24
|
|
|
if isinstance(value, self._filter_type): |
25
|
1 |
|
return filter(self._filter_function(value), items) |
26
|
1 |
|
|
27
|
1 |
|
raise TypeError(f"Expected type: {self._filter_type}") |
28
|
|
|
|
29
|
1 |
|
|
30
|
|
|
class KytosGraph: |
31
|
1 |
|
"""Class responsible for the graph generation.""" |
32
|
1 |
|
|
33
|
1 |
|
def __init__(self): |
34
|
|
|
self.graph = nx.Graph() |
35
|
1 |
|
self._filter_functions = {} |
36
|
1 |
|
|
37
|
1 |
|
def filter_leq(metric): # Lower values are better |
38
|
|
|
return lambda x: (lambda y: y[2].get(metric, x) <= x) |
|
|
|
|
39
|
|
|
|
40
|
|
|
def filter_geq(metric): # Higher values are better |
41
|
|
|
return lambda x: (lambda y: y[2].get(metric, x) >= x) |
|
|
|
|
42
|
1 |
|
|
43
|
|
|
def filter_eeq(metric): # Equivalence |
44
|
1 |
|
return lambda x: (lambda y: y[2].get(metric, x) == x) |
|
|
|
|
45
|
1 |
|
|
46
|
1 |
|
self._filter_functions["ownership"] = Filter( |
47
|
1 |
|
str, filter_eeq("ownership")) |
48
|
1 |
|
self._filter_functions["bandwidth"] = Filter( |
49
|
1 |
|
(int, float), filter_geq("bandwidth")) |
50
|
1 |
|
self._filter_functions["priority"] = Filter( |
51
|
1 |
|
(int, float), filter_geq("priority")) |
52
|
1 |
|
self._filter_functions["reliability"] = Filter( |
53
|
|
|
(int, float), filter_geq("reliability")) |
54
|
1 |
|
self._filter_functions["utilization"] = Filter( |
55
|
|
|
(int, float), filter_leq("utilization")) |
56
|
1 |
|
self._filter_functions["delay"] = Filter( |
57
|
|
|
(int, float), filter_leq("delay")) |
58
|
|
|
self._path_function = nx.all_shortest_paths |
59
|
|
|
|
60
|
|
|
def clear(self): |
61
|
|
|
"""Remove all nodes and links registered.""" |
62
|
1 |
|
self.graph.clear() |
63
|
1 |
|
|
64
|
1 |
|
def update_topology(self, topology): |
65
|
1 |
|
"""Update all nodes and links inside the graph.""" |
66
|
|
|
self.graph.clear() |
67
|
1 |
|
self.update_nodes(topology.switches) |
68
|
|
|
self.update_links(topology.links) |
69
|
|
|
|
70
|
1 |
|
def update_nodes(self, nodes): |
71
|
1 |
|
"""Update all nodes inside the graph.""" |
72
|
1 |
|
for node in nodes.values(): |
73
|
|
|
try: |
74
|
1 |
|
self.graph.add_node(node.id) |
75
|
|
|
|
76
|
1 |
|
for interface in node.interfaces.values(): |
77
|
1 |
|
self.graph.add_node(interface.id) |
78
|
|
|
self.graph.add_edge(node.id, interface.id) |
79
|
|
|
|
80
|
|
|
except AttributeError: |
81
|
|
|
pass |
82
|
1 |
|
|
83
|
|
|
def update_links(self, links): |
84
|
|
|
"""Update all links inside the graph.""" |
85
|
|
|
keys = [] |
86
|
|
|
for link in links.values(): |
87
|
|
|
if link.is_active(): |
88
|
|
|
self.graph.add_edge(link.endpoint_a.id, link.endpoint_b.id) |
89
|
|
|
for key, value in link.metadata.items(): |
90
|
|
|
keys.append(key) |
91
|
|
|
endpoint_a = link.endpoint_a.id |
92
|
|
|
endpoint_b = link.endpoint_b.id |
93
|
|
|
self.graph[endpoint_a][endpoint_b][key] = value |
94
|
|
|
|
95
|
|
|
self._set_default_metadata(keys) |
96
|
|
|
|
97
|
|
|
def _set_default_metadata(self, keys): |
98
|
|
|
"""Set metadata to all links. |
99
|
|
|
|
100
|
|
|
Set the value to zero for inexistent metadata in a link to make those |
101
|
|
|
irrelevant in pathfinding. |
102
|
|
|
""" |
103
|
|
|
for key in keys: |
104
|
|
|
for endpoint_a, endpoint_b in self.graph.edges: |
105
|
|
|
if key not in self.graph[endpoint_a][endpoint_b]: |
106
|
|
|
self.graph[endpoint_a][endpoint_b][key] = 0 |
107
|
|
|
|
108
|
|
|
def get_metadata_from_link(self, endpoint_a, endpoint_b): |
109
|
|
|
"""Return the metadata of a link.""" |
110
|
|
|
return self.graph.edges[endpoint_a, endpoint_b] |
111
|
|
|
|
112
|
|
|
@staticmethod |
113
|
|
|
def _remove_switch_hops(circuit): |
114
|
|
|
"""Remove switch hops from a circuit hops list.""" |
115
|
|
|
for hop in circuit['hops']: |
116
|
|
|
if len(hop.split(':')) == 8: |
117
|
|
|
circuit['hops'].remove(hop) |
118
|
|
|
|
119
|
|
|
def shortest_paths(self, source, destination, parameter=None): |
120
|
|
|
"""Calculate the shortest paths and return them.""" |
121
|
|
|
try: |
122
|
|
|
paths = list(nx.shortest_simple_paths(self.graph, |
123
|
|
|
source, destination, |
124
|
|
|
parameter)) |
125
|
|
|
except (NodeNotFound, NetworkXNoPath): |
126
|
|
|
return [] |
127
|
|
|
return paths |
128
|
|
|
|
129
|
|
|
def constrained_flexible_paths(self, source, destination, |
130
|
|
|
minimum_hits=None, **metrics): |
131
|
|
|
"""Calculate the constrained shortest paths with flexibility.""" |
132
|
|
|
base = metrics.get("base", {}) |
133
|
|
|
flexible = metrics.get("flexible", {}) |
134
|
|
|
default_edge_list = list(self._filter_edges( |
135
|
|
|
self.graph.edges(data=True), **base)) |
136
|
|
|
length = len(flexible) |
137
|
|
|
if minimum_hits is None: |
138
|
|
|
minimum_hits = length |
139
|
|
|
minimum_hits = min(length, max(0, minimum_hits)) |
140
|
|
|
results = [] |
141
|
|
|
paths = [] |
142
|
|
|
i = 0 |
143
|
|
|
while (paths == [] and i in range(0, minimum_hits+1)): |
144
|
|
|
for combo in combinations(flexible.items(), length-i): |
145
|
|
|
additional = dict(combo) |
146
|
|
|
paths = self._constrained_shortest_paths( |
147
|
|
|
source, destination, ((u, v) for u, v, d in |
|
|
|
|
148
|
|
|
self._filter_edges(default_edge_list, |
149
|
|
|
**additional))) |
150
|
|
|
if paths != []: |
151
|
|
|
results.append( |
152
|
|
|
{"paths": paths, "metrics": {**base, **additional}}) |
153
|
|
|
i = i + 1 |
154
|
|
|
return results |
155
|
|
|
|
156
|
|
|
def _constrained_shortest_paths(self, source, destination, edges): |
157
|
|
|
paths = [] |
158
|
|
|
try: |
159
|
|
|
paths = list(self._path_function(self.graph.edge_subgraph(edges), |
160
|
|
|
source, destination)) |
161
|
|
|
except NetworkXNoPath: |
162
|
|
|
pass |
163
|
|
|
except NodeNotFound: |
164
|
|
|
if source == destination: |
165
|
|
|
if source in self.graph.nodes: |
166
|
|
|
paths = [[source]] |
167
|
|
|
return paths |
168
|
|
|
|
169
|
|
|
def _filter_edges(self, edges, **metrics): |
170
|
|
|
for metric, value in metrics.items(): |
171
|
|
|
filter_ = self._filter_functions.get(metric, None) |
172
|
|
|
if filter_ is not None: |
173
|
|
|
try: |
174
|
|
|
edges = filter_.run(value, edges) |
175
|
|
|
except TypeError as err: |
176
|
|
|
raise TypeError(f"Error in {metric} value: {err}") |
177
|
|
|
return edges |
178
|
|
|
|