2023-Nov-03-R11.1a

Create an API Parser

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

  1. Select Logic Nodes from the Node Type list, select Cisco ACI from the second list and then select a specific node type.
    Graphical user interface, application

Description automatically generated
  1. Define the functions to retrieve data from devices. This step contains three functions. Define these functions step by step:
No.Function NameExplanation
1Declare Parameters

This function is to declare the parameters from user inputs at runtime.  
Graphical user interface, application

Description automatically generated

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

'''
Begin Declare 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.

2def BuildParameters (context, device_name, params)

This function declares the parameters 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 from user inputs (defined in the Declare Parameters function. It is used to pass the parameters from user input to the current function.

This function also supports calling 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

3def 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

  1. Click Retrieve and select a device to retrieve sample data.
  2. Define the functions to parse key metrics from the retrieved data. This step contains two functions. Define these functions step by step:
No.Function NameExplanation
1)Declare variable tree

This function declares the metrics (variables) and the types you want to parse from the retrieved device data and generates 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 returns the specific metric values and assigns 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

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

 

See also: