Total Complexity | 4 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Coverage | 90% |
Changes | 0 |
1 | """Module to create a singleton class to track all switches. After being |
||
2 | loaded by the main napp, this class can be reached just using |
||
3 | Switches().desired_method() |
||
4 | |||
5 | For example: |
||
6 | |||
7 | from switches import Switches |
||
8 | |||
9 | list_of_switches = Switches().get_switches() |
||
10 | switch_01 = Switches().get_switch("00:00:00:00:00:00:00:01") |
||
11 | """ |
||
12 | |||
13 | 1 | from napps.amlight.sdntrace.shared.singleton import Singleton |
|
14 | |||
15 | |||
16 | 1 | class Switches(metaclass=Singleton): |
|
17 | """This class is used to easy app development, decoupling |
||
18 | modules from Kytos core. With a Singleton class for Switches, |
||
19 | napps will not need to keep passing Kytos main class to get |
||
20 | a list of switches. |
||
21 | """ |
||
22 | |||
23 | 1 | def __init__(self, switches): |
|
24 | 1 | self._switches = switches |
|
25 | |||
26 | 1 | def __len__(self): |
|
27 | """Return the number of switches instantiated """ |
||
28 | return len(self._switches) |
||
29 | |||
30 | 1 | def get_switch(self, dpid): |
|
31 | """Query the self.switches |
||
32 | Args: |
||
33 | dpid: datapath id 'str' |
||
34 | |||
35 | Returns: |
||
36 | a kytos.core.switch.Switch() object |
||
37 | False if not found |
||
38 | """ |
||
39 | 1 | return self._switches.get(dpid, False) |
|
40 | |||
41 | 1 | def get_switches(self): |
|
42 | """Return all switches """ |
||
43 | return self._switches.copy().values() |
||
44 |