Exemplo n.º 1
0
def delete_port(auth_info, port_id):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    try:
        neutron.delete_port(port_id)
    except NeutronClientException as e:
        logger.warn("[%s]NeutronClientException: %s", fun_name(), e.message)
        return [0, e.message]
    return [0, "Port(%s) is deleted" % port_id]
Exemplo n.º 2
0
def delete_subnet(auth_info, subnet_id):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    try:
        neutron.delete_subnet(subnet_id)
    except NeutronClientException as e:
        logger.warn("[%s]NetworkNotFoundClient: %s", fun_name(), e.message)
        return [0, e.message]
    return [0, "Subnet(%s) is deleted" % subnet_id]
Exemplo n.º 3
0
def rollback(neutron, network_data):
    for subnet_data in network_data["subnet_list"]:
        if subnet_data[const.RES_TYPE_KEY] == const.RES_TYPE_NEW:
            try:
                logger.info("[%s]call neutron.delete_subnet(%s)" % (fun_name(), subnet_data["id"]))
                neutron.delete_subnet(subnet_data["subnet_id"])
            except:
                logger.error("[%s]%s", fun_name(), str(sys.exc_info()))

    if network_data and network_data[const.RES_TYPE_KEY] == const.RES_TYPE_NEW:
        try:
            logger.info("[%s]call neutron.delete_network(%s)" % (fun_name(), network_data["id"]))
            neutron.delete_network(network_data["id"])
        except:
            logger.error("[%s]%s", fun_name(), str(sys.exc_info()))
Exemplo n.º 4
0
def create_image(auth_info, data):
    ret = None
    glance = glancebase.get_glance(fun_name(), auth_info)

    exist_img = [
        img for img in glance.images.list() if img.name == data["image_name"]
    ]
    if exist_img:
        ret = [
            0, {
                "id": exist_img[0].id,
                "name": data["image_name"],
                const.RES_TYPE_KEY: const.RES_TYPE_EXIST
            }
        ]
    else:
        img = glance.images.create(name=data["image_name"],
                                   disk_format=data["image_type"],
                                   visibility='public',
                                   container_format='bare')
        ret = [
            0, {
                "id": img.id,
                "name": data["image_name"],
                const.RES_TYPE_KEY: const.RES_TYPE_NEW
            }
        ]
        try:
            ImageUploadThread(glance, img.id, data["image_path"]).start()
        except:
            logger.error(traceback.format_exc())
            logger.error(str(sys.exc_info()))
    return ret
Exemplo n.º 5
0
def login(connect_info):
    url, user = connect_info["url"], connect_info["user"]
    passwd, tenant = connect_info["passwd"], connect_info["tenant"]
    cacert, clientcert, clientkey = None, None, None
    insecure = url.startswith('https')
    logger.info(
        "[%s]client.Client(auth_url='%s',"
        "username='******',password='******',"
        "tenant_name='%s',insecure=%s,cert='%s',key='%s',cacert='%s')" %
        (fun_name(), url, user, passwd, tenant, insecure, clientcert,
         clientkey, cacert))
    connect_info["cacert"] = cacert
    connect_info["clientcert"] = clientcert
    connect_info["clientkey"] = clientkey
    connect_info["insecure"] = insecure
    connect_info["keystone"] = client.Client(auth_url=url,
                                             username=user,
                                             password=passwd,
                                             interface='public',
                                             tenant_name=tenant,
                                             insecure=insecure,
                                             cert=clientcert,
                                             key=clientkey,
                                             cacert=cacert,
                                             debug=True)
    ret = [0, connect_info]
    return ret
Exemplo n.º 6
0
def delete_image(auth_info, image_id):
    from glanceclient.exc import HTTPNotFound
    glance = glancebase.get_glance(fun_name(), auth_info)
    try:
        glance.images.delete(image_id)
    except HTTPNotFound:
        logger.warn("Exception: %s" % str(sys.exc_info()))
        return [0, "Image(%s) does not exist" % image_id]
    return [0, "Image(%s) has been deleted" % image_id]
