def invoke_api(func, args=[]): api = cc.ApiClient() if func == 'get_sonic_portchannel_sonic_portchannel_lag_table': path = cc.Path( '/restconf/data/sonic-portchannel:sonic-portchannel/LAG_TABLE') return api.get(path) if func == 'get_sonic_portchannel_sonic_portchannel_lag_table_lag_table_list': path = cc.Path( '/restconf/data/sonic-portchannel:sonic-portchannel/LAG_TABLE/LAG_TABLE_LIST={lagname}', lagname=args[0]) return api.get(path) if func == 'get_openconfig_lacp_lacp_interfaces': path = cc.Path('/restconf/data/openconfig-lacp:lacp/interfaces') return api.get(path) if func == 'get_openconfig_lacp_lacp_interfaces_interface': path = cc.Path( '/restconf/data/openconfig-lacp:lacp/interfaces/interface={name}', name=args[0]) return api.get(path) if func == 'get_openconfig_interfaces_interfaces_interface_state_counters': path = cc.Path( '/restconf/data/openconfig-interfaces:interfaces/interface={name}/state/counters', name=args[0]) return api.get(path) #print "invoke_api done" return api.cli_not_implemented(func)
def invoke_api(func, args): body = None api = cc.ApiClient() if func.startswith("rpc") == True: if func == 'rpc_sonic_image_management_image_install': body= validate_imagename(args, 'install') path = cc.Path('/restconf/operations/sonic-image-management:image-install') return api.post(path, body) if func == 'rpc_sonic_image_management_image_remove': body =validate_imagename(args, 'remove') path = cc.Path('/restconf/operations/sonic-image-management:image-remove') return api.post(path,body) if func == 'rpc_sonic_image_management_image_default': body =validate_imagename(args, 'default') path = cc.Path('/restconf/operations/sonic-image-management:image-default') return api.post(path,body) if func == 'get_sonic_image_management_sonic_image_management': path = cc.Path('/restconf/data/sonic-image-management:sonic-image-management') return api.get(path)
def fdb_call(macAddr, vlanName): aa = cc.ApiClient() vlanId = vlanName[len("Vlan"):] macAddr = macAddr.strip() keypath = cc.Path( '/restconf/data/openconfig-network-instance:network-instances/network-instance={name}/fdb/mac-table/entries/entry={macaddress},{vlan}', name='default', macaddress=macAddr, vlan=vlanId) try: response = aa.get(keypath) response = response.content if 'openconfig-network-instance:entry' in response.keys(): instance = response['openconfig-network-instance:entry'][0][ 'interface']['interface-ref']['state']['interface'] if instance is not None: return instance return "-" except: return "-"
def get_tam_int_ifa_ts_flow_stats(args): api_response = {} api = cc.ApiClient() # connect to COUNTERS_DB counters_db = ConfigDBConnector() counters_db.db_connect('COUNTERS_DB') if len(args) == 1 and args[0] != "all": path = cc.Path('/restconf/data/sonic-tam-int-ifa-ts:sonic-tam-int-ifa-ts/TAM_INT_IFA_TS_FLOW_TABLE/TAM_INT_IFA_TS_FLOW_TABLE_LIST={name}', name=args[0]) else: path = cc.Path('/restconf/data/sonic-tam-int-ifa-ts:sonic-tam-int-ifa-ts/TAM_INT_IFA_TS_FLOW_TABLE') response = api.get(path) if response.ok(): if response.content: if len(args) == 1 and args[0] != "all": api_response = response.content['sonic-tam-int-ifa-ts:TAM_INT_IFA_TS_FLOW_TABLE_LIST'] else: api_response = response.content['sonic-tam-int-ifa-ts:TAM_INT_IFA_TS_FLOW_TABLE']['TAM_INT_IFA_TS_FLOW_TABLE_LIST'] for i in range(len(api_response)): api_response[i]['Packets'] = 0 api_response[i]['Bytes'] = 0 if "acl-table-name" not in api_response[i] and "acl-rule-name" not in api_response[i]: return acl_counter_key = 'COUNTERS:' + api_response[i]['acl-table-name'] + ':' + api_response[i]['acl-rule-name'] flow_stats = counters_db.get_all(counters_db.COUNTERS_DB, acl_counter_key) if flow_stats is not None: api_response[i]['Packets'] = flow_stats['Packets'] api_response[i]['Bytes'] = flow_stats['Bytes'] show_cli_output("show_tam_int_ifa_ts_flow_stats.j2", api_response)
def get_vrf_data(vrf_name, vrf_show_data): api = cc.ApiClient() vrf = {} vrf_data = {} keypath = cc.Path( '/restconf/data/openconfig-network-instance:network-instances/network-instance={name}/config', name=vrf_name) vrf_config = api.get(keypath) if vrf_config.ok(): if len(vrf_config.content) == 0: return vrf_config vrf_data['openconfig-network-instance:config'] = vrf_config.content[ 'openconfig-network-instance:config'] if vrf_name == 'mgmt': vrf_data['openconfig-network-instance:interface'] = [] else: keypath = cc.Path( '/restconf/data/openconfig-network-instance:network-instances/network-instance={name}/interfaces/interface', name=vrf_name) vrf_intfs = api.get(keypath) if vrf_intfs.ok(): vrf_data[ 'openconfig-network-instance:interface'] = vrf_intfs.content[ 'openconfig-network-instance:interface'] else: vrf_data['openconfig-network-instance:interface'] = [] vrf[vrf_name] = vrf_data vrf_show_data.append(vrf) return vrf_config
def delete_link_state_tracking_group_downstream(args): aa = cc.ApiClient() uri = cc.Path( '/restconf/data/sonic-link-state-tracking:sonic-link-state-tracking/INTF_TRACKING/INTF_TRACKING_LIST={grp_name}/downstream={downstr}', grp_name=args[0], downstr=args[1]) return aa.delete(uri)
def set_link_state_tracking_group_downstream(args): aa = cc.ApiClient() uri = cc.Path( '/restconf/data/sonic-link-state-tracking:sonic-link-state-tracking/INTF_TRACKING/INTF_TRACKING_LIST={grp_name}/downstream', grp_name=args[0]) body = {"sonic-link-state-tracking:downstream": args[1:]} return aa.patch(uri, body)
def get_sonic_tacacs_server_api(args): api_response = [] api = cc.ApiClient() path = cc.Path( '/restconf/data/openconfig-system:system/aaa/server-groups/server-group=TACACS/servers/', address=args[0]) response = api.get(path) if response.ok(): if response.content: server_list = response.content["openconfig-system:servers"][ "server"] for i in range(len(server_list)): if args[0] == server_list[i]['address'] or args[ 0] == 'show_tacacs_server.j2': api_response_data = {} api_response_data['address'] = server_list[i]['address'] api_response_data['authtype'] = server_list[i]['config'][ 'openconfig-system-ext:auth-type'] api_response_data['priority'] = server_list[i]['config'][ 'openconfig-system-ext:priority'] api_response_data['timeout'] = server_list[i]['config'][ 'timeout'] if 'tacacs' in server_list[i]: tac_cfg = {} tac_cfg = server_list[i]['tacacs']['config'] api_response_data['port'] = tac_cfg['port'] if "secret-key" in tac_cfg: api_response_data['key'] = tac_cfg['secret-key'] api_response.append(api_response_data) return api_response
def invoke_show_api(func, args=[]): api = cc.ApiClient() keypath = [] body = None if func == 'get_bfd_peers': keypath = cc.Path( '/restconf/data/openconfig-bfd:bfd/openconfig-bfd-ext:bfd-state') return api.get(keypath) elif func == 'get_openconfig_bfd_ext_bfd_sessions_single_hop': keypath = cc.Path( '/restconf/data/openconfig-bfd:bfd/openconfig-bfd-ext:bfd-state/single-hop-state={address},{interfacename},{vrfname},{localaddress}', address=args[1], interfacename=args[2], vrfname=args[3], localaddress=args[4]) return api.get(keypath) elif func == 'get_openconfig_bfd_ext_bfd_sessions_multi_hop': keypath = cc.Path( '/restconf/data/openconfig-bfd:bfd/openconfig-bfd-ext:bfd-state/multi-hop-state={address},{interfacename},{vrfname},{localaddress}', address=args[1], interfacename=args[2], vrfname=args[3], localaddress=args[4]) return api.get(keypath) else: return api.cli_not_implemented(func)
def invoke_api(func, args): body = None api = cc.ApiClient() # Set/Get aaa configuration body = { "openconfig-system-ext:failthrough": False, "openconfig-system:authentication-method": ['local'] } failthrough = 'None' authmethod = [] # authentication-method is a leaf-list. So patch is not supported. A put opeartion # would clear existing other parameters as well. So reading existing contents and # trying to change only the user input parameter with a put path = cc.Path( '/restconf/data/openconfig-system:system/aaa/authentication/config') get_response = api.get(path) if get_response.ok(): if get_response.content: api_response = get_response.content if 'failthrough' in api_response['openconfig-system:config']: body["openconfig-system-ext:failthrough"] = api_response[ 'openconfig-system:config']['failthrough'] if 'authentication-method' in api_response[ 'openconfig-system:config']: body["openconfig-system:authentication-method"] = api_response[ 'openconfig-system:config']['authentication-method'] if func == 'put_openconfig_system_ext_system_aaa_authentication_config_failthrough': path = cc.Path( '/restconf/data/openconfig-system:system/aaa/authentication/config/openconfig-system-ext:failthrough' ) body["openconfig-system-ext:failthrough"] = (args[0] == "True") return api.put(path, body) elif func == 'put_openconfig_system_system_aaa_authentication_config_authentication_method': path = cc.Path( '/restconf/data/openconfig-system:system/aaa/authentication/config/authentication-method' ) # tricky logic: xml sends frist selection and values of both local and tacacs+ params # when user selects "local tacacs+", actioner receives "local local tacacs+" # when user selects "tacacs+ local", actioner receives "tacacs+ local tacacs+" authmethod.append(args[0]) if len(args) == 3: if args[0] == args[1]: authmethod.append(args[2]) else: authmethod.append(args[1]) else: pass body["openconfig-system:authentication-method"] = authmethod return api.put(path, body) elif func == 'get_openconfig_system_system_aaa_authentication_config': return get_response else: body = {} return api.cli_not_implemented(func)
def invoke_api(func, args=[]): api = cc.ApiClient() if func == 'get_sonic_vlan_sonic_vlan': path = cc.Path('/restconf/data/sonic-vlan:sonic-vlan') return api.get(path) return api.cli_not_implemented(func)
def run(func, args): aa = cc.ApiClient() path = cc.Path('/restconf/data/openconfig-platform:components/component=%s'%args[0]) response = aa.get(path) if response.ok(): show_cli_output(sys.argv[3], response.content) else: print response.error_message()
def invoke(func, args): body = None aa = cc.ApiClient() if func == 'create_mirror_session': keypath = cc.Path( '/restconf/data/sonic-mirror-session:sonic-mirror-session/MIRROR_SESSION/MIRROR_SESSION_LIST={name}', name=args.session) body = collections.defaultdict(dict) entry = { "name": args.session, } if args.destination is not '': if args.destination != "erspan": entry["dst_port"] = args.destination if args.source is not '': entry["src_port"] = args.source if args.direction is not '': entry["direction"] = args.direction if args.destination == "erspan": if args.dst_ip is not '': entry["dst_ip"] = args.dst_ip if args.src_ip is not '': entry["src_ip"] = args.src_ip if args.dscp is not '': entry["dscp"] = int(args.dscp) if args.ttl is not '': entry["ttl"] = int(args.ttl) if args.gre is not '': entry["gre_type"] = args.gre if args.queue is not '': entry["queue"] = int(args.queue) body["MIRROR_SESSION_LIST"] = [entry] return aa.patch(keypath, body) # Remove mirror session if func == 'delete_mirror_session': keypath = cc.Path( '/restconf/data/sonic-mirror-session:sonic-mirror-session/MIRROR_SESSION/MIRROR_SESSION_LIST={name}', name=args.session) return aa.delete(keypath) else: print("%Error: not implemented") exit(1)
def invoke(func, args): body = None aa = cc.ApiClient() keypath = cc.Path('/restconf/operations/sonic-config-mgmt:copy') body = { "sonic-config-mgmt:input":{"source":args[0], "destination":args[1]}} if len(args) == 3: body["sonic-config-mgmt:input"].update({"overwrite":True}) return aa.post(keypath, body)
def create_link_state_tracking_group(args): aa = cc.ApiClient() body = { "sonic-link-state-tracking:INTF_TRACKING_LIST": [{ "name": args[0] }] } uri = cc.Path( '/restconf/data/sonic-link-state-tracking:sonic-link-state-tracking/INTF_TRACKING/INTF_TRACKING_LIST={grp_name}', grp_name=args[0]) return aa.patch(uri, body)
def set_link_state_tracking_group_timeout(args): timeout = int(args[1]) if timeout > 999: raise RuntimeError("Timeout not in range 1-999") aa = cc.ApiClient() uri = cc.Path( '/restconf/data/sonic-link-state-tracking:sonic-link-state-tracking/INTF_TRACKING/INTF_TRACKING_LIST={grp_name}/timeout', grp_name=args[0]) body = {"sonic-link-state-tracking:timeout": timeout} return aa.patch(uri, body)
def show_link_state_tracking_group_info(args): aa = cc.ApiClient() if len(args): uri = cc.Path( '/restconf/data/sonic-link-state-tracking:sonic-link-state-tracking/INTF_TRACKING_TABLE/INTF_TRACKING_TABLE_LIST={grp_name}', grp_name=args[0]) else: uri = cc.Path( '/restconf/data/sonic-link-state-tracking:sonic-link-state-tracking/INTF_TRACKING_TABLE/INTF_TRACKING_TABLE_LIST' ) return aa.get(uri)
def get_sonic_radius_global(): api_response = {} api = cc.ApiClient() path = cc.Path(RADIUS_SERVER_GROUP + 'config') response = api.get(path) if response.ok(): if response.content: api_response = response.content show_cli_output("show_radius_global.j2", api_response)
def run_show_cmd(data): aa = cc.ApiClient() keypath = cc.Path('/restconf/operations/sonic-kdump:kdump-state') body = {"sonic-kdump:input": data} api_response = aa.post(keypath, body) if api_response.ok(): response = api_response.content if response is not None and 'sonic-kdump:output' in response: print(response['sonic-kdump:output']['result']) else: print(api_response.error_message())
def invoke_api(func, args): api = cc.ApiClient() body = None # Set/Get the rules of all IFA table entries. if func == 'get_sonic_tam_sonic_tam_tam_device_table': path = cc.Path('/restconf/data/sonic-tam:sonic-tam/TAM_DEVICE_TABLE') return api.get(path) elif func == 'get_sonic_tam_sonic_tam_tam_collector_table': if ((len(args) == 2) and args[1] != "all"): path = cc.Path( '/restconf/data/sonic-tam:sonic-tam/TAM_COLLECTOR_TABLE/TAM_COLLECTOR_TABLE_LIST={name}', name=args[1]) return api.get(path) else: path = cc.Path( '/restconf/data/sonic-tam:sonic-tam/TAM_COLLECTOR_TABLE') return api.get(path) elif func == 'patch_sonic_tam_sonic_tam_tam_device_table_tam_device_table_list_deviceid': path = cc.Path( '/restconf/data/sonic-tam:sonic-tam/TAM_DEVICE_TABLE/TAM_DEVICE_TABLE_LIST={name}/deviceid', name=args[0]) body = {"sonic-tam:deviceid": int(args[1])} return api.patch(path, body) elif func == 'delete_sonic_tam_sonic_tam_tam_device_table_tam_device_table_list_deviceid': path = cc.Path( '/restconf/data/sonic-tam:sonic-tam/TAM_DEVICE_TABLE/TAM_DEVICE_TABLE_LIST={name}/deviceid', name=args[0]) return api.delete(path) elif func == 'patch_list_sonic_tam_sonic_tam_tam_collector_table_tam_collector_table_list': path = cc.Path( '/restconf/data/sonic-tam:sonic-tam/TAM_COLLECTOR_TABLE/TAM_COLLECTOR_TABLE_LIST' ) body = { "sonic-tam:TAM_COLLECTOR_TABLE_LIST": [{ "name": args[0], "ipaddress-type": args[1], "ipaddress": args[2], "port": int(args[3]) }] } return api.patch(path, body) elif func == 'delete_sonic_tam_sonic_tam_tam_collector_table_tam_collector_table_list': path = cc.Path( '/restconf/data/sonic-tam:sonic-tam/TAM_COLLECTOR_TABLE/TAM_COLLECTOR_TABLE_LIST={name}', name=args[0]) return api.delete(path) else: body = {} return api.cli_not_implemented(func)
def get_sonic_tacacs_global(): api_response = {} api = cc.ApiClient() path = cc.Path( '/restconf/data/openconfig-system:system/aaa/server-groups/server-group=TACACS/config' ) response = api.get(path) if response.ok(): if response.content: api_response = response.content show_cli_output("show_tacacs_global.j2", api_response)
def get_tam_ifa_flow_stats(args): api_response = {} api = cc.ApiClient() if (len(args) == 1) and (args[0] != "all"): path = cc.Path( '/restconf/data/sonic-ifa:sonic-ifa/TAM_INT_IFA_FLOW_TABLE/TAM_INT_IFA_FLOW_TABLE_LIST={name}', name=args[0]) else: path = cc.Path( '/restconf/data/sonic-ifa:sonic-ifa/TAM_INT_IFA_FLOW_TABLE') response = api.get(path) if response.ok(): if response.content: if (len(args) == 1) and (args[0] != "all"): api_response = response.content[ 'sonic-ifa:TAM_INT_IFA_FLOW_TABLE_LIST'] else: api_response = response.content[ 'sonic-ifa:TAM_INT_IFA_FLOW_TABLE'][ 'TAM_INT_IFA_FLOW_TABLE_LIST'] for i in range(len(api_response)): api_response[i]['Packets'] = 0 api_response[i]['Bytes'] = 0 path = cc.Path('/restconf/data/openconfig-acl:acl/acl-sets') acl_info = api.get(path) if acl_info.ok(): if acl_info.content: acl_list = acl_info.content["openconfig-acl:acl-sets"][ "acl-set"] for acl in acl_list: if acl['name'] == api_response[i][ 'acl-table-name']: rule = api_response[i]['acl-rule-name'] # tokenize the rulename with '_' and fetch last number tmpseq = (rule.split("_", 1))[-1] acl_entry_list = acl["acl-entries"][ "acl-entry"] for entry in acl_entry_list: if int(tmpseq) == int( entry["sequence-id"]): api_response[i]['Packets'] = entry[ "state"]["matched-packets"] api_response[i]['Bytes'] = entry[ "state"]["matched-octets"] show_cli_output("show_tam_ifa_flow_stats.j2", api_response)
def mclag_get_portchannel_oper_status(po_name): ''' call LAG Table Rest API to get LAG Admin Status ''' po_oper_status = 'down' aa = cc.ApiClient() path = cc.Path( '/restconf/data/sonic-portchannel:sonic-portchannel/LAG_TABLE/LAG_TABLE_LIST={lagname}/oper_status', lagname=po_name) api_response = aa.get(path) if api_response.ok(): response = api_response.content if len(response) != 0: po_oper_status = response['sonic-portchannel:oper_status'] return po_oper_status
def invoke(func, args): body = {} aa = cc.ApiClient() if func == 'get_openconfig_ztp_ztp_state': path = cc.Path('/restconf/data/openconfig-ztp:ztp/state') return aa.get(path) else: path = cc.Path('/restconf/data/openconfig-ztp:ztp/config') if 'no' in sys.argv: body["openconfig-ztp:admin_mode"] = False else: body["openconfig-ztp:admin_mode"] = True return aa.post(path, body)
def mclag_get_ethernet_if_oper_status(if_name): ''' call Ethernet iface Rest API to get Ethernet if Admin Status ''' if_oper_status = 'down' aa = cc.ApiClient() path = cc.Path( '/restconf/data/sonic-port:sonic-port/PORT_TABLE/PORT_TABLE_LIST={ifname}/oper_status', ifname=if_name) api_response = aa.get(path) if api_response.ok(): response = api_response.content if len(response) != 0: if_oper_status = response['sonic-port:oper_status'] return if_oper_status
def invoke(func, args): body = None aa = cc.ApiClient() if func == 'get_openconfig_network_instance_network_instances_network_instance_fdb_mac_table_entries': keypath = cc.Path( '/restconf/data/openconfig-network-instance:network-instances/network-instance={name}/fdb/mac-table/entries', name='default') return aa.get(keypath) elif func == 'rpc_sonic_fdb_clear_fdb': keypath = cc.Path('/restconf/operations/sonic-fdb:clear_fdb') body = {"sonic-fdb:input": {"mac-param": "all"}} return aa.post(keypath, body) else: return body
def run(func, args): aa = cc.ApiClient() path = cc.Path( '/restconf/data/openconfig-platform:components/component=%s' % args[0]) api_response = aa.get(path) if api_response.ok( ) and "openconfig-platform:state" in api_response.content: response = api_response.content versionResponse = response["openconfig-platform:state"] responseContent = { "Software Version": versionResponse['software-version'] } show_cli_output(sys.argv[3], responseContent) else: print api_response.error_message()
def mclag_get_portchannel_traffic_disable(po_name): ''' call LAG Table Rest API to get LAG Admin Status ''' traffic_disable = 'No' aa = cc.ApiClient() path = cc.Path( '/restconf/data/sonic-portchannel:sonic-portchannel/LAG_TABLE/LAG_TABLE_LIST={lagname}/traffic_disable', lagname=po_name) api_response = aa.get(path) if api_response.ok(): response = api_response.content if len(response) != 0: if response['sonic-portchannel:traffic_disable']: traffic_disable = 'Yes' return traffic_disable
def mclag_get_local_if_port_isolate(po_name): ''' call MCLAG Local Interface state Table Rest API to get Port isolate property setting ''' port_isolate = 'No' aa = cc.ApiClient() path = cc.Path( '/restconf/data/sonic-mclag:sonic-mclag/MCLAG_LOCAL_INTF_TABLE/MCLAG_LOCAL_INTF_TABLE_LIST={if_name}/port_isolate_peer_link', if_name=po_name) api_response = aa.get(path) if api_response.ok(): response = api_response.content if len(response) != 0: if response['sonic-mclag:port_isolate_peer_link']: port_isolate = 'Yes' return port_isolate
def invoke_api(func, args=[]): api = cc.ApiClient() if func == 'get_sonic_portchannel_sonic_portchannel_lag_table': path = cc.Path('/restconf/data/sonic-portchannel:sonic-portchannel/LAG_TABLE') return api.get(path) if func == 'get_sonic_portchannel_sonic_portchannel_lag_member_table': path = cc.Path('/restconf/data/sonic-portchannel:sonic-portchannel/LAG_MEMBER_TABLE') return api.get(path) if func == 'get_sonic_port_sonic_port_port_table_port_table_list_oper_status': path = cc.Path('/restconf/data/sonic-port:sonic-port/PORT_TABLE/PORT_TABLE_LIST={ifname}/oper_status', ifname=args[0]) return api.get(path) return api.cli_not_implemented(func)