# Path: blazar/context.py
# CONF = cfg.CONF
#     FROM_DICT_EXTRA_KEYS = ['service_catalog']
# class BlazarContext(context.RequestContext):
#     def __init__(self, service_catalog=None, **kwargs):
#     def __enter__(self):
#     def __exit__(self, exc_type, exc_val, exc_tb):
#     def current(cls):
#     def to_dict(self):
#     def admin(cls):
# def current():
# def admin():
# 
# Path: blazar/utils/openstack/base.py
# CONF = cfg.CONF
# def get_os_auth_host(conf):
# def url_for(service_catalog, service_type, admin=False,
#             endpoint_interface=None,
#             os_region_name=None):
# 
# Path: blazar/utils/openstack/exceptions.py
# class ResourceProviderRetrievalFailed(exceptions.BlazarException):
# class ResourceProviderCreationFailed(exceptions.BlazarException):
# class ResourceProviderCreationConflict(exceptions.BlazarException):
# class ResourceProviderDeletionFailed(exceptions.BlazarException):
# class ResourceClassCreationFailed(exceptions.BlazarException):
# class ResourceClassDeletionFailed(exceptions.BlazarException):
# class ResourceProviderNotFound(exceptions.BlazarException):
# class InventoryUpdateFailed(exceptions.BlazarException):
# class InventoryConflict(exceptions.BlazarException):
# class FloatingIPNetworkNotFound(exceptions.InvalidInput):
# class FloatingIPSubnetNotFound(exceptions.NotFound):
# class NeutronUsesFloatingIP(exceptions.InvalidInput):


from keystoneauth1.identity import v3
from keystoneauth1 import session
from neutronclient.common import exceptions as neutron_exceptions
from neutronclient.v2_0 import client as neutron_client
from oslo_config import cfg
from oslo_log import log as logging
from blazar import context
from blazar.utils.openstack import base
from blazar.utils.openstack import exceptions
import netaddr
            raise exceptions.FloatingIPNetworkNotFound(network=network_id)

        self.network_id = network_id

    def fetch_subnet(self, floatingip):
        fip = netaddr.IPAddress(floatingip)
        network = self.neutron.show_network(self.network_id)['network']
        subnet_ids = network['subnets']

        for sub_id in subnet_ids:
            subnet = self.neutron.show_subnet(sub_id)['subnet']
            cidr = netaddr.IPNetwork(subnet['cidr'])

            # skip the subnet because it has not valid cidr for the floating ip
            if fip not in cidr:
                continue

            allocated_ip = netaddr.IPSet()

            allocated_ip.add(netaddr.IPAddress(subnet['gateway_ip']))
            for alloc in subnet['allocation_pools']:
                allocated_ip.add(netaddr.IPRange(alloc['start'], alloc['end']))

            if fip in allocated_ip:
                raise exceptions.NeutronUsesFloatingIP(floatingip=fip,
                                                       subnet=subnet['id'])
            else:
                self.subnet_id = subnet['id']
                return subnet


# Path: blazar/context.py
# CONF = cfg.CONF
#     FROM_DICT_EXTRA_KEYS = ['service_catalog']
# class BlazarContext(context.RequestContext):
#     def __init__(self, service_catalog=None, **kwargs):
#     def __enter__(self):
#     def __exit__(self, exc_type, exc_val, exc_tb):
#     def current(cls):
#     def to_dict(self):
#     def admin(cls):
# def current():
# def admin():
# 
# Path: blazar/utils/openstack/base.py
# CONF = cfg.CONF
# def get_os_auth_host(conf):
# def url_for(service_catalog, service_type, admin=False,
#             endpoint_interface=None,
#             os_region_name=None):
# 
# Path: blazar/utils/openstack/exceptions.py
# class ResourceProviderRetrievalFailed(exceptions.BlazarException):
# class ResourceProviderCreationFailed(exceptions.BlazarException):
# class ResourceProviderCreationConflict(exceptions.BlazarException):
# class ResourceProviderDeletionFailed(exceptions.BlazarException):
# class ResourceClassCreationFailed(exceptions.BlazarException):
# class ResourceClassDeletionFailed(exceptions.BlazarException):
# class ResourceProviderNotFound(exceptions.BlazarException):
# class InventoryUpdateFailed(exceptions.BlazarException):
# class InventoryConflict(exceptions.BlazarException):
# class FloatingIPNetworkNotFound(exceptions.InvalidInput):
# class FloatingIPSubnetNotFound(exceptions.NotFound):
# class NeutronUsesFloatingIP(exceptions.InvalidInput):


# Path: blazar/utils/openstack/neutron.py
from keystoneauth1.identity import v3
from keystoneauth1 import session
from neutronclient.common import exceptions as neutron_exceptions
from neutronclient.v2_0 import client as neutron_client
from oslo_config import cfg
from oslo_log import log as logging
from blazar import context
from blazar.utils.openstack import base
from blazar.utils.openstack import exceptions
import netaddr

            raise exceptions.FloatingIPNetworkNotFound(network=network_id)

        self.network_id = network_id

    def fetch_subnet(self, floatingip):
        fip = netaddr.IPAddress(floatingip)
        network = self.neutron.show_network(self.network_id)['network']
        subnet_ids = network['subnets']

        for sub_id in subnet_ids:
            subnet = self.neutron.show_subnet(sub_id)['subnet']
            cidr = netaddr.IPNetwork(subnet['cidr'])

            # skip the subnet because it has not valid cidr for the floating ip
            if fip not in cidr:
                continue

            allocated_ip = netaddr.IPSet()

            allocated_ip.add(netaddr.IPAddress(subnet['gateway_ip']))
            for alloc in subnet['allocation_pools']:
                allocated_ip.add(netaddr.IPRange(alloc['start'], alloc['end']))

            if fip in allocated_ip:
                raise exceptions.NeutronUsesFloatingIP(floatingip=fip,
                                                       subnet=subnet['id'])
            else:
                self.subnet_id = subnet['id']
                return subnet


        raise exceptions.FloatingIPSubnetNotFound(fip=floatingip)