Skip to content

Commit

Permalink
Migrate uplinkset_mappings checks to driver init
Browse files Browse the repository at this point in the history
Move `check_uplinkset_types_constraint` and
`check_unique_lig_per_provider_constraint` from
the Synchronization periodic start to the Driver
Initialization. Therefore, those checks are
executed only once.
  • Loading branch information
stenioaraujo committed Feb 21, 2018
1 parent f490fa2 commit d0196f3
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 108 deletions.
62 changes: 62 additions & 0 deletions networking_oneview/ml2/drivers/oneview/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,65 @@ def get_uplinkset_by_type(uplinkset_mappings, net_type):
[lig_id, uplinkset_name]
)
return uplinksets_by_type


def check_uplinkset_types_constraint(oneview_client, uplinkset_mappings):
"""Check the number of uplinkset types for a provider in a LIG.
It is only possible to map one provider to at the most one uplink
of each type.
"""
for provider in uplinkset_mappings:
provider_mapping = zip(
uplinkset_mappings.get(provider)[::2],
uplinkset_mappings.get(provider)[1::2])
uplinksets_type = {}
for lig_id, ups_name in provider_mapping:
lig_mappings = uplinksets_type.setdefault(lig_id, [])
lig = oneview_client.logical_interconnect_groups.get(
lig_id
)
uplinkset = get_uplinkset_by_name_from_list(
lig.get('uplinkSets'), ups_name)
lig_mappings.append(uplinkset.get('ethernetNetworkType'))

if len(lig_mappings) != len(set(lig_mappings)):
err = (
"The provider %(provider)s has more than one "
"uplinkset of the same type in the logical "
"interconnect group %(lig_id)s."
) % {"provider": provider, "lig_id": lig_id}
LOG.error(err)
raise Exception(err)


def check_unique_lig_per_provider_constraint(uplinkset_mappings):
for provider in uplinkset_mappings:
for provider2 in uplinkset_mappings:
if provider != provider2:
provider_lig_mapping_tupples = zip(
uplinkset_mappings.get(provider)[::2],
uplinkset_mappings.get(provider)[1::2])
provider2_lig_mapping_tupples = zip(
uplinkset_mappings.get(provider2)[::2],
uplinkset_mappings.get(provider2)[1::2])
identical_mappings = (set(provider_lig_mapping_tupples) &
set(provider2_lig_mapping_tupples))
if identical_mappings:
err_message_attrs = {
"prov1": provider,
"prov2": provider2,
"identical_mappings": "\n".join(
(", ".join(mapping)
for mapping in identical_mappings)
)
}
err = (
"The providers %(prov1)s and %(prov2)s are being "
"mapped to the same Logical Interconnect Group "
"and the same Uplinkset.\n"
"The LIG ids and Uplink names are: "
"%(identical_mappings)s"
) % err_message_attrs
LOG.error(err)
raise Exception(err)
5 changes: 5 additions & 0 deletions networking_oneview/ml2/drivers/oneview/mech_oneview.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def __init__(self):
CONF.DEFAULT.flat_net_mappings)

common.check_valid_resources()
common.check_uplinkset_types_constraint(
self.oneview_client, self.uplinkset_mappings)
common.check_unique_lig_per_provider_constraint(
self.uplinkset_mappings)

