| 1 |
# Copyright 2014 Orange |
| 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 implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from functools import reduce |
| 16 |
|
| 17 |
from oslo_config import cfg |
| 18 |
from oslo_log import log as logging |
| 19 |
|
| 20 |
from networking_bagpipe.bagpipe_bgp.common import log_decorator |
| 21 |
from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg |
| 22 |
from networking_bagpipe.bagpipe_bgp.common import utils |
| 23 |
from networking_bagpipe.bagpipe_bgp import engine |
| 24 |
from networking_bagpipe.bagpipe_bgp.engine import bgp_peer_worker |
| 25 |
from networking_bagpipe.bagpipe_bgp.engine import exa |
| 26 |
from networking_bagpipe.bagpipe_bgp.engine import exabgp_peer_worker |
| 27 |
from networking_bagpipe.bagpipe_bgp.engine import route_table_manager as rtm |
| 28 |
|
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
# SAFIs for which RFC4684 is effective |
| 33 |
RTC_SAFIS = (exa.SAFI.mpls_vpn, exa.SAFI.evpn) |
| 34 |
|
| 35 |
|
| 36 |
class Manager(engine.EventSource, lg.LookingGlassMixin, utils.ClassReprMixin): |
| 37 |
|
| 38 |
_instance = None |
| 39 |
|
| 40 |
def __init__(self): |
| 41 |
|
| 42 |
LOG.debug("Instantiating BGPManager") |
| 43 |
|
| 44 |
if cfg.CONF.BGP.enable_rtc: |
| 45 |
first_local_subscriber_callback = self.rtc_advertisement_for_sub |
| 46 |
last_local_subscriber_callback = self.rtc_withdrawal_for_sub |
| 47 |
else: |
| 48 |
first_local_subscriber_callback = None |
| 49 |
last_local_subscriber_callback = None |
| 50 |
|
| 51 |
self.rtm = rtm.RouteTableManager(first_local_subscriber_callback, |
| 52 |
last_local_subscriber_callback) |
| 53 |
|
| 54 |
self.rtm.start() |
| 55 |
|
| 56 |
self.peers = {} |
| 57 |
if cfg.CONF.BGP.peers: |
| 58 |
for peer_address in cfg.CONF.BGP.peers: |
| 59 |
LOG.debug("Creating a peer worker for %s", peer_address) |
| 60 |
peer_worker = exabgp_peer_worker.ExaBGPPeerWorker(self, |
| 61 |
peer_address) |
| 62 |
self.peers[peer_address] = peer_worker |
| 63 |
peer_worker.start() |
| 64 |
|
| 65 |
# we need a .name since we'll masquerade as a route_entry source |
| 66 |
self.name = "BGPManager" |
| 67 |
|
| 68 |
engine.EventSource.__init__(self, self.rtm) |
| 69 |
|
| 70 |
def __repr__(self): |
| 71 |
return self.__class__.__name__ |
| 72 |
|
| 73 |
@log_decorator.log |
| 74 |
def stop(self): |
| 75 |
for peer in self.peers.values(): |
| 76 |
peer.stop() |
| 77 |
self.rtm.stop() |
| 78 |
for peer in self.peers.values(): |
| 79 |
peer.join() |
| 80 |
self.rtm.join() |
| 81 |
|
| 82 |
def get_local_address(self): |
| 83 |
return cfg.CONF.BGP.local_address |
| 84 |
|
| 85 |
@log_decorator.log |
| 86 |
def rtc_advertisement_for_sub(self, sub): |
| 87 |
if sub.safi in RTC_SAFIS: |
| 88 |
event = engine.RouteEvent( |
| 89 |
engine.RouteEvent.ADVERTISE, |
| 90 |
self._subscription_2_rtc_route_entry(sub), |
| 91 |
self) |
| 92 |
LOG.debug("Based on subscription => synthesized RTC %s", event) |
| 93 |
self.rtm.enqueue(event) |
| 94 |
|
| 95 |
@log_decorator.log |
| 96 |
def rtc_withdrawal_for_sub(self, sub): |
| 97 |
if sub.safi in RTC_SAFIS: |
| 98 |
event = engine.RouteEvent( |
| 99 |
engine.RouteEvent.WITHDRAW, |
| 100 |
self._subscription_2_rtc_route_entry(sub), |
| 101 |
self) |
| 102 |
LOG.debug("Based on unsubscription => synthesized withdraw" |
| 103 |
" for RTC %s", event) |
| 104 |
self.rtm.enqueue(event) |
| 105 |
|
| 106 |
def _subscription_2_rtc_route_entry(self, subscription): |
| 107 |
|
| 108 |
nlri = exa.RTC.new(exa.AFI.ipv4, |
| 109 |
exa.SAFI.rtc, |
| 110 |
cfg.CONF.BGP.my_as, |
| 111 |
subscription.route_target, |
| 112 |
exa.IP.create(self.get_local_address())) |
| 113 |
|
| 114 |
route_entry = engine.RouteEntry(nlri) |
| 115 |
|
| 116 |
return route_entry |
| 117 |
|
| 118 |
# Looking Glass Functions ################### |
| 119 |
|
| 120 |
def get_lg_map(self): |
| 121 |
return {"peers": (lg.COLLECTION, (self.get_lg_peer_list, |
| 122 |
self.get_lg_peer_path_item)), |
| 123 |
"routes": (lg.FORWARD, self.rtm), |
| 124 |
"workers": (lg.FORWARD, self.rtm), |
| 125 |
"route_counts": (lg.SUBITEM, self.get_lg_route_counts)} |
| 126 |
|
| 127 |
def get_established_peers_count(self): |
| 128 |
return reduce(lambda count, peer: count + |
| 129 |
(isinstance(peer, bgp_peer_worker.BGPPeerWorker) and |
| 130 |
peer.is_established()), |
| 131 |
self.peers.values(), 0) |
| 132 |
|
| 133 |
def get_lg_peer_list(self): |
| 134 |
return [{"id": peer.peer_address, |
| 135 |
"state": peer.fsm.state} for peer in self.peers.values()] |
| 136 |
|
| 137 |
def get_lg_peer_path_item(self, path_item): |
| 138 |
return self.peers[path_item] |
| 139 |
|
| 140 |
def get_lg_route_counts(self): |
| 141 |
return {"local_routes_count": self.rtm.get_local_routes_count(), |
| 142 |
"received_routes_count": self.rtm.get_received_routes_count()} |