Synchronizing Site Structure to NetBrain from Third-party System

If you maintain the site structure of your network in a third-party system, you can build the site structure and fully synchronize the sites to NetBrain from the third-party system via NetBrain APIs. Generally, the synchronization takes two steps:

1.Create all sites.

2.Assign all devices into the sites.

Workflow

The pseudo-code below describes the main flow in the API script to synchronize the sites.

1. Login to NetBrain API server and get session
2. Set current domain
3. Create a list of full site names for all sites
4. Call CreateSites
5. For each leaf address
    i.  Get a list of the hostnames of all devices assigned to this leaf site
    ii. Call SetDevicesOfSite to set devices of the leaf site
6. Call Commit to commit the change
7. Call GetSiteTree to confirm that all sites are created and devices are set
8. Logout NetBrain API server

 

API Script

You can refer to the script below to call relative APIs. This sample script first imports a python library maintained by NetBrain and you need to write your own one in practical use.

import netbrain_restful_lib
import time
  
user = "NetBrain"       
pwd = "Netbrain"       
nb_url = "http(s)://<IP address of NetBrain Web Server>/"            
headers = {'Content-Type''application/json''Accept''application/json'}
tenantName = "Initial Tenant"
domainName = "Use Case"
  
#################################################################
  
# Step 1: Login to NetBrain API server and get session
token = netbrain_restful_lib.loginSession(nb_url, user, pwd)
tenantList = netbrain_restful_lib.getTenants(nb_url, token)
domainList = netbrain_restful_lib.getDomains(nb_url, token)
#Sample results
# token : adf5b560-8e9c-4114-87bb-2eed24f0faff
# tenantList : [{'tenantId': 'a50af282-fa02-012f-a7f7-e410cc7c29d3', 'tenantName': 'Initial Tenant'}]
# domainList : [{'domainName': 'UseCase', 'domainId': '4f747a8b-71da-404b-a05c-e8505aa14725'}]
   
tenantId = 'fcf8a380-6b11-0fed-1d51-3b4cdef8f238'
domainId = '25b66365-1ab8-4fed-90c9-1307a4047c3d'
   
# Step 2: Set current domain
print(netbrain_restful_lib.loginDomain(nb_url, token, tenantId, domainId))
# Sample results
# loginDomain: Login successfully!
  
  
# Step 3: Create a list of full site names for all sites. In this "full synchronization" use case we are listing all leaf containers and their corresponding devices
data=[{"sitePath":"My Network/US/MA/Boston""devices":["NBUSCA-ASA1XAact"]} ]
  
  
# Step 4: Call CreateSites to create all leaf containers
print(netbrain_restful_lib.createSiteTransaction(nb_url, token))
sites=[]
for i in data:
    sites.append({"sitePath":i['sitePath'], "isContainer":False})
print(sites)
print(netbrain_restful_lib.createSites(nb_url, token, sites))
print(netbrain_restful_lib.commitSiteTransaction(nb_url, token)) #Call Commit to commit the change
# Sample results
# CreateSiteTransaction: Site transaction created!
# CreateSites: Site Created successfully! [{'sitePath': 'My Network/NA/US/TX', 'isContainer': True}]
# CommitSiteTransaction: Site commit True
  
  
# Step 5: For each leaf address, get a list of the hostnames of all devices assigned to this leaf site. Call SetDevicesOfSite to set devices of the leaf site
netbrain_restful_lib.keepAliveSiteTransaction(nb_url, token)
for i in data:
    print(i['sitePath'])
    print(i["devices"])
    print(netbrain_restful_lib.addSiteDevice(nb_url, token, None, i['sitePath'], i["devices"]))
# Sample results
# AddSiteDevice: Devices added successfully!
  
  
# Step 6: Call Commit to commit the change  
print(netbrain_restful_lib.commitSiteTransaction(nb_url, token))
# Sample results
# CommitSiteTransaction: Site commit True
  
  
# Step 7: Call GetSiteTree to confirm that all sites are created and devices are set
print(netbrain_restful_lib.getChildSites(nb_url, token, None"My Network"))
for i in data:
    print(netbrain_restful_lib.getSiteInfo(nb_url, token, None, i['sitePath']))
    print(netbrain_restful_lib.getSiteDevice(nb_url, token, None, i['sitePath']))
# Sample results
# GetChildSites: [{'siteId': 'aac212cb-fa84-4ddd-8c0b-18a00ec9c323', 'isContainer': False, 'siteType': 2, 'sitePath': 'My Network/NA/US/MA'}, {'siteId': '3ebb978f-323a-4308-8b38-82fa1a0a135f', 'isContainer': False, 'siteType': 2, 'sitePath': 'My Network/NA/US/CA'}]
# GetSiteInfo: {'isContainer': True, 'siteId': 'ede2ed2b-d5cf-48dd-b341-020330757033', 'sitePath': 'My Network/NA/US', 'siteType': 1}
# GetSiteDevice: [{'mgmtIP': '172.16.8.186', 'id': '06840b62-8a67-4cb2-9ed4-e540ea36fdcf', 'hostname': 'NBUSMA-SW7'}, {'mgmtIP': '172.16.8.194', 'id': '0b152c13-34e0-474e-aa4c-b07fe71cc0b7', 'hostname': 'NBUSMA-R1'}]
  
  
# Step 8: Logout Netbrain API server
netbrain_restful_lib.logoutSession(nb_url, token)