示例#1
0
  @Override
  public VirtualNetworkFunctionRecord instantiate(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      Object scripts,
      Map<String, Collection<VimInstance>> vimInstances)
      throws Exception {

    log.info(
        "Instantiation of VirtualNetworkFunctionRecord " + virtualNetworkFunctionRecord.getName());
    if (null != scripts) {
      this.saveScriptOnEms(virtualNetworkFunctionRecord, scripts);
    }

    for (VirtualDeploymentUnit virtualDeploymentUnit : virtualNetworkFunctionRecord.getVdu()) {
      for (VNFCInstance vnfcInstance : virtualDeploymentUnit.getVnfc_instance()) {
        log.debug("VNFCInstance: " + vnfcInstance);
      }
    }

    String output = "\n--------------------\n--------------------\n";
    for (String result : executeScriptsForEvent(virtualNetworkFunctionRecord, Event.INSTANTIATE)) {
      output +=
          this.parser
              .fromJson(result, JsonObject.class)
              .get("output")
              .getAsString()
              .replaceAll("\\\\n", "\n");
      output += "\n--------------------\n";
    }
    output += "\n--------------------\n";
    log.info("Executed script for INSTANTIATE. Output was: \n\n" + output);
    return virtualNetworkFunctionRecord;
  }
示例#2
0
  @Override
  public VirtualNetworkFunctionRecord heal(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      VNFCInstance component,
      String cause)
      throws Exception {

    if ("switchToStandby".equals(cause)) {
      for (VirtualDeploymentUnit virtualDeploymentUnit : virtualNetworkFunctionRecord.getVdu()) {
        for (VNFCInstance vnfcInstance : virtualDeploymentUnit.getVnfc_instance()) {
          if (vnfcInstance.getId().equals(component.getId())
              && "standby".equalsIgnoreCase(vnfcInstance.getState())) {
            log.debug("Activation of the standby component");
            if (VnfmUtils.getLifecycleEvent(
                    virtualNetworkFunctionRecord.getLifecycle_event(), Event.START)
                != null) {
              log.debug(
                  "Executed scripts for event START "
                      + this.executeScriptsForEvent(
                          virtualNetworkFunctionRecord, component, Event.START));
            }
            log.debug("Changing the status from standby to active");
            // This is inside the vnfr
            vnfcInstance.setState("ACTIVE");
            // This is a copy of the object received as parameter and modified.
            // It will be sent to the orchestrator
            component.setState("ACTIVE");
            break;
          }
        }
      }
    } else if (VnfmUtils.getLifecycleEvent(
            virtualNetworkFunctionRecord.getLifecycle_event(), Event.HEAL)
        != null) {
      if (VnfmUtils.getLifecycleEvent(virtualNetworkFunctionRecord.getLifecycle_event(), Event.HEAL)
              .getLifecycle_events()
          != null) {
        log.debug("Heal method started");
        log.info("-----------------------------------------------------------------------");
        String output = "\n--------------------\n--------------------\n";
        for (String result :
            executeScriptsForEvent(virtualNetworkFunctionRecord, component, Event.HEAL, cause)) {
          output +=
              this.parser
                  .fromJson(result, JsonObject.class)
                  .get("output")
                  .getAsString()
                  .replaceAll("\\\\n", "\n");
          output += "\n--------------------\n";
        }
        output += "\n--------------------\n";
        log.info("Executed script for HEAL. Output was: \n\n" + output);
        log.info("-----------------------------------------------------------------------");
      }
    }
    return virtualNetworkFunctionRecord;
  }
示例#3
0
  public Iterable<String> executeScriptsForEvent(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord, Event event)
      throws Exception { // TODO make it parallel
    Map<String, String> env = getMap(virtualNetworkFunctionRecord);
    Collection<String> res = new ArrayList<>();
    LifecycleEvent le =
        VnfmUtils.getLifecycleEvent(virtualNetworkFunctionRecord.getLifecycle_event(), event);

    if (le != null) {
      log.trace(
          "The number of scripts for "
              + virtualNetworkFunctionRecord.getName()
              + " are: "
              + le.getLifecycle_events());
      for (String script : le.getLifecycle_events()) {
        log.info(
            "Sending script: "
                + script
                + " to VirtualNetworkFunctionRecord: "
                + virtualNetworkFunctionRecord.getName());
        for (VirtualDeploymentUnit vdu : virtualNetworkFunctionRecord.getVdu()) {
          for (VNFCInstance vnfcInstance : vdu.getVnfc_instance()) {

            Map<String, String> tempEnv = new HashMap<>();
            for (Ip ip : vnfcInstance.getIps()) {
              log.debug("Adding net: " + ip.getNetName() + " with value: " + ip.getIp());
              tempEnv.put(ip.getNetName(), ip.getIp());
            }
            log.debug("adding floatingIp: " + vnfcInstance.getFloatingIps());
            for (Ip fip : vnfcInstance.getFloatingIps()) {
              tempEnv.put(fip.getNetName() + "_floatingIp", fip.getIp());
            }

            tempEnv.put("hostname", vnfcInstance.getHostname());
            tempEnv = modifyUnsafeEnvVarNames(tempEnv);
            env.putAll(tempEnv);
            log.info("Environment Variables are: " + env);

            String command = getJsonObject("EXECUTE", script, env).toString();
            String output =
                executeActionOnEMS(
                    vnfcInstance.getHostname(),
                    command,
                    virtualNetworkFunctionRecord,
                    vnfcInstance);
            res.add(output);

            saveLogToFile(virtualNetworkFunctionRecord, script, vnfcInstance, output);
            for (String key : tempEnv.keySet()) {
              env.remove(key);
            }
          }
        }
      }
    }
    return res;
  }
