def main():
    logger.info('Start the program')
    logger.info('Initialize OSUtils')


#    endpoints_list = []
    logger.info('\n\nGet tenant details\n\n')
    tenants = osutils.get_tenants_list()

    tenant_id_list = tenants.keys()
    logger.info(tenants)

    logger.info('\n\nGet policy-target-groups for all tenants\n\n')
    ptgs = osutils.get_gbp_ptgs()

    logger.info('\n\nGet policy-rule-sets for all tenants\n\n')
    prss = osutils.get_gbp_prss()

    floating_ip_map = {}

    if config['traffic']['use_floating_ip'] == 'True':

        floating_ip_map = osutils.get_fixed_ip_floating_ip_map()

        

    """
        Code to generate endpoints for with-in ptg traffic
    """
    intra_tenant_intra_ptg_endpoints = get_intra_tenant_intra_ptg_endpoints(
        ptgs, tenants, tenant_id_list, floating_ip_map)
    global endpoints_list
    endpoints_list = endpoints_list + intra_tenant_intra_ptg_endpoints

    """
        Code to generate the endpoints based on the policy-rulesets
    """
    # intra_tenant_inter_ptg_endpoints = get_intra_tenant_inter_ptg_endpoints(
    #     ptgs, tenants, tenant_id_list, prss, floating_ip_map)
    # global endpoints_list
    # endpoints_list = endpoints_list + intra_tenant_inter_ptg_endpoints

    """
        Code to generate the endpoints based on the policy-rulesets inter-tenant
    """
    # inter_tenant_inter_ptg_endpoints = get_inter_tenant_endpoints(
    #    ptgs, tenants, tenant_id_list, prss, floating_ip_map)
    #global endpoints_list
    #endpoints_list = endpoints_list + inter_tenant_inter_ptg_endpoints

    for endpoint in endpoints_list:
        logger.info("*" * 50)
        for k, v in endpoint.items():

            logger.info("%s: %s" % (k, v))
        logger.info("" * 100)

    if tid:
        remote_libs.start_task(
            config, endpoints_list, action, osutils, floating_ip_map, tid)
    else:
        remote_libs.start_task(config, endpoints_list, action)
Пример #2
0
def main():
    logger.info('Start the program')
    logger.info('Initialize OSUtils')

    endpoints_list = []

    logger.info('\n\nGet tenant details\n\n')
    tenants = osutils.get_tenants_list()
    logger.info(tenants)

    logger.info('\n\nGet policy-target-groups for all tenants\n\n')
    ptgs = osutils.get_gbp_ptgs_by_tenant_all(tenants)
    # Debug code
    for tenant, ptgs_list in ptgs.items():
        logger.info('\n\nTennat - %s\n\n' % (tenant))
        for ptg in ptgs_list:
            logger.info('\n\n PTG - %s\n\n' % (ptg['name']))
            for key, val in ptg.items():
                logger.info('%s - %s' % (key, val))

    logger.info('\n\nGet policy-rule-sets for all tenants\n\n')
    prss = osutils.get_gbp_prs_by_tenant_all(tenants)
    # Debug code
    for tenant, prs_list in prss.items():
        logger.info('\n\nTennat - %s\n\n' % (tenant))
        for prs in prs_list:
            logger.info('\n\nPRS - %s\n\n' % (prs['name']))
            for key, val in prs.items():
                logger.info('%s - %s' % (key, val))

    floating_ip_map = {}

    if config['traffic']['use_floating_ip']:
        floating_ip_map = osutils.get_fixed_ip_floating_ip_map()

    for fixed_ip, floating_ip_list in floating_ip_map.items():
        print '%s - %s' % (fixed_ip, floating_ip_list)

    for tenant, ptgs_list in ptgs.items():
        tenant = tenant
        for ptg in ptgs_list:

            if config['traffic']['use_floating_ip']:
                ptg_ip_list = get_ptg_ip_list(tenant, ptg, floating_ip_map)
            else:
                ptg_ip_list = get_ptg_ip_list(tenant, ptg)