Exemplo n.º 7
0
 def create_api(self, connectInfo):
     vimtype = connectInfo['vimtype'] if 'vimtype' in connectInfo else None
     logger.info("call %s, vimtype=%s" % (fun_name(), vimtype))
     if vimtype == const.VIM_OPENSTACK:
         from lcm.pub.nfvi.vim.api.openstack.api import OpenstackApi
         self.apiImpl = OpenstackApi()
     elif vimtype == const.VIM_VMWARE:
         from lcm.pub.nfvi.vim.api.multivim.api import MultiVimApi
         self.apiImpl = MultiVimApi()
     else:
         self.authInfo = [1, "Unsupported vimtype(%s)" % vimtype]
Exemplo n.º 8
0
def delete_network(auth_info, network_id):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    try:
        neutron.delete_network(network_id)
    except Exception as ex:
        logger.error(traceback.format_exc())
        msg = ex.message if ex.message else str(sys.exc_info())
        logger.error(msg)
        if 404 == ex.status_code:
            return [0, ex.message]
        return [1, "Vim exception."]
    return [0, "Network(%s) is deleted" % network_id]
Exemplo n.º 9
0
def get_image(auth_info, image_id):
    from glanceclient.exc import HTTPNotFound
    glance = glancebase.get_glance(fun_name(), auth_info)
    img = None
    try:
        img = glance.images.get(image_id)
    except HTTPNotFound:
        logger.warn("Exception: %s" % str(sys.exc_info()))
        return [2, "Image(%s) does not exist" % image_id]
    ret_img_info = get_single_image(img)
    if 'status' in ret_img_info and 'deleted' == ret_img_info["status"]:
        return [2, "Image(%s) is deleted" % image_id]
    return [0, ret_img_info]
Exemplo n.º 10
0
 def force_login(self, connectInfo):
     if self.apiImpl:
         logger.info("call %s(%s)" % (fun_name(), connectInfo))
         try:
             self.authInfo = self.apiImpl.login(connectInfo)
         except VimException as e:
             self.authInfo = [1, e.args[0]]
         except Exception as e:
             logger.error(traceback.format_exc())
             logger.error(str(sys.exc_info()))
             self.authInfo = [
                 1, e.args[0] if e.args[0] else str(sys.exc_info())
             ]
         except:
             logger.error(traceback.format_exc())
             self.authInfo = [1, str(sys.exc_info())]
         logger.info("self.authInfo=%s" % self.authInfo)
Exemplo n.º 11
0
def query_subnet(auth_info, subnet_id):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    subnet_info = None
    try:
        subnet_info = neutron.show_subnet(subnet_id)["subnet"]
    except SubnetNotFound as e:
        logger.warn("SubnetNotFound: %s", e.message)
        return [2, e.message]
    ret = [0, {}]
    ret[1]["id"] = subnet_id
    ret[1]["name"] = subnet_info["name"]
    ret[1]["status"] = ""
    ret[1]["ip_version"] = subnet_info["ip_version"]
    ret[1]["cidr"] = subnet_info["cidr"]
    ret[1]["allocation_pools"] = subnet_info["allocation_pools"]
    ret[1]["enable_dhcp"] = subnet_info["enable_dhcp"]
    ret[1]["gateway_ip"] = subnet_info["gateway_ip"]
    ret[1]["host_routes"] = subnet_info["host_routes"]
    ret[1]["dns_nameservers"] = subnet_info["dns_nameservers"]
    return ret