示例#4
0
 @Override
 public VirtualNetworkFunctionRecord updateSoftware(
     Script script, VirtualNetworkFunctionRecord virtualNetworkFunctionRecord) throws Exception {
   for (VirtualDeploymentUnit vdu : virtualNetworkFunctionRecord.getVdu()) {
     for (VNFCInstance vnfcInstance : vdu.getVnfc_instance()) {
       updateScript(script, virtualNetworkFunctionRecord, vnfcInstance);
     }
   }
   return virtualNetworkFunctionRecord;
 }
示例#5
0
  public void saveScriptOnEms(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord, Object scripts) throws Exception {

    log.debug("Scripts are: " + scripts.getClass().getName());

    if (scripts instanceof String) {
      String scriptLink = (String) scripts;
      log.debug("Scripts are: " + scriptLink);
      JsonObject jsonMessage = getJsonObject("CLONE_SCRIPTS", scriptLink, this.scriptPath);

      for (VirtualDeploymentUnit virtualDeploymentUnit : virtualNetworkFunctionRecord.getVdu()) {
        for (VNFCInstance vnfcInstance : virtualDeploymentUnit.getVnfc_instance()) {
          executeActionOnEMS(
              vnfcInstance.getHostname(),
              jsonMessage.toString(),
              virtualNetworkFunctionRecord,
              vnfcInstance);
        }
      }
    } else if (scripts instanceof Set) {
      Iterable<Script> scriptSet = (Set<Script>) scripts;

      for (Script script : scriptSet) {
        log.debug("Sending script encoded base64 ");
        String base64String = Base64.encodeBase64String(script.getPayload());
        log.trace("The base64 string is: " + base64String);
        JsonObject jsonMessage =
            getJsonObjectForScript("SAVE_SCRIPTS", base64String, script.getName(), this.scriptPath);
        for (VirtualDeploymentUnit virtualDeploymentUnit : virtualNetworkFunctionRecord.getVdu()) {
          for (VNFCInstance vnfcInstance : virtualDeploymentUnit.getVnfc_instance()) {
            executeActionOnEMS(
                vnfcInstance.getHostname(),
                jsonMessage.toString(),
                virtualNetworkFunctionRecord,
                vnfcInstance);
          }
        }
      }
    }
  }
 private VirtualDeploymentUnit createVDU(int suffix, VimInstance vimInstance) {
   VirtualDeploymentUnit vdu = new VirtualDeploymentUnit();
   vdu.setHostname("mocked_vdu_hostname_" + suffix);
   vdu.setHigh_availability(HighAvailability.ACTIVE_ACTIVE);
   vdu.setVm_image(
       new HashSet<String>() {
         {
           add("mocked_image");
         }
       });
   vdu.setComputation_requirement("high_requirements");
   HashSet<VNFComponent> vnfComponents = new HashSet<>();
   vnfComponents.add(new VNFComponent());
   vnfComponents.add(new VNFComponent());
   vdu.setVnfc(vnfComponents);
   HashSet<VNFCInstance> vnfc_instance = new HashSet<>();
   vnfc_instance.add(new VNFCInstance());
   vdu.setVnfc_instance(vnfc_instance);
   vdu.setLifecycle_event(new HashSet<LifecycleEvent>());
   vdu.setMonitoring_parameter(new HashSet<String>());
   vdu.setVimInstance(vimInstance);
   return vdu;
 }
