| 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 setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         """Setup for most tests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         switches, links = self.generate_topology() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         self.graph = KytosGraph() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         self.graph.clear() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         self.graph.update_nodes(switches) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.graph.update_links(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     def get_path(self, source, destination): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         """Return the shortest path""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         results = self.graph.shortest_paths(source, destination) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     def get_path_constrained(self, source, destination, maximum_misses=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                              **metrics): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         """Return the constrained shortest path""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return self.graph.constrained_flexible_paths(source, destination, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                                                      maximum_misses, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                                                      **metrics) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     def generate_topology(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         """Generates a predetermined topology""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         switches = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         links = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return (switches, links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     @ staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     def create_switch(name, switches): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         '''Add a new switch to the list of switches''' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         switches[name] = Switch(name) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     @ staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     def add_interfaces(count, switch, interfaces): | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         '''Add a new interface to the list of interfaces''' | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         for i in range(1, count + 1): | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             str1 = "{}:{}".format(switch.dpid, i) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             interface = Interface(str1, i, switch) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             interfaces[str1] = interface | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             switch.update_interface(interface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     @ staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     def create_link(interface_a, interface_b, interfaces, links): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         '''Add a new link between two interfaces into the list of links''' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         compounded = "{}|{}".format(interface_a, interface_b) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         final_name = compounded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         links[final_name] = Link( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             interfaces[interface_a], interfaces[interface_b]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     @ staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def add_metadata_to_link(interface_a, interface_b, metrics, links): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         '''Add metadata to an existing link in the list of links''' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         compounded = "{}|{}".format(interface_a, interface_b) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |         links[compounded].extend_metadata(metrics) | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  |