adding external port to SDN-Assist when connected to WIM

Change-Id: Ic6902950915a8fde50d162cf425c1851e5bc001c
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
diff --git a/osm_ro/nfvo.py b/osm_ro/nfvo.py
index 4341422..4b7ecc4 100644
--- a/osm_ro/nfvo.py
+++ b/osm_ro/nfvo.py
@@ -3205,13 +3205,15 @@
 
             # --> WIM
             # TODO: use this information during network creation
-            wim_account_id = None
+            wim_account_id = wim_account_name = None
             if len(involved_datacenters) > 1 and 'uuid' in sce_net:
                 # OBS: sce_net without uuid are used internally to VNFs
                 # and the assumption is that VNFs will not be split among
                 # different datacenters
-                wim_account_id = wim_engine.find_suitable_wim_account(
+                wim_account = wim_engine.find_suitable_wim_account(
                     involved_datacenters, tenant_id)
+                wim_account_id = wim_account['uuid']
+                wim_account_name = wim_account['name']
                 wim_usage[sce_net['uuid']] = wim_account_id
             # <-- WIM
 
@@ -3303,7 +3305,7 @@
                 task_extra = {}
                 if create_network:
                     task_action = "CREATE"
-                    task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None))
+                    task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None), wim_account_name)
                     if lookfor_network:
                         task_extra["find"] = (lookfor_filter,)
                 elif lookfor_network:
@@ -5550,9 +5552,8 @@
             pci = port.get("pci")
             element["switch_port"] = port.get("switch_port")
             element["switch_mac"] = port.get("switch_mac")
-            if not pci or not (element["switch_port"] or element["switch_mac"]):
-                raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
-                                     " or 'switch_mac'", httperrors.Bad_Request)
+            if not element["switch_port"] and not element["switch_mac"]:
+                raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors.Bad_Request)
             for pci_expanded in utils.expand_brackets(pci):
                 element["pci"] = pci_expanded
                 maps.append(dict(element))
diff --git a/osm_ro/openmano_schemas.py b/osm_ro/openmano_schemas.py
index b53bbac..80795b5 100644
--- a/osm_ro/openmano_schemas.py
+++ b/osm_ro/openmano_schemas.py
@@ -1224,7 +1224,7 @@
                         "items": {
                             "type": "object",
                             "properties": {
-                                "pci": pci_extended_schema,       # pci_schema,
+                                "pci": {"OneOf": [{"type": "null"}, pci_extended_schema]},       # pci_schema,
                                 "switch_port": nameshort_schema,
                                 "switch_mac": mac_schema
                             },
diff --git a/osm_ro/utils.py b/osm_ro/utils.py
index 2afbc85..1e3a8ee 100644
--- a/osm_ro/utils.py
+++ b/osm_ro/utils.py
@@ -225,6 +225,8 @@
     :param text:
     :return:
     """
+    if text is None:
+        return (None, )
     start = text.find("[")
     end = text.find("]")
     if start < 0 or end < 0:
diff --git a/osm_ro/vim_thread.py b/osm_ro/vim_thread.py
index f7bc40b..57ced9c 100644
--- a/osm_ro/vim_thread.py
+++ b/osm_ro/vim_thread.py
@@ -160,6 +160,10 @@
             vim_config['datacenter_tenant_id'] = vim.get('datacenter_tenant_id')
             vim_config['datacenter_id'] = vim.get('datacenter_id')
 
+            # get port_mapping
+            vim_config["wim_external_ports"] = self.ovim.get_of_port_mappings(
+                db_filter={"region": vim_config['datacenter_id'], "pci": None})
+
             self.vim = vim_module[vim["type"]].vimconnector(
                 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
                 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
@@ -995,10 +999,11 @@
             # CREATE
             params = task["params"]
             action_text = "creating VIM"
-            vim_net_id = self.vim.new_network(*params)
+            vim_net_id = self.vim.new_network(*params[0:2])
 
             net_name = params[0]
             net_type = params[1]
+            wim_account_name = params[3]
 
             sdn_controller = self.vim.config.get('sdn-controller')
             if sdn_controller and (net_type == "data" or net_type == "ptp"):
@@ -1013,6 +1018,28 @@
                 action_text = "creating SDN"
                 with self.db_lock:
                     sdn_net_id = self.ovim.new_network(network)
+
+                if wim_account_name and self.vim.config["wim_external_ports"]:
+                    # add external port to connect WIM. Try with compute node __WIM:wim_name and __WIM
+                    action_text = "attaching external port to ovim network"
+                    sdn_port_name = sdn_net_id + "." + task["vim_id"]
+                    sdn_port_name = sdn_port_name[:63]
+                    sdn_port_data = {
+                        "compute_node": "__WIM:" + wim_account_name[0:58],
+                        "pci": None,
+                        "vlan": network["vlan"],
+                        "net_id": sdn_net_id,
+                        "region": self.vim["config"]["datacenter_id"],
+                        "name": sdn_port_name,
+                    }
+                    try:
+                        sdn_external_port_id = self.ovim.new_external_port(sdn_port_data)
+                    except ovimException:
+                        sdn_port_data["compute_node"] = "__WIM"
+                        sdn_external_port_id = self.ovim.new_external_port(sdn_port_data)
+                    self.logger.debug("Added sdn_external_port {} to sdn_network {}".format(sdn_external_port_id,
+                                                                                            sdn_net_id))
+
             task["status"] = "DONE"
             task["extra"]["vim_info"] = {}
             task["extra"]["sdn_net_id"] = sdn_net_id
diff --git a/osm_ro/wim/engine.py b/osm_ro/wim/engine.py
index 6ff2b4f..39defb1 100644
--- a/osm_ro/wim/engine.py
+++ b/osm_ro/wim/engine.py
@@ -322,11 +322,11 @@
             tenant (str): UUID of the OSM tenant
 
         Returns:
-            str: UUID of the WIM account that is able to connect all the
+            object with the WIM account that is able to connect all the
                  datacenters.
         """
         wim_id = self.find_common_wim(datacenter_ids, tenant)
-        return self.persist.get_wim_account_by(wim_id, tenant)['uuid']
+        return self.persist.get_wim_account_by(wim_id, tenant)
 
     def derive_wan_link(self,
                         wim_usage,