|
1
|
|
|
"""Flow serializer for OF 1.0.""" |
|
2
|
|
|
from pyof.v0x01.common.action import ActionOutput, ActionType, ActionVlanVid |
|
3
|
|
|
from pyof.v0x01.common.phy_port import Port |
|
4
|
|
|
from pyof.v0x01.controller2switch.flow_mod import FlowMod |
|
5
|
|
|
|
|
6
|
|
|
from napps.kytos.flow_manager.serializers.base import FlowSerializer |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class FlowSerializer10(FlowSerializer): |
|
10
|
|
|
"""Flow serializer for OpenFlow 1.0.""" |
|
11
|
|
|
|
|
12
|
|
|
def __init__(self): |
|
13
|
|
|
"""Initialize OF 1.0 specific variables.""" |
|
14
|
|
|
super().__init__() |
|
15
|
|
|
self.match_attributes = set(( |
|
16
|
|
|
'in_port', |
|
17
|
|
|
'dl_src', |
|
18
|
|
|
'dl_dst', |
|
19
|
|
|
'dl_type', |
|
20
|
|
|
'dl_vlan', |
|
21
|
|
|
'dl_vlan_pcp', |
|
22
|
|
|
'nw_src', |
|
23
|
|
|
'nw_dst', |
|
24
|
|
|
'nw_proto')) |
|
25
|
|
|
|
|
26
|
|
|
def from_dict(self, dictionary): |
|
27
|
|
|
"""Return an OF 1.0 FlowMod message from serialized dictionary.""" |
|
28
|
|
|
flow_mod = FlowMod() |
|
29
|
|
|
for field, data in dictionary.items(): |
|
30
|
|
|
if field in self.flow_attributes: |
|
31
|
|
|
setattr(flow_mod, field, data) |
|
32
|
|
|
elif field == 'match': |
|
33
|
|
|
self._update_match(flow_mod.match, data) |
|
34
|
|
|
elif field == 'actions': |
|
35
|
|
|
actions = self._actions_from_list(data) |
|
36
|
|
|
flow_mod.actions.extend(actions) |
|
37
|
|
|
return flow_mod |
|
38
|
|
|
|
|
39
|
|
|
def _update_match(self, match, dictionary): |
|
40
|
|
|
"""Update match attributes found in dictionary.""" |
|
41
|
|
|
for field, data in dictionary.items(): |
|
42
|
|
|
if field in self.match_attributes: |
|
43
|
|
|
setattr(match, field, data) |
|
44
|
|
|
|
|
45
|
|
|
@staticmethod |
|
46
|
|
|
def _actions_from_list(action_list): |
|
47
|
|
|
"""Return actions found in the action list.""" |
|
48
|
|
|
actions = [] |
|
49
|
|
|
for action in action_list: |
|
50
|
|
|
if action['action_type'] == 'set_vlan': |
|
51
|
|
|
new_action = ActionVlanVid(vlan_id=action['vlan_id']) |
|
52
|
|
|
elif action['action_type'] == 'output': |
|
53
|
|
|
if action['port'] == 'controller': |
|
54
|
|
|
new_action = ActionOutput(port=Port.OFPP_CONTROLLER) |
|
55
|
|
|
else: |
|
56
|
|
|
new_action = ActionOutput(port=action['port']) |
|
57
|
|
|
else: |
|
58
|
|
|
continue |
|
59
|
|
|
actions.append(new_action) |
|
60
|
|
|
return actions |
|
61
|
|
|
|
|
62
|
|
|
def to_dict(self, flow_stats): |
|
63
|
|
|
"""Return a dictionary created from OF 1.0 FlowStats.""" |
|
64
|
|
|
flow_dict = {field: data.value |
|
65
|
|
|
for field, data in vars(flow_stats).items() |
|
66
|
|
|
if field in self.flow_attributes} |
|
67
|
|
|
|
|
68
|
|
|
match_dict = {} |
|
69
|
|
|
for field, data in vars(flow_stats.match).items(): |
|
70
|
|
|
if field in self.match_attributes: |
|
71
|
|
|
match_dict[field] = data.value |
|
72
|
|
|
|
|
73
|
|
|
actions_list = [] |
|
74
|
|
|
for action in flow_stats.actions: |
|
75
|
|
|
if action.action_type == ActionType.OFPAT_SET_VLAN_VID: |
|
76
|
|
|
actions_list.append({'action_type': 'set_vlan', |
|
77
|
|
|
'vlan_id': action.vlan_id.value}) |
|
78
|
|
|
elif action.action_type == ActionType.OFPAT_OUTPUT: |
|
79
|
|
|
if action.port == Port.OFPP_CONTROLLER: |
|
80
|
|
|
actions_list.append({'action_type': 'output', |
|
81
|
|
|
'port': 'controller'}) |
|
82
|
|
|
else: |
|
83
|
|
|
actions_list.append({'action_type': 'output', |
|
84
|
|
|
'port': action.port.value}) |
|
85
|
|
|
|
|
86
|
|
|
flow_dict.update({'match': match_dict, 'actions': actions_list}) |
|
87
|
|
|
return flow_dict |
|
88
|
|
|
|