Merge branch 'WIM' 13/7113/2
authortierno <alfonso.tiernosepulveda@telefonica.com>
Mon, 21 Jan 2019 09:55:46 +0000 (09:55 +0000)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Mon, 21 Jan 2019 09:55:46 +0000 (09:55 +0000)
Change-Id: Id314390ba524d22d4e1f74cfe8ef7c808bb8ac0e
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
1  2 
osm_ro/nfvo.py

diff --combined osm_ro/nfvo.py
@@@ -61,6 -61,7 +61,7 @@@ from .http_tools import errors as httpe
  from .wim.engine import WimEngine
  from .wim.persistence import WimPersistence
  from copy import deepcopy
+ from pprint import pformat
  #
  
  global global_config
@@@ -343,7 -344,7 +344,7 @@@ def get_imagelist(mydb, vnf_id, nfvo_te
      image_list = []
      vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
      for vm in vms:
 -        if vm["image_id"] not in image_list:
 +        if vm["image_id"] and vm["image_id"] not in image_list:
              image_list.append(vm["image_id"])
          if vm["image_list"]:
              vm_image_list = yaml.load(vm["image_list"])
@@@ -1069,11 -1070,6 +1070,11 @@@ def new_vnfd_v3(mydb, tenant_id, vnf_de
  
                              devices.append(device)
  
 +                if not db_vm.get("image_id"):
 +                    if not db_vm["pdu_type"]:
 +                        raise NfvoException("Not defined image for VDU")
 +                    # create a fake image
 +
                  # cloud-init
                  boot_data = {}
                  if vdu.get("cloud-init"):
@@@ -2464,41 -2460,25 +2465,41 @@@ def new_nsd_v3(mydb, tenant_id, nsd_des
                                                      str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
                                                  httperrors.Bad_Request)
  
 -                        existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
 -                                                        FROM="interfaces as i join vms on i.vm_id=vms.uuid",
 -                                                        WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
 -                                                               'external_name': get_str(iface, "vnfd-connection-point-ref",
 -                                                                                        255)})
 -                        if not existing_ifaces:
 +                        ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
 +                                                                FROM="interfaces as i join vms on i.vm_id=vms.uuid",
 +                                                                WHERE={
 +                                                                    'vnf_id': vnf_index2vnf_uuid[vnf_index],
 +                                                                    'external_name': get_str(iface, "vnfd-ingress-connection-point-ref",
 +                                                                                             255)})
 +                        if not ingress_existing_ifaces:
                              raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
 -                                                "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
 +                                                "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
                                                  "connection-point name at VNFD '{}'".format(
 -                                                    str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
 -                                                    str(iface.get("vnfd-id-ref"))[:255]),
 -                                                httperrors.Bad_Request)
 -                        interface_uuid = existing_ifaces[0]["uuid"]
 +                                str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]),
 +                                str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request)
 +
 +                        egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
 +                                                               FROM="interfaces as i join vms on i.vm_id=vms.uuid",
 +                                                               WHERE={
 +                                                                   'vnf_id': vnf_index2vnf_uuid[vnf_index],
 +                                                                   'external_name': get_str(iface, "vnfd-egress-connection-point-ref",
 +                                                                                            255)})
 +                        if not egress_existing_ifaces:
 +                            raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
 +                                                "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
 +                                                "connection-point name at VNFD '{}'".format(
 +                                str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]),
 +                                str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request)
 +
 +                        ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"]
 +                        egress_interface_uuid = egress_existing_ifaces[0]["uuid"]
                          sce_rsp_hop_uuid = str(uuid4())
                          uuid_list.append(sce_rsp_hop_uuid)
                          db_sce_rsp_hop = {
                              "uuid": sce_rsp_hop_uuid,
                              "if_order": if_order,
 -                            "interface_id": interface_uuid,
 +                            "ingress_interface_id": ingress_interface_uuid,
 +                            "egress_interface_id": egress_interface_uuid,
                              "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
                              "sce_rsp_id": sce_rsp_uuid,
                          }