self.neutron_oneview_client = neutron_oneview_client.Client(
self.oneview_client,
self.uplinkset_mappings,
Expand Down
63 changes: 0 additions & 63 deletions networking_oneview/ml2/drivers/oneview/synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ def __init__(self, oneview_client, neutron_oneview_client, connection,
self.flat_net_mappings = flat_net_mappings

def start(self):
self.check_uplinkset_types_constraint()
self.check_unique_lig_per_provider_constraint()

heartbeat = loopingcall.FixedIntervalLoopingCall(self.synchronize)
heartbeat.start(interval=CONF.DEFAULT.sync_interval, initial_delay=0)

Expand Down Expand Up @@ -72,66 +69,6 @@ def get_oneview_network(self, oneview_net_id):
except exceptions.HPOneViewException as err:
LOG.error(err)

def check_uplinkset_types_constraint(self):
"""Check the number of uplinkset types for a provider in a LIG.
It is only possible to map one provider to at the most one uplink
of each type.
"""
for provider in self.uplinkset_mappings:
provider_mapping = zip(
self.uplinkset_mappings.get(provider)[::2],
self.uplinkset_mappings.get(provider)[1::2])
uplinksets_type = {}
for lig_id, ups_name in provider_mapping:
lig_mappings = uplinksets_type.setdefault(lig_id, [])
lig = self.oneview_client.logical_interconnect_groups.get(
lig_id
)
uplinkset = common.get_uplinkset_by_name_from_list(
lig.get('uplinkSets'), ups_name)
lig_mappings.append(uplinkset.get('ethernetNetworkType'))

if len(lig_mappings) != len(set(lig_mappings)):
err = (
"The provider %(provider)s has more than one "
"uplinkset of the same type in the logical "
"interconnect group %(lig_id)s."
) % {"provider": provider, "lig_id": lig_id}
LOG.error(err)
raise Exception(err)

def check_unique_lig_per_provider_constraint(self):
for provider in self.uplinkset_mappings:
for provider2 in self.uplinkset_mappings:
if provider != provider2:
provider_lig_mapping_tupples = zip(
self.uplinkset_mappings.get(provider)[::2],
self.uplinkset_mappings.get(provider)[1::2])
provider2_lig_mapping_tupples = zip(
self.uplinkset_mappings.get(provider2)[::2],
self.uplinkset_mappings.get(provider2)[1::2])
identical_mappings = (set(provider_lig_mapping_tupples) &
set(provider2_lig_mapping_tupples))
if identical_mappings:
err_message_attrs = {
"prov1": provider,
"prov2": provider2,
"identical_mappings": "\n".join(
(", ".join(mapping)
for mapping in identical_mappings)
)
}
err = (
"The providers %(prov1)s and %(prov2)s are being "
"mapped to the same Logical Interconnect Group "
"and the same Uplinkset.\n"
"The LIG ids and Uplink names are: "
"%(identical_mappings)s"
) % err_message_attrs
LOG.error(err)
raise Exception(err)

def create_oneview_networks_from_neutron(self):
session = self.get_session()
for network, network_segment in (
Expand Down
48 changes: 48 additions & 0 deletions networking_oneview/tests/unit/ml2/drivers/oneview/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,51 @@ def test_check_server_profile_availability_unlocked(
common._check_server_profile_availability(
mock_oneview_client, mock.MagicMock()))
self.assertEqual(mock_ov.get_server_profile_state.call_count, 1)

@mock.patch.object(common, "get_oneview_client")
def test_check_unique_lig_per_provider_constraint(
self, mock_oneview_client):
success = True
try:
common.check_unique_lig_per_provider_constraint(
test_oneview_mech_driver.UPLINKSET_MAPPINGS)
except Exception:
success = False

self.assertTrue(success)

@mock.patch.object(common, "get_oneview_client")
def test_check_unique_lig_per_provider_constraint_fails(
self, mock_oneview_client):
uplinkset_mappings_err = {
'physnet': ['lig_123', 'uplinkset_flat'],
'physnet2': ['lig_123', 'uplinkset_flat']
}
self.assertRaises(
Exception, common.check_unique_lig_per_provider_constraint,
uplinkset_mappings_err
)

@mock.patch.object(common, "get_oneview_client")
def test_check_uplinkset_types_constraint(self, mock_oneview_client):
client = mock_oneview_client()
common.check_unique_lig_per_provider_constraint(
test_oneview_mech_driver.UPLINKSET_MAPPINGS)
client.logical_interconnect_groups.get.assert_called_with = (
'lig_123'
)

@mock.patch.object(common, "get_oneview_client")
def test_check_uplinkset_types_constraint_fails(self, mock_oneview_client):
client = mock_oneview_client()
client.logical_interconnect_groups.get.return_value = (
test_oneview_mech_driver.FAKE_LIG
)
uplinkset_mappings_err = {
'physnet': ['lig_123', 'uplinkset_vlan',
'lig_123', 'uplinkset_vlan']
}
self.assertRaises(
Exception, common.check_uplinkset_types_constraint,
uplinkset_mappings_err
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,12 @@ def setUp(self):
uplinkset_mappings, flat_net_mappings)

@mock.patch.object(loopingcall, 'FixedIntervalLoopingCall')
@mock.patch.object(sync, 'check_uplinkset_types_constraint')
@mock.patch.object(sync, 'check_unique_lig_per_provider_constraint')
def test_start(self, mock_check_provider, mock_check_uplinkset, mock_loop):
def test_start(self, mock_loop):
self.sync.start()
self.assertTrue(mock_check_provider.called)
self.assertTrue(mock_check_uplinkset.called)
heartbeat = mock_loop.return_value
mock_loop.assert_called_with(self.sync.synchronize)
self.assertTrue(heartbeat.start.called)

def test_check_unique_lig_per_provider_constraint(self):
success = True
try:
self.sync.check_unique_lig_per_provider_constraint()
except Exception:
success = False

self.assertTrue(success)

def test_check_unique_lig_per_provider_constraint_fails(self):
uplinkset_mappings_err = {
'physnet': ['lig_123', 'uplinkset_flat'],
'physnet2': ['lig_123', 'uplinkset_flat']
}
self.sync.uplinkset_mappings = uplinkset_mappings_err
self.assertRaises(
Exception, self.sync.check_unique_lig_per_provider_constraint
)

def test_check_uplinkset_types_constraint(self):
client = self.sync.oneview_client
self.sync.check_unique_lig_per_provider_constraint()
client.logical_interconnect_groups.get.assert_called_with = (
'lig_123'
)

def test_check_uplinkset_types_constraint_fails(self):
client = self.sync.oneview_client
client.logical_interconnect_groups.get.return_value = (
mech_test.FAKE_LIG
)
uplinkset_mappings_err = {
'physnet': ['lig_123', 'uplinkset_vlan',
'lig_123', 'uplinkset_vlan']
}
self.sync.uplinkset_mappings = uplinkset_mappings_err
self.assertRaises(
Exception, self.sync.check_uplinkset_types_constraint
)

@mock.patch.object(sync, 'recreate_connection')
@mock.patch.object(sync, 'synchronize_uplinkset_from_mapped_networks')
@mock.patch.object(sync, 'delete_unmapped_oneview_networks')
Expand Down

0 comments on commit d0196f3

Please sign in to comment.