Example #1
0
    def select_subnet(self, context, net_id, ip_address, segment_id,
                      subnet_ids=None, **filters):
        subnets = db_api.subnet_find_allocation_counts(
            context, net_id, segment_id=segment_id, scope=db_api.ALL,
            subnet_id=subnet_ids, **filters)

        for subnet, ips_in_subnet in subnets:
            ipnet = netaddr.IPNetwork(subnet["cidr"])
            if ip_address and ip_address not in ipnet:
                if subnet_ids is not None:
                    raise q_exc.IPAddressNotInSubnet(
                        ip_addr=ip_address, subnet_id=subnet["id"])
                continue

            ip_policy = None
            if not ip_address:
                # Policies don't prevent explicit assignment, so we only
                # need to check if we're allocating a new IP
                ip_policy = subnet.get("ip_policy")

            policy_size = ip_policy["size"] if ip_policy else 0

            if ipnet.size > (ips_in_subnet + policy_size - 1):
                if not ip_address:
                    ip = subnet["next_auto_assign_ip"]
                    # If ip is somehow -1 in here don't touch it anymore
                    if ip != -1:
                        ip += 1
                    # and even then if it is outside the valid range set it to
                    # -1 to be safe
                    if ip < subnet["first_ip"] or ip > subnet["last_ip"]:
                        ip = -1
                    db_api.subnet_update(context, subnet,
                                         next_auto_assign_ip=ip)
                return subnet
Example #2
0
    def _stubs(self, network, subnet, address, lock=False):
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            next_ip = subnet.pop("next_auto_assign_ip", 0)
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            sub_mod = db_api.subnet_create(self.context, **subnet)

            address["network_id"] = net_mod["id"]
            address["subnet_id"] = sub_mod["id"]
            ip = db_api.ip_address_create(self.context, **address)
            address.pop("address")
            ip = db_api.ip_address_update(self.context, ip, **address)

            # NOTE(asadoughi): update after cidr constructor has been invoked
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_ip)

        if lock:
            db_api.lock_holder_create(self.context,
                                      ip,
                                      name="testlock",
                                      type="ip_address")
        yield net_mod
Example #3
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         next_ip = subnet.pop("next_auto_assign_ip", 0)
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
         # NOTE(asadoughi): update after cidr constructor has been invoked
         db_api.subnet_update(self.context,
                              sub_mod,
                              next_auto_assign_ip=next_ip)
     yield net_mod
Example #4
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         next_ip = subnet.pop("next_auto_assign_ip", 0)
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
         # NOTE(asadoughi): update after cidr constructor has been invoked
         db_api.subnet_update(self.context,
                              sub_mod,
                              next_auto_assign_ip=next_ip)
     yield net_mod
Example #5
0
    def _stubs(self, network, subnet, dealloc=True):
        self.plugin = quark.plugin.Plugin()
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            next_auto = subnet.pop("next_auto_assign_ip", 0)
            sub_mod = db_api.subnet_create(self.context, **subnet)
            db_api.subnet_update(self.context, sub_mod, next_auto_assign_ip=next_auto)

        ip_addr = []
        self.ipam.allocate_ip_address(self.context, ip_addr, net_mod["id"], 0, 0)
        if dealloc:
            self.ipam.deallocate_ip_address(self.context, ip_addr[0])
        yield net_mod
Example #6
0
    def select_subnet(self,
                      context,
                      net_id,
                      ip_address,
                      segment_id,
                      subnet_ids=None,
                      **filters):
        subnets = db_api.subnet_find_allocation_counts(context,
                                                       net_id,
                                                       segment_id=segment_id,
                                                       scope=db_api.ALL,
                                                       subnet_id=subnet_ids,
                                                       **filters)

        for subnet, ips_in_subnet in subnets:
            ipnet = netaddr.IPNetwork(subnet["cidr"])
            if ip_address:
                na_ip = netaddr.IPAddress(ip_address)
                if ipnet.version == 4 and na_ip.version != 4:
                    na_ip = na_ip.ipv4()
                if na_ip not in ipnet:
                    if subnet_ids is not None:
                        raise q_exc.IPAddressNotInSubnet(
                            ip_addr=ip_address, subnet_id=subnet["id"])
                    continue

            ip_policy = None
            if not ip_address:
                # Policies don't prevent explicit assignment, so we only
                # need to check if we're allocating a new IP
                ip_policy = subnet.get("ip_policy")

            policy_size = ip_policy["size"] if ip_policy else 0

            if ipnet.size > (ips_in_subnet + policy_size - 1):
                if not ip_address:
                    ip = subnet["next_auto_assign_ip"]
                    # If ip is somehow -1 in here don't touch it anymore
                    if ip != -1:
                        ip += 1
                    # and even then if it is outside the valid range set it to
                    # -1 to be safe
                    if ip < subnet["first_ip"] or ip > subnet["last_ip"]:
                        ip = -1
                    db_api.subnet_update(context,
                                         subnet,
                                         next_auto_assign_ip=ip)
                return subnet
