def test_lbaas_l7switching(cmgr, lb_name, image_name=None, platform='os',
                           **kwargs):
    lb_name = lb_name or 'cmgr-lb2-http'
    build_cfg = dict(groupid=1, group_num_sever=2, num_servers=4)
    build_cfg.update(**kwargs)
    if platform == 'nsx':
        image_name = image_name or u'cirros-0.3.3-x86_64-disk'
        lb2_config = lbaas2.build_nsx_lbaas(
            cmgr, lb_name, image_name=image_name, **build_cfg)
    else:
        image_name = image_name or "cirros-0.3.3-x86_64-ESX"
        lb2_config = lbaas2.build_os_lbaas(
            cmgr, lb_name, image_name=image_name, **build_cfg)
    keypair = lb2_config['network']['keypair']
    security_group_id = lb2_config['network']['security_group'].get('id')
    http_server_id_list = lb2_config.get('group_server_id_list')
    l7_server_id_list = lb2_config.get('other_server_id_list')
    http_server_name_list = lb2_config.get('group_server_name_list')
    l7_server_name_list = lb2_config.get('other_server_name_list')

    netaggr.show_toplogy(cmgr)
    lbaas2.show_lbaas_tree(cmgr, lb_name)
    cmgr.lbaas('loadbalancer-status', lb_name)
    vip_public_ip = lbaas2.get_loadbalancer_floatingip(
        cmgr, lb_name)[1][u'floating_ip_address']

    lb = cmgr.lbaas('loadbalancer-show', lb_name)
    lb_id = lb.get('id')
    vip_subnet_id = lb.get('vip_subnet_id')
    redirect_to_listener_id = lb.get('listeners')[0].get('id')

    l7_cfg = ll7.build_l7_switching(
        cmgr, vip_subnet_id, lb_id,
        redirect_to_listener_id, l7_server_id_list)

    # every server return its server-name upon http request
    ll7.run_l7_switching(http_server_name_list, vip_public_ip, '')
    ll7.run_l7_switching(http_server_name_list, vip_public_ip, 'v2/api')
    # wait little bit before l7 switching kicked in?
    time.sleep(5.0)
    ll7.run_l7_switching(l7_server_name_list, vip_public_ip, 'api')
    ll7.run_l7_switching(l7_server_name_list, vip_public_ip, 'api/firewalls')

    return dict(
        name=lb_name, image_name=image_name, keypair=keypair,
        vip_public_ip=vip_public_ip,
        subnet_id=vip_subnet_id,
        security_group_id=security_group_id,
        http_server_id_list=http_server_id_list,
        http_server_name_list=http_server_name_list,
        l7_server_id_list=l7_server_id_list,
        l7_server_name_list=l7_server_name_list,
        l7_pool=l7_cfg.get('pool')
    )
def poke_http_stats_change(nsxt_client, cmgr, lb2_name,
                           interval=5.0, poke_count=100,
                           no_http_traffic=False):
    lb2_fip = lbaas2.get_loadbalancer_floatingip(cmgr, lb2_name)
    lb2_web_ip = lb2_fip[1]['floating_ip_address']
    lb = cmgr.lbaas('loadbalancer-show', lb2_name)
    lb_port = cmgr.qsvc('port-show', lb['vip_port_id'])
    os_security_group_id = lb_port['security_groups'][0]
    _sgroup = cmgr.qsvc('security-group-show', os_security_group_id)
    msg = "os-security-group-id = %s" % (os_security_group_id)
    utils.log_msg(msg, 'CHK-Stats')

    sg_rules = _sgroup['security_group_rules']
    http_rule = [x for x in sg_rules if x['port_range_min'] >= 80][0]

    filters = {'os-neutron-secgr-id': os_security_group_id}
    fw_list = nsxt_client.list_firewall_sections(**filters)
    sect_id = fw_list[0].get('id')

    fw_rules = nsxt_client.get_firewall_section_rules(sect_id)
    rule = [x for x in fw_rules if x['display_name'] == http_rule.get('id')]
    rule_id = rule[0].get('id')

    msg = "NSX STATS is at section[%s] rule_id[%s]" % (sect_id, rule_id)
    utils.log_msg(msg, 'CHK-Stats')
    nsx_stats = nsxt_client.get_firewall_section_rule_stats(sect_id, rule_id)
    os_stats1 = log_lb2_stats(cmgr, lb2_name)

    if  no_http_traffic:
        utils.log_msg("No http traffic initiated", 'CHK-Stats')
    else:
        lbaas2.count_http_servers(lb2_web_ip)

    m_stats_change(nsxt_client, sect_id, rule_id, nsx_stats,
                   cmgr, lb2_name,
                   interval=interval, poke_count=poke_count)

    os_stats2 = log_lb2_stats(cmgr, lb2_name)

    dd = {}
    for k in os_stats2.keys():
        dd[k] = int(os_stats2[k]) - int(os_stats1[k])
    utils.log_msg("%s" % str(dd), "OS-STATS-DIFF")