2019-05-09 01:00:49 -05:00
#!/usr/bin/env python
import sys
import subprocess
2019-10-24 09:35:14 -05:00
import datetime
import time
2019-05-09 01:00:49 -05:00
import syslog
2019-10-24 09:35:14 -05:00
import signal
import traceback
2019-08-29 12:26:48 -05:00
import os
2019-10-24 09:35:14 -05:00
import shutil
import tempfile
import json
from collections import defaultdict
from pprint import pprint
import jinja2
import netaddr
2019-08-29 12:26:48 -05:00
from swsscommon import swsscommon
2019-05-09 01:00:49 -05:00
2019-08-29 12:26:48 -05:00
2019-10-24 09:35:14 -05:00
g_run = True
g_debug = False
def run_command(command, shell=False):
str_cmd = " ".join(command)
if g_debug:
syslog.syslog(syslog.LOG_DEBUG, "execute command {}.".format(str_cmd))
p = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
2019-08-29 12:26:48 -05:00
if p.returncode != 0:
2019-10-24 09:35:14 -05:00
syslog.syslog(syslog.LOG_ERR, 'command execution returned {}. Command: "{}", stdout: "{}", stderr: "{}"'.format(p.returncode, str_cmd, stdout, stderr))
return p.returncode, stdout, stderr
class TemplateFabric(object):
def __init__(self):
j2_template_paths = ['/usr/share/sonic/templates']
j2_loader = jinja2.FileSystemLoader(j2_template_paths)
j2_env = jinja2.Environment(loader=j2_loader, trim_blocks=True)
j2_env.filters['ipv4'] = self.is_ipv4
j2_env.filters['ipv6'] = self.is_ipv6
self.env = j2_env
def from_file(self, filename):
return self.env.get_template(filename)
def from_string(self, tmpl):
return self.env.from_string(tmpl)
@staticmethod
def is_ipv4(value):
if not value:
return False
if isinstance(value, netaddr.IPNetwork):
addr = value
else:
try:
addr = netaddr.IPNetwork(str(value))
except:
return False
return addr.version == 4
@staticmethod
def is_ipv6(value):
if not value:
return False
if isinstance(value, netaddr.IPNetwork):
addr = value
else:
try:
addr = netaddr.IPNetwork(str(value))
except:
return False
return addr.version == 6
2019-08-29 12:26:48 -05:00
class BGPConfigManager(object):
def __init__(self, daemon):
self.bgp_asn = None
2019-10-24 09:35:14 -05:00
self.meta = None
2019-11-04 11:30:18 -06:00
self.neig_meta = {}
2019-10-24 09:35:14 -05:00
self.bgp_messages = []
self.peers = self.load_peers() # we can have bgp monitors peers here. it could be fixed by adding support for it here
fabric = TemplateFabric()
self.bgp_peer_add_template = fabric.from_file('bgpd.peer.conf.j2')
self.bgp_peer_del_template = fabric.from_string('no neighbor {{ neighbor_addr }}')
self.bgp_peer_shutdown = fabric.from_string('neighbor {{ neighbor_addr }} shutdown')
self.bgp_peer_no_shutdown = fabric.from_string('no neighbor {{ neighbor_addr }} shutdown')
2019-08-29 12:26:48 -05:00
daemon.add_manager(swsscommon.CONFIG_DB, swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, self.__metadata_handler)
2019-11-04 11:30:18 -06:00
daemon.add_manager(swsscommon.CONFIG_DB, swsscommon.CFG_DEVICE_NEIGHBOR_METADATA_TABLE_NAME, self.__neighbor_metadata_handler)
2019-08-29 12:26:48 -05:00
daemon.add_manager(swsscommon.CONFIG_DB, swsscommon.CFG_BGP_NEIGHBOR_TABLE_NAME, self.__bgp_handler)
2019-10-24 09:35:14 -05:00
def load_peers(self):
peers = set()
command = ["vtysh", "-c", "show bgp neighbors json"]
rc, out, err = run_command(command)
if rc == 0:
js_bgp = json.loads(out)
peers = set(js_bgp.keys())
return peers
2019-08-29 12:26:48 -05:00
def __metadata_handler(self, key, op, data):
if key != "localhost" \
or "bgp_asn" not in data \
or self.bgp_asn == data["bgp_asn"]:
return
2019-10-24 09:35:14 -05:00
# TODO add ASN update commands
2019-08-29 12:26:48 -05:00
2019-10-24 09:35:14 -05:00
self.meta = { 'localhost': data }
2019-08-29 12:26:48 -05:00
self.bgp_asn = data["bgp_asn"]
self.__update_bgp()
2019-11-04 11:30:18 -06:00
def __neighbor_metadata_handler(self, key, op, data):
if op == swsscommon.SET_COMMAND:
self.neig_meta[key] = data
elif op == swsscommon.DEL_COMMAND:
if key in self.neig_meta:
del self.neig_meta[key]
else:
syslog.syslog(syslog.LOG_ERR,"Can't remove key '%s' from neighbor metadata handler. The key doesn't exist" % key)
else:
syslog.syslog(syslog.LOG_ERR,"Wrong operation '%s' for neighbor metadata handler" % op)
self.__update_bgp()
2019-08-29 12:26:48 -05:00
def __update_bgp(self):
2019-10-24 09:35:14 -05:00
cmds = []
2019-11-04 11:30:18 -06:00
new_bgp_messages = []
2019-10-24 09:35:14 -05:00
for key, op, data in self.bgp_messages:
2019-08-29 12:26:48 -05:00
if op == swsscommon.SET_COMMAND:
2019-10-24 09:35:14 -05:00
if key not in self.peers:
2019-11-04 11:30:18 -06:00
if 'name' in data and data['name'] not in self.neig_meta:
# DEVICE_NEIGHBOR_METADATA should be populated before the rendering
new_bgp_messages.append((key, op, data))
continue
2019-10-29 11:07:50 -05:00
try:
2019-11-04 11:30:18 -06:00
txt = self.bgp_peer_add_template.render(DEVICE_METADATA=self.meta, DEVICE_NEIGHBOR_METADATA=self.neig_meta, neighbor_addr=key, bgp_session=data)
2019-10-29 11:07:50 -05:00
cmds.append(txt)
except:
syslog.syslog(syslog.LOG_ERR, 'Peer {}. Error in rendering the template for "SET" command {}'.format(key, data))
else:
syslog.syslog(syslog.LOG_INFO, 'Peer {} added with attributes {}'.format(key, data))
self.peers.add(key)
2019-10-24 09:35:14 -05:00
else:
# when the peer is already configured we support "shutdown/no shutdown"
# commands for the peers only
if "admin_status" in data:
if data['admin_status'] == 'up':
cmds.append(self.bgp_peer_no_shutdown.render(neighbor_addr=key))
syslog.syslog(syslog.LOG_INFO, 'Peer {} admin state is set to "up"'.format(key))
elif data['admin_status'] == 'down':
cmds.append(self.bgp_peer_shutdown.render(neighbor_addr=key))
syslog.syslog(syslog.LOG_INFO, 'Peer {} admin state is set to "down"'.format(key))
else:
syslog.syslog(syslog.LOG_ERR, "Peer {}: Can't update the peer. has wrong attribute value attr['admin_status'] = '{}'".format(key, data['admin_status']))
else:
syslog.syslog(syslog.LOG_INFO, "Peer {}: Can't update the peer. No 'admin_status' attribute in the request".format(key))
2019-08-29 12:26:48 -05:00
elif op == swsscommon.DEL_COMMAND:
2019-10-24 09:35:14 -05:00
if key in self.peers:
cmds.append(self.bgp_peer_del_template.render(neighbor_addr=key))
syslog.syslog(syslog.LOG_INFO, 'Peer {} has been removed'.format(key))
self.peers.remove(key)
else:
syslog.syslog(syslog.LOG_WARNING, 'Peer {} is not found'.format(key))
2019-11-04 11:30:18 -06:00
self.bgp_messages = new_bgp_messages
2019-10-24 09:35:14 -05:00
if len(cmds) == 0:
return
fd, tmp_filename = tempfile.mkstemp(dir='/tmp')
os.close(fd)
with open (tmp_filename, 'w') as fp:
fp.write('router bgp %s\n' % self.bgp_asn)
for cmd in cmds:
fp.write("%s\n" % cmd)
command = ["vtysh", "-f", tmp_filename]
run_command(command) #FIXME
os.remove(tmp_filename)
2019-08-29 12:26:48 -05:00
def __bgp_handler(self, key, op, data):
2019-10-24 09:35:14 -05:00
self.bgp_messages.append((key, op, data))
2019-08-29 12:26:48 -05:00
# If ASN is not set, we just cache this message until the ASN is set.
2019-10-24 09:35:14 -05:00
if self.bgp_asn is not None:
self.__update_bgp()
2019-08-29 12:26:48 -05:00
class Daemon(object):
SELECT_TIMEOUT = 1000
2019-10-24 09:35:14 -05:00
DATABASE_LIST = [ swsscommon.CONFIG_DB ]
2019-05-09 01:00:49 -05:00
def __init__(self):
2019-10-24 09:35:14 -05:00
self.db_connectors = { db : swsscommon.DBConnector(db, swsscommon.DBConnector.DEFAULT_UNIXSOCKET, 0) for db in Daemon.DATABASE_LIST }
2019-08-29 12:26:48 -05:00
self.selector = swsscommon.Select()
2019-10-24 09:35:14 -05:00
self.callbacks = defaultdict(lambda : defaultdict(list)) # db -> table -> []
2019-08-29 12:26:48 -05:00
self.subscribers = set()
def add_manager(self, db, table_name, callback):
2019-10-24 09:35:14 -05:00
if db not in Daemon.DATABASE_LIST:
raise ValueError("database {} isn't supported. Supported '{}' only.".format(db, ",".join(Daemon.DATABASE_LIST)))
2019-08-29 12:26:48 -05:00
if table_name not in self.callbacks[db]:
2019-10-24 09:35:14 -05:00
conn = self.db_connectors[db]
2019-08-29 12:26:48 -05:00
subscriber = swsscommon.SubscriberStateTable(conn, table_name)
self.subscribers.add(subscriber)
self.selector.addSelectable(subscriber)
self.callbacks[db][table_name].append(callback)
2019-05-09 01:00:49 -05:00
2019-10-24 09:35:14 -05:00
def run(self):
while g_run:
state, _ = self.selector.select(Daemon.SELECT_TIMEOUT)
if state == self.selector.TIMEOUT:
2019-08-29 12:26:48 -05:00
continue
2019-10-24 09:35:14 -05:00
elif state == self.selector.ERROR:
raise Exception("Received error from select")
2019-08-29 12:26:48 -05:00
for subscriber in self.subscribers:
key, op, fvs = subscriber.pop()
if not key:
continue
2019-10-24 09:35:14 -05:00
if g_debug:
syslog.syslog(syslog.LOG_DEBUG, "Received message : {}".format((key, op, fvs)))
2019-08-29 12:26:48 -05:00
for callback in self.callbacks[subscriber.getDbConnector().getDbId()][subscriber.getTableName()]:
2019-10-24 09:35:14 -05:00
callback(key, op, dict(fvs))
def wait_for_bgpd():
# wait for 20 seconds
stop_time = datetime.datetime.now() + datetime.timedelta(seconds=20)
syslog.syslog(syslog.LOG_INFO, "Start waiting for bgpd: %s" % str(datetime.datetime.now()))
while datetime.datetime.now() < stop_time:
rc, out, err = run_command(["vtysh", "-c", "show daemons"])
if rc == 0 and "bgpd" in out:
syslog.syslog(syslog.LOG_INFO, "bgpd connected to vtysh: %s" % str(datetime.datetime.now()))
return
time.sleep(0.1) # sleep 100 ms
raise RuntimeError("bgpd hasn't been started in 20 seconds")
2019-05-09 01:00:49 -05:00
def main():
2019-10-24 09:35:14 -05:00
wait_for_bgpd()
2019-08-29 12:26:48 -05:00
daemon = Daemon()
bgp_manager = BGPConfigManager(daemon)
2019-10-24 09:35:14 -05:00
daemon.run()
def signal_handler(signum, frame):
global g_run
g_run = False
if __name__ == '__main__':
rc = 0
try:
syslog.openlog('bgpcfgd')
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
main()
except KeyboardInterrupt:
syslog.syslog(syslog.LOG_NOTICE, "Keyboard interrupt")
except RuntimeError as e:
syslog.syslog(syslog.LOG_CRIT, "%s" % str(e))
rc = -2
except Exception as e:
syslog.syslog(syslog.LOG_CRIT, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc()))
rc = -1
finally:
syslog.closelog()
try:
sys.exit(rc)
except SystemExit:
os._exit(rc)