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;
  }
  private Iterable<String> executeScriptsForEvent(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      VNFCInstance vnfcInstance,
      Event event,
      VNFRecordDependency dependency)
      throws Exception {
    Map<String, String> env = getMap(virtualNetworkFunctionRecord);
    List<String> res = new ArrayList<>();
    LifecycleEvent le =
        VnfmUtils.getLifecycleEvent(virtualNetworkFunctionRecord.getLifecycle_event(), event);
    log.trace(
        "The number of scripts for "
            + virtualNetworkFunctionRecord.getName()
            + " are: "
            + le.getLifecycle_events());
    log.debug("DEPENDENCY IS: " + dependency);
    if (le != null) {
      for (String script : le.getLifecycle_events()) {
        int indexOf = script.indexOf('_');
        VNFCDependencyParameters vnfcDependencyParameters = null;
        String type = null;
        if (indexOf != -1) {
          type = script.substring(0, indexOf);
          vnfcDependencyParameters = dependency.getVnfcParameters().get(type);
        }
        if (vnfcDependencyParameters != null) {
          log.debug(
              "There are "
                  + vnfcDependencyParameters.getParameters().size()
                  + " VNFCInstanceForeign");
          for (String vnfcForeignId : vnfcDependencyParameters.getParameters().keySet()) {
            log.info("Running script: " + script + " for VNFCInstance foreign id " + vnfcForeignId);

            log.info(
                "Sending command: "
                    + script
                    + " to adding relation with type: "
                    + type
                    + " from VirtualNetworkFunctionRecord "
                    + virtualNetworkFunctionRecord.getName());

            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
            log.debug("adding floatingIp: " + vnfcInstance.getFloatingIps());
            for (Ip fip : vnfcInstance.getFloatingIps()) {
              tempEnv.put(fip.getNetName() + "_floatingIp", fip.getIp());
            }
            // Adding foreign parameters such as ip
            if (script.contains("_")) {
              // Adding foreign parameters such as ip
              Map<String, String> parameters = dependency.getParameters().get(type).getParameters();
              for (Entry<String, String> param : parameters.entrySet()) {
                tempEnv.put(type + "_" + param.getKey(), param.getValue());
              }

              Map<String, String> parametersVNFC =
                  vnfcDependencyParameters.getParameters().get(vnfcForeignId).getParameters();
              for (Entry<String, String> param : parametersVNFC.entrySet()) {
                tempEnv.put(type + "_" + param.getKey(), param.getValue());
              }
            }

            tempEnv.put("hostname", vnfcInstance.getHostname());
            tempEnv = modifyUnsafeEnvVarNames(tempEnv);
            env.putAll(tempEnv);
            log.info("The Environment Variables for script " + script + " 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;
  }
  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;
  }
  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;
  }