File: model.py

package info (click to toggle)
python-os-ken 3.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,320 kB
  • sloc: python: 100,703; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (194 lines) | stat: -rw-r--r-- 5,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
 Defines some model classes related BGP.

 These class include types used in saving information sent/received over BGP
 sessions.
"""
import logging
from time import gmtime


LOG = logging.getLogger('bgpspeaker.model')


class Counter(object):
    """Simple counter for keeping count of several keys."""

    def __init__(self):
        self._counters = {}

    def incr(self, counter_name, incr_by=1):
        self._counters[counter_name] = \
            self._counters.get(counter_name, 0) + incr_by

    def get_count(self, counter_name):
        return self._counters.get(counter_name, 0)

    def get_counters(self):
        return self._counters.copy()


class OutgoingRoute(object):
    """Holds state about a route that is queued for being sent to a given sink.
    """

    __slots__ = ('_path', '_for_route_refresh',
                 'sink', 'next_outgoing_route', 'prev_outgoing_route',
                 'next_sink_out_route', 'prev_sink_out_route')

    def __init__(self, path, for_route_refresh=False):
        assert path

        self.sink = None

        self._path = path

        # Is this update in response for route-refresh request.
        # No sent-route is queued for the destination for this update.
        self._for_route_refresh = for_route_refresh

        # Automatically generated, for list off of Destination.
        #
        # self.next_outgoing_route
        # self.prev_outgoing_route

        # Automatically generated for list off of sink.
        #
        # self.next_sink_out_route
        # self.prev_sink_out_route

    @property
    def path(self):
        return self._path

    @property
    def for_route_refresh(self):
        return self._for_route_refresh

    def __str__(self):
        return ('OutgoingRoute(path: %s, for_route_refresh: %s)' %
                (self.path, self.for_route_refresh))


class FlexinetOutgoingRoute(object):
    """Holds state about a route that is queued for being sent to a given sink.

    In this case the sink is flexinet peer and this route information is from
    a VRF which holds Ipv4(v6) NLRIs.
    """

    __slots__ = ('_path', 'sink', 'next_outgoing_route', 'prev_outgoing_route',
                 'next_sink_out_route', 'prev_sink_out_route', '_route_dist')

    def __init__(self, path, route_dist):
        from os_ken.services.protocols.bgp.info_base.vrf4 import Vrf4Path
        from os_ken.services.protocols.bgp.info_base.vrf6 import Vrf6Path
        from os_ken.services.protocols.bgp.info_base.vrfevpn import VrfEvpnPath
        from os_ken.services.protocols.bgp.info_base.vrf4fs import (
            Vrf4FlowSpecPath)
        from os_ken.services.protocols.bgp.info_base.vrf6fs import (
            Vrf6FlowSpecPath)
        from os_ken.services.protocols.bgp.info_base.vrfl2vpnfs import (
            L2vpnFlowSpecPath)
        assert path.route_family in (Vrf4Path.ROUTE_FAMILY,
                                     Vrf6Path.ROUTE_FAMILY,
                                     VrfEvpnPath.ROUTE_FAMILY,
                                     Vrf4FlowSpecPath.ROUTE_FAMILY,
                                     Vrf6FlowSpecPath.ROUTE_FAMILY,
                                     L2vpnFlowSpecPath.ROUTE_FAMILY,
                                     )

        self.sink = None
        self._path = path
        self._route_dist = route_dist

        # Automatically generated, for list off of Destination.
        #
        # self.next_outgoing_route
        # self.prev_outgoing_route

        # Automatically generated for list off of sink.
        #
        # self.next_sink_out_route
        # self.prev_sink_out_route

    @property
    def path(self):
        return self._path

    @property
    def route_dist(self):
        return self._route_dist

    def __str__(self):
        return ('FlexinetOutgoingRoute(path: %s, route_dist: %s)' %
                (self.path, self.route_dist))


class SentRoute(object):
    """Holds the information that has been sent to one or more sinks
    about a particular BGP destination.
    """

    def __init__(self, path, peer, filtered=None, timestamp=None):
        assert (path and hasattr(peer, 'version_num'))

        self.path = path

        # Peer to which this path was sent.
        self._sent_peer = peer

        self.filtered = filtered

        if timestamp:
            self.timestamp = timestamp
        else:
            self.timestamp = gmtime()

        # Automatically generated.
        #
        # self.next_sent_route
        # self.prev_sent_route

    @property
    def sent_peer(self):
        return self._sent_peer


class ReceivedRoute(object):
    """Holds the information that has been received to one sinks
    about a particular BGP destination.
    """

    def __init__(self, path, peer, filtered=None, timestamp=None):
        assert (path and hasattr(peer, 'version_num'))

        self.path = path

        # Peer to which this path was received.
        self._received_peer = peer

        self.filtered = filtered

        if timestamp:
            self.timestamp = timestamp
        else:
            self.timestamp = gmtime()

    @property
    def received_peer(self):
        return self._received_peer