def _update_subnet_host_routes(self, context, id, s):
        def _combine(ht):
            return "{}_{}".format(ht['destination'], ht['nexthop'])

        old_route_list = self._get_route_by_subnet(context, id)

        new_route_set = set([_combine(route) for route in s['host_routes']])

        old_route_set = set([_combine(route) for route in old_route_list])

        for route_str in old_route_set - new_route_set:
            for route in old_route_list:
                if _combine(route) == route_str:
                    route.delete()
        for route_str in new_route_set - old_route_set:
            route = subnet_obj.Route(context,
                                     destination=net_utils.AuthenticIPNetwork(
                                         route_str.partition("_")[0]),
                                     nexthop=netaddr.IPAddress(
                                         route_str.partition("_")[2]),
                                     subnet_id=id)
            route.create()

        # Gather host routes for result
        new_routes = []
        for route_str in new_route_set:
            new_routes.append({
                'destination': route_str.partition("_")[0],
                'nexthop': route_str.partition("_")[2]
            })
        del s["host_routes"]
        return new_routes
Ejemplo n.º 2
0
    def _save_subnet(self, context, network, subnet_args, dns_nameservers,
                     host_routes, subnet_request):
        network_scope = addr_scope_obj.AddressScope.get_network_address_scope(
            context, network.id, subnet_args['ip_version'])
        # 'subnetpool' is not necessarily an object
        subnetpool = subnet_args.get('subnetpool_id')
        if subnetpool and subnetpool != const.IPV6_PD_POOL_ID:
            subnetpool = self._get_subnetpool(context, subnetpool)

        self._validate_subnet_cidr(context, network, subnet_args['cidr'])
        self._validate_network_subnetpools(network, subnet_args['ip_version'],
                                           subnetpool, network_scope)

        service_types = subnet_args.pop('service_types', [])

        segment_id = subnet_args.get('segment_id')
        if segment_id:
            # TODO(slaweq): integrate check if segment exists in
            # self._validate_segment() method
            if not network_obj.NetworkSegment.get_object(context,
                                                         id=segment_id):
                raise segment_exc.SegmentNotFound(segment_id=segment_id)

        subnet = subnet_obj.Subnet(context, **subnet_args)
        subnet.create()
        # TODO(slaweq): when check is segment exists will be integrated in
        # self._validate_segment() method, it should be moved to be done before
        # subnet object is created
        self._validate_segment(context, network['id'], segment_id)

        # NOTE(changzhi) Store DNS nameservers with order into DB one
        # by one when create subnet with DNS nameservers
        if validators.is_attr_set(dns_nameservers):
            for order, server in enumerate(dns_nameservers):
                dns = subnet_obj.DNSNameServer(context,
                                               address=server,
                                               order=order,
                                               subnet_id=subnet.id)
                dns.create()

        if validators.is_attr_set(host_routes):
            for rt in host_routes:
                route = subnet_obj.Route(
                    context,
                    subnet_id=subnet.id,
                    destination=net_utils.AuthenticIPNetwork(
                        rt['destination']),
                    nexthop=netaddr.IPAddress(rt['nexthop']))
                route.create()

        if validators.is_attr_set(service_types):
            for service_type in service_types:
                service_type_obj = subnet_obj.SubnetServiceType(
                    context, subnet_id=subnet.id, service_type=service_type)
                service_type_obj.create()

        self.save_allocation_pools(context, subnet,
                                   subnet_request.allocation_pools)

        return subnet_obj.Subnet.get_object(context, id=subnet.id)
Ejemplo n.º 3
0
    def _save_subnet(self, context,
                     network,
                     subnet_args,
                     dns_nameservers,
                     host_routes,
                     subnet_request):
        self._validate_subnet_cidr(context, network, subnet_args['cidr'])
        self._validate_network_subnetpools(network,
                                           subnet_args['subnetpool_id'],
                                           subnet_args['ip_version'])

        service_types = subnet_args.pop('service_types', [])

        subnet = models_v2.Subnet(**subnet_args)
        segment_id = subnet_args.get('segment_id')
        try:
            context.session.add(subnet)
            context.session.flush()
        except db_exc.DBReferenceError:
            raise segment_exc.SegmentNotFound(segment_id=segment_id)
        self._validate_segment(context, network['id'], segment_id)

        # NOTE(changzhi) Store DNS nameservers with order into DB one
        # by one when create subnet with DNS nameservers
        if validators.is_attr_set(dns_nameservers):
            for order, server in enumerate(dns_nameservers):
                dns = subnet_obj.DNSNameServer(context,
                                               address=server,
                                               order=order,
                                               subnet_id=subnet.id)
                dns.create()

        if validators.is_attr_set(host_routes):
            for rt in host_routes:
                route = subnet_obj.Route(
                    context,
                    subnet_id=subnet.id,
                    destination=common_utils.AuthenticIPNetwork(
                        rt['destination']),
                    nexthop=netaddr.IPAddress(rt['nexthop']))
                route.create()

        if validators.is_attr_set(service_types):
            for service_type in service_types:
                service_type_entry = sst_model.SubnetServiceType(
                    subnet_id=subnet.id,
                    service_type=service_type)
                context.session.add(service_type_entry)

        self.save_allocation_pools(context, subnet,
                                   subnet_request.allocation_pools)

        return subnet
Ejemplo n.º 4
0
        self.data = packet.data


PORT_INFO = {
    'device_owner':
    'compute:nova',
    'admin_state_up':
    True,
    'network_id':
    'd666ccb3-69e9-46cb-b157-bb3741d87d5a',
    'fixed_ips': [{
        'version':
        4,
        'host_routes': [
            subnet_obj.Route(destination=netaddr.IPNetwork('1.1.1.0/24'),
                             nexthop='192.168.1.100',
                             subnet_id='daed3c3d-d95a-48a8-a8b1-17d408cd760f'),
            subnet_obj.Route(destination=netaddr.IPNetwork('2.2.2.2/32'),
                             nexthop='192.168.1.101',
                             subnet_id='daed3c3d-d95a-48a8-a8b1-17d408cd760f')
        ],
        'subnet_id':
        'daed3c3d-d95a-48a8-a8b1-17d408cd760f',
        'dns_nameservers': [
            subnet_obj.DNSNameServer(
                address='8.8.8.8',
                order=0,
                subnet_id='daed3c3d-d95a-48a8-a8b1-17d408cd760f'),
            subnet_obj.DNSNameServer(
                address='8.8.4.4',
                order=1,