Example #7
0
def update_subnet(context, id, subnet):
    """Update values of a subnet.

    : param context: neutron api request context
    : param id: UUID representing the subnet to update.
    : param subnet: dictionary with keys indicating fields to update.
        valid keys are those that have a value of True for 'allow_put'
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.
    """
    LOG.info("update_subnet %s for tenant %s" %
             (id, context.tenant_id))

    with context.session.begin():
        subnet_db = db_api.subnet_find(context, id=id, scope=db_api.ONE)
        if not subnet_db:
            raise exceptions.SubnetNotFound(id=id)

        s = subnet["subnet"]

        dns_ips = s.pop("dns_nameservers", [])
        host_routes = s.pop("host_routes", [])
        gateway_ip = s.pop("gateway_ip", None)

        if gateway_ip:
            default_route = None
            for route in host_routes:
                netaddr_route = netaddr.IPNetwork(route["destination"])
                if netaddr_route.value == routes.DEFAULT_ROUTE.value:
                    default_route = route
                    break
            if default_route is None:
                route_model = db_api.route_find(
                    context, cidr=str(routes.DEFAULT_ROUTE), subnet_id=id,
                    scope=db_api.ONE)
                if route_model:
                    db_api.route_update(context, route_model,
                                        gateway=gateway_ip)
                else:
                    db_api.route_create(context,
                                        cidr=str(routes.DEFAULT_ROUTE),
                                        gateway=gateway_ip, subnet_id=id)

        if dns_ips:
            subnet_db["dns_nameservers"] = []
        for dns_ip in dns_ips:
            subnet_db["dns_nameservers"].append(db_api.dns_create(
                context,
                ip=netaddr.IPAddress(dns_ip)))

        if host_routes:
            subnet_db["routes"] = []
        for route in host_routes:
            subnet_db["routes"].append(db_api.route_create(
                context, cidr=route["destination"], gateway=route["nexthop"]))

        subnet = db_api.subnet_update(context, subnet_db, **s)
    return v._make_subnet_dict(subnet)
Example #8
0
def update_subnet(context, id, subnet):
    """Update values of a subnet.

    : param context: neutron api request context
    : param id: UUID representing the subnet to update.
    : param subnet: dictionary with keys indicating fields to update.
        valid keys are those that have a value of True for 'allow_put'
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.
    """
    LOG.info("update_subnet %s for tenant %s" % (id, context.tenant_id))

    subnet_db = db_api.subnet_find(context, id=id, scope=db_api.ONE)
    if not subnet_db:
        raise exceptions.SubnetNotFound(id=id)

    s = subnet["subnet"]

    dns_ips = s.pop("dns_nameservers", [])
    host_routes = s.pop("host_routes", [])
    gateway_ip = s.pop("gateway_ip", None)

    if gateway_ip:
        default_route = None
        for route in host_routes:
            netaddr_route = netaddr.IPNetwork(route["destination"])
            if netaddr_route.value == routes.DEFAULT_ROUTE.value:
                default_route = route
                break
        if default_route is None:
            route_model = db_api.route_find(context,
                                            cidr=str(routes.DEFAULT_ROUTE),
                                            subnet_id=id,
                                            scope=db_api.ONE)
            if route_model:
                db_api.route_update(context, route_model, gateway=gateway_ip)
            else:
                db_api.route_create(context,
                                    cidr=str(routes.DEFAULT_ROUTE),
                                    gateway=gateway_ip,
                                    subnet_id=id)

    if dns_ips:
        subnet_db["dns_nameservers"] = []
    for dns_ip in dns_ips:
        subnet_db["dns_nameservers"].append(
            db_api.dns_create(context, ip=netaddr.IPAddress(dns_ip)))

    if host_routes:
        subnet_db["routes"] = []
    for route in host_routes:
        subnet_db["routes"].append(
            db_api.route_create(context,
                                cidr=route["destination"],
                                gateway=route["nexthop"]))

    subnet = db_api.subnet_update(context, subnet_db, **s)
    return v._make_subnet_dict(subnet, default_route=routes.DEFAULT_ROUTE)
