MAC Address Table Application Examples - amybuck/SONiC-NAS GitHub Wiki
Create MAC Address Table Entry
NOTE: Refer to the
dell-base-l2-mac.yang
model which defines the MAC address object and attributes.
1. Import the CPS object Python library.
import cps_utils
2. Register the attribute type to convert between the string and byte-array format.
cps_utils.add_attr_type("base-mac/table/mac-address", "mac")
3. Define the MAC address, interface index, and VLAN attributes.
d = {"mac-address": "00:0a:0b:cc:0d:0e","ifindex": 18,"vlan": "100"}
4. Create a CPS object.
obj = cps_utils.CPSObject('base-mac/table',data= d)
5. Associate the operation to the CPS object.
tr_obj = ('create', obj.get())
6. Create a transaction object.
transaction = cps_utils.CPSTransaction([tr_obj])
7. Commit the transaction.
ret = transaction.commit()
8. Verify the return value.
if not ret:
raise RuntimeError ("Error creating MAC Table Entry")
NOTE: When you do not enter a key qualifier, the target is used by default when creating an object.
See create-mac-table-entry.py to view the Python application example, or see create-mac-table-entry.c to view the C application example.
Verify MAC Address Table Entry Creation using CPS get
# cps_get_oid.py base-mac/query
Key: 1.16.1048594.
base-mac/query/ifindex = 18
base-mac/query/actions = 2
base-mac/query/static = 1
base-mac/query/mac-address = 000a0bcc0d0e
base-mac/query/vlan = 100
Request MAC Address Table Entry
1. Import the CPS and CPS object Python library.
import cps_utils
import cps
2. Register the attribute type to convert between string and byte-array format.
cps_utils.add_attr_type("base-mac/query/mac-address", "mac")
3. Define the MAC address request type.
d = {"mac-address": "00:0a:0b:cc:0d:0e","request-type":"2"}
4. Associate a get operation with the CPS object.
obj = cps_utils.CPSObject('base-mac/query',data= d)
5. Create an object filter list.
filter_list = []
6. Add the filter object.
filter_list.append(obj.get())
7. Create a list for the response.
lst = []
8. Verify objects returned.
if cps.get(filter_list,lst):
# Check if get returned objects in the response list
if lst:
for ret_obj in lst:
# Print the returned Objects
cps_utils.print_obj(ret_obj)
else:
# Empty list
else:
print "No objects found"
raise RuntimeError ("Error Getting MAC Table Entries")
See get-mac-table-entry.py to view the Python application example, or see get-mac-table-entry.c to view the C application example.
Delete MAC Address Table Entry
1. Import the CPS object Python library.
import cps_utils
2. Register the attribute type to convert between string and byte-array format.
cps_utils.add_attr_type("base-mac/table/mac-address", "mac")
3. Define the MAC address, interface index, VLAN to delete the static address entry.
d = {"mac-address": "00:0a:0b:cc:0d:0e", "ifindex": 18, "vlan": "100"}
4. Create a CPS object.
obj = cps_utils.CPSObject('base-mac/table',data= d)
5. Add the operation to the object.
tr_obj = ('delete', obj.get())
6. Create a transaction object.
transaction = cps_utils.CPSTransaction([tr_obj])
7. Commit the transaction object.
ret = transaction.commit()
8. Verify the return value.
if not ret:
raise RuntimeError("Error deleting entry from MAC Table")
See delete-mac-table-entry.py to view the Python application example, or see delete-mac-table-entry.c to view the C application example.
To delete the MAC address from all VLANs, specify the vlan
attribute and its value in the object. To delete all MAC entries from an interface, specify the ifindex
attribute and its value in the object. To delete MAC entries from both a VLAN and member interface, specify the vlan
and ifindex
attributes and their values in the object.
Deletion of static entries based only on VLAN, interface or a VLAN/interface combination is not supported. To delete a static entry, you must add the mac-address, vlan, and ifindex attributes and their values to the object.
Delete MAC Address Table Entries from Multiple VLANs
1. Import the CPS object Python library.
import cps_utils
2. Define the VLANS to remove MAC address entries from.
vlan_list=[1,2,3,4,5]
3. Create the CPS object.
obj = cps_utils.CPSObject('base-mac/flush')
4. Add the VLAN list to the CPS object.
count = 0
el = ["input/filter","0","vlan"]
for vlan in vlan_list:
obj.add_embed_attr(el, vlan)
count = count + 1
el[1] = str(count)
5. Associate the operation to the object.
tr_obj = ('rpc', obj.get())
6. Create a transaction object.
transaction = cps_utils.CPSTransaction([tr_obj])
7. Commit the transaction object.
ret = transaction.commit()
8. Verify the return value.
if not ret:
raise RuntimeError("Error Flushing entries from MAC Table")
See remove-mac-table-entries-from-multiple-vlans.py to view the Python application example, or see remove-mac-table-entries-from-multiple-vlans.c to view the C application example.