def create_dci_vn(cls, obj_dict, db_conn): vn_int_name = get_dci_internal_vn_name(obj_dict.get('uuid')) vn_obj = VirtualNetwork(name=vn_int_name) id_perms = IdPermsType(enable=True, user_visible=False) vn_obj.set_id_perms(id_perms) int_vn_property = VirtualNetworkType(forwarding_mode='l3') if 'data_center_interconnect_vxlan_network_identifier' in obj_dict: vni_id = obj_dict[ 'data_center_interconnect_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( 'data_center_interconnect_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 = cls.server try: return api_server.internal_request_create('virtual-network', json.loads(vn_int_dict)) except HttpError as e: return False, (e.status_code, e.content)
def create_pt_vn_refs(cls, obj_dict, db_conn): left_vn_int_name = '__contrail_pt_internal_vn_' + \ obj_dict.get('uuid') + '-left' + '__' right_vn_int_name = '__contrail_pt_internal_vn_' + \ obj_dict.get('uuid') + '-right' + '__' vn_list = [left_vn_int_name, right_vn_int_name] api_server = db_conn.get_api_server() for vn in vn_list: vn_obj = VirtualNetwork(name=vn) id_perms = IdPermsType(enable=True, user_visible=False) vn_obj.set_id_perms(id_perms) int_vn_property = VirtualNetworkType(forwarding_mode='l3') vn_obj.set_virtual_network_properties(int_vn_property) vn_int_dict = json.dumps(vn_obj, default=_obj_serializer_all) status, obj = api_server.internal_request_create( 'virtual-network', json.loads(vn_int_dict)) api_server.internal_request_ref_update( 'port-tuple', obj_dict['uuid'], 'ADD', 'virtual-network', obj['virtual-network']['uuid'], obj['virtual-network']['fq_name']) 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, vxlan_routing = cls.is_vxlan_routing_enabled(db_conn, obj_dict) if not ok: return ok, vxlan_routing ok, lr_orig_dict = db_conn.dbe_read( 'logical_router', obj_dict['uuid'], obj_fields=['virtual_network_refs']) if not ok: return ok, lr_orig_dict if (obj_dict.get('configured_route_target_list') is None and 'vxlan_network_identifier' not in obj_dict): return True, '' if vxlan_routing is True: vn_int_name = get_lr_internal_vn_name(obj_dict.get('uuid')) vn_id = None for vn_ref in lr_orig_dict['virtual_network_refs']: 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 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 _create_logical_interface(cls, dev_name, link, vlan_tag, subscriber_tag, network_name): api_server = cls.server db_conn = cls.db_conn li_fqname = cls._construct_li_fq_name(dev_name, link, vlan_tag) li_display_name = li_fqname[-1] li_display_name = li_display_name.replace("_", ":") id_perms = IdPermsType(enable=True, user_visible=True) li_obj = LogicalInterface(parent_type='physical-interface', fq_name=li_fqname, logical_interface_vlan_tag=vlan_tag, display_name=li_display_name, id_perms=id_perms) li_int_dict = json.dumps(li_obj, default=_obj_serializer_all) try: li_uuid = db_conn.fq_name_to_uuid('logical_interface', li_fqname) try: api_server.internal_request_update( 'logical-interface', li_uuid, json.loads(li_int_dict)) except HttpError as e: return False, (e.status_code, e.content) except NoIdError: try: api_server.internal_request_create( 'logical-interface', json.loads(li_int_dict)) except HttpError as e: return False, (e.status_code, e.content) # Allocate IP address for this logical interface iip_name = cls._construct_iip_fq_name(dev_name, link, vlan_tag) if subscriber_tag is not None: iip_obj = InstanceIp( name=iip_name, instance_ip_family='v4', instance_ip_subscriber_tag=subscriber_tag ) else: iip_obj = InstanceIp( name=iip_name, instance_ip_family='v4' ) nw_fq_name = ['default-domain', 'default-project', network_name] ok, nw_obj_result = cls.server.get_resource_class( 'virtual_network').locate(nw_fq_name, create_it=False) if not ok: return False, nw_obj_result nw_obj = VirtualNetwork(name=network_name) iip_obj.set_virtual_network(nw_obj) iip_obj.set_logical_interface(li_obj) iip_obj.set_id_perms(id_perms) iip_int_dict = json.dumps(iip_obj, default=_obj_serializer_all) try: iip_uuid = db_conn.fq_name_to_uuid('instance_ip', [iip_name]) try: api_server.internal_request_update( 'instance-ip', iip_uuid, json.loads(iip_int_dict)) except HttpError as e: return False, (e.status_code, e.content) except NoIdError: try: api_server.internal_request_create( 'instance-ip', json.loads(iip_int_dict)) except HttpError as e: return False, (e.status_code, e.content) 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)
def _create_logical_interface(cls, db_conn, dev_name, link, vlan_tag, subscriber_tag, network_name): li_fq_name = ['default-global-system-config', dev_name, link] li_fq_name = li_fq_name + ['%s.%s' % (link, vlan_tag)] li_display_name = li_fq_name[-1] li_display_name = li_display_name.replace("_", ":") api_server = cls.server try: db_conn.fq_name_to_uuid('logical_interface', li_fq_name) except NoIdError: li_obj = LogicalInterface(parent_type='physical-interface', fq_name=li_fq_name, logical_interface_vlan_tag=vlan_tag, display_name=li_display_name) id_perms = IdPermsType(enable=True, user_visible=False) li_obj.set_id_perms(id_perms) li_int_dict = json.dumps(li_obj, default=_obj_serializer_all) ok, li_obj_resp = api_server.internal_request_create( 'logical-interface', json.loads(li_int_dict)) if not ok: return (ok, 400, li_obj_resp) # Allocate IP address for this logical interface iip_name = "%s.%s.%s" % (dev_name, link, vlan_tag) if subscriber_tag is not None: iip_obj = InstanceIp( name=iip_name, instance_ip_family='v4', instance_ip_subscriber_tag=subscriber_tag ) else: iip_obj = InstanceIp( name=iip_name, instance_ip_family='v4' ) nw_fq_name = ['default-domain', 'default-project', network_name] nw_id = db_conn.fq_name_to_uuid('virtual_network', nw_fq_name) ok, nw_obj_result = cls.dbe_read(db_conn, 'virtual_network', nw_id, obj_fields=['fq_name']) if not ok: return ok, nw_obj_result nw_obj = VirtualNetwork(name=nw_obj_result.get('fq_name')[-1]) li_obj = LogicalInterface( fq_name=li_fq_name, parent_type='physical-interface') iip_obj.set_virtual_network(nw_obj) iip_obj.set_logical_interface(li_obj) iip_obj.set_id_perms(id_perms) iip_int_dict = json.dumps(iip_obj, default=_obj_serializer_all) ok, resp = api_server.internal_request_create( 'instance-ip', json.loads(iip_int_dict)) if not ok: return (ok, 400, resp) return True, ''