Move _convert_to_nsx_transport_zones into nsx_utils

This routine does not need to be in the plugin class. This patch
transforms it into an utility method and adds unit tests to
increase coverage.

Change-Id: Ieacc36e729c4db90efd33944f7cf05dcb30e1de9
Closes-Bug: 1340210
This commit is contained in:
Salvatore Orlando 2014-07-10 07:25:04 -07:00
parent 1cf3b80549
commit 6902da7a8c
3 changed files with 129 additions and 50 deletions

View File

@ -13,10 +13,14 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron.api.v2 import attributes as attr
from neutron.common import exceptions as n_exc
from neutron.extensions import multiprovidernet as mpnet
from neutron.extensions import providernet as pnet
from neutron.openstack.common import log
from neutron.plugins.vmware.api_client import client
from neutron.plugins.vmware.api_client import exception as api_exc
from neutron.plugins.vmware.common import utils as vmw_utils
from neutron.plugins.vmware.dbexts import db as nsx_db
from neutron.plugins.vmware.dbexts import networkgw_db
from neutron.plugins.vmware import nsx_cluster
@ -245,3 +249,70 @@ def get_nsx_device_statuses(cluster, tenant_id):
else:
LOG.warn(_("Unable to retrieve operational status for "
"gateway devices"))
def _convert_bindings_to_nsx_transport_zones(bindings):
nsx_transport_zones_config = []
for binding in bindings:
transport_entry = {}
if binding.binding_type in [vmw_utils.NetworkTypes.FLAT,
vmw_utils.NetworkTypes.VLAN]:
transport_entry['transport_type'] = (
vmw_utils.NetworkTypes.BRIDGE)
transport_entry['binding_config'] = {}
vlan_id = binding.vlan_id
if vlan_id:
transport_entry['binding_config'] = (
{'vlan_translation': [{'transport': vlan_id}]})
else:
transport_entry['transport_type'] = binding.binding_type
transport_entry['zone_uuid'] = binding.phy_uuid
nsx_transport_zones_config.append(transport_entry)
return nsx_transport_zones_config
def _convert_segments_to_nsx_transport_zones(segments, default_tz_uuid):
nsx_transport_zones_config = []
for transport_zone in segments:
for value in [pnet.NETWORK_TYPE, pnet.PHYSICAL_NETWORK,
pnet.SEGMENTATION_ID]:
if transport_zone.get(value) == attr.ATTR_NOT_SPECIFIED:
transport_zone[value] = None
transport_entry = {}
transport_type = transport_zone.get(pnet.NETWORK_TYPE)
if transport_type in [vmw_utils.NetworkTypes.FLAT,
vmw_utils.NetworkTypes.VLAN]:
transport_entry['transport_type'] = (
vmw_utils.NetworkTypes.BRIDGE)
transport_entry['binding_config'] = {}
vlan_id = transport_zone.get(pnet.SEGMENTATION_ID)
if vlan_id:
transport_entry['binding_config'] = (
{'vlan_translation': [{'transport': vlan_id}]})
else:
transport_entry['transport_type'] = transport_type
transport_entry['zone_uuid'] = (
transport_zone[pnet.PHYSICAL_NETWORK] or default_tz_uuid)
nsx_transport_zones_config.append(transport_entry)
return nsx_transport_zones_config
def convert_to_nsx_transport_zones(
default_tz_uuid, network=None, bindings=None,
default_transport_type=None):
# Convert fields from provider request to nsx format
if (network and not attr.is_attr_set(
network.get(mpnet.SEGMENTS))):
return [{"zone_uuid": default_tz_uuid,
"transport_type": default_transport_type}]
# Convert fields from db to nsx format
if bindings:
return _convert_bindings_to_nsx_transport_zones(bindings)
# If we end up here we need to convert multiprovider segments into nsx
# transport zone configurations
return _convert_segments_to_nsx_transport_zones(
network.get(mpnet.SEGMENTS), default_tz_uuid)

View File

