#!/usr/bin/env python import sys import redis import subprocess import syslog from swsssdk import ConfigDBConnector # Returns BGP ASN as a string def _get_bgp_asn_from_minigraph(): # Get BGP ASN from minigraph proc = subprocess.Popen( ['sonic-cfggen', '-m', '/etc/sonic/minigraph.xml', '-v', 'minigraph_bgp_asn'], stdout=subprocess.PIPE, shell=False, stderr=subprocess.STDOUT) stdout = proc.communicate()[0] proc.wait() return stdout.rstrip('\n') def bgp_config(asn, ip, config): syslog.syslog(syslog.LOG_INFO, '[bgp cfgd] value for {} changed to {}'.format(ip, config)) # Currently dynamic config is supported only for bgp admin status if config.has_key('admin_status'): command_mod = 'no ' if config['admin_status'] == 'up' else '' command = "vtysh -c 'configure terminal' -c 'router bgp {}' -c '{}neighbor {} shutdown'".format(asn, command_mod, ip) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) stdout = p.communicate()[0] p.wait() if p.returncode != 0: syslog.syslog(syslog.LOG_ERR, '[bgp cfgd] command execution returned {}. Command: "{}", stdout: "{}"'.format(p.returncode, command, stdout)) def main(): sub = ConfigDBConnector() bgp_asn = _get_bgp_asn_from_minigraph() handler = lambda table, key, data: bgp_config(bgp_asn, key, data) sub.subscribe('BGP_NEIGHBOR', handler) sub.connect() sub.listen() main()