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 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 wait_for_register():
    ifidx = get_one_interface()
    time.sleep(1)

    p = nas_qos.IngPortCPSObj(ifindex=ifidx)
    r = []

    while cps.get([p.data()], r) == False:
        time.sleep(1)
def get_one_interface():
    while True:
        ifs = nas_os_if_utils.nas_os_if_list()
        if not ifs:
            time.sleep(1)
            continue

        for intf in ifs:
            obj = cps_object.CPSObject(obj=intf)
            ifidx = obj.get_attr_data(
                'dell-base-if-cmn/if/interfaces/interface/if-index')
            p = nas_qos.IngPortCPSObj(ifindex=ifidx)
            r = []

            if cps.get([p.data()], r) == True:
                return ifidx

        time.sleep(1)
def bind_obj_to_port(attr_name, obj_id, direction, ifname):
    dbg_print('{0} Binding on port {1}: {2} = {3}'.format(
        direction, ifname, attr_name, str(obj_id)))
    port = None
    if direction == 'ingress':
        port = nas_qos.IngPortCPSObj(ifname=ifname,
                                     list_of_attr_value_pairs=[(attr_name,
                                                                obj_id)])
    else:
        port = nas_qos.EgPortCPSObj(ifname=ifname,
                                    list_of_attr_value_pairs=[(attr_name,
                                                               obj_id)])
    upd = ('set', port.data())
    ret_cps_data = cps_utils.CPSTransaction([upd]).commit()

    if ret_cps_data == False:
        raise RuntimeError("Failed to bind " + attr_name + " to port " +
                           ifname)
