backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / model.py
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 some model classes related BGP.
17
18  These class include types used in saving information sent/received over BGP
19  sessions.
20 """
21 import logging
22 from time import gmtime
23
24
25 LOG = logging.getLogger('bgpspeaker.model')
26
27
28 class Counter(object):
29     """Simple counter for keeping count of several keys."""
30
31     def __init__(self):
32         self._counters = {}
33
34     def incr(self, counter_name, incr_by=1):
35         self._counters[counter_name] = \
36             self._counters.get(counter_name, 0) + incr_by
37
38     def get_count(self, counter_name):
39         return self._counters.get(counter_name, 0)
40
41     def get_counters(self):
42         return self._counters.copy()
43
44
45 class OutgoingRoute(object):
46     """Holds state about a route that is queued for being sent to a given sink.
47     """
48
49     __slots__ = ('_path', '_for_route_refresh',
50                  'sink', 'next_outgoing_route', 'prev_outgoing_route',
51                  'next_sink_out_route', 'prev_sink_out_route')
52
53     def __init__(self, path, for_route_refresh=False):
54         assert(path)
55
56         self.sink = None
57
58         self._path = path
59
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
63
64         # Automatically generated, for list off of Destination.
65         #
66         # self.next_outgoing_route
67         # self.prev_outgoing_route
68
69         # Automatically generated for list off of sink.
70         #
71         # self.next_sink_out_route
72         # self.prev_sink_out_route
73
74     @property
75     def path(self):
76         return self._path
77
78     @property
79     def for_route_refresh(self):
80         return self._for_route_refresh
81
82     def __str__(self):
83         return ('OutgoingRoute(path: %s, for_route_refresh: %s)' %
84                 (self.path, self.for_route_refresh))
85
86
87 class FlexinetOutgoingRoute(object):
88     """Holds state about a route that is queued for being sent to a given sink.
89
90     In this case the sink is flexinet peer and this route information is from
91     a VRF which holds Ipv4(v6) NLRIs.
92     """
93
94     __slots__ = ('_path', 'sink', 'next_outgoing_route', 'prev_outgoing_route',
95                  'next_sink_out_route', 'prev_sink_out_route', '_route_dist')
96
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 (
102             Vrf4FlowSpecPath)
103         from ryu.services.protocols.bgp.info_base.vrf6fs import (
104             Vrf6FlowSpecPath)
105         from ryu.services.protocols.bgp.info_base.vrfl2vpnfs import (
106             L2vpnFlowSpecPath)
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,
113                                      )
114
115         self.sink = None
116         self._path = path
117         self._route_dist = route_dist
118
119         # Automatically generated, for list off of Destination.
120         #
121         # self.next_outgoing_route
122         # self.prev_outgoing_route
123
124         # Automatically generated for list off of sink.
125         #
126         # self.next_sink_out_route
127         # self.prev_sink_out_route
128
129     @property
130     def path(self):
131         return self._path
132
133     @property
134     def route_dist(self):
135         return self._route_dist
136
137     def __str__(self):
138         return ('FlexinetOutgoingRoute(path: %s, route_dist: %s)' %
139                 (self.path, self.route_dist))
140
141
142 class SentRoute(object):
143     """Holds the information that has been sent to one or more sinks
144     about a particular BGP destination.
145     """
146
147     def __init__(self, path, peer, filtered=None, timestamp=None):
148         assert(path and hasattr(peer, 'version_num'))
149
150         self.path = path
151
152         # Peer to which this path was sent.
153         self._sent_peer = peer
154
155         self.filtered = filtered
156
157         if timestamp:
158             self.timestamp = timestamp
159         else:
160             self.timestamp = gmtime()
161
162         # Automatically generated.
163         #
164         # self.next_sent_route
165         # self.prev_sent_route
166
167     @property
168     def sent_peer(self):
169         return self._sent_peer
170
171
172 class ReceivedRoute(object):
173     """Holds the information that has been received to one sinks
174     about a particular BGP destination.
175     """
176
177     def __init__(self, path, peer, filtered=None, timestamp=None):
178         assert(path and hasattr(peer, 'version_num'))
179
180         self.path = path
181
182         # Peer to which this path was received.
183         self._received_peer = peer
184
185         self.filtered = filtered
186
187         if timestamp:
188             self.timestamp = timestamp
189         else:
190             self.timestamp = gmtime()
191
192     @property
193     def received_peer(self):
194         return self._received_peer