1
|
|
|
"""Module to test the KytosGraph in graph.py""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
|
4
|
|
|
from kytos.core.interface import Interface |
5
|
|
|
from kytos.core.link import Link |
6
|
|
|
from kytos.core.switch import Switch |
7
|
|
|
|
8
|
|
|
# module under test |
9
|
|
|
from graph import KytosGraph |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestResults(TestCase): |
13
|
|
|
"""Tests for the graph class.""" |
14
|
|
|
|
15
|
|
|
def initializer(self, val=0): |
16
|
|
|
"""Test setup for a specific topology""" |
17
|
|
|
|
18
|
|
|
method_name = "generate_topology" if not val else "generate_topology_" + str(val) |
19
|
|
|
method = getattr(self, method_name) |
20
|
|
|
method() |
21
|
|
|
switches, links = method() |
22
|
|
|
|
23
|
|
|
self.graph = KytosGraph() |
24
|
|
|
self.graph.clear() |
25
|
|
|
self.graph.update_nodes(switches) |
26
|
|
|
self.graph.update_links(links) |
27
|
|
|
|
28
|
|
|
def get_path(self, source, destination): |
29
|
|
|
"""Return the shortest path""" |
30
|
|
|
results = self.graph.shortest_paths(source, destination) |
31
|
|
|
return results |
32
|
|
|
|
33
|
|
|
def get_path_constrained(self, source, destination, minimum_hits=None, |
34
|
|
|
**metrics): |
35
|
|
|
"""Return the constrained shortest path""" |
36
|
|
|
return self.graph.constrained_flexible_paths(source, destination, |
37
|
|
|
minimum_hits, |
38
|
|
|
**metrics) |
39
|
|
|
|
40
|
|
|
@staticmethod |
41
|
|
|
def generate_topology(): |
42
|
|
|
"""Generates a predetermined topology""" |
43
|
|
|
switches = {} |
44
|
|
|
links = {} |
45
|
|
|
return switches, links |
46
|
|
|
|
47
|
|
|
@staticmethod |
48
|
|
|
def create_switch(name, switches): |
49
|
|
|
"""Add a new switch to the list of switches""" |
50
|
|
|
switches[name] = Switch(name) |
51
|
|
|
|
52
|
|
|
@staticmethod |
53
|
|
|
def add_interfaces(count, switch, interfaces): |
54
|
|
|
"""Add a new interface to the list of interfaces""" |
55
|
|
|
for i in range(1, count + 1): |
56
|
|
|
str1 = "{}:{}".format(switch.dpid, i) |
57
|
|
|
interface = Interface(str1, i, switch) |
58
|
|
|
interfaces[str1] = interface |
59
|
|
|
switch.update_interface(interface) |
60
|
|
|
|
61
|
|
|
@staticmethod |
62
|
|
|
def create_link(interface_a, interface_b, interfaces, links): |
63
|
|
|
"""Add a new link between two interfaces into the list of links""" |
64
|
|
|
compounded = "{}|{}".format(interface_a, interface_b) |
65
|
|
|
final_name = compounded |
66
|
|
|
links[final_name] = Link( |
67
|
|
|
interfaces[interface_a], interfaces[interface_b]) |
68
|
|
|
|
69
|
|
|
@staticmethod |
70
|
|
|
def add_metadata_to_link(interface_a, interface_b, metrics, links): |
71
|
|
|
"""Add metadata to an existing link in the list of links""" |
72
|
|
|
compounded = "{}|{}".format(interface_a, interface_b) |
73
|
|
|
links[compounded].extend_metadata(metrics) |
74
|
|
|
|