Exemplo n.º 12
0
def create_subnet(neutron, network_id, data):
    all_subnets = neutron.list_subnets()
    filter_subnets = [subnet for subnet in all_subnets["subnets"]
                      if subnet["network_id"] == network_id and subnet["name"] == data["subnet_name"]]
    if filter_subnets:
        return [0, {
                    "id": filter_subnets[0]["id"],
                    "name": data["subnet_name"],
                    const.RES_TYPE_KEY: const.RES_TYPE_EXIST}]
    try:
        create_params = {
            'subnet': {
                'network_id': network_id,
                'name': data["subnet_name"],
                'cidr': data["cidr"],
                'ip_version': int(data["ip_version"]) if "ip_version" in data else const.IPV4, }}
        create_params["subnet"]["enable_dhcp"] = ("enable_dhcp" in data
                                                  and int(data["enable_dhcp"]) == const.ENABLE_DHCP)
        if "gateway_ip" in data and data["gateway_ip"]:
            create_params["subnet"]["gateway_ip"] = data["gateway_ip"]
        else:
            create_params["subnet"]["gateway_ip"] = None
        if "dns_nameservers" in data and data["dns_nameservers"]:
            create_params["subnet"]["dns_nameservers"] = data["dns_nameservers"]
        if "allocation_pools" in data and data["allocation_pools"]:
            create_params["subnet"]["allocation_pools"] = data["allocation_pools"]
        if "host_routes" in data and data["host_routes"]:
            create_params["subnet"]["host_routes"] = data["host_routes"]

        logger.info("[%s]call neutron.create_subnet(%s)" % (fun_name(), str(create_params)))
        subnet_created = neutron.create_subnet(create_params)
        return [0, {"id": subnet_created["subnet"]["id"],
                    "name": data["subnet_name"],
                    const.RES_TYPE_KEY: const.RES_TYPE_NEW}]
    except Exception as ex:
        logger.error(traceback.format_exc())
        logger.error(str(sys.exc_info()))
        return [1, ex.message if ex.message else str(sys.exc_info())]
Exemplo n.º 13
0
def query_nets(auth_info):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    keystone = auth_info["keystone"]
    tenants_map = {}
    tenants = keystone.tenants.list()
    for tenant in tenants:
        tenants_map[tenant.id] = tenant.name

    nets = neutron.list_networks()
    return [0, {"networks": [{
        "id": net["id"],
        "name": net["name"],
        "status": net["status"],
        "admin_state_up": net["admin_state_up"],
        "network_type": net["provider:network_type"],
        "physical_network": net["provider:physical_network"],
        "segmentation_id": net["provider:segmentation_id"],
        "tenant_id": net["tenant_id"],
        "tenant_name": tenants_map[net["tenant_id"]] if net["tenant_id"] in tenants_map else "unknown",
        "subnets": net["subnets"],
        "shared": net["shared"],
        "router_external": net["router:external"]
        } for net in nets["networks"]]}]
Exemplo n.º 14
0
def query_port(auth_info, port_id):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    port_info = None
    try:
        port_info = neutron.show_port(port_id)["port"]
    except NeutronClientException as e:
        logger.warn("NeutronClientException: %s", e.message)
        return [2, e.message]
    ret = [0, {}]
    ret[1]["id"] = port_id
    ret[1]["name"] = port_info["name"]
    ret[1]["network_id"] = port_info["network_id"]
    ret[1]["tenant_id"] = port_info["tenant_id"]
    if "fixed_ips" in port_info and port_info["fixed_ips"]:
        ret[1]["ip"] = port_info["fixed_ips"][0]["ip_address"]
        ret[1]["subnet_id"] = port_info["fixed_ips"][0]["subnet_id"]
    else:
        ret[1]["ip"] = ""
        ret[1]["subnet_id"] = ""
    ret[1]["mac_address"] = port_info["mac_address"]
    ret[1]["status"] = port_info["status"]
    ret[1]["admin_state_up"] = port_info["admin_state_up"]
    ret[1]["device_id"] = port_info["device_id"]
    return ret