@@@ -2979,8 -2959,8 +2980,8 @@@ def get_datacenter_by_name_uuid(mydb, t
  
  
  def update(d, u):
 -    '''Takes dict d and updates it with the values in dict u.'''
 -    '''It merges all depth levels'''
 +    """Takes dict d and updates it with the values in dict u.
 +       It merges all depth levels"""
      for k, v in u.iteritems():
          if isinstance(v, collections.Mapping):
              r = update(d.get(k, {}), v)
@@@ -3055,6 -3035,8 +3056,8 @@@ def create_instance(mydb, tenant_id, in
      # Auxiliary dictionaries from x to y
      sce_net2instance = {}
      net2task_id = {'scenario': {}}
+     # Mapping between local networks and WIMs
+     wim_usage = {}
  
      def ip_profile_IM2RO(ip_profile_im):
          # translate from input format to database format
                      raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), httperrors.Bad_Request)
                  if net_instance_desc.get("vim-network-name"):
                      scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"]
 +                if net_instance_desc.get("vim-network-id"):
 +                    scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"]
                  if net_instance_desc.get("name"):
                      scenario_net["name"] = net_instance_desc["name"]
                  if 'ip-profile' in net_instance_desc:
              if not involved_datacenters:
                  involved_datacenters.append(default_datacenter_id)
  
+             # --> WIM
+             # TODO: use this information during network creation
+             wim_account_id = 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(
+                     involved_datacenters, tenant_id)
+                 wim_usage[sce_net['uuid']] = wim_account_id
+             # <-- WIM
              descriptor_net = {}
              if instance_dict.get("networks") and instance_dict["networks"].get(sce_net["name"]):
                  descriptor_net = instance_dict["networks"][sce_net["name"]]
                      lookfor_network = True
                      lookfor_filter["name"] = sce_net.get("vim_network_name")
                  elif sce_net["external"]:
 -                    if sce_net['vim_id'] is not None:
 +                    if sce_net.get('vim_id'):
                          # there is a netmap at datacenter_nets database   # TODO REVISE!!!!
                          create_network = False
                          lookfor_network = True
                  sfs_created = []
                  for cp in rsp['connection_points']:
                      count = mydb.get_rows(
 -                            SELECT=('vms.count'),
 -                            FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id",
 +                            SELECT='vms.count',
 +                            FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
 +                                 "on interfaces.uuid=h.ingress_interface_id",
                              WHERE={'h.uuid': cp['uuid']})[0]['count']
                      instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
                      instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
                      for i in range(count):
                          # create sfis
                          sfi_uuid = str(uuid4())
 +                        extra_params = {
 +                            "ingress_interface_id": cp["ingress_interface_id"],
 +                            "egress_interface_id": cp["egress_interface_id"]
 +                        }
                          uuid_list.append(sfi_uuid)
                          db_sfi = {
                              "uuid": sfi_uuid,
                              "status": "SCHEDULED",
                              "item": "instance_sfis",
                              "item_id": sfi_uuid,
 -                            "extra": yaml.safe_dump({"params": "", "depends_on": [dependencies[i]]},
 +                            "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]},
                                                      default_flow_style=True, width=256)
                          }
                          sfis_created.append(task_index)
          db_instance_action["number_tasks"] = task_index
  
          # --> WIM
-         wan_links = wim_engine.derive_wan_links(db_instance_nets, tenant_id)
+         logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage))
+         wan_links = wim_engine.derive_wan_links(wim_usage, db_instance_nets, tenant_id)
          wim_actions = wim_engine.create_actions(wan_links)
          wim_actions, db_instance_action = (
              wim_engine.incorporate_actions(wim_actions, db_instance_action))
@@@ -3641,12 -3629,8 +3657,12 @@@ def instantiate_vnf(mydb, sce_vnf, para
          }
          db_instance_nets.append(db_net)
  
 +        lookfor_filter = {}
          if net.get("vim-network-name"):
 -            lookfor_filter = {"name": net["vim-network-name"]}
 +            lookfor_filter["name"] = net["vim-network-name"]
 +        if net.get("vim-network-id"):
 +            lookfor_filter["id"] = net["vim-network-id"]
 +        if lookfor_filter:
              task_action = "FIND"
              task_extra = {"params": (lookfor_filter,)}
          else:
@@@ -4539,7 -4523,7 +4555,7 @@@ def instance_action(mydb,nfvo_tenant,in
                  iface2iface = {}
                  where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
  
 -                vim_action_to_clone = mydb.get_rows(FROM="vim_actions", WHERE=where)
 +                vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where)
                  if not vim_action_to_clone:
                      raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error)
                  vim_action_to_clone = vim_action_to_clone[0]
              # {"instance_sfs": db_instance_sfs},
              # {"instance_classifications": db_instance_classifications},
              # {"instance_sfps": db_instance_sfps},
 -            {"vim_actions": db_vim_actions}
 +            {"vim_wim_actions": db_vim_actions}
          ]
          logger.debug("create_vdu done DB tables: %s",
                       yaml.safe_dump(db_tables, indent=4, default_flow_style=False))