backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / packet / packet_base.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import abc
17 import six
18 from ryu.lib import stringify
19
20
21 @six.add_metaclass(abc.ABCMeta)
22 class PacketBase(stringify.StringifyMixin):
23     """A base class for a protocol (ethernet, ipv4, ...) header."""
24     _TYPES = {}
25
26     @classmethod
27     def get_packet_type(cls, type_):
28         """Per-protocol dict-like get method.
29
30         Provided for convenience of protocol implementers.
31         Internal use only."""
32         return cls._TYPES.get(type_)
33
34     @classmethod
35     def register_packet_type(cls, cls_, type_):
36         """Per-protocol dict-like set method.
37
38         Provided for convenience of protocol implementers.
39         Internal use only."""
40         cls._TYPES[type_] = cls_
41
42     def __init__(self):
43         super(PacketBase, self).__init__()
44
45     def __len__(self):
46         return self._MIN_LEN
47
48     @property
49     def protocol_name(self):
50         return self.__class__.__name__
51
52     @classmethod
53     @abc.abstractmethod
54     def parser(cls, buf):
55         """Decode a protocol header.
56
57         This method is used only when decoding a packet.
58
59         Decode a protocol header at offset 0 in bytearray *buf*.
60         Returns the following three objects.
61
62         * An object to describe the decoded header.
63
64         * A packet_base.PacketBase subclass appropriate for the rest of
65           the packet.  None when the rest of the packet should be considered
66           as raw payload.
67
68         * The rest of packet.
69
70         """
71         pass
72
73     def serialize(self, payload, prev):
74         """Encode a protocol header.
75
76         This method is used only when encoding a packet.
77
78         Encode a protocol header.
79         Returns a bytearray which contains the header.
80
81         *payload* is the rest of the packet which will immediately follow
82         this header.
83
84         *prev* is a packet_base.PacketBase subclass for the outer protocol
85         header.  *prev* is None if the current header is the outer-most.
86         For example, *prev* is ipv4 or ipv6 for tcp.serialize.
87         """
88         pass