-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
208 lines (169 loc) · 7.52 KB
/
graph.py
File metadata and controls
208 lines (169 loc) · 7.52 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
"""Module Graph of kytos/pathfinder Kytos Network Application."""
from itertools import combinations
from kytos.core import log
try:
import networkx as nx
from networkx.exception import NodeNotFound, NetworkXNoPath
except ImportError:
PACKAGE = 'networkx>=2.2'
log.error(f"Package {PACKAGE} not found. Please 'pip install {PACKAGE}'")
try:
from exactdelaypathfinder.core import ExactDelayPathfinder
except ImportError:
PACKAGE = 'exactdelaypathfinder>=0.1.0'
log.error(f"Package {PACKAGE} not found. Please 'pip install {PACKAGE}'")
class Filter:
"""Class responsible for removing items with disqualifying values."""
def __init__(self, filter_type, filter_function):
self._filter_type = filter_type
self._filter_function = filter_function
def run(self, value, items):
"""Filter out items."""
if isinstance(value, self._filter_type):
return filter(self._filter_function(value), items)
raise TypeError(f"Expected type: {self._filter_type}")
class KytosGraph:
"""Class responsible for the graph generation."""
def __init__(self):
self.graph = nx.Graph()
self._filter_functions = {}
def filter_leq(metric): # Lower values are better
return lambda x: (lambda y: y[2].get(metric, x) <= x)
def filter_geq(metric): # Higher values are better
return lambda x: (lambda y: y[2].get(metric, x) >= x)
def filter_eeq(metric): # Equivalence
return lambda x: (lambda y: y[2].get(metric, x) == x)
self._filter_functions["ownership"] = Filter(
str, filter_eeq("ownership"))
self._filter_functions["bandwidth"] = Filter(
(int, float), filter_geq("bandwidth"))
self._filter_functions["priority"] = Filter(
(int, float), filter_geq("priority"))
self._filter_functions["reliability"] = Filter(
(int, float), filter_geq("reliability"))
self._filter_functions["utilization"] = Filter(
(int, float), filter_leq("utilization"))
self._filter_functions["delay"] = Filter(
(int, float), filter_leq("delay"))
self._path_function = nx.all_shortest_paths
def clear(self):
"""
Remove all nodes and links registered.
"""
self.graph.clear()
def update_topology(self, topology):
"""Update all nodes and links inside the graph."""
self.graph.clear()
self.update_nodes(topology.switches)
self.update_links(topology.links)
def update_nodes(self, nodes):
"""Update all nodes inside the graph."""
for node in nodes.values():
try:
self.graph.add_node(node.id)
for interface in node.interfaces.values():
self.graph.add_node(interface.id)
self.graph.add_edge(node.id, interface.id)
except AttributeError:
raise TypeError("Problems encountered updating nodes inside the graph")
def update_links(self, links):
"""Update all links inside the graph."""
keys = []
for link in links.values():
if link.is_active():
self.graph.add_edge(link.endpoint_a.id, link.endpoint_b.id)
for key, value in link.metadata.items():
keys.append(key) if key not in keys else keys
endpoint_a = link.endpoint_a.id
endpoint_b = link.endpoint_b.id
self.graph[endpoint_a][endpoint_b][key] = value
# self._set_default_metadata(keys) # It creates errors during the path construction
# def _set_default_metadata(self, keys):
# """Set metadata to all links.
#
# Set the value to zero for inexistent metadata in a link to make those
# irrelevant in pathfinding.
# """
# for key in keys:
# for endpoint_a, endpoint_b in self.graph.edges:
# if key not in self.graph[endpoint_a][endpoint_b]:
# self.graph[endpoint_a][endpoint_b][key] = 0
def get_link_metadata(self, endpoint_a, endpoint_b):
"""Return the metadata of a link."""
return self.graph.get_edge_data(endpoint_a, endpoint_b)
@staticmethod
def _remove_switch_hops(circuit):
"""Remove switch hops from a circuit hops list."""
for hop in circuit['hops']:
if len(hop.split(':')) == 8:
circuit['hops'].remove(hop)
def shortest_paths(self, source, destination, parameter=None):
"""Calculate the shortest paths and return them."""
try:
paths = list(nx.shortest_simple_paths(self.graph,
source, destination,
parameter))
except (NodeNotFound, NetworkXNoPath):
return []
return paths
def exact_path(self, total_delay, source, destination):
"""Obtain paths with total delays equal or close to the user's requirements.
This function utilizes the ExactDelayPathfinder
library developed by the AmLight team at FIU.
"""
pathfinder = ExactDelayPathfinder()
result = pathfinder.search(self.graph, total_delay, source, destination)
return result
def constrained_flexible_paths(self, source, destination,
minimum_hits=None, **metrics):
"""Calculate the constrained shortest paths with flexibility."""
base = metrics.get("base", {})
flexible = metrics.get("flexible", {})
first_pass_links = list(self._filter_links(self.graph.edges(data=True),
**base))
length = len(flexible)
if minimum_hits is None:
minimum_hits = length
minimum_hits = min(length, max(0, minimum_hits))
results = []
paths = []
i = 0
while paths == [] and i in range(0, minimum_hits + 1):
for combo in combinations(flexible.items(), length - i):
additional = dict(combo)
paths = self._constrained_shortest_paths(
source, destination,
self._filter_links(first_pass_links,
metadata=False, **additional))
if paths:
results.append(
{"paths": paths, "metrics": {**base, **additional}})
i = i + 1
return results
def _constrained_shortest_paths(self, source, destination, links):
paths = []
try:
paths = list(self._path_function(self.graph.edge_subgraph(links),
source, destination))
except NetworkXNoPath:
pass
except NodeNotFound:
if source == destination:
if source in self.graph.nodes:
paths = [[source]]
return paths
def _filter_links(self, links, metadata=True, **metrics):
for metric, value in metrics.items():
filter_ = self._filter_functions.get(metric, None)
if filter_ is not None:
try:
links = filter_.run(value, links)
except TypeError as err:
raise TypeError(f"Error in {metric} value: {err}")
if not metadata:
links = ((u, v) for u, v, d in links)
return links
def get_nodes(self):
return self.graph.nodes
def get_edges(self):
return self.graph.edges