backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / ofp_pktinfilter.py
1 # Copyright (C) 2013 Stratosphere Inc.
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 # vim: tabstop=4 shiftwidth=4 softtabstop=4
16
17 import logging
18 from abc import ABCMeta, abstractmethod
19 import six
20
21 from ryu.lib.packet import packet
22
23 LOG = logging.getLogger(__name__)
24
25
26 def packet_in_filter(cls, args=None, logging=False):
27     def _packet_in_filter(packet_in_handler):
28         def __packet_in_filter(self, ev):
29             pkt = packet.Packet(ev.msg.data)
30             if not packet_in_handler.pkt_in_filter.filter(pkt):
31                 if logging:
32                     LOG.debug('The packet is discarded by %s: %s', cls, pkt)
33                 return
34             return packet_in_handler(self, ev)
35         pkt_in_filter = cls(args)
36         packet_in_handler.pkt_in_filter = pkt_in_filter
37         return __packet_in_filter
38     return _packet_in_filter
39
40
41 @six.add_metaclass(ABCMeta)
42 class PacketInFilterBase(object):
43     def __init__(self, args):
44         self.args = args
45
46     @abstractmethod
47     def filter(self, pkt):
48         pass
49
50
51 class RequiredTypeFilter(PacketInFilterBase):
52
53     def filter(self, pkt):
54         required_types = self.args.get('types') or []
55         for required_type in required_types:
56             if not pkt.get_protocol(required_type):
57                 return False
58         return True