1# Copyright (C) 2014 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 Defines data types and models required specifically for IPv4 support.
17"""
18
19import logging
20
21from ryu.lib.packet.bgp import IPAddrPrefix
22from ryu.lib.packet.bgp import RF_IPv4_UC
23
24from ryu.services.protocols.bgp.info_base.base import Path
25from ryu.services.protocols.bgp.info_base.base import Table
26from ryu.services.protocols.bgp.info_base.base import Destination
27from ryu.services.protocols.bgp.info_base.base import NonVrfPathProcessingMixin
28from ryu.services.protocols.bgp.info_base.base import PrefixFilter
29
30LOG = logging.getLogger('bgpspeaker.info_base.ipv4')
31
32
33class IPv4Dest(Destination, NonVrfPathProcessingMixin):
34    """VPNv4 Destination
35
36    Store IPv4 Paths.
37    """
38    ROUTE_FAMILY = RF_IPv4_UC
39
40    def _best_path_lost(self):
41        old_best_path = self._best_path
42        NonVrfPathProcessingMixin._best_path_lost(self)
43        self._core_service._signal_bus.best_path_changed(old_best_path, True)
44
45    def _new_best_path(self, best_path):
46        NonVrfPathProcessingMixin._new_best_path(self, best_path)
47        self._core_service._signal_bus.best_path_changed(best_path, False)
48
49
50class Ipv4Table(Table):
51    """Global table to store IPv4 routing information.
52
53    Uses `IPv4Dest` to store destination information for each known vpnv4
54    paths.
55    """
56    ROUTE_FAMILY = RF_IPv4_UC
57    VPN_DEST_CLASS = IPv4Dest
58
59    def __init__(self, core_service, signal_bus):
60        super(Ipv4Table, self).__init__(None, core_service, signal_bus)
61
62    def _table_key(self, nlri):
63        """Return a key that will uniquely identify this NLRI inside
64        this table.
65        """
66        return nlri.prefix
67
68    def _create_dest(self, nlri):
69        return self.VPN_DEST_CLASS(self, nlri)
70
71    def __str__(self):
72        return '%s(scope_id: %s, rf: %s)' % (
73            self.__class__.__name__, self.scope_id, self.route_family
74        )
75
76
77class Ipv4Path(Path):
78    """Represents a way of reaching an VPNv4 destination."""
79    ROUTE_FAMILY = RF_IPv4_UC
80    VRF_PATH_CLASS = None  # defined in init - anti cyclic import hack
81    NLRI_CLASS = IPAddrPrefix
82
83    def __init__(self, *args, **kwargs):
84        super(Ipv4Path, self).__init__(*args, **kwargs)
85        from ryu.services.protocols.bgp.info_base.vrf4 import Vrf4Path
86        self.VRF_PATH_CLASS = Vrf4Path
87
88
89class Ipv4PrefixFilter(PrefixFilter):
90    """IPv4 Prefix Filter class"""
91    ROUTE_FAMILY = RF_IPv4_UC
92