Example #9
0
    def _fixtures(self, models):

        self.ipam = quark.ipam.QuarkIpamANY()
        net = dict(name="public", tenant_id='fake')
        net_mod = db_api.network_create(self.context, **net)
        with self.context.session.begin():
            for model in models:
                policy_mod = db_api.ip_policy_create(self.context,
                                                     **model['ip_policy'])
                model['subnet']["network"] = net_mod
                model['subnet']["ip_policy"] = policy_mod
                next_ip = model['subnet'].pop("next_auto_assign_ip", 0)
                sub_mod = db_api.subnet_create(self.context, **model['subnet'])
                # NOTE(amir): update after cidr constructor has been invoked
                db_api.subnet_update(self.context,
                                     sub_mod,
                                     next_auto_assign_ip=next_ip)
        yield net_mod
Example #10
0
    def _fixtures(self, models):

        self.ipam = quark.ipam.QuarkIpamANY()
        net = dict(name="public", tenant_id='fake')
        net_mod = db_api.network_create(self.context, **net)
        with self.context.session.begin():
            for model in models:
                policy_mod = db_api.ip_policy_create(
                    self.context, **model['ip_policy'])
                model['subnet']["network"] = net_mod
                model['subnet']["ip_policy"] = policy_mod
                next_ip = model['subnet'].pop("next_auto_assign_ip", 0)
                sub_mod = db_api.subnet_create(self.context, **model['subnet'])
                # NOTE(amir): update after cidr constructor has been invoked
                db_api.subnet_update(self.context,
                                     sub_mod,
                                     next_auto_assign_ip=next_ip)
        yield net_mod
Example #11
0
    def _stubs(self, network, subnet, dealloc=True):
        self.plugin = quark.plugin.Plugin()
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            next_auto = subnet.pop("next_auto_assign_ip", 0)
            sub_mod = db_api.subnet_create(self.context, **subnet)
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_auto)

        ip_addr = []
        self.ipam.allocate_ip_address(self.context, ip_addr, net_mod["id"], 0,
                                      0)
        if dealloc:
            self.ipam.deallocate_ip_address(self.context, ip_addr[0])
        yield net_mod
Example #12
0
 def _stubs(self, network, subnets, ipam_strategy):
     self.ipam = ipam_strategy
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         next_ip = []
         sub_mod = []
         for sub in subnets:
             next_ip.append(sub.pop("next_auto_assign_ip", 0))
             sub["network"] = net_mod
             sub_mod.append(db_api.subnet_create(self.context, **sub))
         for sub, ip_next in zip(sub_mod, next_ip):
             # NOTE(asadoughi): update after cidr constructor has been
             # invoked
             db_api.subnet_update(self.context,
                                  sub,
                                  next_auto_assign_ip=ip_next)
     yield net_mod, sub_mod
     with self.context.session.begin():
         for sub in sub_mod:
             db_api.subnet_delete(self.context, sub)
         db_api.network_delete(self.context, net_mod)
Example #13
0
 def _stubs(self, network, subnets, ipam_strategy):
     self.ipam = ipam_strategy
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         next_ip = []
         sub_mod = []
         for sub in subnets:
             next_ip.append(sub.pop("next_auto_assign_ip", 0))
             sub["network"] = net_mod
             sub_mod.append(db_api.subnet_create(self.context, **sub))
         for sub, ip_next in zip(sub_mod, next_ip):
             # NOTE(asadoughi): update after cidr constructor has been
             # invoked
             db_api.subnet_update(self.context,
                                  sub,
                                  next_auto_assign_ip=ip_next)
     yield net_mod, sub_mod
     with self.context.session.begin():
         for sub in sub_mod:
             db_api.subnet_delete(self.context, sub)
         db_api.network_delete(self.context, net_mod)