Exemplo n.º 15
0
def query_net(auth_info, net_id):
    neutron = neutronbase.get_neutron_default(fun_name(), auth_info)
    net = None
    try:
        net = neutron.show_network(net_id)["network"]
        keystone = auth_info["keystone"]
        tenant = keystone.tenants.get(tenant_id=net["tenant_id"])
    except NetworkNotFoundClient as e:
        logger.warn("NetworkNotFoundClient: %s", e.message)
        return [2, e.message]
    return [0, {
        "id": net["id"],
        "name": net["name"],
        "status": net["status"],
        "admin_state_up": net["admin_state_up"],
        "network_type": net["provider:network_type"],
        "physical_network": net["provider:physical_network"],
        "segmentation_id": net["provider:segmentation_id"],
        "tenant_id": net["tenant_id"],
        "tenant_name": tenant.name,
        "subnets": net["subnets"],
        "shared": net["shared"],
        "router_external": net["router:external"]
        }]
Exemplo n.º 16
0
 def query_subnet(self, subnet_id):
     return self.api_call(fun_name(), self.apiImpl.query_subnet, subnet_id)
Exemplo n.º 17
0
 def query_nets(self):
     return self.api_call(fun_name(), self.apiImpl.query_nets)
Exemplo n.º 18
0
 def create_api(self, connectInfo):
     vimtype = connectInfo['vimtype'] if 'vimtype' in connectInfo else None
     logger.info("call %s, vimtype=%s" % (fun_name(), vimtype))
     from lcm.pub.nfvi.vim.api.multivim.api import MultiVimApi
     self.apiImpl = MultiVimApi()
Exemplo n.º 19
0
 def delete_port(self, port_id):
     return self.api_call(fun_name(), self.apiImpl.delete_port, port_id)
Exemplo n.º 20
0
 def get_image(self, image_id):
     return self.api_call(fun_name(), self.apiImpl.get_image, image_id)
Exemplo n.º 21
0
 def delete_network(self, network_id):
     return self.api_call(fun_name(), self.apiImpl.delete_network,
                          network_id)
Exemplo n.º 22
0
 def create_network(self, data):
     return self.api_call(fun_name(), self.apiImpl.create_network, data)
Exemplo n.º 23
0
 def delete_image(self, image_id):
     return self.api_call(fun_name(), self.apiImpl.delete_image, image_id)
