Cisco DNA Center - Flow-Analysis (Python)

DNAC Series

This is part of a DNAC series:

Disclaimer: the code in this post is not production-grade code obviously. The examples in the post are merely conceptual and for informational purposes.

Introduction

In this we have been showing some POSTMAN samples for running a Flow Analysis. In the next sections, we will explore how we can achieve exactly the same using Python Requests. I recommend you to first go through the POSTMAN post before attempting this one.

Note about equipment

In this post, I’m using my own DNAC in my lab. However, if you want to follow along, you could also use a Cisco sandbox environment delivered by Cisco Devnet. To get a list of all sandboxes, check out this link. For this tutorial, you could use this one. Note that this is a reservable instance as the always-on is restricted in functionality.

Flow Analysis

import requests
from authenticate import get_token
import json, time
from jinja2 import Environment
from jinja2 import FileSystemLoader

dnac = "10.48.82.183"
token = get_token(dnac)

url = f"https://{dnac}"

headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "X-auth-Token": token 
}

def flowanalysis():
    payload = {
        "sourceIP": "192.168.30.60",
        "destIP": "192.168.13.64",
        "inclusions": [
            "INTERFACE-STATS",
            "DEVICE-STATS",
            "QOS-STATS"
        ],
        "controlPath": False,
        "periodicRefresh": False
    }
      
    flow_url = "/dna/intent/api/v1/flow-analysis"
    print(url + flow_url)
    response_flow =  requests.post(url + flow_url, data=json.dumps(payload), headers=headers, verify=False ).json()
    analysis_url = response_flow['response']['url']
    print(url + analysis_url)
    
    response =  requests.get(url + analysis_url, headers=headers, verify=False ).json()
    print(f"{response['response']['request']['status']}")

if __name__ == "__main__":
   flowanalysis()

The full source code can be found on my Github repo here.