def _check_hg_port_breakout(hybrid_group_name, port_id, br_mode):
    #Get Hybrid Group Object
    resp = hg_utils.get_hg(str(hybrid_group_name))
    assert len(resp) == 1
    for o in resp:
        assert o is not None
    obj = cps_object.CPSObject(obj=o)

    #Check if name is correct
    hg_name = nas_if.get_cps_attr(obj, hg_utils.hg_attr('id'))
    assert hg_name == str(hybrid_group_name)

    port_list = nas_if.get_cps_attr(obj, hg_utils.hg_attr('port'))
    for port_idx in port_list:
        port = port_list[port_idx]
        pr_id = port['port-id']
        if str(pr_id) == str(port_id):
            phy_mode = port['phy-mode']
            breakout_mode = port['breakout-mode']
            port_speed = port['port-speed']
            breakout_option = (breakout_mode, port_speed)
            breakout = nas_comm.yang.get_key(breakout_option,
                                             'yang-breakout-port-speed')
            assert str(breakout) == str(br_mode)
            break
def _check_hg_profile(hybrid_group_name, profile_name):
    #Get Hybrid Group Object
    resp = hg_utils.get_hg(str(hybrid_group_name))
    assert len(resp) == 1
    for o in resp:
        assert o is not None
    obj = cps_object.CPSObject(obj=o)

    #Check if name is correct
    hg_name = nas_if.get_cps_attr(obj, hg_utils.hg_attr('id'))
    assert hg_name == str(hybrid_group_name)

    #Check profile Type
    hg_profile = nas_if.get_cps_attr(obj, hg_utils.hg_attr('profile'))
    assert hg_profile == str(profile_name)
def _get_hg_hdlr(methods, params):
    '''CPS Get Helper for Hybrid Group'''
    obj = cps_object.CPSObject(obj=params['filter'])
    resp = params['list']

    if obj.get_key() == hg_utils.hg_key:
        hg_name = nas_if.get_cps_attr(obj, hg_utils.hg_attr('id'))
        return _service_get_hg(hg_name, resp)

    nas_if.log_err("Key Error: Hybrid Group Key issue")
    return False
def create_hg_cps_obj(hg_obj):
    '''Method to create a Hybrid Group object'''
    if hg_obj is None:
        return None
    obj = cps_object.CPSObject(
        module='base-pg/dell-pg/port-groups/hybrid-group',
        qual='target',
        data={
            hg_utils.hg_attr('id'): hg_obj.name,
            hg_utils.hg_attr('profile'):
            hg_obj.get_hybrid_group_profile_mode()
        })
    for fpp_idx, fpp_num in enumerate(hg_obj.get_fp_ports()):
        port = fp.find_front_panel_port(fpp_num)
        obj.add_embed_attr([hg_utils.hg_attr('port'),
                            str(fpp_idx), 'port-id'], str(fpp_num), 6)
        obj.add_embed_attr(
            [hg_utils.hg_attr('port'),
             str(fpp_idx), 'phy-mode'], port.get_phy_mode(), 6)
        obj.add_embed_attr(
            [hg_utils.hg_attr('port'),
             str(fpp_idx), 'breakout-mode'], port.get_breakout_mode(), 6)
        obj.add_embed_attr(
            [hg_utils.hg_attr('port'),
             str(fpp_idx), 'port-speed'], port.get_port_speed(), 6)
    return obj
def apply_cps_config_to_hg(obj, hg_obj):
    '''Method to configure a Hybrid Group'''

    #Configure Profile Mode
    rollback_list = []
    prev_profile_mode = hg_obj.get_hybrid_group_profile_mode()
    cur_profile_mode = nas_if.get_cps_attr(obj, hg_utils.hg_attr('profile'))
    if cur_profile_mode is not None and prev_profile_mode != cur_profile_mode:
        hg_obj.set_hybrid_group_profile_mode(cur_profile_mode)

        # Configure Default Breakout for profile mode
        port_list = hg_obj.get_fp_ports()
        if port_list is not None:
            for fpp_num in port_list:

                port = fp.find_front_panel_port(fpp_num)

                # Previous configuration
                prev_br_mode = port.get_breakout_mode()
                prev_port_speed = port.get_port_speed()
                prev_phy_mode = port.get_phy_mode()
                prev_port_profile_name = port.get_profile_type()

                # Current configuration
                hybrid_profile = port.get_hybrid_profile()
                cur_port_profile_name = str(
                    hybrid_profile.get_port_profile(cur_profile_mode))
                port.apply_port_profile(
                    fp.get_port_profile(cur_port_profile_name))
                cur_phy_mode = port.get_def_phy_mode()
                cur_br_mode = port.get_def_breakout()
                cur_port_speed = port.get_default_phy_port_speed()

                rollback_list.append((fpp_num, prev_br_mode, prev_port_speed,
                                      prev_phy_mode, prev_port_profile_name))

                # Apply configuration
                rc = fp_utils.set_fp_port_config(fpp_num, cur_br_mode,
                                                 cur_port_speed, cur_phy_mode)

                if rc is False or fpp_num not in hg_obj.get_fp_ports():
                    nas_if.log_err(
                        'failed to config fp_port %d, start rollback' %
                        fpp_num)

                    # Rollback
                    for (fpp, br_mode, port_speed, phy_mode,
                         port_profile_name) in rollback_list:
                        nas_if.log_info('rollback port %d config' % fpp)
                        port = fp.find_front_panel_port(fpp)
                        fp_utils.set_fp_port_config(fpp, br_mode, port_speed,
                                                    phy_mode)
                        port.apply_port_profile(
                            fp.get_port_profile(port_profile_name))
                    hg_obj.set_hybrid_group_profile_mode(prev_profile_mode)
                    return False

                hwp_speed = pg_utils.get_hwp_speed(cur_br_mode, cur_port_speed,
                                                   cur_phy_mode)
                port.set_hwp_speed(hwp_speed)

    # Configure User Specified Breakout for profile mode
    port_list = nas_if.get_cps_attr(obj, hg_utils.hg_attr('port'))
    if port_list is not None:
        for fpp_idx in port_list:

            fpp_num = int(port_list[fpp_idx]['port-id'])
            port = fp.find_front_panel_port(fpp_num)

            # Current configuration
            cur_phy_mode = port_list[fpp_idx]['phy-mode']
            cur_br_mode = port_list[fpp_idx]['breakout-mode']
            cur_port_speed = port_list[fpp_idx]['port-speed']

            # Apply configuration
            rc = fp_utils.set_fp_port_config(fpp_num, cur_br_mode,
                                             cur_port_speed, cur_phy_mode)

            if rc is False or fpp_num not in hg_obj.get_fp_ports():
                nas_if.log_err('failed to config fp_port %d, start rollback' %
                               fpp_num)

                # Rollback
                for (fpp, br_mode, port_speed, phy_mode,
                     port_profile_name) in rollback_list:
                    nas_if.log_info('rollback port %d config' % fpp)
                    port = fp.find_front_panel_port(fpp)
                    fp_utils.set_fp_port_config(fpp, br_mode, port_speed,
                                                phy_mode)
                    port.apply_port_profile(
                        fp.get_port_profile(port_profile_name))
                hg_obj.set_hybrid_group_profile_mode(prev_profile_mode)
                return False

            hwp_speed = pg_utils.get_hwp_speed(cur_br_mode, cur_port_speed,
                                               cur_phy_mode)
            port.set_hwp_speed(hwp_speed)
    return True