Issues (33)

pyof/v0x01/common/utils.py (2 issues)

1
"""Helper python-openflow functions."""
2
3
# System imports
4
5
# Third-party imports
6
7
# Local source tree imports
8
# Importing asynchronous messages
9 1
from pyof.v0x01.asynchronous.error_msg import ErrorMsg
10 1
from pyof.v0x01.asynchronous.flow_removed import FlowRemoved
11 1
from pyof.v0x01.asynchronous.packet_in import PacketIn
12 1
from pyof.v0x01.asynchronous.port_status import PortStatus
13
# Importing controller2switch messages
14 1
from pyof.v0x01.common.header import Header, Type
15 1
from pyof.v0x01.controller2switch.barrier_reply import BarrierReply
16 1
from pyof.v0x01.controller2switch.barrier_request import BarrierRequest
17 1
from pyof.v0x01.controller2switch.features_reply import FeaturesReply
18 1
from pyof.v0x01.controller2switch.features_request import FeaturesRequest
19 1
from pyof.v0x01.controller2switch.flow_mod import FlowMod
20 1
from pyof.v0x01.controller2switch.get_config_reply import GetConfigReply
21 1
from pyof.v0x01.controller2switch.get_config_request import GetConfigRequest
22 1
from pyof.v0x01.controller2switch.packet_out import PacketOut
23 1
from pyof.v0x01.controller2switch.port_mod import PortMod
24 1
from pyof.v0x01.controller2switch.queue_get_config_reply import (
25
    QueueGetConfigReply)
26 1
from pyof.v0x01.controller2switch.queue_get_config_request import (
27
    QueueGetConfigRequest)
28 1
from pyof.v0x01.controller2switch.set_config import SetConfig
29 1
from pyof.v0x01.controller2switch.stats_reply import StatsReply
30 1
from pyof.v0x01.controller2switch.stats_request import StatsRequest
31
# Importing symmetric messages
32 1
from pyof.v0x01.symmetric.echo_reply import EchoReply
33 1
from pyof.v0x01.symmetric.echo_request import EchoRequest
34 1
from pyof.v0x01.symmetric.hello import Hello
35 1
from pyof.v0x01.symmetric.vendor_header import VendorHeader
36
37 1
__all__ = ('MESSAGE_TYPES', 'new_message_from_header',
38
           'new_message_from_message_type', 'unpack_message')
39
40 1
MESSAGE_TYPES = {
41
    str(Type.OFPT_HELLO): Hello,
42
    str(Type.OFPT_ERROR): ErrorMsg,
43
    str(Type.OFPT_ECHO_REQUEST): EchoRequest,
44
    str(Type.OFPT_ECHO_REPLY): EchoReply,
45
    str(Type.OFPT_VENDOR): VendorHeader,
46
    str(Type.OFPT_FEATURES_REQUEST): FeaturesRequest,
47
    str(Type.OFPT_FEATURES_REPLY): FeaturesReply,
48
    str(Type.OFPT_GET_CONFIG_REQUEST): GetConfigRequest,
49
    str(Type.OFPT_GET_CONFIG_REPLY): GetConfigReply,
50
    str(Type.OFPT_SET_CONFIG): SetConfig,
51
    str(Type.OFPT_PACKET_IN): PacketIn,
52
    str(Type.OFPT_FLOW_REMOVED): FlowRemoved,
53
    str(Type.OFPT_PORT_STATUS): PortStatus,
54
    str(Type.OFPT_PACKET_OUT): PacketOut,
55
    str(Type.OFPT_FLOW_MOD): FlowMod,
56
    str(Type.OFPT_PORT_MOD): PortMod,
57
    str(Type.OFPT_STATS_REQUEST): StatsRequest,
58
    str(Type.OFPT_STATS_REPLY): StatsReply,
59
    str(Type.OFPT_BARRIER_REQUEST): BarrierRequest,
60
    str(Type.OFPT_BARRIER_REPLY): BarrierReply,
61
    str(Type.OFPT_QUEUE_GET_CONFIG_REQUEST): QueueGetConfigRequest,
62
    str(Type.OFPT_QUEUE_GET_CONFIG_REPLY): QueueGetConfigReply
63
}
64
65
66 1
def new_message_from_message_type(message_type):
67
    """Given an OpenFlow Message Type, return an empty message of that type.
68
69
    Args:
70
        messageType (:class:`~pyof.v0x01.common.header.Type`):
71
            Python-openflow message.
72
73
    Returns:
74
        Empty OpenFlow message of the requested message type.
75
76
    Raises:
77
        KytosUndefinedMessageType: Unkown Message_Type.
78
79
    """
80 1
    message_type = str(message_type)
81
82 1
    if message_type not in MESSAGE_TYPES:
83
        raise ValueError('"{}" is not known.'.format(message_type))
84
85 1
    message_class = MESSAGE_TYPES.get(message_type)
86 1
    message_instance = message_class()
87
88 1
    return message_instance
89
90
91 1 View Code Duplication
def new_message_from_header(header):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
92
    """Given an OF Header, return an empty message of header's message_type.
93
94
    Args:
95
        header (~pyof.v0x01.common.header.Header): Unpacked OpenFlow Header.
96
97
    Returns:
98
        Empty OpenFlow message of the same type of message_type attribute from
99
        the given header.
100
        The header attribute of the message will be populated.
101
102
    Raises:
103
        KytosUndefinedMessageType: Unkown Message_Type.
104
105
    """
106 1
    message_type = header.message_type
107 1
    if not isinstance(message_type, Type):
108 1
        try:
109 1
            if isinstance(message_type, str):
110
                message_type = Type[message_type]
111 1
            elif isinstance(message_type, int):
112
                message_type = Type(message_type)
113
        except ValueError:
114
            raise ValueError
115
116 1
    message = new_message_from_message_type(message_type)
117 1
    message.header.xid = header.xid
118 1
    message.header.length = header.length
119
120 1
    return message
121
122
123 1 View Code Duplication
def unpack_message(buffer):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
124
    """Unpack the whole buffer, including header pack.
125
126
    Args:
127
        buffer (bytes): Bytes representation of a openflow message.
128
129
    Returns:
130
        object: Instance of openflow message.
131
132
    """
133 1
    hdr_size = Header().get_size()
134 1
    hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:]
135 1
    header = Header()
136 1
    header.unpack(hdr_buff)
137 1
    message = new_message_from_header(header)
138 1
    message.unpack(msg_buff)
139
    return message
140