Use Representational State Transfer Configuration Protocol (RESTCONF) to Configure a Switch

The documentation does not include information about how to use RESTCONF clients. This example documents some common tasks using Python.

Before you begin

Configure the RESTCONF server on the switch.

Procedure

  1. Import classes, define variables, and prepare the session object:
    #!/usr/bin/env python
    import sys
    import json
    import requests
    from requests import Request, Session
    from requests.auth import HTTPBasicAuth
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    ########################################################################
    Host     = '192.0.2.1'
    TcpPort  = ‘8080'
    UserName = 'rwa'
    PassWord = 'rwa'
    LoginUrl = 'https://%s:%s/auth/token' % (Host, TcpPort)QueryUrl = 'https://%s:%s/rest/restconf/data/' % (Host, TcpPort)
    vlan     = 123
    ########################################################################
    session         = Session()
    session.verify  = False
    session.timeout = 5
    
    session.headers.update({    'Accept': 	  'application/json',
                                'Accept-Encoding': 'gzip, deflate, br',
                                'Connection':      'keep-alive',
                                'Cache-Control':   'no-cache',
                                'Pragma': 	  'no-cache‘, })
    
  2. Learn the authentication token:
    Note

    Note

    The token expires after 24 hours.

    body = '{"username": "%s", "password" : "%s" }' % (UserName,PassWord)
    
    
    response = session.put( LoginUrl, body )
    
    if response.status_code != 200:
        print "ERROR: Login failed"
        sys.exit(1)
    else:    session.headers.update({
                 'X-Auth-Token': response.headers['X-Auth-Token‘]
        })
        print "INFO: login passed"
  3. Query all VLANs:
    response = session.get( QueryUrl + 'openconfig-vlan:vlans' )
    if response.status_code != 200:
        print 'ERROR: can‘t fetch VLANs'
    
  4. Query specific VLANs:
    response = session.get( QueryUrl + 'openconfig-vlan:vlans/vlan=%s' % vlan )
    
    if response.status_code != 200:
        print 'INFO: VLAN %s doesn‘t exists' % vlan
    else:
        print 'INFO: VLAN %s exists' % vlan
  5. Access data:
    inbound_data = json.loads( response.text )
    
    for vlan in inbound_data['openconfig-vlan:vlans' ]['vlan']:
         print 'VLAN: %s [%s]' % ( vlan['state']['name'], vlan['vlan-id‘] )
    
  6. Present data:
    inbound_data = json.loads( response.text )
    
    for dataVlan in inbound_data[ dataObject ]['vlan']:
        print ''
        print 'VLAN: ' + dataVlan['state']['name'] + '[' + dataVlan['vlan-id'] + ']'
        interfaces = ' '
        if 'members' in dataVlan :
            for interface in dataVlan['members']['member ']:
                interfaces = interfaces + interface['interface-ref']['state']['interface'] + ','
            
            print interfaces
  7. Add a VLAN:
    dataObject = 'openconfig-vlan:vlans‘
    
    data = { "openconfig-vlan:vlan": [
                                       { "config":
    						     { "name":    "Test-VLAN",
    						       "extreme-mod-oc-vlan:stg-id": 1,
    						       "vlan-id": vlan
    						     }
    					        }
                                     ] 
           }
    
    response = session.post( QueryUrl + dataObject, json=data )
    
    if response.status_code != 201:
    	print "ERROR: add VLAN %s failed" % vlan
    else:
    	print "INFO: VLAN %s added " % vlan
    
  8. Update a VLAN:
    dataObject = 'openconfig-vlan:vlans/vlan=%s/config' % vlan
    
    data = { "openconfig-vlan:config":
                   { "name":    "New-VLAN-Name" }
            }
    
    response = session.patch( QueryUrl + dataObject )
    
    if response.status_code != 204:
        print "ERROR: update VLAN %s fails" % vlan
    else:
        print "INFO: VLAN %s updated" % vlan
  9. Delete a VLAN:
    dataObject = 'openconfig-vlan:vlans/vlan=%s' % vlan
    
    response = session.delete( QueryUrl + dataObject )
    
    if response.status_code != 204:
        print "ERROR: delete VLAN %s fails" % vlan
    else:
        print "INFO: VLAN %s deleted" % vlan