from contextlib import contextmanager
from itertools import groupby
from operator import itemgetter
-from sys import exc_info
+# from sys import exc_info
from uuid import uuid4
from ..utils import remove_none_items
DbBaseException,
NoWimConnectedToDatacenters,
UnexpectedDatabaseError,
- WimAccountNotActive
+ WimAccountNotActive,
+ UndefinedWimConnector
)
from .wim_thread import WimThread
+# from ..http_tools.errors import Bad_Request
+from pkg_resources import iter_entry_points
class WimEngine(object):
"""Logic supporting the establishment of WAN links when NS spans across
different datacenters.
"""
- def __init__(self, persistence, logger=None, ovim=None):
+ def __init__(self, persistence, plugins, logger=None, ovim=None):
self.persist = persistence
+ self.plugins = plugins if plugins is not None else {}
self.logger = logger or logging.getLogger('openmano.wim.engine')
self.threads = {}
self.connectors = {}
self.ovim = ovim
+ def _load_plugin(self, name, type="sdn"):
+ # type can be vim or sdn
+ for v in iter_entry_points('osm_ro{}.plugins'.format(type), name):
+ self.plugins[name] = v.load()
+ if name and name not in self.plugins:
+ raise UndefinedWimConnector(type, name)
+
def create_wim(self, properties):
"""Create a new wim record according to the properties
"""
port_mapping = ((properties.get('config', {}) or {})
.pop('wim_port_mapping', {}))
+ plugin_name = "rosdn_" + properties["type"]
+ if plugin_name not in self.plugins:
+ self._load_plugin(plugin_name, type="sdn")
+
uuid = self.persist.create_wim(properties)
if port_mapping:
def create_actions(self, wan_links):
"""For an array of wan_links, create all the corresponding actions"""
- return [self.create_action(l) for l in wan_links]
+ return [self.create_action(li) for li in wan_links]
def delete_action(self, wan_link):
"""For a single wan_link create the corresponding create action"""
past"""
if instance_scenario_id:
wan_links = self.persist.get_wan_links(
- instance_scenario_id=instance_scenario_id)
- return [self.delete_action(l) for l in wan_links]
+ instance_scenario_id=instance_scenario_id, sdn='false')
+ return [self.delete_action(li) for li in wan_links]
def incorporate_actions(self, wim_actions, instance_action):
"""Make the instance action consider new WIM actions and make the WIM
"""
thread = None
try:
- thread = WimThread(self.persist, wim_account, ovim=self.ovim)
+ thread = WimThread(self.persist, self.plugins, wim_account, ovim=self.ovim)
self.threads[wim_account['uuid']] = thread
thread.start()
except: # noqa
def start_threads(self):
"""Start the threads responsible for processing WIM Actions"""
accounts = self.persist.get_wim_accounts(error_if_none=False)
- self.threads = remove_none_items(
- {a['uuid']: self._spawn_thread(a) for a in accounts})
+ thread_dict = {}
+ for account in accounts:
+ try:
+ plugin_name = "rosdn_" + account["wim"]["type"]
+ if plugin_name not in self.plugins:
+ self._load_plugin(plugin_name, type="sdn")
+ thread_dict[account["uuid"]] = self._spawn_thread(account)
+ except UndefinedWimConnector as e:
+ self.logger.error(e)
+ self.threads = remove_none_items(thread_dict)
def stop_threads(self):
"""Stop the threads responsible for processing WIM Actions"""