def __create_map_table(self, map_name): map_wr = utl.CPSObjWrp(self.yang_name, self.get_type_map()) if self.yang_name in self.get_old_maps(): map_wr.add_leaf_attr('switch-id', self.switch_id) upd = ('create', map_wr.get()) ret_data_list = cps_utils.CPSTransaction([upd]).commit() if ret_data_list == False: print "Map Table Create failed - " + self.yang_name return None self.map_result = ret_data_list[0] map_id = utl.extract_cps_attr(self, ret_data_list[0], 'id') if map_name != None: map_wr.add_leaf_attr('id', map_id) map_wr.add_leaf_attr('name', map_name) upd = ('set', map_wr.get()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: print "map name modification failed" return map_id
def apply_table_cfg(etree_table, stage, prio): table_name = etree_table.attrib['tag'] dbg_print("Creating table stage = ", stage, "prio = ", prio) t = nas_acl.TableCPSObj(table_id=table_name, stage=stage, priority=int(prio)) for field in etree_table.findall('allow-match'): dbg_print("Add allow filter ", field.text) t.add_allow_filter(field.text) for field in etree_table.findall('allow-action'): dbg_print("Add allow action ", field.text) t.add_allow_action(field.text) dbg_print(t.data()) cps_upd = ('create', t.data()) ret = cps_utils.CPSTransaction([cps_upd]).commit() if ret == False: raise RuntimeError("ACL INIT - Table creation failed: " + table_name) t = nas_acl.TableCPSObj(cps_data=ret[0]) table_id = t.extract_id() dbg_print("Created Table " + table_name + "-" + str(table_id)) return table_id
def acl_ut_entry_delete_rollback(table_id, entry_id, counter_id): global total, passed total.append(sys._getframe().f_code.co_name) # Transaction with multiple updates upd = [] # Delete entry e = nas_acl.EntryCPSObj(table_id=table_id, entry_id=entry_id) upd.append(('delete', e.data())) # Delete counter e = nas_acl.CounterCPSObj(table_id=table_id, counter_id=counter_id) upd.append(('delete', e.data())) # Delete table e = nas_acl.TableCPSObj(table_id=table_id) upd.append(('delete', e.data())) # Delete entry again - should fail e = nas_acl.EntryCPSObj(table_id=table_id, entry_id=entry_id) upd.append(('delete', e.data())) print upd r = cps_utils.CPSTransaction(upd).commit() if r == False: print "Error deleting entry twice (Expected) - Should have rolled back to create table, counter and entry again" raw_input( "Check table and entry is Roll back recreated and Press Enter to continue...") passed.append(sys._getframe().f_code.co_name) else: print "#### Failed - Deleting twice did not give error"
def scheduler_profile_create_example( algo, weight, min_rate, min_burst, max_rate, max_burst): attr_list = { 'algorithm': algo, 'weight': weight, 'meter-type': 'PACKET', 'min-rate': min_rate, 'min-burst': min_burst, 'max-rate': min_rate, 'max-burst': max_burst, 'npu-id-list': [0], } sched_obj = nas_qos.SchedulerCPSObj(map_of_attr=attr_list) upd = ('create', sched_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: print 'Scheduler profile creation failed' return None print 'Return = ', ret_cps_data sched_obj = nas_qos.SchedulerCPSObj(cps_data=ret_cps_data[0]) sched_id = sched_obj.extract_id() print 'Successfully installed Scheduler profile id = ', sched_id return sched_id
def create_entry(table_id, prio, filter_map, action_map, name=None, switch_id=0): e = EntryCPSObj(table_id=table_id, priority=prio, entry_id=name, switch_id=switch_id) for ftype, fval in filter_map.items(): e.add_match_filter(filter_type=ftype, filter_val=fval) for atype, aval in action_map.items(): e.add_action(action_type=atype, action_val=aval) upd = ('create', e.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError("Entry create failed") e = EntryCPSObj(cps_data=r[0]) entry_id = e.extract_id() return entry_id
def create(self): obj_id = None try: obj_id = self.get_attr_data('id') except ValueError: pass if obj_id is not None: raise RuntimeError('Object with id could not be created') for attr_id in self.cr_mand_attrs: attr_val = None try: attr_val = self.get_attr_data(attr_id) except ValueError: pass if attr_val is None: raise RuntimeError('Mandatory attribute %s not found' % attr_id) upd = ('create', self.get()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError('Object create failed') if len(r) == 0: raise RuntimeError('No returned object') ret_obj = r[0] if 'change' in ret_obj: ret_obj = ret_obj['change'] obj = CPSObject(obj=ret_obj) id_attr = self.generate_path('id') self.add_attr('id', obj.get_attr_data(id_attr)) return self
def queue_modify_buffer_profile_example(port_id, queue_type, queue_number, buf_prof_id): attr_list = { 'type': queue_type, 'queue-number': queue_number, 'port-id': port_id, 'buffer-profile-id': buf_prof_id, } queue_obj = nas_qos.QueueCPSObj(map_of_attr=attr_list) upd = ('set', queue_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: print 'Queue buffer profile modification failed' return None print 'Return = ', ret_cps_data m = nas_qos.QueueCPSObj(cps_data=ret_cps_data[0]) port_id = m.extract_attr('port-id') queue_number = m.extract_attr('queue-number') print "Successfully modified Queue of Port %d Number %d" % (port_id, queue_number) return ret_cps_data
def create_buf_profile(xnode_profile, ingress_pool_id, egress_pool_id): attrs = copy.deepcopy(xnode_profile.attrib) if "-egr-" in attrs['name']: pool_id = egress_pool_id else: pool_id = ingress_pool_id list_of_attrs_in_kb = {'buffer-size', 'shared-static-threshold', 'shared-dynamic-threshold', 'xoff-threshold', 'xon-threshold'} for key in attrs: if key in list_of_attrs_in_kb: if attrs[key] != '0': attrs[key] = attrs[key] + '000' attrs.update({'pool-id': pool_id}) buf_prof_obj = nas_qos.BufferProfileCPSObj(map_of_attr=attrs) upd = ('create', buf_prof_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: raise RuntimeError( 'Buffer profile {0} creation failed'.format(xnode_profile.attrib['name'])) buf_prof_obj = nas_qos.BufferProfileCPSObj(cps_data=ret_cps_data[0]) buf_prof_id = buf_prof_obj.extract_id() dbg_print(' Buffer profile {0} = {1}'.format( xnode_profile.attrib['name'], buf_prof_id)) return buf_prof_id
def init_ingress_buffer_on_port(xnode_obj, lookup_buf_prof, ifidx, ifname, iftype, speed): speed_str = get_speed_str_by_iftype_speed(iftype, speed) buffer_profile_name = xnode_obj.attrib[ 'buffer-profile'] + '-ing-' + speed_str buffer_profile_id = None if (buffer_profile_name in lookup_buf_prof): buffer_profile_id = lookup_buf_prof[buffer_profile_name] if (dbg_on): print "speed: {0}, buffer_profile_id: {1}".format( speed, buffer_profile_id) if (buffer_profile_id is None): # no matching buffer profile is defined, no configuration return m = nas_qos.IngPortCPSObj(ifname=ifname) m.set_attr('buffer-profile-id-list', [buffer_profile_id]) upd = ('set', m.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: syslog.syslog("Set Ingress buffer failed for port {0}".format(ifname)) return None if (dbg_on): print 'Return = ', ret_cps_data if (dbg_on): print "Successfully modified ingress buffer of Port %s" % (ifname)
def deleteIpv4RouteEntry(route_prefix, prefix_len): # deleteIpv4RouteEntry("10.1.1.0", 24) ip_version = "ipv4" obj = cps_utils.CPSObject('base-route/obj/entry') obj.add_attr("vrf-id", 0) obj.add_attr("af", socket.AF_INET) ip = net.IPNetwork(route_prefix) obj.add_attr_type("route-prefix", ip_version) obj.add_attr("route-prefix", str(ip.network)) obj.add_attr("prefix-len", int(prefix_len)) cps_update = ('delete', obj.get()) transaction = cps_utils.CPSTransaction([cps_update]) ret = transaction.commit() if not ret: raise RuntimeError("Error deleting Route") Utils.timeLogger("CPSOperations| CPS delete route:") Utils.cliLogger( "CPSOPErations: Deleted route: " + route_prefix + "/" + str(prefix_len), 0)
def buffer_profile_create_example(pool_id, pool_type = 'INGRESS'): if (pool_type == 'EGRESS'): attr_list = { 'pool-id': pool_id, 'buffer-size': 2000, 'threshold-mode': 'DYNAMIC', 'shared-dynamic-threshold': 0, 'name': 'default-egress-buffer-profile', } else : attr_list = { 'pool-id': pool_id, 'buffer-size': 2000, 'threshold-mode': 'STATIC', 'shared-static-threshold': 500, 'xoff-threshold': 1000, 'xon-threshold': 5000, 'name': 'default-ingress-buffer-profile', } buffer_profile_obj = nas_qos.BufferProfileCPSObj(map_of_attr=attr_list) upd = ('create', buffer_profile_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: print "buffer profile creation failed" return None print 'Return = ', ret_cps_data buffer_profile_obj = nas_qos.BufferProfileCPSObj(cps_data=ret_cps_data[0]) buffer_profile_id = buffer_profile_obj.extract_attr('id') print "Successfully installed buffer profile id = ", buffer_profile_id return buffer_profile_id
def create_table(stage, prio, allow_filters, switch_id=0, only_if_not_exist=False): if only_if_not_exist: _table = find_table(priority=prio,matchfields=allow_filters,\ table_stage=stage) if _table != None: return _table.extract_id() t = TableCPSObj(stage=stage, priority=prio, switch_id=switch_id) for f in allow_filters: t.add_allow_filter(f) upd = ('create', t.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError("Table create failed") t = TableCPSObj(cps_data=r[0]) table_id = t.extract_id() return table_id
def setInterfaceIpAddress(if_name, ip_addr, pfix_len): # Populate the attributes for the CPS Object # From the arguments if_index = cps_operations.getIfIndexFromName(if_name) ip_attributes = { "base-ip/ipv4/ifindex": if_index, "ip": ip_addr, "prefix-length": pfix_len } # Create CPS Object cps_utils.add_attr_type('base-ip/ipv4/address/ip', "ipv4") cps_obj = cps_utils.CPSObject('base-ip/ipv4/address', data=ip_attributes) # Create the CPS Transaction for object create cps_update = ('create', cps_obj.get()) transaction = cps_utils.CPSTransaction([cps_update]) # Verify return value ret = transaction.commit() if not ret: raise RuntimeError("Error changing interface ip address") Utils.cliLogger( "CPSOPErations: Changed interface ip address: " + ip_addr, 0)
def wred_profile_create_example(): attr_list = { 'green-enable': 1, 'green-min-threshold': 20, 'green-max-threshold': 80, 'green-drop-probability': 10, 'yellow-enable': 1, 'yellow-min-threshold': 40, 'yellow-max-threshold': 60, 'yellow-drop-probability': 30, 'weight': 8, 'ecn-enable': 1, 'npu-id-list': [0], } wred_obj = nas_qos.WredCPSObj(map_of_attr=attr_list) upd = ('create', wred_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: print "WRED profile creation failed" return None print 'Return = ', ret_cps_data wred_obj = nas_qos.WredCPSObj(cps_data=ret_cps_data[0]) wred_id = wred_obj.extract_attr('id') print "Successfully installed WRED id = ", wred_id return wred_id
def set_port_egress_scheduler_id(port_name, sched_id): attr_entries = [('scheduler-profile-id', sched_id)] port_eg_obj = EgPortCPSObj(ifname=port_name, list_of_attr_value_pairs=attr_entries) upd = ('set', port_eg_obj.data()) if cps_utils.CPSTransaction([upd]).commit() == False: raise RuntimeError('Failed to set scheduler for port %s' % port_name)
def set_port_ingress_bcast_storm_control(port_name, policer_id): attr_entries = [('broadcast_storm_control', policer_id)] port_ing_obj = IngPortCPSObj(ifname=port_name, list_of_attr_value_pairs=attr_entries) upd = ('set', port_ing_obj.data()) if cps_utils.CPSTransaction([upd]).commit() == False: raise RuntimeError('Failed to set policer for port %s' % port_name)
def delete_table(table_id): t = TableCPSObj(table_id=table_id) upd = ('delete', t.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError("Table delete failed")
def delete_entry(table_id, entry_id): e = EntryCPSObj(table_id=table_id, entry_id=entry_id) upd = ('delete', e.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError("Entry delete failed")
def delete_policer(meter_id): meter_obj = MeterCPSObj(meter_id=meter_id) upd = ('delete', meter_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: raise RuntimeError('Policer delete failed') print 'Successfully deleted Policer Id = %d' % meter_id
def delete_counter(table_id, counter_id): c = CounterCPSObj(table_id=table_id, counter_id=counter_id) upd = ('delete', c.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError("Counter delete failed")
def set_cps_object(module_name, attr_list={}, op='set'): cps_obj = cps_object.CPSObject(module=module_name, data=attr_list) upd = (op, cps_obj.get()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: return False cps_utils.print_obj(ret_cps_data[0]) return True
def qos_meter_delete(meter_id): m = nas_qos.MeterCPSObj(meter_id=meter_id) upd = ('delete', m.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: print ("Error deleting meter" + str(meter_id)) else: print ("Successfully deleted meter" + str(meter_id))
def set_port_ing_map(port_name, map_id, in_map_name): inp = nas_qos.IngPortCPSObj(ifname=port_name) inp.set_attr(in_map_name, map_id) upd = ('set', inp.data()) if cps_utils.CPSTransaction([upd]).commit() == False: print "Failed to set " + in_map_name + " for port " + port_name else: print "Successfully set " + in_map_name + " for port " + port_name
def delete_policer_example(meter_id): meter_obj = nas_qos.MeterCPSObj(meter_id=meter_id) upd = ('delete', meter_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: print 'Policer delete failed' return None print 'Successfully deleted Policer Id = %d' % meter_id
def acl_ut_entry_incr_rollback(table_id, entry_id): global total, passed total.append(sys._getframe().f_code.co_name) # Transaction with multiple updates upd = [] # Modify inports e = nas_acl.EntryCPSObj( table_id=table_id, entry_id=entry_id, filter_type='IN_PORTS') e.set_filter_val([a_utl.get_if_name(4), a_utl.get_if_name(8)]) upd.append(('set', e.data())) # Delete Counter action e = nas_acl.EntryCPSObj( table_id=table_id, entry_id=entry_id, action_type='SET_COUNTER') upd.append(('delete', e.data())) # Modify mirror action e = nas_acl.EntryCPSObj( table_id=table_id, entry_id=entry_id, action_type='MIRROR_INGRESS') e.set_action_val( [{'index': mirror_id_2, 'data': mir_opq_2}, {'index': mirror_id_1, 'data': mir_opq_1}]) upd.append(('set', e.data())) # Delete Meter e = nas_acl.EntryCPSObj( table_id=table_id, entry_id=entry_id, action_type='SET_POLICER') upd.append(('delete', e.data())) # Introduce error - Add IN_PORT filter even though IN_PORTS is already # present e = nas_acl.EntryCPSObj( table_id=table_id, entry_id=entry_id, filter_type='IN_PORT') e.set_filter_val(a_utl.get_if_name(1)) upd.append(('create', e.data())) print upd r = cps_utils.CPSTransaction(upd).commit() if r == False: print "Error adding inport filter (Expected) - Should have rolled back previous updates in transaction" raw_input("Check entry is Rolled back and Press Enter to continue...") passed.append(sys._getframe().f_code.co_name) else: print "#### Failed - Adding Inport filter did not give error"
def mirror_delete(mirr_id): cfg_mo = cps_utils.CPSObject('base-mirror/entry') cfg_mo.add_attr('id', mirr_id) r = cps_utils.CPSTransaction([('delete', cfg_mo.get())]).commit() if r == False: print ("Error deleting mirror" + str(mirr_id)) else: print ("Successfully deleted mirror" + str(mirr_id))
def check_mac_addr_get(if_type, fp_port=None, vlan_id=None, lag_id=None): cps_obj = cps_object.CPSObject(module='dell-base-if-cmn/get-mac-address') if if_type == 'front-panel': if fp_port is None: print 'Front panel port ID is required for front-panel type' return False print 'checking mac address service for front-panel-port %d' % fp_port cps_obj.add_attr(nas_comm.yang.get_value('fp_port', 'attr_name'), fp_port) cps_obj.add_attr(nas_comm.yang.get_value('intf_type', 'attr_name'), 'ianaift:ethernetCsmacd') elif if_type == 'vlan': if vlan_id is None: print 'VLAN ID is required for vlan type' return False print 'checking mac address service for vlan %d' % vlan_id cps_obj.add_attr(nas_comm.yang.get_value('vlan_id', 'attr_name'), vlan_id) cps_obj.add_attr(nas_comm.yang.get_value('intf_type', 'attr_name'), 'ianaift:l2vlan') elif if_type == 'lag': if lag_id is None: print 'LAG ID is required for lag type' return False print 'checking mac address service for lag %d' % lag_id lag_name = 'bo%d' % lag_id cps_obj.add_attr(nas_comm.yang.get_value('if_name', 'attr_name'), lag_name) cps_obj.add_attr(nas_comm.yang.get_value('intf_type', 'attr_name'), 'ianaift:ieee8023adLag') else: print 'Invalid interface type: %s' % if_type return False param_list = get_alloc_mac_addr_params(if_type, cps_obj, fp_cache) assert param_list is not None if if_type == 'front-panel': assert 'fp_mac_offset' in param_list elif if_type == 'vlan': assert 'vlan_id' in param_list assert param_list['vlan_id'] == vlan_id elif if_type == 'lag': assert 'lag_id' in param_list assert param_list['lag_id'] == lag_id mac_addr = if_get_mac_addr(**param_list) assert mac_addr is not None tr = cps_utils.CPSTransaction([('rpc', cps_obj.get())]) ret_list = tr.commit() assert ret_list != False assert len(ret_list) == 1 ret_obj = cps_object.CPSObject(obj=ret_list[0]['change']) ret_mac_addr = ret_obj.get_attr_data( 'dell-if/if/interfaces/interface/phys-address') assert mac_addr == ret_mac_addr print 'mac address: %s' % mac_addr return True
def scheduler_profile_delete(sched_id): sched_obj = SchedulerCPSObj(sched_id=sched_id) upd = ('delete', sched_obj.data()) ret_cps_data = cps_utils.CPSTransaction([upd]).commit() if ret_cps_data == False: raise RuntimeError('Scheduler profile delete failed') print 'Successfully deleted Scheduler id = ', sched_id
def set_port_egr_map(port_name, map_id, eg_map_name): # Set the TC to Q map for port e00-1 op = nas_qos.EgPortCPSObj(ifname=port_name) op.set_attr(eg_map_name, map_id) upd = ('set', op.data()) if cps_utils.CPSTransaction([upd]).commit() == False: print "Failed to set " + eg_map_name + " for port " + port_name else: print "Successfully set " + eg_map_name + " for port " + port_name
def remove_entry_action(table_id, entry_id, action_type): e = EntryCPSObj(table_id=table_id, entry_id=entry_id, action_type=action_type) upd = ('delete', e.data()) r = cps_utils.CPSTransaction([upd]).commit() if r == False: raise RuntimeError("Entry action remove failed")