Total Complexity | 6 |
Total Lines | 56 |
Duplicated Lines | 0 % |
Coverage | 87.5% |
Changes | 0 |
1 | """ |
||
2 | Module to interact with Coloring Napp. |
||
3 | """ |
||
4 | |||
5 | |||
6 | 1 | import json |
|
7 | 1 | import requests |
|
8 | 1 | from kytos.core import log |
|
9 | 1 | from napps.amlight.sdntrace import settings |
|
10 | |||
11 | |||
12 | 1 | class Colors(object): |
|
13 | """ Class to handle the gathering of colors from |
||
14 | amlight/coloring Napp |
||
15 | |||
16 | """ |
||
17 | |||
18 | 1 | def __init__(self): |
|
19 | """ Instantiate Colors and get list of colors |
||
20 | """ |
||
21 | 1 | self._url = settings.COLORS_URL |
|
22 | 1 | self._colors = dict() |
|
23 | 1 | self._get_colors() |
|
24 | |||
25 | 1 | def _get_colors(self): |
|
26 | """ Get list of colors |
||
27 | """ |
||
28 | 1 | try: |
|
29 | 1 | result = requests.get(url=self._url) |
|
30 | 1 | if result.status_code == 200: |
|
31 | 1 | result = json.loads(result.content) |
|
32 | 1 | self._colors = result['colors'] |
|
33 | else: |
||
34 | raise Exception |
||
35 | 1 | except Exception as err: # pylint: disable=broad-except |
|
36 | 1 | log.error('Error: Can not connect to Kytos/Coloring: %s' % err) |
|
37 | |||
38 | 1 | def get_switch_color(self, dpid): |
|
39 | """ Get the color_field and color_value of a specific |
||
40 | switch. At every call, queries Coloring Napp to make sure |
||
41 | colors reflects a possible topology. |
||
42 | |||
43 | Args: |
||
44 | dpid: switch.dpid |
||
45 | |||
46 | Return: |
||
47 | dict: {'color_field': str, 'color_value': str} |
||
48 | or |
||
49 | dict: {} if not found |
||
50 | """ |
||
51 | 1 | self._get_colors() |
|
52 | 1 | try: |
|
53 | 1 | return self._colors[dpid] |
|
54 | except KeyError: |
||
55 | return {} |
||
56 |