#            contract = get_policy_rule_per_tenant(tenant, i)
            contract = get_default_icmp_contract()
            endpoint = get_traffic_testing_endpoint(tenant, tenant, ptg['name'], ptg[
                                                    'name'], ptg_ip_list[ptg['id']], ptg_ip_list[ptg['id']], contract)
            endpoints_list.append(endpoint)

    for tenant, prs_list in prss.items():
        src_ptg = {}
        dest_ptg = {}
        tenant = tenant
        src_ptgs = get_src_ptgs_by_prs(tenant, ptgs[tenant], prs_list)
        dest_ptgs = get_dest_ptgs_by_prs(tenant, ptgs[tenant], prs_list)
        for ptg in src_ptgs:
            if config['traffic']['use_floating_ip']:
                src_ptg_ip = get_ptg_ip_list(tenant, ptg, floating_ip_map)
            else:
                src_ptg_ip = get_ptg_ip_list(tenant, ptg)
            src_ptg.update(src_ptg_ip)

        for ptg in dest_ptgs:
            if config['traffic']['use_floating_ip']:
                dest_ptg_ip = get_ptg_ip_list(tenant, ptg, floating_ip_map)
            else:
                dest_ptg_ip = get_ptg_ip_list(tenant, ptg)
            dest_ptg.update(dest_ptg_ip)
#        contract = get_policy_rule_for_prs(tenant, v)
        contract = get_default_icmp_contract()
        for srcptg in src_ptgs:
            for destptg in dest_ptgs:
                endpoint = get_traffic_testing_endpoint(tenant, tenant, srcptg['name'], destptg[
                                                        'name'], src_ptg[srcptg['id']], dest_ptg[destptg['id']], contract)
                endpoints_list.append(endpoint)


#    print "Endpoints values are....", endpoints_list

    for endpoint in endpoints_list:
        logger.info("*" * 50)
        for k, v in endpoint.items():
            logger.info("%s: %s" % (k, v))
        logger.info("" * 100)

    if tid:
        remote_libs.start_task(config, endpoints_list, action, tid)
    else:
        remote_libs.start_task(config, endpoints_list, action)