Example #14
0
    def _stubs(self, network, subnet, address, lock=False):
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            next_ip = subnet.pop("next_auto_assign_ip", 0)
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            sub_mod = db_api.subnet_create(self.context, **subnet)

            address["network_id"] = net_mod["id"]
            address["subnet_id"] = sub_mod["id"]
            ip = db_api.ip_address_create(self.context, **address)
            address.pop("address")
            ip = db_api.ip_address_update(self.context, ip, **address)

            # NOTE(asadoughi): update after cidr constructor has been invoked
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_ip)

        if lock:
            db_api.lock_holder_create(self.context, ip,
                                      name="testlock", type="ip_address")
        yield net_mod
Example #15
0
def update_subnet(context, id, subnet):
    """Update values of a subnet.

    : param context: neutron api request context
    : param id: UUID representing the subnet to update.
    : param subnet: dictionary with keys indicating fields to update.
        valid keys are those that have a value of True for 'allow_put'
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.
    """
    LOG.info("update_subnet %s for tenant %s" %
             (id, context.tenant_id))

    with context.session.begin():
        subnet_db = db_api.subnet_find(context, None, None, None, False, id=id,
                                       scope=db_api.ONE)
        if not subnet_db:
            raise exceptions.SubnetNotFound(id=id)

        s = subnet["subnet"]
        always_pop = ["_cidr", "cidr", "first_ip", "last_ip", "ip_version",
                      "segment_id", "network_id"]
        admin_only = ["do_not_use", "created_at", "tenant_id",
                      "next_auto_assign_ip", "enable_dhcp"]
        utils.filter_body(context, s, admin_only, always_pop)

        dns_ips = utils.pop_param(s, "dns_nameservers", [])
        host_routes = utils.pop_param(s, "host_routes", [])
        gateway_ip = utils.pop_param(s, "gateway_ip", None)
        allocation_pools = utils.pop_param(s, "allocation_pools", None)
        if not CONF.QUARK.allow_allocation_pool_update:
            if allocation_pools:
                raise exceptions.BadRequest(
                    resource="subnets",
                    msg="Allocation pools cannot be updated.")
            alloc_pools = allocation_pool.AllocationPools(
                subnet_db["cidr"],
                policies=models.IPPolicy.get_ip_policy_cidrs(subnet_db))
        else:
            alloc_pools = allocation_pool.AllocationPools(subnet_db["cidr"],
                                                          allocation_pools)

        quota.QUOTAS.limit_check(
            context,
            context.tenant_id,
            alloc_pools_per_subnet=len(alloc_pools))
        if gateway_ip:
            alloc_pools.validate_gateway_excluded(gateway_ip)
            default_route = None
            for route in host_routes:
                netaddr_route = netaddr.IPNetwork(route["destination"])
                if netaddr_route.value == routes.DEFAULT_ROUTE.value:
                    default_route = route
                    break

            if default_route is None:
                route_model = db_api.route_find(
                    context, cidr=str(routes.DEFAULT_ROUTE), subnet_id=id,
                    scope=db_api.ONE)
                if route_model:
                    db_api.route_update(context, route_model,
                                        gateway=gateway_ip)
                else:
                    db_api.route_create(context,
                                        cidr=str(routes.DEFAULT_ROUTE),
                                        gateway=gateway_ip, subnet_id=id)

        if dns_ips:
            subnet_db["dns_nameservers"] = []
            quota.QUOTAS.limit_check(context, context.tenant_id,
                                     dns_nameservers_per_subnet=len(dns_ips))

        for dns_ip in dns_ips:
            subnet_db["dns_nameservers"].append(db_api.dns_create(
                context,
                ip=netaddr.IPAddress(dns_ip)))

        if host_routes:
            subnet_db["routes"] = []
            quota.QUOTAS.limit_check(context, context.tenant_id,
                                     routes_per_subnet=len(host_routes))

        for route in host_routes:
            subnet_db["routes"].append(db_api.route_create(
                context, cidr=route["destination"], gateway=route["nexthop"]))
        if CONF.QUARK.allow_allocation_pool_update:
            if isinstance(allocation_pools, list):
                cidrs = alloc_pools.get_policy_cidrs()
                ip_policies.ensure_default_policy(cidrs, [subnet_db])
                subnet_db["ip_policy"] = db_api.ip_policy_update(
                    context, subnet_db["ip_policy"], exclude=cidrs)
                # invalidate the cache
                db_api.subnet_update_set_alloc_pool_cache(context, subnet_db)
        subnet = db_api.subnet_update(context, subnet_db, **s)
    return v._make_subnet_dict(subnet)