示例#7
0
  public Iterable<String> executeScriptsForEvent(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      Event event,
      VNFRecordDependency dependency)
      throws Exception {
    Map<String, String> env = getMap(virtualNetworkFunctionRecord);
    LifecycleEvent le =
        VnfmUtils.getLifecycleEvent(virtualNetworkFunctionRecord.getLifecycle_event(), event);
    List<String> res = new ArrayList<>();
    if (le != null) {
      for (String script : le.getLifecycle_events()) {

        String type = null;
        if (script.contains("_")) {
          type = script.substring(0, script.indexOf('_'));
          log.info(
              "Sending command: "
                  + script
                  + " to adding relation with type: "
                  + type
                  + " from VirtualNetworkFunctionRecord "
                  + virtualNetworkFunctionRecord.getName());
        }

        for (VirtualDeploymentUnit vdu : virtualNetworkFunctionRecord.getVdu()) {
          for (VNFCInstance vnfcInstance : vdu.getVnfc_instance()) {
            if (dependency.getVnfcParameters().get(type) != null) {
              for (String vnfcId :
                  dependency.getVnfcParameters().get(type).getParameters().keySet()) {

                Map<String, String> tempEnv = new HashMap<>();

                // Adding own ips
                for (Ip ip : vnfcInstance.getIps()) {
                  log.debug("Adding net: " + ip.getNetName() + " with value: " + ip.getIp());
                  tempEnv.put(ip.getNetName(), ip.getIp());
                }

                // Adding own floating ip
                for (Ip fip : vnfcInstance.getFloatingIps()) {
                  log.debug("adding floatingIp: " + fip.getNetName() + " = " + fip.getIp());
                  tempEnv.put(fip.getNetName() + "_floatingIp", fip.getIp());
                }

                if (script.contains("_")) {
                  // Adding foreign parameters such as ip
                  log.debug("Fetching parameter from dependency of type: " + type);
                  Map<String, String> parameters =
                      dependency.getParameters().get(type).getParameters();

                  for (Map.Entry<String, String> param : parameters.entrySet()) {
                    log.debug(
                        "adding param: " + type + "_" + param.getKey() + " = " + param.getValue());
                    tempEnv.put(type + "_" + param.getKey(), param.getValue());
                  }

                  Map<String, String> parametersVNFC =
                      dependency
                          .getVnfcParameters()
                          .get(type)
                          .getParameters()
                          .get(vnfcId)
                          .getParameters();
                  for (Map.Entry<String, String> param : parametersVNFC.entrySet()) {
                    log.debug(
                        "adding param: " + type + "_" + param.getKey() + " = " + param.getValue());
                    tempEnv.put(type + "_" + param.getKey(), param.getValue());
                  }
                }

                tempEnv.put("hostname", vnfcInstance.getHostname());
                tempEnv = modifyUnsafeEnvVarNames(tempEnv);
                env.putAll(tempEnv);
                log.info("Environment Variables are: " + env);

                String command = getJsonObject("EXECUTE", script, env).toString();
                String output =
                    executeActionOnEMS(
                        vnfcInstance.getHostname(),
                        command,
                        virtualNetworkFunctionRecord,
                        vnfcInstance);
                res.add(output);

                saveLogToFile(virtualNetworkFunctionRecord, script, vnfcInstance, output);
                for (String key : tempEnv.keySet()) {
                  env.remove(key);
                }
              }
            }
          }
        }
      }
    }
    return res;
  }
