1 # Copyright (C) 2013 Stratosphere Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # vim: tabstop=4 shiftwidth=4 softtabstop=4
18 from abc import ABCMeta, abstractmethod
21 from ryu.lib.packet import packet
23 LOG = logging.getLogger(__name__)
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):
32 LOG.debug('The packet is discarded by %s: %s', cls, pkt)
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
41 @six.add_metaclass(ABCMeta)
42 class PacketInFilterBase(object):
43 def __init__(self, args):
47 def filter(self, pkt):
51 class RequiredTypeFilter(PacketInFilterBase):
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):