Passed
Pull Request — master (#42)
by Jose
02:25
created

build.user_speed   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 8
eloc 22
dl 0
loc 44
ccs 7
cts 21
cp 0.3333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A UserSpeed.get_speed() 0 19 5
A UserSpeed.__init__() 0 8 3
1
"""Manage link speeds when OF spec is not enough."""
2 1
import json
3 1
from os.path import dirname
4 1
from pathlib import Path
5
6
7 1
class UserSpeed:
8
    """User-defined interface speeds.
9
10
    In case there is no matching speed in OF spec or the speed is not correctly
11
    detected.
12
    """
13
14 1
    _FILE = Path(dirname(__file__)) / 'user_speed.json'
15
16 1
    def __init__(self):
17
        """Load user-created file."""
18
        if self._FILE.exists():
19
            with self._FILE.open() as user_file:
20
                # print('UserSpeed', user_file.read())
21
                self._speed = json.load(user_file)
22
        else:
23
            self._speed = {}
24
25 1
    def get_speed(self, dpid, port=None):
26
        """Return speed in bits/sec or None if not defined by the user.
27
28
        Args:
29
            dpid (str): Switch dpid.
30
            port (int or str): Port number.
31
        """
32
        if not isinstance(port, str):
33
            port = str(port)
34
        speed = None
35
        switch = self._speed.get(dpid)
36
        if switch is None:
37
            speed = self._speed.get('default')
38
        else:
39
            if port is None or port not in switch:
40
                speed = switch.get('default')
41
            else:
42
                speed = switch.get(port)
43
        return speed
44