示例#8
0
  public static VirtualNetworkFunctionRecord createVirtualNetworkFunctionRecord(
      VirtualNetworkFunctionDescriptor vnfd,
      String flavourKey,
      String nsr_id,
      Set<VirtualLinkRecord> vlr,
      Map<String, Collection<VimInstance>> vimInstances)
      throws NotFoundException, BadFormatException {
    VirtualNetworkFunctionRecord virtualNetworkFunctionRecord = new VirtualNetworkFunctionRecord();
    virtualNetworkFunctionRecord.setLifecycle_event_history(
        new LinkedList<HistoryLifecycleEvent>());
    virtualNetworkFunctionRecord.setParent_ns_id(nsr_id);
    virtualNetworkFunctionRecord.setName(vnfd.getName());
    virtualNetworkFunctionRecord.setType(vnfd.getType());
    Configuration configuration = new Configuration();
    if (vnfd.getConfigurations() != null) {
      configuration.setName(vnfd.getConfigurations().getName());
    } else configuration.setName(virtualNetworkFunctionRecord.getName());

    configuration.setConfigurationParameters(new HashSet<ConfigurationParameter>());
    if (vnfd.getConfigurations() != null) {
      for (ConfigurationParameter configurationParameter :
          vnfd.getConfigurations().getConfigurationParameters()) {
        ConfigurationParameter cp = new ConfigurationParameter();
        cp.setConfKey(configurationParameter.getConfKey());
        cp.setValue(configurationParameter.getValue());
        configuration.getConfigurationParameters().add(cp);
      }
    }
    virtualNetworkFunctionRecord.setConfigurations(configuration);
    virtualNetworkFunctionRecord.setCyclicDependency(vnfd.hasCyclicDependency());

    Configuration requires = new Configuration();
    requires.setName("requires");
    requires.setConfigurationParameters(new HashSet<ConfigurationParameter>());
    virtualNetworkFunctionRecord.setRequires(requires);

    if (vnfd.getRequires() != null) {
      for (String vnfdName : vnfd.getRequires().keySet()) {
        for (String key : vnfd.getRequires().get(vnfdName).getParameters()) {
          ConfigurationParameter configurationParameter = new ConfigurationParameter();
          log.debug("Adding " + key + " to requires");
          configurationParameter.setConfKey(key);
          virtualNetworkFunctionRecord
              .getRequires()
              .getConfigurationParameters()
              .add(configurationParameter);
        }
      }
    }

    Configuration provides = new Configuration();
    provides.setConfigurationParameters(new HashSet<ConfigurationParameter>());
    provides.setName("provides");
    virtualNetworkFunctionRecord.setProvides(provides);

    if (vnfd.getProvides() != null) {
      for (String key : vnfd.getProvides()) {
        ConfigurationParameter configurationParameter = new ConfigurationParameter();
        log.debug("Adding " + key + " to provides");
        configurationParameter.setConfKey(key);
        virtualNetworkFunctionRecord
            .getProvides()
            .getConfigurationParameters()
            .add(configurationParameter);
      }
    }

    //        if (vnfd.getVnfPackageLocation() != null) {
    //            VNFPackage vnfPackage = new VNFPackage();
    //            vnfPackage.setImageLink(vnfd.getVnfPackageLocation().getImageLink());
    //            vnfPackage.setScriptsLink(vnfd.getVnfPackageLocation().getScriptsLink());
    //            vnfPackage.setName(vnfd.getVnfPackageLocation().getName());
    //
    //            //TODO check for ordering
    //            vnfPackage.setScripts(new HashSet<Script>());
    //
    //            for (Script script : vnfd.getVnfPackageLocation().getScripts()) {
    //                Script s = new Script();
    //                s.setName(script.getName());
    //                s.setPayload(script.getPayload());
    //                vnfPackage.getScripts().add(s);
    //            }
    //
    //            vnfPackage.setImage(vnfd.getVnfPackageLocation().getImage());
    //        }
    virtualNetworkFunctionRecord.setPackageId(vnfd.getVnfPackageLocation());

    if (vnfd.getEndpoint() != null) {
      virtualNetworkFunctionRecord.setEndpoint(vnfd.getEndpoint());
    } else virtualNetworkFunctionRecord.setEndpoint(vnfd.getType());

    virtualNetworkFunctionRecord.setMonitoring_parameter(new HashSet<String>());
    virtualNetworkFunctionRecord.getMonitoring_parameter().addAll(vnfd.getMonitoring_parameter());
    virtualNetworkFunctionRecord.setVendor(vnfd.getVendor());
    virtualNetworkFunctionRecord.setAuto_scale_policy(new HashSet<AutoScalePolicy>());
    for (AutoScalePolicy autoScalePolicy : vnfd.getAuto_scale_policy()) {
      AutoScalePolicy newAutoScalePolicy = new AutoScalePolicy();
      newAutoScalePolicy.setName(autoScalePolicy.getName());
      newAutoScalePolicy.setType(autoScalePolicy.getType());
      newAutoScalePolicy.setCooldown(autoScalePolicy.getCooldown());
      newAutoScalePolicy.setPeriod(autoScalePolicy.getPeriod());
      newAutoScalePolicy.setComparisonOperator(autoScalePolicy.getComparisonOperator());
      newAutoScalePolicy.setThreshold(autoScalePolicy.getThreshold());
      newAutoScalePolicy.setMode(autoScalePolicy.getMode());
      newAutoScalePolicy.setActions(new HashSet<ScalingAction>());
      for (ScalingAction action : autoScalePolicy.getActions()) {
        ScalingAction newAction = new ScalingAction();
        newAction.setValue(action.getValue());
        newAction.setType(action.getType());
        if (action.getTarget() == null || action.getTarget().equals("")) {
          newAction.setTarget(vnfd.getType());
        } else {
          newAction.setTarget(action.getTarget());
        }
        newAutoScalePolicy.getActions().add(newAction);
      }
      newAutoScalePolicy.setAlarms(new HashSet<ScalingAlarm>());
      for (ScalingAlarm alarm : autoScalePolicy.getAlarms()) {
        ScalingAlarm newAlarm = new ScalingAlarm();
        newAlarm.setComparisonOperator(alarm.getComparisonOperator());
        newAlarm.setMetric(alarm.getMetric());
        newAlarm.setStatistic(alarm.getStatistic());
        newAlarm.setThreshold(alarm.getThreshold());
        newAlarm.setWeight(alarm.getWeight());
        newAutoScalePolicy.getAlarms().add(newAlarm);
      }
      virtualNetworkFunctionRecord.getAuto_scale_policy().add(newAutoScalePolicy);
    }

    // TODO mange the VirtualLinks and links...
    //
    // virtualNetworkFunctionRecord.setConnected_external_virtual_link(vnfd.getVirtual_link());

    virtualNetworkFunctionRecord.setVdu(new HashSet<VirtualDeploymentUnit>());
    for (VirtualDeploymentUnit virtualDeploymentUnit : vnfd.getVdu()) {
      VirtualDeploymentUnit vdu_new = new VirtualDeploymentUnit();
      vdu_new.setParent_vdu(virtualDeploymentUnit.getId());

      HashSet<VNFComponent> vnfComponents = new HashSet<>();
      for (VNFComponent component : virtualDeploymentUnit.getVnfc()) {
        VNFComponent component_new = new VNFComponent();
        HashSet<VNFDConnectionPoint> connectionPoints = new HashSet<>();
        for (VNFDConnectionPoint connectionPoint : component.getConnection_point()) {
          VNFDConnectionPoint connectionPoint_new = new VNFDConnectionPoint();
          connectionPoint_new.setVirtual_link_reference(
              connectionPoint.getVirtual_link_reference());
          connectionPoint_new.setType(connectionPoint.getType());
          connectionPoint_new.setFloatingIp(connectionPoint.getFloatingIp());
          connectionPoints.add(connectionPoint_new);
        }
        component_new.setConnection_point(connectionPoints);
        vnfComponents.add(component_new);
      }
      vdu_new.setVnfc(vnfComponents);
      vdu_new.setVnfc_instance(new HashSet<VNFCInstance>());
      HashSet<LifecycleEvent> lifecycleEvents = new HashSet<>();
      for (LifecycleEvent lifecycleEvent : virtualDeploymentUnit.getLifecycle_event()) {
        LifecycleEvent lifecycleEvent_new = new LifecycleEvent();
        lifecycleEvent_new.setEvent(lifecycleEvent.getEvent());
        lifecycleEvent_new.setLifecycle_events(lifecycleEvent.getLifecycle_events());
        lifecycleEvents.add(lifecycleEvent_new);
      }
      vdu_new.setLifecycle_event(lifecycleEvents);
      vdu_new.setVimInstanceName(virtualDeploymentUnit.getVimInstanceName());
      vdu_new.setHostname(virtualDeploymentUnit.getHostname());
      vdu_new.setHigh_availability(virtualDeploymentUnit.getHigh_availability());
      vdu_new.setComputation_requirement(virtualDeploymentUnit.getComputation_requirement());
      vdu_new.setScale_in_out(virtualDeploymentUnit.getScale_in_out());
      HashSet<String> monitoringParameters = new HashSet<>();
      monitoringParameters.addAll(virtualDeploymentUnit.getMonitoring_parameter());
      vdu_new.setMonitoring_parameter(monitoringParameters);
      vdu_new.setVdu_constraint(virtualDeploymentUnit.getVdu_constraint());

      // Set Faultmanagement policies
      Set<VRFaultManagementPolicy> vrFaultManagementPolicies = new HashSet<>();
      if (virtualDeploymentUnit.getFault_management_policy() != null) {
        log.debug(
            "Adding the fault management policies: "
                + virtualDeploymentUnit.getFault_management_policy());
        for (VRFaultManagementPolicy vrfmp : virtualDeploymentUnit.getFault_management_policy()) {
          vrFaultManagementPolicies.add(vrfmp);
        }
      }
      vdu_new.setFault_management_policy(vrFaultManagementPolicies);
      // Set Faultmanagement policies end

      HashSet<String> vmImages = new HashSet<>();
      vmImages.addAll(virtualDeploymentUnit.getVm_image());
      vdu_new.setVm_image(vmImages);

      vdu_new.setVirtual_network_bandwidth_resource(
          virtualDeploymentUnit.getVirtual_network_bandwidth_resource());
      vdu_new.setVirtual_memory_resource_element(
          virtualDeploymentUnit.getVirtual_memory_resource_element());
      virtualNetworkFunctionRecord.getVdu().add(vdu_new);
    }
    virtualNetworkFunctionRecord.setVersion(vnfd.getVersion());
    virtualNetworkFunctionRecord.setConnection_point(new HashSet<ConnectionPoint>());
    virtualNetworkFunctionRecord.getConnection_point().addAll(vnfd.getConnection_point());

    // TODO find a way to choose between deployment flavors and create the new one
    virtualNetworkFunctionRecord.setDeployment_flavour_key(flavourKey);
    for (VirtualDeploymentUnit virtualDeploymentUnit : vnfd.getVdu()) {
      for (VimInstance vi : vimInstances.get(virtualDeploymentUnit.getId())) {
        for (String name : virtualDeploymentUnit.getVimInstanceName()) {
          if (name.equals(vi.getName())) {
            if (!existsDeploymentFlavor(
                virtualNetworkFunctionRecord.getDeployment_flavour_key(), vi)) {
              throw new BadFormatException(
                  "no key "
                      + virtualNetworkFunctionRecord.getDeployment_flavour_key()
                      + " found in vim instance: "
                      + vi);
            }
          }
        }
      }
    }

    virtualNetworkFunctionRecord.setDescriptor_reference(vnfd.getId());
    virtualNetworkFunctionRecord.setLifecycle_event(new LinkedHashSet<LifecycleEvent>());
    HashSet<LifecycleEvent> lifecycleEvents = new HashSet<>();
    for (LifecycleEvent lifecycleEvent : vnfd.getLifecycle_event()) {
      LifecycleEvent lifecycleEvent_new = new LifecycleEvent();
      lifecycleEvent_new.setEvent(lifecycleEvent.getEvent());
      lifecycleEvent_new.setLifecycle_events(new ArrayList<String>());
      for (String event : lifecycleEvent.getLifecycle_events()) {
        lifecycleEvent_new.getLifecycle_events().add(event);
      }
      log.debug(
          "Found SCRIPTS for EVENT "
              + lifecycleEvent_new.getEvent()
              + ": "
              + lifecycleEvent_new.getLifecycle_events().size());
      lifecycleEvents.add(lifecycleEvent_new);
    }
    virtualNetworkFunctionRecord.setLifecycle_event(lifecycleEvents);
    virtualNetworkFunctionRecord.setVirtual_link(new HashSet<InternalVirtualLink>());
    HashSet<InternalVirtualLink> internalVirtualLinks = new HashSet<>();
    for (InternalVirtualLink internalVirtualLink : vnfd.getVirtual_link()) {
      InternalVirtualLink internalVirtualLink_new = new InternalVirtualLink();
      internalVirtualLink_new.setName(internalVirtualLink.getName());

      for (VirtualLinkRecord virtualLinkRecord : vlr) {
        if (virtualLinkRecord.getName().equals(internalVirtualLink_new.getName())) {
          internalVirtualLink_new.setExtId(virtualLinkRecord.getExtId());
        }
      }

      internalVirtualLink_new.setLeaf_requirement(internalVirtualLink.getLeaf_requirement());
      internalVirtualLink_new.setRoot_requirement(internalVirtualLink.getRoot_requirement());
      internalVirtualLink_new.setConnection_points_references(new HashSet<String>());
      for (String conn : internalVirtualLink.getConnection_points_references()) {
        internalVirtualLink_new.getConnection_points_references().add(conn);
      }
      internalVirtualLink_new.setQos(new HashSet<String>());
      for (String qos : internalVirtualLink.getQos()) {
        internalVirtualLink_new.getQos().add(qos);
      }
      internalVirtualLink_new.setTest_access(new HashSet<String>());
      for (String test : internalVirtualLink.getTest_access()) {
        internalVirtualLink_new.getTest_access().add(test);
      }
      internalVirtualLink_new.setConnectivity_type(internalVirtualLink.getConnectivity_type());
      internalVirtualLinks.add(internalVirtualLink_new);
    }
    virtualNetworkFunctionRecord.getVirtual_link().addAll(internalVirtualLinks);

    virtualNetworkFunctionRecord.setVnf_address(new HashSet<String>());
    virtualNetworkFunctionRecord.setStatus(Status.NULL);
    return virtualNetworkFunctionRecord;
  }
