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 5, 2018
1 parent f4410bd commit 573ed22
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 108 deletions.
63 changes: 63 additions & 0 deletions networking_oneview/ml2/drivers/oneview/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,66 @@ def is_rack_server(server_hardware):
:return: True or False;
"""
return False if server_hardware.get('locationUri') else True


def check_uplinkset_types_constraint(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.
"""
oneview_client = get_oneview_client()
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 @@ -44,6 +44,11 @@ def initialize(self):
CONF.oneview.uplinkset_mappings)
self.flat_net_mappings = common.load_conf_option_to_dict(
CONF.oneview.flat_net_mappings)

common.check_uplinkset_types_constraint(self.uplinkset_mappings)
common.check_unique_lig_per_provider_constraint(
self.uplinkset_mappings)

self.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 @@ -40,9 +40,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=3600, initial_delay=0)

Expand Down Expand Up @@ -75,66 +72,6 @@ def _remove_inconsistence_from_db(
session, oneview_network_id=oneview_network_id
)

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
46 changes: 46 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 @@ -21,6 +21,8 @@

from networking_oneview.conf import CONF
from networking_oneview.ml2.drivers.oneview import common
from networking_oneview.tests.unit.ml2.drivers.oneview import \
test_oneview_mech_driver as mech_test


@mock.patch.object(common, 'OneViewClient')
Expand Down Expand Up @@ -63,3 +65,47 @@ def test_get_oneview_client_insecure_cafile(self, mock_oneview_client):
self.credentials["ssl_certificate"] = None
common.get_oneview_client()
mock_oneview_client.assert_called_once_with(self.credentials)

def test_check_unique_lig_per_provider_constraint(
self, mock_oneview_client):
success = True
try:
common.check_unique_lig_per_provider_constraint(
mech_test.UPLINKSET_MAPPINGS)
except Exception:
success = False

self.assertTrue(success)

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
)

def test_check_uplinkset_types_constraint(self, mock_oneview_client):
client = mock_oneview_client()
common.check_unique_lig_per_provider_constraint(
mech_test.UPLINKSET_MAPPINGS)
client.logical_interconnect_groups.get.assert_called_with = (
'lig_123'
)

def test_check_uplinkset_types_constraint_fails(self, mock_oneview_client):
client = mock_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.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, 'get_session')
def test_synchronize(self, mock_session):
functions_to_sync = 5
Expand Down

0 comments on commit 573ed22

Please sign in to comment.