def test_qos_port_ingress(port_name):
    print 'Test qos port-ingress for port %s' % port_name
    print 'Start to create port-ingress map profiles'
    tc_queue_id = None
    if not port_name.find('npu') == 0:
        tc_queue_entries = [(0, 'UCAST', 0), (0, 'MULTICAST', 0),
                            (1, 'UCAST', 1), (2, 'MULTICAST', 1),
                            (2, 'UCAST', 2), (3, 'MULTICAST', 2)]
        tc_queue_id = create_map_example('tc-to-queue-map', tc_queue_entries)
        if tc_queue_id is None:
            print 'Failed to create tc-to-queue map'
            return False
        get_map_example('tc-to-queue-map', tc_queue_id)
    dot1p_tc_entries = [(2, 0), (3, 1), (4, 5)]
    dot1p_tc_id = create_map_example('dot1p-to-tc-map', dot1p_tc_entries)
    if dot1p_tc_id is None:
        print 'Failed to create dot1p-to-tc map'
        return False
    get_map_example('dot1p-to-tc-map', dot1p_tc_id)

    dot1p_color_entries = [(2, 'RED'), (3, 'GREEN'), (4, 'RED')]
    dot1p_color_id = create_map_example('dot1p-to-color-map', dot1p_color_entries)
    if dot1p_color_id is None:
        print 'Failed to create dot1p-to-color map'
        return False
    get_map_example('dot1p-to-color-map', dot1p_color_id)

    dscp_tc_entries = [(0, 0), (1, 1)]
    dscp_tc_id = create_map_example('dscp-to-tc-map', dscp_tc_entries)
    if dscp_tc_id is None:
        print 'Failed to create dscp-to-tc map'
        return False
    get_map_example('dscp-to-tc-map', dscp_tc_id)

    dscp_color_entries = [(0, 'GREEN'), (1, 'YELLOW')]
    dscp_color_id = create_map_example('dscp-to-color-map', dscp_color_entries)
    if dscp_color_id is None:
        print 'Failed to create dscp-to-color map'
        return False
    get_map_example('dscp-to-color-map', dscp_color_id)

    print 'Start to create storm control policer profiles'
    flood_storm_control_id = create_storm_control_policer_example(200)
    if flood_storm_control_id is None:
        print 'Falied to create flood storm control policer'
        return False

    bcast_storm_control_id = create_storm_control_policer_example(300)
    if bcast_storm_control_id is None:
        print 'Failed to create unicast storm control policer'
        return False

    mcast_storm_control_id = create_storm_control_policer_example(500)
    if mcast_storm_control_id is None:
        print 'Failed to create unicast storm control policer'
        return False

    print 'Setup port ingress profile with maps and policers'
    attr_entries = [('dot1p-to-tc-map', dot1p_tc_id),
                    ('dot1p-to-color-map', dot1p_color_id),
                    ('dscp-to-tc-map', dscp_tc_id),
                    ('dscp-to-color-map', dscp_color_id),
                    ('flood_storm_control', flood_storm_control_id),
                    ('broadcast_storm_control', bcast_storm_control_id),
                    ('multicast_storm_control', mcast_storm_control_id)]
    if tc_queue_id is not None:
        attr_entries.append(('tc-to-queue-map', tc_queue_id))
    port_ing_obj = nas_qos.IngPortCPSObj(ifname=port_name,
                                         list_of_attr_value_pairs=attr_entries)
    upd = ('set', port_ing_obj.data())
    if cps_utils.CPSTransaction([upd]).commit() == False:
        print 'Failed to set map and policer for port %s' % port_name
        return False

    return_data_list = []
    ret = cps.get([port_ing_obj.data()], return_data_list)
    if ret:
        print '#### Port Ingress Porfile ####'
        for cps_ret_data in return_data_list:
            m = nas_qos.IngPortCPSObj(cps_data=cps_ret_data)
            m.print_obj()
    else:
        print 'Error in get'
        return False

    print 'Reset map and policer in port ingress profile'
    attr_entries = [('dot1p-to-tc-map', 0),
                    ('dot1p-to-color-map', 0),
                    ('dscp-to-tc-map', 0),
                    ('dscp-to-color-map', 0),
                    ('flood_storm_control', 0),
                    ('broadcast_storm_control', 0),
                    ('multicast_storm_control', 0)]
    if tc_queue_id is not None:
        attr_entries.append(('tc-to-queue-map', 0))
    port_ing_obj = nas_qos.IngPortCPSObj(ifname=port_name,
                                         list_of_attr_value_pairs=attr_entries)
    upd = ('set', port_ing_obj.data())
    if cps_utils.CPSTransaction([upd]).commit() == False:
        print 'Failed to set map and policer for port %s' % port_name
        return False

    return_data_list = []
    ret = cps.get([port_ing_obj.data()], return_data_list)
    if ret:
        print '#### Port Ingress Porfile ####'
        for cps_ret_data in return_data_list:
            m = nas_qos.IngPortCPSObj(cps_data=cps_ret_data)
            m.print_obj()
    else:
        print 'Error in get'
        return False

    print 'Start to delete port-ingress map profiles'
    if tc_queue_id is not None:
        ret = delete_map_example('tc-to-queue-map', tc_queue_id)
        if ret == False:
            print 'Failed to delete tc-to-queue map'
            return False
    ret = delete_map_example('dot1p-to-tc-map', dot1p_tc_id)
    if ret == False:
        print 'Failed to delete dot1p-to-tc map'
        return False
    ret = delete_map_example('dot1p-to-color-map', dot1p_color_id)
    if ret == False:
        print 'Failed to delete dot1p-to-color map'
        return False
    ret = delete_map_example('dscp-to-tc-map', dscp_tc_id)
    if ret == False:
        print 'Failed to delete dscp-to-tc map'
        return False
    ret = delete_map_example('dscp-to-color-map', dscp_color_id)
    if ret == False:
        print 'Failed to delete dscp-to-color map'
        return False

    print 'Start to delete storm-control policer profiles'
    delete_policer_example(flood_storm_control_id)
    delete_policer_example(bcast_storm_control_id)
    delete_policer_example(mcast_storm_control_id)


    attr_entries = [('per_priority_flow_control', 240)]
    inp = nas_qos.IngPortCPSObj(ifname=port_name, list_of_attr_value_pairs=attr_entries)
    upd = ('set', inp.data())
    if cps_utils.CPSTransaction([upd]).commit() == False:
        print "fail to set PFC bitmap"
    else:
        print "successfully set PFC bitmap"


    return True