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