build.user_speed   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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 1
        if self._FILE.exists():
19 1
            with self._FILE.open() as user_file:
20
                # print('UserSpeed', user_file.read())
21 1
                self._speed = json.load(user_file)
22
        else:
23 1
            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 1
        if not isinstance(port, str):
33 1
            port = str(port)
34 1
        speed = None
35 1
        switch = self._speed.get(dpid)
36 1
        if switch is None:
37 1
            speed = self._speed.get('default')
38
        else:
39 1
            if port is None or port not in switch:
40 1
                speed = switch.get('default')
41
            else:
42 1
                speed = switch.get(port)
43
        return speed
44