Пример #3
0
def main():
    logger.info('Start the program')
    logger.info('Initialize OSUtils')
    endpoints_list = []
    endpoint_list = []
    logger.info('Get tenant details')
    tenants = osutils.get_tenants_list()
    tenant_data = {}
    routers = {}
    for tenant in tenants.items():
        router_data = {}
        # retrieve all rotuers for a tenant
        routers = get_routers(tenant)
        ports = get_ports(tenant)
        instance_ports = get_instance_ports(ports)
        for router in routers:
            router_detail = {}
            router_detail['id'] = router['id']
            router_detail['ports'] = get_router_ports(router, ports)
            subnets = []
            instance_list = []
            for port in router_detail['ports']:
                subnets.append(get_port_subnets(tenant, port))
                router_detail['subnets'] = subnets
            for subnet in router_detail['subnets']:
                if subnet['id'] != '':
                    endpoint_detail = get_subnet_endpoints(tenant, instance_ports, subnet)
                    ip_list = []
                    endpoint_list = []
                    subnet['instances'] = endpoint_detail
                    for entry in endpoint_detail:
                        server = get_instance_detail(tenant, entry['device_id'])
                        if entry['device_id'] not in instance_list and \
                           server.status == 'ACTIVE':
                            instance_list.append(entry['device_id'])
                            ip_list.append(entry['ip_address'])
                        endpoint_list.append({'instances': instance_list,
                                              'endpoints': ip_list})
                    subnet['endpoints'] = ip_list
            router_data[router['name'].encode('unicode_escape')] = router_detail
        tenant_data[tenant[1]] = router_data
    # print tenant_data
    
    test_method = config['traffic']['test_method']
    contract = []
    if 'icmp' in test_method or len(test_method) == 0:
        contract.append(get_default_icmp_contract()[0])
    if 'tcp' in test_method:
        contract.append(get_default_tcp_contract()[0])
    if 'udp' in test_method:
        contract.append(get_default_udp_contract()[0])
    print contract
    tenant = config['tenants']['tenants']
    
    if config['traffic']['type'] == 'intra-tenant':
        # intra-tenant
        for tenant in tenants.items():
            current_router = {}
            current_net = {}
            routers = get_routers(tenant)
            for route in routers:
                rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
                if len(rsubnets) > 1:
                    current_net = {}
                    for entry in rsubnets:
                        tsrc = []
                        tdest = []
                        tsrc.append(entry['endpoints'])
                        for nextep in rsubnets:
                            if entry['name'] != nextep['name']:
                                tdest.append(nextep['endpoints'])
                        subnet_data = {'src_eps': [ep for eps in tsrc for ep in eps],
                                       'dest_eps': [ep for eps in tdest for ep in eps]}
                        current_net[entry['name']] = subnet_data
                current_router[route['name']] = current_net
    
            for route in routers:
                if route['name'] in current_router:
                    rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
                    for entry in rsubnets:
                        if entry['name'] in current_router[route['name']]:
                            net = current_router[route['name']][entry['name']]
                            if len(net['src_eps']) > 0 and \
                               len(net['dest_eps']) > 0:
                                endpoints_list.append(
                                    get_traffic_testing_endpoint(tenant[1],
                                                                 tenant[1],
                                                                 net['src_eps'],
                                                                 net['dest_eps'],
                                                                 contract))
        logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))

    if config['traffic']['type'] == 'inter-tenant':
        # inter-tenant
        for tenant in tenants.items():
            current_router = {}
            current_net = {}
            routers = get_routers(tenant)
            for route in routers:
                rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
                if len(rsubnets) > 0:
                    current_net = {}
                    tsrc = []
                    tdest = []
                    for entry in rsubnets:
                        tsrc.append(entry['endpoints'])
                    src_eps = [ep for eps in tsrc for ep in eps]
                    for nexttenant in tenants.items():
                        if tenant[1] != nexttenant[1]:
                            for nroute in tenant_data[nexttenant[1]]:
                                for entry in tenant_data[nexttenant[1]][nroute]['subnets']:
                                    if len(rsubnets) > 0:
                                        for ip in entry['endpoints']:
                                            floating_ip = get_endpoint_floatingips(nexttenant, ip)
                                            if floating_ip != '':
                                                tdest.append(floating_ip)
                            dest_eps = tdest
                            if len(src_eps) > 0 and len(dest_eps) > 0:
                                endpoints_list.append(
                                    get_traffic_testing_endpoint(tenant[1],
                                                                 nexttenant[1],
                                                                 src_eps,
                                                                 dest_eps,
                                                                 contract))
        logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))

    if config['traffic']['type'] == 'south-north':
        # south-north
        for tenant in tenants.items():
            current_router = {}
            current_net = {}
            routers = get_routers(tenant)
            for route in routers:
                rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
                if len(rsubnets) > 0:
                    current_net = {}
                    tsrc = []
                    for entry in rsubnets:
                        tsrc.append(entry['endpoints'])
                    src_eps = [ep for eps in tsrc for ep in eps]
                    if config['external_host']['host'] != '':
                        dest_eps = [config['external_host']['host']]
                    else:
                        dest_eps = []
                        print "Please specify external host in the config"
                    if len(src_eps) > 0 and len(dest_eps) > 0:
                        endpoints_list.append(
                            get_traffic_testing_endpoint(tenant[1],
                                                         'External Host',
                                                         src_eps,
                                                         dest_eps,
                                                         contract))
        logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))

    if config['traffic']['type'] == 'north-south':
        # north-south
        for tenant in tenants.items():
            current_router = {}
            current_net = {}
            routers = get_routers(tenant)
            for route in routers:
                rsubnets = tenant_data[tenant[1]][route['name']]['subnets']
                if len(rsubnets) > 0:
                    current_net = {}
                    tdest = []
                    if config['external_host']['host'] != '':
                        src_eps = [config['external_host']['host']]
                    else:
                        src_eps = []
                        print "Please specify external host in the config"
                    for entry in rsubnets:
                        for ip in entry['endpoints']:
                            floating_ip = get_endpoint_floatingips(tenant, ip)
                            if floating_ip != '':
                                tdest.append(floating_ip)
                    dest_eps = tdest
                    if len(src_eps) > 0 and len(dest_eps) > 0:
                        endpoints_list.append(
                            get_traffic_testing_endpoint('External Host',
                                                         tenant[1],
                                                         src_eps,
                                                         dest_eps,
                                                         contract))
        logger.debug("endpoints list = %s" % (pprint.pformat(endpoints_list)))


    # src_ip_list = ['192.168.61.131']
    # dest_ip_list = ['192.168.61.129', '192.168.61.132']
    # contract = [{'name': 'allow_ssh',
    #             'protocol': 'tcp',
    #             'port': 22,
    #             'direction': 'in',
    #             'action': 'allow'},
    #            {'name': 'allow_icmp',
    #             'protocol': 'icmp',
    #             'port': 'None',
    #             'direction': 'in',
    #             'action': 'allow'}]

    # endpoints = {'src_tenant': 'dummy_tenant',
    #             'dest_tenant': 'dummy_tenant',
    #             'src_eps': src_ip_list,
    #             'dest_eps': dest_ip_list,
    #             'contract': contract}
    # endpoints_list = [endpoints]

    if tid:
        remote_libs.start_task(config, endpoints_list, action, tid)
    else:
        remote_libs.start_task(config, endpoints_list, action)