def set_vlan_dhcp_relay(config_db, vid, ip_addresses): for ip_addr in ip_addresses: if not is_ipaddress(ip_addr): util_utl.utl_err("{} is an invalid IP address".format(ip_addr)) return False vlan_name = "Vlan{}".format(vid) vlan = config_db.get_entry('VLAN', vlan_name) if len(vlan) == 0: util_utl.utl_err("{} does not exist".format(vlan_name)) return False dhcp_relays = vlan.get('dhcp_servers', []) if len(ip_addresses) == 0: del vlan['dhcp_servers'] else: del dhcp_relays[:] for ip_addr in ip_addresses: dhcp_relays.append(ip_addr) vlan['dhcp_servers'] = dhcp_relays config_db.set_entry('VLAN', vlan_name, vlan) util_utl.utl_log("Set DHCP relay destination addresses {} for {}".format( ip_addresses, vlan_name)) # try: # util_utl.utl_execute_cmd("systemctl restart dhcp_relay") # except SystemExit as e: # util_utl.utl_err("Restart service dhcp_relay failed with error {}" # .format(e)) # return False return True
def interface_db_set_vlan_member(db, is_add, vid, interface_name, untagged=True): vlan_name = 'Vlan{}'.format(vid) vlan = db.get_entry('VLAN', vlan_name) if len(vlan) == 0: util_utl.utl_err("{} doesn't exist".format(vlan_name)) return if is_add: members = vlan.get('members', []) if interface_name in members: util_utl.utl_log("{} is already a member of {}".format(interface_name, vlan_name)) return members.append(interface_name) vlan['members'] = members db.set_entry('VLAN', vlan_name, vlan) db.set_entry('VLAN_MEMBER', (vlan_name, interface_name), {'tagging_mode': "untagged" if untagged else "tagged"}) else: members = vlan.get('members', []) if interface_name not in members: util_utl.utl_log("{} is not a member of {}".format(interface_name, vlan_name)) return members.remove(interface_name) if len(members) == 0: del vlan['members'] else: vlan['members'] = members db.set_entry('VLAN', vlan_name, vlan) db.set_entry('VLAN_MEMBER', (vlan_name, interface_name), None)
def del_vlan_dhcp_relay(config_db, vid, ip_addr): if not is_ipaddress(ip_addr): util_utl.utl_err("{} is an invalid IP address".format(ip_addr)) return False vlan_name = "Vlan{}".format(vid) vlan = config_db.get_entry('VLAN', vlan_name) if len(vlan) == 0: util_utl.utl_err("{} does not exist".format(vlan_name)) return False dhcp_relays = vlan.get('dhcp_servers', []) if ip_addr not in dhcp_relays: util_utl.utl_log("{} is not a DHCP relay destination for {}".format( ip_addr, vlan_name)) else: dhcp_relays.remove(ip_addr) if len(dhcp_relays) == 0: del vlan['dhcp_servers'] else: vlan['dhcp_servers'] = dhcp_relays config_db.set_entry('VLAN', vlan_name, vlan) util_utl.utl_log( "Remove DHCP relay destination address {} from {}".format( ip_addr, vlan_name)) # try: # util_utl.utl_execute_cmd("systemctl restart dhcp_relay") # except SystemExit as e: # util_utl.utl_err("Restart service dhcp_relay failed with error {}" # .format(e)) # return False return True
def vrf_delete_management_vrf(config_db): """Disable management vrf in config DB""" entry = config_db.get_entry('MGMT_VRF_CONFIG', "vrf_global") if not entry or entry['mgmtVrfEnabled'] == 'false': util_utl.utl_log("ManagementVRF is already Disabled.") return None config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "false"}) mvrf_restart_services()
def vrf_add_management_vrf(config_db): """Enable management vrf in config DB""" entry = config_db.get_entry('MGMT_VRF_CONFIG', "vrf_global") if entry and entry['mgmtVrfEnabled'] == 'true': util_utl.utl_log("ManagementVRF is already Enabled.") return None config_db.mod_entry('MGMT_VRF_CONFIG', "vrf_global", {"mgmtVrfEnabled": "true"}) mvrf_restart_services()
def sonic_set_sonic_db(root_yph, pkey_ar, val, is_create, disp_args): if util_utl.utl_is_flag_on(util_utl.TAG_SKIP_QOS): for tbl in SONIC_QOS_TBL: if tbl in val: util_utl.utl_log('SKIP QOS: ' + val) return True exec_cmd = 'sonic-cfggen -a \'%s\' --write-to-db' % val ret_val = util_utl.utl_execute_cmd(exec_cmd) return ret_val
def del_loopback(config_db, loopback_name): if not is_loopback_name_valid(loopback_name): util_utl.utl_log("loopback name-{} is invalid".format(loopback_name)) return False loopback_table = config_db.get_table('LOOPBACK_INTERFACE') loopback_interfaces = [k for k, v in loopback_table.iteritems() if type(k) != tuple] if loopback_name not in loopback_interfaces: util_utl.utl_log("{} does not exists".format(loopback_name)) return True ips = [k[1] for k in loopback_table if type(k) == tuple and k[0] == loopback_name] for ip in ips: config_db.set_entry('LOOPBACK_INTERFACE', (loopback_name, ip), None) config_db.set_entry('LOOPBACK_INTERFACE', loopback_name, None) return True
def bcm_get_execute_diag_cmd_output(exe_cmd): diag_cmd = BCM_DIAG_CMD_TMPL % exe_cmd p = subprocess.Popen(diag_cmd, stdout=subprocess.PIPE, shell=True) (output, err) = p.communicate() ## Wait for end of command. Get return code ## returncode = p.wait() if returncode != 0: # if no decorator, use inspect.stack()[1][3] to get caller util_utl.utl_log("Failed to [%s] by %s !!!" % (diag_cmd, inspect.stack()[2][3]), logging.ERROR) return (False, None) if any(x in output for x in ['Fail', 'Err']): util_utl.utl_log("Failed to [%s] by %s !!!" % (diag_cmd + '(' + output +')', inspect.stack()[2][3]), logging.ERROR) return (False, None) return (True, output)
def interface_is_vlan_lst_valid(oc_yph, vid_lst, disp_args): ret_val = True vlan_cfg = disp_args.cfgdb.get_table(util_utl.CFGDB_TABLE_NAME_VLAN) # is_add_vlan = False for vid in vid_lst: if "Vlan%s" % str(vid) not in vlan_cfg: if not VLAN_AUTO_CREATE: ret_val = False break else: # auto create vlan interface_set_cfg_name_vlan(oc_yph, ["Vlan%s" % str(vid)], True, disp_args) util_utl.utl_log("auto create vlan %d" % vid) # is_add_vlan = True # if is_add_vlan: # time.sleep(2) return ret_val
def add_loopback(config_db, loopback_name, ip_addr): if not is_loopback_name_valid(loopback_name): util_utl.utl_log("loopback name-{} is invalid".format(loopback_name)) return False try: ipaddress.ip_network(unicode(ip_addr), strict=False) except ValueError: util_utl.utl_log("ip_addr-{} is not valid".format(ip_addr)) return False loopback_names = config_db.get_entry('LOOPBACK_INTERFACE', []) if loopback_name in loopback_names: util_utl.utl_log("{} already exists in config db".format(loopback_name)) else: config_db.set_entry('LOOPBACK_INTERFACE', loopback_name, {"NULL": "NULL"}) config_db.set_entry('LOOPBACK_INTERFACE', (loopback_name, ip_addr), {"NULL": "NULL"}) return True
def dhcp_relay_restart(oc_yph, pkey_ar, val, is_create, disp_args): util_utl.utl_log("Restart dhcp relay.") return restart_dhcp_relay()