@ -877,56 +877,10 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
def _convert_to_nsx_transport_zones(self, cluster, network=None,
bindings=None):
nsx_transport_zones_config = []
# Convert fields from provider request to nsx format
if (network and not attr.is_attr_set(
network.get(mpnet.SEGMENTS))):
return [{"zone_uuid": cluster.default_tz_uuid,
"transport_type": cfg.CONF.NSX.default_transport_type}]
# Convert fields from db to nsx format
if bindings:
transport_entry = {}
for binding in bindings:
if binding.binding_type in [c_utils.NetworkTypes.FLAT,
c_utils.NetworkTypes.VLAN]:
transport_entry['transport_type'] = (
c_utils.NetworkTypes.BRIDGE)
transport_entry['binding_config'] = {}
vlan_id = binding.vlan_id
if vlan_id:
transport_entry['binding_config'] = (
{'vlan_translation': [{'transport': vlan_id}]})
else:
transport_entry['transport_type'] = binding.binding_type
transport_entry['zone_uuid'] = binding.phy_uuid
nsx_transport_zones_config.append(transport_entry)
return nsx_transport_zones_config
for transport_zone in network.get(mpnet.SEGMENTS):
for value in [pnet.NETWORK_TYPE, pnet.PHYSICAL_NETWORK,
pnet.SEGMENTATION_ID]:
if transport_zone.get(value) == attr.ATTR_NOT_SPECIFIED:
transport_zone[value] = None
transport_entry = {}
transport_type = transport_zone.get(pnet.NETWORK_TYPE)
if transport_type in [c_utils.NetworkTypes.FLAT,
c_utils.NetworkTypes.VLAN]:
transport_entry['transport_type'] = c_utils.NetworkTypes.BRIDGE
transport_entry['binding_config'] = {}
vlan_id = transport_zone.get(pnet.SEGMENTATION_ID)
if vlan_id:
transport_entry['binding_config'] = (
{'vlan_translation': [{'transport': vlan_id}]})
else:
transport_entry['transport_type'] = transport_type
transport_entry['zone_uuid'] = (
transport_zone[pnet.PHYSICAL_NETWORK] or
cluster.default_tz_uuid)
nsx_transport_zones_config.append(transport_entry)
return nsx_transport_zones_config
# TODO(salv-orlando): Remove this method and call nsx-utils direct
return nsx_utils.convert_to_nsx_transport_zones(
cluster.default_tz_uuid, network, bindings,
default_transport_type=cfg.CONF.NSX.default_transport_type)
def _convert_to_transport_zones_dict(self, network):
"""Converts the provider request body to multiprovider.

View File

@ -16,11 +16,14 @@
import mock
from neutron.db import api as db_api
from neutron.extensions import multiprovidernet as mpnet
from neutron.extensions import providernet as pnet
from neutron.openstack.common import uuidutils
from neutron.plugins.vmware.api_client import exception as api_exc
from neutron.plugins.vmware.common import exceptions as nsx_exc
from neutron.plugins.vmware.common import nsx_utils
from neutron.plugins.vmware.common import utils
from neutron.plugins.vmware.dbexts import models
from neutron.plugins.vmware import nsxlib
from neutron.tests import base
from neutron.tests.unit import vmware
@ -306,6 +309,57 @@ class NsxUtilsTestCase(base.BaseTestCase):
return_value=[]):
self._verify_get_nsx_sec_profile_id(None)
def test_convert_to_nsx_transport_zones_no_multiprovider(self):
test_net = {'id': 'whatever'}
results = nsx_utils.convert_to_nsx_transport_zones(
'meh_zone_uuid', test_net,
default_transport_type='meh_transport_type')
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual('meh_zone_uuid', result['zone_uuid'])
self.assertEqual('meh_transport_type', result['transport_type'])
def _verify_nsx_transport_zones(self, results):
self.assertEqual(2, len(results))
result_1 = results[0]
self.assertEqual(utils.NetworkTypes.BRIDGE,
result_1['transport_type'])
self.assertEqual([{'transport': 66}],
result_1['binding_config']['vlan_translation'])
self.assertEqual('whatever_tz_1', result_1['zone_uuid'])
result_2 = results[1]
self.assertEqual(utils.NetworkTypes.STT,
result_2['transport_type'])
self.assertNotIn('binding_config', result_2)
self.assertEqual('whatever_tz_2', result_2['zone_uuid'])
def test_convert_to_nsx_transport_zones_with_bindings(self):
binding_1 = models.TzNetworkBinding(
'whatever',
utils.NetworkTypes.VLAN,
'whatever_tz_1',
66)
binding_2 = models.TzNetworkBinding(
'whatever',
utils.NetworkTypes.STT,
'whatever_tz_2',
None)
results = nsx_utils.convert_to_nsx_transport_zones(
'meh_zone_uuid', None, bindings=[binding_1, binding_2])
self._verify_nsx_transport_zones(results)
def test_convert_to_nsx_transport_zones_with_multiprovider(self):
segments = [
{pnet.NETWORK_TYPE: utils.NetworkTypes.VLAN,
pnet.PHYSICAL_NETWORK: 'whatever_tz_1',
pnet.SEGMENTATION_ID: 66},
{pnet.NETWORK_TYPE: utils.NetworkTypes.STT,
pnet.PHYSICAL_NETWORK: 'whatever_tz_2'},
]
results = nsx_utils.convert_to_nsx_transport_zones(
'meh_zone_uuid', {'id': 'whatever_net', mpnet.SEGMENTS: segments})
self._verify_nsx_transport_zones(results)
class ClusterManagementTestCase(nsx_base.NsxlibTestCase):