VLAN Application Examples - amybuck/SONiC-NAS GitHub Wiki

Create VLAN using Python

NOTE: VLAN refers to an NPU VLAN object which is modeled as a Linux bridge.

1. Import the CPS and CPS object Python library.

import cps_object
import cps

2. Create a CPS object.

cps_obj = cps_object.CPSObject('dell-base-if-cmn/if/interfaces/interface')

3. Define the VLAN ID.

VLAN_ID=100
cps_obj.add_attr("base-if-vlan/if/interfaces/interface/id",VLAN_ID)

4. Define the VLAN type with a L2 or L2 VLAN ID.

cps_obj.add_attr('if/interfaces/interface/type','ianaift:l2vlan')

5. Associate a create operation with the CPS object to create a new VLAN.

cps_update = {'change':cps_obj.get(),'operation': 'create'}

6. Add the CPS operation and object pair to a new CPS transaction.

transaction = cps.transaction([cps_update])

7. Verify the return value.

if not transaction:
     raise RuntimeError ("Error creating Vlan")

See create_vlan.py to view the Python application example.

Verify VLAN Creation using CPS get

# cps_get_oid.py dell-base-if-cmn/if/interfaces/interface if/interfaces/interface/type=ianaift:l2vlan 

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 40
dell-if/if/interfaces/interface/phys-address =base-if-vlan/if/interfaces/interface/id = 100
if/interfaces/interface/name = br100dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0 

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 3
dell-if/if/interfaces/interface/phys-address = 90:b1:1c:f4:ab:ed
base-if-vlan/if/interfaces/interface/id = 0
if/interfaces/interface/name = docker0
dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0

NOTE: SONiC allocates an ifindex for each VLAN created and further CPS set and get operations can use ifindex as the key.

Verify VLAN Creation using Linux

$ brctl show
bridge   name             bridge   id                        STP   enabled             interfaces
br100                     8000.000000000000                  no

Add VLAN Port using Python

1. Import the CPS and CPS object Python library.

import cps
import cps_object

2. Create a CPS object.

cps_obj = cps_object.CPSObject('dell-base-if-cmn/if/interfaces/interface')

3. Define the VLAN Interface name.

VLAN_IF_NAME='br100'
cps_obj.add_attr('if/interfaces/interface/name',VLAN_IF_NAME)

4. Define the untagged port to add to the VLAN.

if_port_list=['e101-001-0']    
cps_obj.add_attr('dell-if/if/interfaces/interface/untagged-ports',if_port_list)

5. Associate a set operation with the CPS object to modify the property of an existing VLAN.

cps_update = {'change':cps_obj.get(),'operation': 'set'}

6. Add the CPS operation and object pair to a new CPS transaction.

transaction = cps.transaction([cps_update])

7. Verify the return value.

if not transaction:
     raise RuntimeError ("Error adding ports to Vlan")

See add_vlan_port.py to view the Python application example.

Verify VLAN Port Addition using CPS get

# cps_get_oid.py dell-base-if-cmn/if/interfaces/interface if/interfaces/interface/type=ianaift:l2vlan

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 43
dell-if/if/interfaces/interface/phys-address =
dell-if/if/interfaces/interface/untagged-ports = e101-003-0,e101-002-0,e101-001-0
base-if-vlan/if/interfaces/interface/id = 100
if/interfaces/interface/name = br100
dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 3
dell-if/if/interfaces/interface/phys-address = 90:b1:1c:f4:ab:ed
base-if-vlan/if/interfaces/interface/id = 0
if/interfaces/interface/name = docker0
dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0

Verify VLAN Port Addition using Linux

$ brctl show
bridge name   bridge id            STP     enabled interfaces
br100         8000.90b11cf4abee    no      e101-001-0
                                           e101-002-0
                                           e101-003-0

Delete VLAN Port using Python

1. Import the CPS and CPS object Python library.

import cps
import cps_object

2. Create a CPS object.

cps_obj = cps_object.CPSObject('dell-base-if-cmn/if/interfaces/interface')

3. Define the VLAN Interface Name.

VLAN_IF_NAME='br100'
cps_obj.add_attr('if/interfaces/interface/name',VLAN_IF_NAME)

4. Define the untagged port to be included in the VLAN (all other ports will be deleted from the VLAN).

if_port_list=['e101-002-0']
cps_obj.add_attr('dell-if/if/interfaces/interface/untagged-ports',if_port_list)

5. Associate a set operation with the CPS object to modify the property of an existing VLAN.

cps_update = {'change':cps_obj.get(),'operation': 'set'}

6. Add the CPS operation and object pair to a new CPS transaction.

transaction = cps.transaction([cps_update])

7. Verify the return value.

if not transaction:
    raise RuntimeError ("Error in deleting ports to Vlan")
print "successful"

See delete_ports_vlan.py to view the Python application example.

Verify VLAN Port Deletion using CPS get

# cps_get_oid.py dell-base-if-cmn/if/interfaces/interface if/interfaces/interface/type=ianaift:l2vlan

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 47
dell-if/if/interfaces/interface/phys-address =
dell-if/if/interfaces/interface/untagged-ports = e101-002-0
base-if-vlan/if/interfaces/interface/id = 100
if/interfaces/interface/name = br100
dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 3
dell-if/if/interfaces/interface/phys-address = 90:b1:1c:f4:ab:ed
base-if-vlan/if/interfaces/interface/id = 0
if/interfaces/interface/name = docker0
dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0

Verify VLAN Port Deletion using Linux

$ brctl show
bridge name bridge id STP enabled interfaces
br100 8000.000000000000 no         e101-002-0

Delete VLAN using Python

1. Import the CPS and CPS object Python library.

import cps
import cps_object

2. Create a CPS object.

cps_obj = cps_object.CPSObject('dell-base-if-cmn/if/interfaces/interface')

3. Define the name of the VLAN interface to delete

VLAN_IF_NAME='br100'
cps_obj.add_attr('if/interfaces/interface/name',VLAN_IF_NAME)

4. Associate a delete operation with the CPS object to delete an existing VLAN.

cps_update = {'change':cps_obj.get(),'operation': 'delete'}

5. Add the CPS operation and object pair to a new CPS transaction.

transaction = cps.transaction([cps_update])

6. Verify return value.

if not transaction:
    raise RuntimeError ("Error in deleting Vlan")
print "successful"

See delete_vlan.py to view the Python application example.

Verify VLAN Deletion using CPS get

# cps_get_oid.py dell-base-if-cmn/if/interfaces/interface if/interfaces/interface/type=ianaift:l2vlan

Key: 1.19.44.2883617.2883612.2883613.
dell-base-if-cmn/if/interfaces/interface/if-index = 3
dell-if/if/interfaces/interface/phys-address = 90:b1:1c:f4:ab:ed
base-if-vlan/if/interfaces/interface/id = 0
if/interfaces/interface/name = docker0
dell-if/if/interfaces/interface/learning-mode = 1
if/interfaces/interface/enabled = 0

Verify VLAN Deletion using Linux

$ brctl show br100