Exemplo n.º 24
0
def create_port(auth_info, data):
    tenant_id = project.get_tenant_id(fun_name(), auth_info, data["tenant_name"])

    neutron_admin = neutronbase.get_neutron_default(fun_name(), auth_info)
    all_nets = neutron_admin.list_networks()
    filter_nets = [net for net in all_nets['networks'] if net['name'] == data["network_name"]]
    sel_nets = [net for net in filter_nets if net['tenant_id'] == tenant_id or
                (net['tenant_id'] != tenant_id and net['shared'])]
    count_sel_nets = len(sel_nets)
    if 1 > count_sel_nets:
        logger.error("Network(%s) does not exist" % data["network_name"])
        raise VimException("Network(%s) does not exist" % data["network_name"])
    if 1 < count_sel_nets:
        for net in sel_nets:
            logger.error("net_id=%s", net["id"])
        raise VimException("%d networks(%s) exist in tenant(%s)"
                           % (count_sel_nets, data["network_name"], data["tenant_name"]))
    network_id = sel_nets[0]['id']
    if tenant_id != sel_nets[0]['tenant_id']:
        neutron = neutronbase.get_neutron_by_tenant_id(fun_name(), auth_info, sel_nets[0]['tenant_id'])
    else:
        neutron = neutronbase.get_neutron(fun_name(), auth_info, data["tenant_name"])

    # get subnet id
    subnet_id = get_subnet_id(neutron_admin, data, network_id)

    # check port
    port_data = None
    ports = neutron.list_ports()
    sel_ports = [port for port in ports['ports'] if port['tenant_id'] == tenant_id
                 and port['network_id'] == network_id]
    filter_ports = []
    for port in sel_ports:
        if port['name'] == data["port_name"] and port['fixed_ips']:
            for fixed_ip in port['fixed_ips']:
                if fixed_ip['subnet_id'] == subnet_id:
                    filter_ports.append(port)
                    break
    count_filter_ports = len(filter_ports)
    if 1 < count_filter_ports:
        for port in filter_ports:
            logger.error("port_id=%s", port["id"])
        raise VimException("%d port(%s) exist in subnet(%s)"
                           % (count_filter_ports, data["port_name"], data["subnet_name"]))
    if 1 == len(filter_ports):
        logger.debug("Port(%s) is exist!" % data["port_name"])
        port_data = {'status': filter_ports[0]['status'],
                     'id': filter_ports[0]['id'],
                     'name': filter_ports[0]['name'],
                     'network_id': filter_ports[0]['network_id'],
                     const.RES_TYPE_KEY: const.RES_TYPE_EXIST}
        return [0, port_data]

    # create port
    create_param = {'port': {
            'name': data["port_name"],
            'admin_state_up': True,
            'network_id': network_id,
            'security_groups': [],
            'tenant_id': tenant_id}}
    if "mac_address" in data and data["mac_address"]:
        create_param['port']['mac_address'] = data["mac_address"]
    if "vnic_type" in data and data["vnic_type"]:
        create_param['port']['binding:vnic_type'] = data["vnic_type"]
    if "bandwidth" in data and data["bandwidth"]:
        create_param['port']['bandwidth'] = int(data["bandwidth"])
    if "bond" in data and data["bond"]:
        create_param['port']['bond'] = int(data["bond"])
    if "macbond" in data and data["macbond"]:
        if 'mac_address' in create_param['port']:
            create_param['port']['mac_address'] += (',' + data["macbond"])
        else:
            create_param['port']['mac_address'] = data["macbond"]
    if "ip" in data and data["ip"]:
        if subnet_id:
            create_param['port']['fixed_ips'] = [{"subnet_id": subnet_id, "ip_address": data["ip"]}]

    if "allowed_address_pairs" in data and data["allowed_address_pairs"]:
        create_param['port']['allowed_address_pairs'] = data["allowed_address_pairs"]
    logger.info("[%s]call neutron.create_port(%s)" % (fun_name(), str(create_param)))
    port_created = None
    try:
        port_created = neutron.create_port(create_param)
    except NeutronClientException as ex:
        logger.info("create_port exception: %s, %s", str(sys.exc_info()), ex.message)
        create_param['port'].pop('security_groups')
        if 'allowed_address_pairs' in create_param['port']:
            create_param['port'].pop('allowed_address_pairs')
        logger.info("[%s]recall neutron.create_port(%s)" % (fun_name(), str(create_param)))
        port_created = neutron.create_port(create_param)
    if port_created:
        port_data = {'status': port_created['port']['status'],
                     'id': port_created['port']['id'],
                     'name': port_created['port']['name'],
                     'network_id': port_created['port']['network_id'],
                     const.RES_TYPE_KEY: const.RES_TYPE_NEW}
    return [0, port_data]
