build.shared.switches   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 44
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Switches.__init__() 0 2 1
A Switches.__len__() 0 3 1
A Switches.get_switches() 0 3 1
A Switches.get_switch() 0 10 1
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