This section introduces how to create an API parser to extract key metrics from a Cisco ACI network.

1.Click the icon on the taskbar and select New Parser.

2.In the Device Type list, select a device type that the API parser applies to. By default, the All Device Types option is selected.

Tip: To filter the devices that the parser applies to, click Advanced Filter and define more criteria. See Advanced Filter for details.

3.In the Parser Type list, select API and further select Cisco ACI from the source type list.

4.Define the functions to retrieve data from devices. This step contains three functions. Define these functions step by step:

No.

Function Name

Explanation

1

Declare Input Parameters

This function is to declare the parameters that are from user inputs at runtime.  

This function is in the JSON format and a sample code is as follows:

'''
Begin Declare Input Parameters
 [
    {"name": "$intf_name"} # specify the parameter name
 ]
 End Declare
 '''

The item name defined in this function refers to the name of the parameters (variables) that receives values from user input.

2

def BuildParameters (context, device_name, params)

This function is used to declare the parameters that are from GDR, DeviceSetting, and SDN. It contains three fixed parameters:

context — context objects called by python. It contains tenant, domain or user information.

device_name — the names of target devices.

params — the parameters that are from user inputs (defined in the Declare Parameters function. It is used to pass the parameters that are from user input to the current function.

This function also supports to call built-in APIs to get device or interface parameters:

GetDeviceProperties(context, dev_name, params) — the params is the parameter and its type to be retrieved. For example, {'techName': 'Cisco ACI', 'paramType': 'SDN', 'params' : ['dn', 'model'] }.

GetInterfaceProperties(context, dev_name, intf_name, params) — refer to the GetDeviceProperties API.

The type of returned values by this function is tuple (bool, list).

def BuildParameters(context, device_name, params):
    intf_name = params['intf_name']
    # get_dn = GetDeviceProperties(context, device_name, {'techName': 'Cisco ACI', 'paramType': 'SDN', 'params' : ['dn'] })
    get_dn = GetInterfaceProperties(context, device_name,intf_name, {'techName''Cisco ACI''paramType''SDN''params' : ['dn'] })
    dn = get_dn['params']['dn']
    rtn_params = [{ 'devName' : device_name, 'dn' : dn }]
    # raise NameError(rtn_params)
    # [{'devName': 'Leaf1', 'dn': 'topology/pod-1/node-101/sys/inst-overlay-1/lb-[lo0]'}]
    return rtn_params

3

def RetrieveData(devices)

This function is used to retrieve the data of devices based on the parameters declared in the Declare Parameters and BuildParameters functions. This function contains one parameter.

The following is a sample code of this function:

def RetrieveData(rtn_params):
    # raise NameError(rtn_params)
    # endpoint=rtn_params['endpoint']
    dn =rtn_params['dn']
    url = '/api/mo/' + dn + '.json'
    rtn_params['url'] = url
    data = getData(rtn_params)
    return data

5.Click Retrieve and select a device to retrieve sample data.

6.Define the functions to parse key metrics from the retrieved data. This step contains two functions. Define these functions step by step:

No.

Function Name

Explanation

1

Declare variable tree

This function is to declare the metrics (variables) and their types that you want to parse from the retrieved device data, and generate a variable tree. The following parameters of a variable are defined:

name — the name of the variable

type — the value type of a variable. The type includes string, int, bool, double and table.

This function is in the JSON format and a sample code is as follows:

'''
Begin Declare Variable
[
    {"name": "Intf_Info", "type": "table","columns": [
        {"name": "Interface_Name", "type": "string"},
        {"name": "adminSt", "type": "string"},
        {"name": "dn", "type": "string"}
    ]}
]
End Declare
 
'''

2

def ParseText(original_result)

This function is used to return the specific metric values and assign the values to corresponding variables in the variable tree.

The following is a sample code of this function:

import re 
import json
def ParseText(_original_result):
    original_result = json.loads(_original_result)
    intf_name = original_result[0]['l1PhysIf']['attributes']['id']
    dn_original = original_result[0]['l1PhysIf']['attributes']['dn']
    adminSt_original =original_result[0]['l1PhysIf']['attributes']['adminSt']
    Interface_Name =[]
    adminSt=[]
    dn=[]
    Interface_Name.append(intf_name)
    adminSt.append(adminSt_original)
    dn.append(dn_original)
    value = {}
    value['Intf_Info']= {'Interface_Name' : Interface_Name,'adminSt':adminSt,'dn':dn}
    return value

7.Click Save on the upper right corner of the page. The parser will be saved in the Parser Library.