Exemplo n.º 25
0
def create_network(auth_info, data):
    neutron = neutronbase.get_neutron(fun_name(), auth_info, data["tenant"])
    tenant_id = project.get_tenant_id(fun_name(), auth_info, data["tenant"])

    neutron_admin = neutronbase.get_neutron_default(fun_name(), auth_info)
    all_nets = neutron_admin.list_networks()
    filter_nets = [net for net in all_nets['networks'] if net['name'] == data["network_name"]]
    sel_nets = [net for net in filter_nets if net['tenant_id'] == tenant_id or
                (net['tenant_id'] != tenant_id and net['shared'])]
    count_sel_nets = len(sel_nets)
    if 1 < count_sel_nets:
        for sel_net in sel_nets:
            logger.info("net_id=%s, net_tenant_id=%s", sel_net["id"], sel_net['tenant_id'])
        raise VimException("Already %d networks are found with name %s" % (count_sel_nets, data["network_name"]))

    network_data = None
    if sel_nets:
        if sel_nets[0]['tenant_id'] != tenant_id:
            neutron = neutronbase.get_neutron_by_tenant_id(fun_name(), auth_info, sel_nets[0]['tenant_id'])
        all_subnets = neutron_admin.list_subnets()
        filter_subnets = [subnet for subnet in all_subnets["subnets"] if subnet["network_id"] == sel_nets[0]["id"]]
        network_data = {"status": sel_nets[0]["status"],
                        "id": sel_nets[0]["id"],
                        "name": data["network_name"],
                        "provider:segmentation_id": sel_nets[0]["provider:segmentation_id"],
                        "provider:network_type": sel_nets[0]["provider:network_type"],
                        const.RES_TYPE_KEY: const.RES_TYPE_EXIST,
                        "subnet_list": [{
                                            "id": subnet["id"],
                                            "name": subnet["name"],
                                            const.RES_TYPE_KEY: const.RES_TYPE_EXIST
                                        } for subnet in filter_subnets]}
    else:
        create_params = {
            'network': {
                'name': data["network_name"],
                'admin_state_up': True,
                'tenant_id': tenant_id,
                'shared': "shared" in data and int(data["shared"]) == const.SHARED_NET}}
        if "mtu" in data and int(data["mtu"]) != const.DEFAULT_MTU:
            create_params['network']['mtu'] = int(data["mtu"])
        if "vlan_transparent" in data and int(data["vlan_transparent"]) == const.SUPPORT_VLAN_TRANSPARENT:
            create_params['network']['vlan-transparent'] = True
        if "network_type" in data and data['network_type']:
            create_params['network']['provider:network_type'] = data['network_type']
        if "segmentation_id" in data and data['segmentation_id']:
            create_params['network']['provider:segmentation_id'] = int(data['segmentation_id'])
        if "physical_network" in data and data['physical_network']:
            create_params['network']['provider:physical_network'] = data['physical_network']

        logger.info("[%s]call neutron.create_network(%s)" % (fun_name(), str(create_params)))
        network_created = neutron.create_network(create_params)
        network_data = {"status": network_created['network']['status'],
                        "id": network_created['network']['id'],
                        "name": data["network_name"],
                        "provider:segmentation_id": network_created['network']['provider:segmentation_id'],
                        "provider:network_type": network_created['network']['provider:network_type'],
                        const.RES_TYPE_KEY: const.RES_TYPE_NEW,
                        "subnet_list": []}

    # subnet create
    exist_subnet_names = [subnet["name"] for subnet in network_data["subnet_list"]]
    need_rollback, ret_error = False, None
    if "subnet_list" in data and data["subnet_list"]:
        for subnet_data in data["subnet_list"]:
            if subnet_data["subnet_name"] in exist_subnet_names:
                continue
            ret = create_subnet(neutron, network_data["id"], subnet_data)
            if ret[0] != 0:
                need_rollback, ret_error = True, ret
                break
            network_data["subnet_list"].append(ret[1])

    # rollback when failed to create subnet
    if need_rollback:
        rollback(neutron_admin, network_data)
        return ret_error

    return [0, network_data]
Exemplo n.º 26
0
 def query_port(self, port_id):
     return self.api_call(fun_name(), self.apiImpl.query_port, port_id)
Exemplo n.º 27
0
 def create_image(self, data):
     return self.api_call(fun_name(), self.apiImpl.create_image, data)
Exemplo n.º 28
0
 def delete_subnet(self, subnet_id):
     return self.api_call(fun_name(), self.apiImpl.delete_subnet, subnet_id)
Exemplo n.º 29
0
 def get_images(self):
     return self.api_call(fun_name(), self.apiImpl.get_images)
Exemplo n.º 30
0
 def create_port(self, data):
     return self.api_call(fun_name(), self.apiImpl.create_port, data)