Example #16
0
 def _set_subnet_next_auto_assign_ip(self, context, subnet, ip):
     with context.session.begin():
         db_api.subnet_update(context, subnet, next_auto_assign_ip=ip)
Example #17
0
def update_subnet(context, id, subnet):
    """Update values of a subnet.

    : param context: neutron api request context
    : param id: UUID representing the subnet to update.
    : param subnet: dictionary with keys indicating fields to update.
        valid keys are those that have a value of True for 'allow_put'
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.
    """
    LOG.info("update_subnet %s for tenant %s" % (id, context.tenant_id))

    with context.session.begin():
        subnet_db = db_api.subnet_find(context,
                                       None,
                                       None,
                                       None,
                                       False,
                                       id=id,
                                       scope=db_api.ONE)
        if not subnet_db:
            raise exceptions.SubnetNotFound(id=id)

        s = subnet["subnet"]
        always_pop = [
            "_cidr", "cidr", "first_ip", "last_ip", "ip_version", "segment_id",
            "network_id"
        ]
        admin_only = [
            "do_not_use", "created_at", "tenant_id", "next_auto_assign_ip",
            "enable_dhcp"
        ]
        utils.filter_body(context, s, admin_only, always_pop)

        dns_ips = utils.pop_param(s, "dns_nameservers", [])
        host_routes = utils.pop_param(s, "host_routes", [])
        gateway_ip = utils.pop_param(s, "gateway_ip", None)
        allocation_pools = utils.pop_param(s, "allocation_pools", None)
        if not CONF.QUARK.allow_allocation_pool_update:
            if allocation_pools:
                raise exceptions.BadRequest(
                    resource="subnets",
                    msg="Allocation pools cannot be updated.")
            alloc_pools = allocation_pool.AllocationPools(
                subnet_db["cidr"],
                policies=models.IPPolicy.get_ip_policy_cidrs(subnet_db))
        else:
            alloc_pools = allocation_pool.AllocationPools(
                subnet_db["cidr"], allocation_pools)

        quota.QUOTAS.limit_check(context,
                                 context.tenant_id,
                                 alloc_pools_per_subnet=len(alloc_pools))
        if gateway_ip:
            alloc_pools.validate_gateway_excluded(gateway_ip)
            default_route = None
            for route in host_routes:
                netaddr_route = netaddr.IPNetwork(route["destination"])
                if netaddr_route.value == routes.DEFAULT_ROUTE.value:
                    default_route = route
                    break

            if default_route is None:
                route_model = db_api.route_find(context,
                                                cidr=str(routes.DEFAULT_ROUTE),
                                                subnet_id=id,
                                                scope=db_api.ONE)
                if route_model:
                    db_api.route_update(context,
                                        route_model,
                                        gateway=gateway_ip)
                else:
                    db_api.route_create(context,
                                        cidr=str(routes.DEFAULT_ROUTE),
                                        gateway=gateway_ip,
                                        subnet_id=id)

        if dns_ips:
            subnet_db["dns_nameservers"] = []
            quota.QUOTAS.limit_check(context,
                                     context.tenant_id,
                                     dns_nameservers_per_subnet=len(dns_ips))

        for dns_ip in dns_ips:
            subnet_db["dns_nameservers"].append(
                db_api.dns_create(context, ip=netaddr.IPAddress(dns_ip)))

        if host_routes:
            subnet_db["routes"] = []
            quota.QUOTAS.limit_check(context,
                                     context.tenant_id,
                                     routes_per_subnet=len(host_routes))

        for route in host_routes:
            subnet_db["routes"].append(
                db_api.route_create(context,
                                    cidr=route["destination"],
                                    gateway=route["nexthop"]))
        if CONF.QUARK.allow_allocation_pool_update:
            if isinstance(allocation_pools, list):
                cidrs = alloc_pools.get_policy_cidrs()
                ip_policies.ensure_default_policy(cidrs, [subnet_db])
                subnet_db["ip_policy"] = db_api.ip_policy_update(
                    context, subnet_db["ip_policy"], exclude=cidrs)
                # invalidate the cache
                db_api.subnet_update_set_alloc_pool_cache(context, subnet_db)
        subnet = db_api.subnet_update(context, subnet_db, **s)
    return v._make_subnet_dict(subnet)