1 # Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
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.
16 Defines some model classes related BGP.
18 These class include types used in saving information sent/received over BGP
22 from time import gmtime
25 LOG = logging.getLogger('bgpspeaker.model')
28 class Counter(object):
29 """Simple counter for keeping count of several keys."""
34 def incr(self, counter_name, incr_by=1):
35 self._counters[counter_name] = \
36 self._counters.get(counter_name, 0) + incr_by
38 def get_count(self, counter_name):
39 return self._counters.get(counter_name, 0)
41 def get_counters(self):
42 return self._counters.copy()
45 class OutgoingRoute(object):
46 """Holds state about a route that is queued for being sent to a given sink.
49 __slots__ = ('_path', '_for_route_refresh',
50 'sink', 'next_outgoing_route', 'prev_outgoing_route',
51 'next_sink_out_route', 'prev_sink_out_route')
53 def __init__(self, path, for_route_refresh=False):
60 # Is this update in response for route-refresh request.
61 # No sent-route is queued for the destination for this update.
62 self._for_route_refresh = for_route_refresh
64 # Automatically generated, for list off of Destination.
66 # self.next_outgoing_route
67 # self.prev_outgoing_route
69 # Automatically generated for list off of sink.
71 # self.next_sink_out_route
72 # self.prev_sink_out_route
79 def for_route_refresh(self):
80 return self._for_route_refresh
83 return ('OutgoingRoute(path: %s, for_route_refresh: %s)' %
84 (self.path, self.for_route_refresh))
87 class FlexinetOutgoingRoute(object):
88 """Holds state about a route that is queued for being sent to a given sink.
90 In this case the sink is flexinet peer and this route information is from
91 a VRF which holds Ipv4(v6) NLRIs.
94 __slots__ = ('_path', 'sink', 'next_outgoing_route', 'prev_outgoing_route',
95 'next_sink_out_route', 'prev_sink_out_route', '_route_dist')
97 def __init__(self, path, route_dist):
98 from ryu.services.protocols.bgp.info_base.vrf4 import Vrf4Path
99 from ryu.services.protocols.bgp.info_base.vrf6 import Vrf6Path
100 from ryu.services.protocols.bgp.info_base.vrfevpn import VrfEvpnPath
101 from ryu.services.protocols.bgp.info_base.vrf4fs import (
103 from ryu.services.protocols.bgp.info_base.vrf6fs import (
105 from ryu.services.protocols.bgp.info_base.vrfl2vpnfs import (
107 assert path.route_family in (Vrf4Path.ROUTE_FAMILY,
108 Vrf6Path.ROUTE_FAMILY,
109 VrfEvpnPath.ROUTE_FAMILY,
110 Vrf4FlowSpecPath.ROUTE_FAMILY,
111 Vrf6FlowSpecPath.ROUTE_FAMILY,
112 L2vpnFlowSpecPath.ROUTE_FAMILY,
117 self._route_dist = route_dist
119 # Automatically generated, for list off of Destination.
121 # self.next_outgoing_route
122 # self.prev_outgoing_route
124 # Automatically generated for list off of sink.
126 # self.next_sink_out_route
127 # self.prev_sink_out_route
134 def route_dist(self):
135 return self._route_dist
138 return ('FlexinetOutgoingRoute(path: %s, route_dist: %s)' %
139 (self.path, self.route_dist))
142 class SentRoute(object):
143 """Holds the information that has been sent to one or more sinks
144 about a particular BGP destination.
147 def __init__(self, path, peer, filtered=None, timestamp=None):
148 assert(path and hasattr(peer, 'version_num'))
152 # Peer to which this path was sent.
153 self._sent_peer = peer
155 self.filtered = filtered
158 self.timestamp = timestamp
160 self.timestamp = gmtime()
162 # Automatically generated.
164 # self.next_sent_route
165 # self.prev_sent_route
169 return self._sent_peer
172 class ReceivedRoute(object):
173 """Holds the information that has been received to one sinks
174 about a particular BGP destination.
177 def __init__(self, path, peer, filtered=None, timestamp=None):
178 assert(path and hasattr(peer, 'version_num'))
182 # Peer to which this path was received.
183 self._received_peer = peer
185 self.filtered = filtered
188 self.timestamp = timestamp
190 self.timestamp = gmtime()
193 def received_peer(self):
194 return self._received_peer