def post_dbe_update(cls,
                     id,
                     fq_name,
                     obj_dict,
                     db_conn,
                     prop_collection_updates=None,
                     ref_update=None):
     if fq_name == Project().fq_name:
         cls.server.default_project = None
         cls.server.default_project
     return True, ''
    def create_intvn_and_ref(cls, obj_dict, db_conn):
        ok, proj_dict = db_conn.dbe_read('project', obj_dict['parent_uuid'])
        if not ok:
            return (ok, proj_dict)

        vn_int_name = get_lr_internal_vn_name(obj_dict.get('uuid'))
        proj_obj = Project(name=proj_dict.get('fq_name')[-1],
                           parent_type='domain',
                           fq_name=proj_dict.get('fq_name'))

        vn_obj = VirtualNetwork(name=vn_int_name, parent_obj=proj_obj)
        id_perms = IdPermsType(enable=True, user_visible=False)
        vn_obj.set_id_perms(id_perms)

        int_vn_property = VirtualNetworkType(forwarding_mode='l3')
        if 'vxlan_network_identifier' in obj_dict:
            vni_id = obj_dict['vxlan_network_identifier']
            int_vn_property.set_vxlan_network_identifier(vni_id)
        vn_obj.set_virtual_network_properties(int_vn_property)

        rt_list = obj_dict.get('configured_route_target_list',
                               {}).get('route_target')
        if rt_list:
            vn_obj.set_route_target_list(RouteTargetList(rt_list))

        vn_int_dict = json.dumps(vn_obj, default=_obj_serializer_all)
        api_server = db_conn.get_api_server()
        status, obj = api_server.internal_request_create(
            'virtual-network', json.loads(vn_int_dict))
        attr_obj = LogicalRouterVirtualNetworkType('InternalVirtualNetwork')
        attr_dict = attr_obj.__dict__
        api_server.internal_request_ref_update(
            'logical-router',
            obj_dict['uuid'],
            'ADD',
            'virtual-network',
            obj['virtual-network']['uuid'],
            obj['virtual-network']['fq_name'],
            attr=attr_dict)
        return True, ''
    def post_dbe_update(cls, uuid, fq_name, obj_dict, db_conn,
                        prop_collection_updates=None):

        ok, result = db_conn.dbe_read(
            'logical_router',
            obj_dict['uuid'],
            obj_fields=['virtual_network_refs', 'logical_router_type'])
        if not ok:
            return ok, result
        lr_orig_dict = result

        if (obj_dict.get('configured_route_target_list') is None and
                'vxlan_network_identifier' not in obj_dict):
            return True, ''

        logical_router_type_in_db = cls.check_lr_type(lr_orig_dict)
        if logical_router_type_in_db == 'vxlan-routing':
            # If logical_router_type was set to vxlan-routing in DB,
            # it means that an existing LR used for VXLAN
            # support was updated to either change the
            # vxlan_network_identifer or configured_route_target_list

            vn_int_name = get_lr_internal_vn_name(obj_dict.get('uuid'))
            vn_id = None
            for vn_ref in lr_orig_dict.get('virtual_network_refs') or []:
                if (vn_ref.get('attr', {}).get(
                        'logical_router_virtual_network_type') ==
                        'InternalVirtualNetwork'):
                    vn_id = vn_ref.get('uuid')
                    break
            if vn_id is None:
                return True, ''
            ok, vn_dict = db_conn.dbe_read(
                'virtual_network',
                vn_id,
                obj_fields=['route_target_list',
                            'fq_name',
                            'uuid',
                            'parent_uuid',
                            'virtual_network_properties'])
            if not ok:
                return ok, vn_dict
            vn_rt_dict_list = vn_dict.get('route_target_list')
            vn_rt_list = []
            if vn_rt_dict_list:
                vn_rt_list = vn_rt_dict_list.get('route_target', [])
            lr_rt_list_obj = obj_dict.get('configured_route_target_list')
            lr_rt_list = []
            if lr_rt_list_obj:
                lr_rt_list = lr_rt_list_obj.get('route_target', [])

            vxlan_id_in_db = vn_dict.get('virtual_network_properties', {}).get(
                'vxlan_network_identifier')

            if(vxlan_id_in_db != obj_dict.get('vxlan_network_identifier') or
                    set(vn_rt_list) != set(lr_rt_list)):
                ok, proj_dict = db_conn.dbe_read('project',
                                                 vn_dict['parent_uuid'])
                if not ok:
                    return ok, proj_dict
                proj_obj = Project(name=vn_dict.get('fq_name')[-2],
                                   parent_type='domain',
                                   fq_name=proj_dict.get('fq_name'))

                vn_obj = VirtualNetwork(name=vn_int_name, parent_obj=proj_obj)

                if (set(vn_rt_list) != set(lr_rt_list)):
                    vn_obj.set_route_target_list(lr_rt_list_obj)

                # If vxlan_id has been set, we need to propogate it to the
                # internal VN.
                if vxlan_id_in_db != obj_dict.get('vxlan_network_identifier'):
                    prop = vn_dict.get('virtual_network_properties', {})
                    prop['vxlan_network_identifier'] =\
                        obj_dict['vxlan_network_identifier']
                    vn_obj.set_virtual_network_properties(prop)

                vn_int_dict = json.dumps(vn_obj, default=_obj_serializer_all)
                status, obj = cls.server.internal_request_update(
                    'virtual-network',
                    vn_dict['uuid'],
                    json.loads(vn_int_dict))
        return True, ''
    def pre_dbe_create(cls, tenant_name, obj_dict, db_conn):
        ok, result = cls.check_for_external_gateway(db_conn, obj_dict)
        if not ok:
            return ok, result

        ok, result = cls._ensure_lr_dci_association(obj_dict)
        if not ok:
            return ok, result

        ok, result = cls.check_port_gateway_not_in_same_network(
            db_conn, obj_dict)
        if not ok:
            return ok, result

        ok, result = cls.is_port_in_use_by_vm(obj_dict, db_conn)
        if not ok:
            return ok, result

        ok, result = cls.is_vxlan_routing_enabled(db_conn, obj_dict)
        if not ok:
            return ok, result
        vxlan_routing = result

        vxlan_id = None
        vxlan_id = cls._check_vxlan_id_in_lr(obj_dict)

        if vxlan_routing and vxlan_id:
            # If input vxlan_id is not None, that means we need to reserve it.
            # First, check if vxlan_id is set for other fq_name
            existing_fq_name = cls.vnc_zk_client.get_vn_from_id(int(vxlan_id))
            if existing_fq_name is not None:
                msg = ("Cannot set VXLAN_ID: %s, it has already been set" %
                       vxlan_id)
                return False, (400, msg)

            # Second, if vxlan_id is not None, set it in Zookeeper and set the
            # undo function for when any failures happen later.
            # But first, get the internal_vlan name using which the resource
            # in zookeeper space will be reserved.

            ok, proj_dict = db_conn.dbe_read('project',
                                             obj_dict['parent_uuid'])
            if not ok:
                return (ok, proj_dict)

            vn_int_name = get_lr_internal_vn_name(obj_dict.get('uuid'))
            proj_obj = Project(name=proj_dict.get('fq_name')[-1],
                               parent_type='domain',
                               fq_name=proj_dict.get('fq_name'))

            vn_obj = VirtualNetwork(name=vn_int_name, parent_obj=proj_obj)

            try:
                vxlan_fq_name = ':'.join(vn_obj.fq_name) + '_vxlan'
                # Now that we have the internal VN name, allocate it in
                # zookeeper only if the resource hasn't been reserved already
                cls.vnc_zk_client.alloc_vxlan_id(vxlan_fq_name, int(vxlan_id))
            except ResourceExistsError:
                msg = ("Cannot set VXLAN_ID: %s, it has already been set" %
                       vxlan_id)
                return False, (400, msg)

            def undo_vxlan_id():
                cls.vnc_zk_client.free_vxlan_id(int(vxlan_id), vxlan_fq_name)
                return True, ""

            get_context().push_undo(undo_vxlan_id)

        # Check if type of all associated BGP VPN are 'l3'
        ok, result = cls.server.get_resource_class(
            'bgpvpn').check_router_supports_vpn_type(obj_dict)
        if not ok:
            return ok, result

        # Check if we can reference the BGP VPNs
        return cls.server.get_resource_class(
            'bgpvpn').check_router_has_bgpvpn_assoc_via_network(obj_dict)