Passed
Pull Request — master (#69)
by Humberto
01:53
created

build.serializers.v0x01   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
eloc 68
dl 0
loc 88
rs 10
c 0
b 0
f 0
ccs 0
cts 51
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A FlowSerializer10._update_match() 0 5 3
A FlowSerializer10.from_dict() 0 12 5
A FlowSerializer10._actions_from_list() 0 16 5
B FlowSerializer10.to_dict() 0 26 7
A FlowSerializer10.__init__() 0 13 1
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