示例#9
0
  private NFVImage getImage(
      VNFPackage vnfPackage,
      VirtualNetworkFunctionDescriptor virtualNetworkFunctionDescriptor,
      String projectId)
      throws NotFoundException, PluginException, VimException, IncompatibleVNFPackage {

    Map<String, Object> metadata;
    NFVImage image = new NFVImage();
    Map<String, Object> imageDetails = new HashMap<>();
    List<String> vimInstances = new ArrayList<>();
    byte[] imageFile = null;

    YamlJsonParser yaml = new YamlJsonParser();
    metadata = yaml.parseMap(new String(this.vnfMetadata.toByteArray()));
    // Get configuration for NFVImage
    String[] REQUIRED_PACKAGE_KEYS = new String[] {"name", "image", "vim_types"};
    for (String requiredKey : REQUIRED_PACKAGE_KEYS) {
      if (!metadata.containsKey(requiredKey)) {
        throw new NotFoundException("Not found " + requiredKey + " of VNFPackage in Metadata.yaml");
      }
      if (metadata.get(requiredKey) == null) {
        throw new NullPointerException(
            "Not defined " + requiredKey + " of VNFPackage in Metadata.yaml");
      }
    }
    vnfPackage.setName((String) metadata.get("name"));
    if (metadata.containsKey("nfvo_version")) {
      String nfvo_version = (String) metadata.get("nfvo_version");
      String actualNfvoVersion = getNfvoVersion();
      if (nfvo_version.equals(actualNfvoVersion)) {
        vnfPackage.setNfvo_version(nfvo_version);
      } else {
        throw new IncompatibleVNFPackage(
            "The NFVO Version: "
                + nfvo_version
                + " specified in the Metadata"
                + " is not compatible with the this NFVOs version: "
                + actualNfvoVersion);
      }
    }
    if (metadata.containsKey("scripts-link"))
      vnfPackage.setScriptsLink((String) metadata.get("scripts-link"));
    if (metadata.containsKey("vim_types")) {
      List<String> vimTypes = (List<String>) metadata.get("vim_types");
      vnfPackage.setVimTypes(vimTypes);
    }
    if (metadata.containsKey("image")) {
      imageDetails = (Map<String, Object>) metadata.get("image");
      String[] REQUIRED_IMAGE_DETAILS = new String[] {"upload"};
      log.debug("image: " + imageDetails);
      for (String requiredKey : REQUIRED_IMAGE_DETAILS) {
        if (!imageDetails.containsKey(requiredKey)) {
          throw new NotFoundException(
              "Not found key: " + requiredKey + "of image in Metadata.yaml");
        }
        if (imageDetails.get(requiredKey) == null) {
          throw new NullPointerException(
              "Not defined value of key: " + requiredKey + " of image in Metadata.yaml");
        }
      }
      // If upload==true -> create a new Image
      if (imageDetails.get("upload").equals("true") || imageDetails.get("upload").equals("check")) {
        vnfPackage.setImageLink((String) imageDetails.get("link"));
        if (metadata.containsKey("image-config")) {
          log.debug("image-config: " + metadata.get("image-config"));
          Map<String, Object> imageConfig = (Map<String, Object>) metadata.get("image-config");
          // Check if all required keys are available
          String[] REQUIRED_IMAGE_CONFIG =
              new String[] {
                "name", "diskFormat", "containerFormat", "minCPU", "minDisk", "minRam", "isPublic"
              };
          for (String requiredKey : REQUIRED_IMAGE_CONFIG) {
            if (!imageConfig.containsKey(requiredKey)) {
              throw new NotFoundException(
                  "Not found key: " + requiredKey + " of image-config in Metadata.yaml");
            }
            if (imageConfig.get(requiredKey) == null) {
              throw new NullPointerException(
                  "Not defined value of key: " + requiredKey + " of image-config in Metadata.yaml");
            }
          }
          image.setName((String) imageConfig.get("name"));
          image.setDiskFormat(((String) imageConfig.get("diskFormat")).toUpperCase());
          image.setContainerFormat(((String) imageConfig.get("containerFormat")).toUpperCase());
          image.setMinCPU(Integer.toString((Integer) imageConfig.get("minCPU")));
          image.setMinDiskSpace((Integer) imageConfig.get("minDisk"));
          image.setMinRam((Integer) imageConfig.get("minRam"));
          image.setIsPublic(
              Boolean.parseBoolean(Integer.toString((Integer) imageConfig.get("minRam"))));
        } else {
          throw new NotFoundException(
              "The image-config is not defined. Please define it to upload a new image");
        }
      }
    } else {
      throw new NotFoundException(
          "The image details are not defined. Please define it to use the right image");
    }
    nsdUtils.fetchVimInstances(virtualNetworkFunctionDescriptor, projectId);
    if (imageDetails.get("upload").equals("true")) {
      log.debug("VNFPackageManagement: Uploading a new Image");
      if (vnfPackage.getImageLink() == null && imageFile == null) {
        throw new NotFoundException(
            "VNFPackageManagement: Neither the image link is defined nor the image file is available. Please define at least one if you want to upload a new image");
      } else if (vnfPackage.getImageLink() != null) {
        log.debug("VNFPackageManagement: Uploading a new Image by using the image link");
        for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) {
          if (vdu.getVimInstanceName() != null) {
            for (String vimName : vdu.getVimInstanceName()) {
              VimInstance vimInstance = null;

              for (VimInstance vi : vimInstanceRepository.findByProjectId(projectId)) {
                if (vimName.equals(vi.getName())) vimInstance = vi;
              }

              if (!vimInstances.contains(
                  vimInstance.getId())) { // check if we didn't already upload it
                Vim vim = vimBroker.getVim(vimInstance.getType());
                log.debug(
                    "VNFPackageManagement: Uploading a new Image to VimInstance "
                        + vimInstance.getName());
                image = vim.add(vimInstance, image, vnfPackage.getImageLink());
                if (vdu.getVm_image() == null) vdu.setVm_image(new HashSet<String>());
                vdu.getVm_image().add(image.getExtId());
                vimInstances.add(vimInstance.getId());
              }
            }
          }
        }
      } else if (imageFile != null) {
        log.debug("VNFPackageManagement: Uploading a new Image by using the image file");
        for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) {
          if (vdu.getVimInstanceName() != null) {
            for (String vimName : vdu.getVimInstanceName()) {
              VimInstance vimInstance = null;

              for (VimInstance vi : vimInstanceRepository.findByProjectId(projectId)) {
                if (vimName.equals(vi.getName())) vimInstance = vi;
              }

              if (!vimInstances.contains(
                  vimInstance.getId())) { // check if we didn't already upload it
                Vim vim = vimBroker.getVim(vimInstance.getType());
                log.debug(
                    "VNFPackageManagement: Uploading a new Image to VimInstance "
                        + vimInstance.getName());
                image = vim.add(vimInstance, image, imageFile);
                if (vdu.getVm_image() == null) vdu.setVm_image(new HashSet<String>());
                vdu.getVm_image().add(image.getExtId());
                vimInstances.add(vimInstance.getId());
              }
            }
          }
        }
      }
    } else {
      if (!imageDetails.containsKey("ids") && !imageDetails.containsKey("names")) {
        throw new NotFoundException(
            "VNFPackageManagement: Upload option 'false' or 'check' requires at least a list of ids or names to find "
                + " the right image.");
      }
      for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) {
        if (vdu.getVimInstanceName() != null) {
          if (vdu.getVimInstanceName().size() != 0) {
            for (String vimName : vdu.getVimInstanceName()) {

              VimInstance vimInstance = null;

              for (VimInstance vi : vimInstanceRepository.findByProjectId(projectId)) {
                if (vimName.equals(vi.getName())) {
                  vimInstance = vi;
                }
              }

              if (vimInstance == null) {
                throw new NotFoundException(
                    "Vim Instance with name "
                        + vimName
                        + " was not found in project: "
                        + projectId);
              }

              boolean found = false;
              // First, check for image ids
              if (imageDetails.containsKey("ids")) {
                for (NFVImage nfvImage : vimInstance.getImages()) {
                  if (((List) imageDetails.get("ids")).contains(nfvImage.getExtId())) {
                    if (!found) {
                      vdu.getVm_image().add(nfvImage.getExtId());
                      found = true;
                    } else {
                      throw new NotFoundException(
                          "VNFPackageManagement: Multiple images found with the defined list of IDs. Do not know "
                              + "which one to choose");
                    }
                  }
                }
              }

              // If no one was found, check for the names
              if (!found) {
                if (imageDetails.containsKey("names")) {
                  for (NFVImage nfvImage : vimInstance.getImages()) {
                    if (((List) imageDetails.get("names")).contains(nfvImage.getName())) {
                      if (!found) {
                        vdu.getVm_image().add(nfvImage.getExtId());
                        found = true;
                      } else {
                        throw new NotFoundException(
                            "VNFPackageManagement: Multiple images found with the same name. Do not know which one to"
                                + " choose. To avoid this, define the id");
                      }
                    }
                  }
                }
              }
              // if no image was found with the defined ids or names, the image doesn't exist
              if (!found) {
                if (imageDetails.get("upload").equals("check")) {
                  if (vnfPackage.getImageLink() == null && imageFile == null) {
                    throw new NotFoundException(
                        "VNFPackageManagement: Neither the image link is defined nor the image file is available. "
                            + "Please define at least one if you want to upload a new image");
                  } else if (vnfPackage.getImageLink() != null) {
                    log.debug(
                        "VNFPackageManagement: Uploading a new Image by using the image link");
                    if (!vimInstances.contains(
                        vimInstance.getId())) { // check if we didn't already upload it
                      Vim vim = vimBroker.getVim(vimInstance.getType());
                      log.debug(
                          "VNFPackageManagement: Uploading a new Image to VimInstance "
                              + vimInstance.getName());
                      image = vim.add(vimInstance, image, vnfPackage.getImageLink());
                      if (vdu.getVm_image() == null) {
                        vdu.setVm_image(new HashSet<String>());
                      }
                      vdu.getVm_image().add(image.getExtId());
                      vimInstances.add(vimInstance.getId());
                    }
                  } else if (imageFile != null) {
                    log.debug(
                        "VNFPackageManagement: Uploading a new Image by using the image file");
                    if (!vimInstances.contains(
                        vimInstance.getId())) { // check if we didn't already upload it
                      Vim vim = vimBroker.getVim(vimInstance.getType());
                      log.debug(
                          "VNFPackageManagement: Uploading a new Image to VimInstance "
                              + vimInstance.getName());
                      image = vim.add(vimInstance, image, imageFile);
                      if (vdu.getVm_image() == null) {
                        vdu.setVm_image(new HashSet<String>());
                      }
                      vimInstances.add(vimInstance.getId());
                      vdu.getVm_image().add(image.getExtId());
                    }
                  }
                } else {
                  throw new NotFoundException(
                      "VNFPackageManagement: Neither the defined ids nor the names were found. Use upload option "
                          + "'check' to get sure that the image will be available");
                }
              } else {
                log.debug("VNFPackageManagement: Found image");
              }
            }
          } else { // vimInstanceName is not defined, just put the name into the vdu
            List names = (List) imageDetails.get("names");
            log.debug("Adding names: " + names);
            vdu.getVm_image().addAll(names);
          }
        } else { // vimInstanceName is not defined, just put the name into the vdu
          List names = (List) imageDetails.get("names");
          log.debug("Adding names: " + names);
          vdu.getVm_image().addAll(names);
        }
      }
    }

    return image;
  }