From be1e629393e7dda443902f0eec78be5436acb6dc Mon Sep 17 00:00:00 2001 From: Varun Prasad Date: Tue, 4 Oct 2016 04:41:07 -0400 Subject: [PATCH] RIFT-14721 Signed-off-by: Varun Prasad --- common/python/rift/mano/dts/__init__.py | 5 ++- .../rift/mano/dts/subscriber/ns_subscriber.py | 10 ++++++ .../rwnsm/rift/tasklets/rwnsmtasklet/cloud.py | 32 ++++++++++++------- rwlaunchpad/test/CMakeLists.txt | 5 +++ rwlaunchpad/test/utest_ro_account.py | 28 +++++++++++++--- 5 files changed, 64 insertions(+), 16 deletions(-) mode change 100644 => 100755 rwlaunchpad/test/utest_ro_account.py diff --git a/common/python/rift/mano/dts/__init__.py b/common/python/rift/mano/dts/__init__.py index a56fa049..20d39787 100644 --- a/common/python/rift/mano/dts/__init__.py +++ b/common/python/rift/mano/dts/__init__.py @@ -20,6 +20,9 @@ from .core import DtsHandler # Subscribers from .subscriber.core import AbstractOpdataSubscriber, AbstractConfigSubscriber from .subscriber.vnf_subscriber import VnfdCatalogSubscriber, VnfrCatalogSubscriber -from .subscriber.ns_subscriber import NsrCatalogSubscriber, NsdCatalogSubscriber +from .subscriber.ns_subscriber import ( + NsrCatalogSubscriber, + NsdCatalogSubscriber, + NsInstanceConfigSubscriber) from .subscriber.store import SubscriberStore from .subscriber.ro_account import ROAccountConfigSubscriber \ No newline at end of file diff --git a/common/python/rift/mano/dts/subscriber/ns_subscriber.py b/common/python/rift/mano/dts/subscriber/ns_subscriber.py index 1078e853..c16f7716 100644 --- a/common/python/rift/mano/dts/subscriber/ns_subscriber.py +++ b/common/python/rift/mano/dts/subscriber/ns_subscriber.py @@ -50,3 +50,13 @@ class NsdCatalogSubscriber(core.AbstractConfigSubscriber): def get_xpath(self): return "C,/nsd:nsd-catalog/nsd:nsd" + + +class NsInstanceConfigSubscriber(core.AbstractConfigSubscriber): + """ The network service descriptor DTS handler """ + + def key_name(self): + return "id" + + def get_xpath(self): + return "C,/nsr:ns-instance-config/nsr:nsr" diff --git a/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/cloud.py b/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/cloud.py index 6be37611..32efff2a 100644 --- a/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/cloud.py +++ b/rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/cloud.py @@ -177,22 +177,33 @@ class ROAccountPluginSelector(object): self._loop, callback=self.on_ro_account_change ) + self._nsr_sub = mano_dts.NsrCatalogSubscriber( + self._log, + self._dts, + self._loop, + self.handle_nsr) # The default plugin will be RwNsPlugin - self._plugin_instances = {} self._ro_plugin = self._create_plugin(self.DEFAULT_PLUGIN, None) + self.live_instances = 0 @property def ro_plugin(self): return self._ro_plugin - def on_ro_account_change(self, ro_account, action): + def handle_nsr(self, nsr, action): if action == rwdts.QueryAction.CREATE: - self._on_ro_account_added(ro_account) + self.live_instances += 1 + elif action == rwdts.QueryAction.DELETE: + self.live_instances -= 1 + + def on_ro_account_change(self, ro_account, action): + if action in [rwdts.QueryAction.CREATE, rwdts.QueryAction.UPDATE]: + self._on_ro_account_change(ro_account) elif action == rwdts.QueryAction.DELETE: self._on_ro_account_deleted(ro_account) - def _on_ro_account_added(self, ro_account): + def _on_ro_account_change(self, ro_account): self._log.debug("Got nsm plugin RO account: %s", ro_account) try: nsm_cls = self._nsm_plugins.class_by_plugin_name( @@ -205,25 +216,24 @@ class ROAccountPluginSelector(object): ) nsm_cls = self.DEFAULT_PLUGIN - self._ro_plugin = self._create_plugin(nsm_cls, ro_account) + ro_plugin = self._create_plugin(nsm_cls, ro_account) + if self.live_instances == 0: + self._ro_plugin = ro_plugin + else: + raise ValueError("Unable to change the plugin when live NS instances exists!") def _on_ro_account_deleted(self, ro_account): self._ro_plugin = None def _create_plugin(self, nsm_cls, ro_account): - # Check to see if the plugin was already instantiated - if nsm_cls in self._plugin_instances: - self._log.debug("RO account nsm plugin already instantiated. Using existing.") - return self._plugin_instances[nsm_cls] - # Otherwise, instantiate a new plugin using the cloud account self._log.debug("Instantiating new RO account using class: %s", nsm_cls) nsm_instance = nsm_cls(self._dts, self._log, self._loop, self._records_publisher, ro_account) - self._plugin_instances[nsm_cls] = nsm_instance return nsm_instance @asyncio.coroutine def register(self): yield from self._ro_sub.register() + yield from self._nsr_sub.register() diff --git a/rwlaunchpad/test/CMakeLists.txt b/rwlaunchpad/test/CMakeLists.txt index bd1a51ea..1c18e262 100644 --- a/rwlaunchpad/test/CMakeLists.txt +++ b/rwlaunchpad/test/CMakeLists.txt @@ -59,6 +59,11 @@ rift_py3test(utest_rwnsm ${CMAKE_CURRENT_SOURCE_DIR}/utest_rwnsm.py ) +rift_py3test(utest_ro_account + TEST_ARGS + ${CMAKE_CURRENT_SOURCE_DIR}/utest_ro_account.py + ) + rift_py3test(tosca_ut TEST_ARGS ${CMAKE_CURRENT_SOURCE_DIR}/tosca_ut.py diff --git a/rwlaunchpad/test/utest_ro_account.py b/rwlaunchpad/test/utest_ro_account.py old mode 100644 new mode 100755 index 6e480d40..aa485efe --- a/rwlaunchpad/test/utest_ro_account.py +++ b/rwlaunchpad/test/utest_ro_account.py @@ -114,7 +114,7 @@ class RoAccountDtsTestCase(rift.test.dts.AbstractDTSTest): mock_orch_acc = launchpadyang.ResourceOrchestrator.from_dict( {'name': 'rift-ro', 'account_type': 'rift_ro', 'rift_ro': {'rift_ro': True}}) - # Test rift-ro plugin + # Test rift-ro plugin CREATE w_xpath = "C,/rw-launchpad:resource-orchestrator" xpath = w_xpath yield from self.publisher.publish(w_xpath, xpath, mock_orch_acc) @@ -122,16 +122,36 @@ class RoAccountDtsTestCase(rift.test.dts.AbstractDTSTest): assert type(orch.ro_plugin) is cloud.RwNsPlugin - # Test Openmano plugin + # Test Openmano plugin CREATE mock_orch_acc = launchpadyang.ResourceOrchestrator.from_dict( {'name': 'openmano', 'account_type': 'openmano', - 'openmano': {'tenant_id': "abc"}}) + 'openmano': {'tenant_id': "abc", + "port": 9999, + "host": "10.64.11.77"}}) yield from self.publisher.publish(w_xpath, xpath, mock_orch_acc) yield from asyncio.sleep(5, loop=self.loop) - print (type(orch.ro_plugin)) assert type(orch.ro_plugin) is openmano_nsm.OpenmanoNsPlugin + assert orch.ro_plugin._cli_api._port == mock_orch_acc.openmano.port + assert orch.ro_plugin._cli_api._host == mock_orch_acc.openmano.host + + # Test update + mock_orch_acc.openmano.port = 9789 + mock_orch_acc.openmano.host = "10.64.11.78" + yield from self.dts.query_update("C,/rw-launchpad:resource-orchestrator", + rwdts.XactFlag.ADVISE, mock_orch_acc) + assert orch.ro_plugin._cli_api._port == mock_orch_acc.openmano.port + assert orch.ro_plugin._cli_api._host == mock_orch_acc.openmano.host + + # Test update when a live instance exists + # Exception should be thrown + orch.handle_nsr(None, rwdts.QueryAction.CREATE) + mock_orch_acc.openmano.port = 9788 + + with self.assertRaises(Exception): + yield from self.dts.query_update("C,/rw-launchpad:resource-orchestrator", + rwdts.XactFlag.ADVISE, mock_orch_acc) # Test delete yield from self.dts.query_delete("C,/rw-launchpad:resource-orchestrator", -- 2.25.1