Passed
Push — master ( 75d007...4d9626 )
by Humberto
01:10 queued 12s
created

build.v0x01.flow.ActionOutput.as_of_action()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 3
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 3
loc 3
ccs 1
cts 2
cp 0.5
crap 1.125
rs 10
c 0
b 0
f 0
1
"""Deal with OpenFlow 1.0 specificities related to flows."""
2 1
from pyof.v0x01.common.action import ActionOutput as OFActionOutput
3 1
from pyof.v0x01.common.action import ActionVlanVid as OFActionVlanVid
4 1
from pyof.v0x01.common.flow_match import Match as OFMatch
5 1
from pyof.v0x01.controller2switch.flow_mod import FlowMod
6
7 1
from napps.kytos.of_core.flow import (ActionBase, ActionFactoryBase, FlowBase,
8
                                      FlowStats, MatchBase, PortStats)
9
10 1
__all__ = ('ActionOutput', 'ActionSetVlan', 'Action', 'Flow', 'FlowStats',
11
           'PortStats')
12
13
14 1
class Match(MatchBase):
15
    """High-level Match for OpenFlow 1.0."""
16
17 1
    @classmethod
18
    def from_of_match(cls, of_match):
19
        """Return an instance from a pyof Match."""
20 1
        match = cls(in_port=of_match.in_port,
21
                    dl_src=of_match.dl_src.value,
22
                    dl_dst=of_match.dl_dst.value,
23
                    dl_vlan=of_match.dl_vlan,
24
                    dl_vlan_pcp=of_match.dl_vlan_pcp,
25
                    dl_type=of_match.dl_type,
26
                    nw_proto=of_match.nw_proto,
27
                    nw_src=of_match.nw_src.value,
28
                    nw_dst=of_match.nw_dst.value,
29
                    tp_src=of_match.tp_src,
30
                    tp_dst=of_match.tp_dst)
31 1
        return match
32
33 1
    def as_of_match(self):
34
        """Return a pyof Match instance with current contents."""
35 1
        match = OFMatch()
36 1
        for field, value in self.__dict__.items():
37 1
            if value is not None:
38 1
                setattr(match, field, value)
39 1
        return match
40
41
42 1 View Code Duplication
class ActionOutput(ActionBase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
43
    """Action with an output port."""
44
45 1
    def __init__(self, port):
46
        """Require an output port.
47
48
        Args:
49
            port (int): Specific port number.
50
        """
51 1
        self.port = port
52 1
        self.action_type = 'output'
53
54 1
    @classmethod
55
    def from_of_action(cls, of_action):
56
        """Return a high-level ActionOuput instance from pyof ActionOutput."""
57
        return ActionOutput(port=of_action.port.value)
58
59 1
    def as_of_action(self):
60
        """Return a pyof ActionOuput instance."""
61
        return OFActionOutput(port=self.port)
62
63
64 1
class ActionSetVlan(ActionBase):
65
    """Action to set VLAN ID."""
66
67 1
    def __init__(self, vlan_id):
68
        """Require a VLAN ID."""
69 1
        self.vlan_id = vlan_id
70 1
        self.action_type = 'set_vlan'
71
72 1
    @classmethod
73
    def from_of_action(cls, of_action):
74
        """Return a high-level ActionSetVlan object from pyof ActionVlanVid."""
75
        return cls(vlan_id=of_action.vlan_id.value)
76
77 1
    def as_of_action(self):
78
        """Return a pyof ActionVlanVid instance."""
79 1
        return OFActionVlanVid(vlan_id=self.vlan_id)
80
81
82 1
class Action(ActionFactoryBase):
83
    """An action to be executed once a flow is activated.
84
85
    This class behavies like a factory but has no "Factory" suffix for end-user
86
    usability issues.
87
    """
88
89
    # Set v0x01 classes for action types and pyof classes
90 1
    _action_class = {
91
        'output': ActionOutput,
92
        'set_vlan': ActionSetVlan,
93
        OFActionOutput: ActionOutput,
94
        OFActionVlanVid: ActionSetVlan
95
    }
96
97
98 1
class Flow(FlowBase):
99
    """High-level flow representation for OpenFlow 1.0.
100
101
    This is a subclass that only deals with 1.0 flow actions.
102
    """
103
104 1
    _action_factory = Action
105 1
    _flow_mod_class = FlowMod
106 1
    _match_class = Match
107
108 1
    @staticmethod
109
    def _get_of_actions(of_flow_stats):
110
        """Return the pyof actions from pyof ``FlowStats.actions``."""
111
        return of_flow_stats.actions
112
113 1
    def _as_of_flow_mod(self, command):
114
        """Return pyof FlowMod with a ``command`` to add or delete a flow."""
115 1
        flow_mod = super()._as_of_flow_mod(command)
116 1
        flow_mod.actions = [action.as_of_action() for action in self.actions]
117
        return flow_mod
118