Merge "Segregate the VSM calls from database calls in N1kv plugin"

This commit is contained in:
Jenkins 2014-05-02 20:56:17 +00:00 committed by Gerrit Code Review
commit 2b4ce6882f

View File

@ -826,45 +826,34 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
n1kvclient.update_network_segment(subnet['network_id'], body=body) n1kvclient.update_network_segment(subnet['network_id'], body=body)
n1kvclient.delete_ip_pool(subnet['id']) n1kvclient.delete_ip_pool(subnet['id'])
def _send_create_port_request(self, context, port): def _send_create_port_request(self,
context,
port,
port_count,
policy_profile,
vm_network_name):
""" """
Send create port request to VSM. Send create port request to VSM.
Create a VM network for a network and policy profile combination. Create a VM network for a network and policy profile combination.
If the VM network already exists, bind this port to the existing If the VM network already exists, bind this port to the existing
VM network and increment its port count. VM network on the VSM.
:param context: neutron api request context :param context: neutron api request context
:param port: port dictionary :param port: port dictionary
:param port_count: integer representing the number of ports in one
VM Network
:param policy_profile: object of type policy profile
:param vm_network_name: string representing the name of the VM
network
""" """
LOG.debug(_('_send_create_port_request: %s'), port) LOG.debug(_('_send_create_port_request: %s'), port)
try:
vm_network = n1kv_db_v2.get_vm_network(
context.session,
port[n1kv.PROFILE_ID],
port['network_id'])
except cisco_exceptions.VMNetworkNotFound:
policy_profile = n1kv_db_v2.get_policy_profile(
context.session, port[n1kv.PROFILE_ID])
vm_network_name = (c_const.VM_NETWORK_NAME_PREFIX +
str(port[n1kv.PROFILE_ID]) +
"_" + str(port['network_id']))
port_count = 1
n1kv_db_v2.add_vm_network(context.session,
vm_network_name,
port[n1kv.PROFILE_ID],
port['network_id'],
port_count)
n1kvclient = n1kv_client.Client() n1kvclient = n1kv_client.Client()
if port_count == 1:
n1kvclient.create_vm_network(port, n1kvclient.create_vm_network(port,
vm_network_name, vm_network_name,
policy_profile) policy_profile)
else: else:
vm_network_name = vm_network['name']
n1kvclient = n1kv_client.Client()
n1kvclient.create_n1kv_port(port, vm_network_name) n1kvclient.create_n1kv_port(port, vm_network_name)
vm_network['port_count'] += 1
n1kv_db_v2.update_vm_network_port_count(
context.session, vm_network_name, vm_network['port_count'])
def _send_update_port_request(self, port_id, mac_address, vm_network_name): def _send_update_port_request(self, port_id, mac_address, vm_network_name):
""" """
@ -880,29 +869,20 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
n1kvclient = n1kv_client.Client() n1kvclient = n1kv_client.Client()
n1kvclient.update_n1kv_port(vm_network_name, port_id, body) n1kvclient.update_n1kv_port(vm_network_name, port_id, body)
def _send_delete_port_request(self, context, id): def _send_delete_port_request(self, context, port, vm_network):
""" """
Send delete port request to VSM. Send delete port request to VSM.
Decrement the port count of the VM network after deleting the port. Delete the port on the VSM. If it is the last port on the VM Network,
If the port count reaches zero, delete the VM network. delete the VM Network.
:param context: neutron api request context :param context: neutron api request context
:param id: UUID of the port to be deleted :param port: port object which is to be deleted
:param vm_network: VM network object with which the port is associated
""" """
LOG.debug(_('_send_delete_port_request: %s'), id) LOG.debug(_('_send_delete_port_request: %s'), port['id'])
port = self.get_port(context, id)
vm_network = n1kv_db_v2.get_vm_network(context.session,
port[n1kv.PROFILE_ID],
port['network_id'])
vm_network['port_count'] -= 1
n1kv_db_v2.update_vm_network_port_count(
context.session, vm_network['name'], vm_network['port_count'])
n1kvclient = n1kv_client.Client() n1kvclient = n1kv_client.Client()
n1kvclient.delete_n1kv_port(vm_network['name'], id) n1kvclient.delete_n1kv_port(vm_network['name'], port['id'])
if vm_network['port_count'] == 0: if vm_network['port_count'] == 0:
n1kv_db_v2.delete_vm_network(context.session,
port[n1kv.PROFILE_ID],
port['network_id'])
n1kvclient.delete_vm_network(vm_network['name']) n1kvclient.delete_vm_network(vm_network['name'])
def _get_segmentation_id(self, context, id): def _get_segmentation_id(self, context, id):
@ -990,19 +970,16 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
self._process_l3_create(context, net, network['network']) self._process_l3_create(context, net, network['network'])
self._extend_network_dict_provider(context, net) self._extend_network_dict_provider(context, net)
self._extend_network_dict_profile(context, net) self._extend_network_dict_profile(context, net)
try: try:
if network_type == c_const.NETWORK_TYPE_MULTI_SEGMENT: if network_type == c_const.NETWORK_TYPE_MULTI_SEGMENT:
self._send_add_multi_segment_request(context, net['id'], self._send_add_multi_segment_request(context, net['id'],
segment_pairs) segment_pairs)
else: else:
self._send_create_network_request(context, net, segment_pairs) self._send_create_network_request(context, net, segment_pairs)
# note - exception will rollback entire transaction
except(cisco_exceptions.VSMError, except(cisco_exceptions.VSMError,
cisco_exceptions.VSMConnectionFailed): cisco_exceptions.VSMConnectionFailed):
super(N1kvNeutronPluginV2, self).delete_network(context, net['id']) super(N1kvNeutronPluginV2, self).delete_network(context, net['id'])
else: else:
# note - exception will rollback entire transaction
LOG.debug(_("Created network: %s"), net['id']) LOG.debug(_("Created network: %s"), net['id'])
return net return net
@ -1088,12 +1065,11 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
elif binding.network_type == c_const.NETWORK_TYPE_VLAN: elif binding.network_type == c_const.NETWORK_TYPE_VLAN:
n1kv_db_v2.release_vlan(session, binding.physical_network, n1kv_db_v2.release_vlan(session, binding.physical_network,
binding.segmentation_id) binding.segmentation_id)
self._process_l3_delete(context, id)
super(N1kvNeutronPluginV2, self).delete_network(context, id)
# the network_binding record is deleted via cascade from # the network_binding record is deleted via cascade from
# the network record, so explicit removal is not necessary # the network record, so explicit removal is not necessary
self._send_delete_network_request(context, network) self._send_delete_network_request(context, network)
self._process_l3_delete(context, id)
super(N1kvNeutronPluginV2, self).delete_network(context, id)
LOG.debug(_("Deleted network: %s"), id) LOG.debug(_("Deleted network: %s"), id)
def get_network(self, context, id, fields=None): def get_network(self, context, id, fields=None):
@ -1147,6 +1123,12 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
:param port: port dictionary :param port: port dictionary
:returns: port object :returns: port object
""" """
p_profile = None
port_count = None
vm_network_name = None
profile_id_set = False
# Set the network policy profile id for auto generated L3/DHCP ports
if ('device_id' in port['port'] and port['port']['device_owner'] in if ('device_id' in port['port'] and port['port']['device_owner'] in
[constants.DEVICE_OWNER_DHCP, constants.DEVICE_OWNER_ROUTER_INTF, [constants.DEVICE_OWNER_DHCP, constants.DEVICE_OWNER_ROUTER_INTF,
constants.DEVICE_OWNER_ROUTER_GW, constants.DEVICE_OWNER_ROUTER_GW,
@ -1156,11 +1138,11 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
if p_profile: if p_profile:
port['port']['n1kv:profile_id'] = p_profile['id'] port['port']['n1kv:profile_id'] = p_profile['id']
profile_id_set = False
if n1kv.PROFILE_ID in port['port']: if n1kv.PROFILE_ID in port['port']:
profile_id = port['port'].get(n1kv.PROFILE_ID) profile_id = port['port'].get(n1kv.PROFILE_ID)
profile_id_set = attributes.is_attr_set(profile_id) profile_id_set = attributes.is_attr_set(profile_id)
# Set the default policy profile id for ports if no id is set
if not profile_id_set: if not profile_id_set:
p_profile_name = c_conf.CISCO_N1K.default_policy_profile p_profile_name = c_conf.CISCO_N1K.default_policy_profile
p_profile = self._get_policy_profile_by_name(p_profile_name) p_profile = self._get_policy_profile_by_name(p_profile_name)
@ -1168,7 +1150,6 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
port['port']['n1kv:profile_id'] = p_profile['id'] port['port']['n1kv:profile_id'] = p_profile['id']
profile_id_set = True profile_id_set = True
if profile_id_set:
profile_id = self._process_policy_profile(context, profile_id = self._process_policy_profile(context,
port['port']) port['port'])
LOG.debug(_('Create port: profile_id=%s'), profile_id) LOG.debug(_('Create port: profile_id=%s'), profile_id)
@ -1178,11 +1159,41 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
port) port)
n1kv_db_v2.add_port_binding(session, pt['id'], profile_id) n1kv_db_v2.add_port_binding(session, pt['id'], profile_id)
self._extend_port_dict_profile(context, pt) self._extend_port_dict_profile(context, pt)
try:
vm_network = n1kv_db_v2.get_vm_network(
context.session,
profile_id,
pt['network_id'])
except cisco_exceptions.VMNetworkNotFound:
# Create a VM Network if no VM network exists.
vm_network_name = "%s%s_%s" % (c_const.VM_NETWORK_NAME_PREFIX,
profile_id,
pt['network_id'])
port_count = 1
n1kv_db_v2.add_vm_network(context.session,
vm_network_name,
profile_id,
pt['network_id'],
port_count)
else:
# Update port count of the VM network.
vm_network_name = vm_network['name']
port_count = vm_network['port_count'] + 1
n1kv_db_v2.update_vm_network_port_count(context.session,
vm_network_name,
port_count)
self._process_portbindings_create_and_update(context, self._process_portbindings_create_and_update(context,
port['port'], port['port'],
pt) pt)
# Extract policy profile for VM network create in VSM.
if not p_profile:
p_profile = n1kv_db_v2.get_policy_profile(session, profile_id)
try: try:
self._send_create_port_request(context, pt) self._send_create_port_request(context,
pt,
port_count,
p_profile,
vm_network_name)
except(cisco_exceptions.VSMError, except(cisco_exceptions.VSMError,
cisco_exceptions.VSMConnectionFailed): cisco_exceptions.VSMConnectionFailed):
super(N1kvNeutronPluginV2, self).delete_port(context, pt['id']) super(N1kvNeutronPluginV2, self).delete_port(context, pt['id'])
@ -1220,9 +1231,21 @@ class N1kvNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
if l3_port_check: if l3_port_check:
self.prevent_l3_port_deletion(context, id) self.prevent_l3_port_deletion(context, id)
with context.session.begin(subtransactions=True): with context.session.begin(subtransactions=True):
port = self.get_port(context, id)
vm_network = n1kv_db_v2.get_vm_network(context.session,
port[n1kv.PROFILE_ID],
port['network_id'])
vm_network['port_count'] -= 1
n1kv_db_v2.update_vm_network_port_count(context.session,
vm_network['name'],
vm_network['port_count'])
if vm_network['port_count'] == 0:
n1kv_db_v2.delete_vm_network(context.session,
port[n1kv.PROFILE_ID],
port['network_id'])
self.disassociate_floatingips(context, id) self.disassociate_floatingips(context, id)
self._send_delete_port_request(context, id)
super(N1kvNeutronPluginV2, self).delete_port(context, id) super(N1kvNeutronPluginV2, self).delete_port(context, id)
self._send_delete_port_request(context, port, vm_network)
def get_port(self, context, id, fields=None): def get_port(self, context, id, fields=None):
""" """