Example #1
0
  public void saveScriptOnEms(
      VNFCInstance vnfcInstance,
      Object scripts,
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord)
      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);
      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);
        executeActionOnEMS(
            vnfcInstance.getHostname(),
            jsonMessage.toString(),
            virtualNetworkFunctionRecord,
            vnfcInstance);
      }
    }
  }
Example #2
0
  @Override
  public VirtualNetworkFunctionRecord stopVNFCInstance(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord, VNFCInstance vnfcInstance)
      throws Exception {

    log.info("Stopping vnfc instance: " + vnfcInstance.getHostname());

    if (VnfmUtils.getLifecycleEvent(virtualNetworkFunctionRecord.getLifecycle_event(), Event.STOP)
        != null) {
      if (VnfmUtils.getLifecycleEvent(virtualNetworkFunctionRecord.getLifecycle_event(), Event.STOP)
              .getLifecycle_events()
          != null) {
        String output = "\n--------------------\n--------------------\n";
        for (String result :
            executeScriptsForEvent(virtualNetworkFunctionRecord, vnfcInstance, Event.STOP)) {
          output +=
              this.parser
                  .fromJson(result, JsonObject.class)
                  .get("output")
                  .getAsString()
                  .replaceAll("\\\\n", "\n");
          output += "\n--------------------\n";
        }
        output += "\n--------------------\n";
        log.info(
            "Executed script for STOP on VNFC Instance "
                + vnfcInstance.getHostname()
                + ". Output was: \n\n"
                + output);
      }
    }

    return virtualNetworkFunctionRecord;
  }
Example #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;
  }
Example #4
0
 private void updateScript(
     Script script,
     VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
     VNFCInstance vnfcInstance)
     throws Exception {
   JsonObject jsonMessage =
       getJsonObjectForScript(
           "SCRIPTS_UPDATE",
           Base64.encodeBase64String(script.getPayload()),
           script.getName(),
           scriptPath);
   executeActionOnEMS(
       vnfcInstance.getHostname(),
       jsonMessage.toString(),
       virtualNetworkFunctionRecord,
       vnfcInstance);
 }
Example #5
0
  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;
  }
Example #6
0
  private void saveLogToFile(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      String script,
      VNFCInstance vnfcInstance1,
      String output,
      boolean error)
      throws IOException {
    if (this.old > 0) {
      String path = "";
      if (!error) {
        path =
            this.scriptsLogPath
                + virtualNetworkFunctionRecord.getName()
                + "/"
                + vnfcInstance1.getHostname()
                + ".log";
      } else {
        path =
            this.scriptsLogPath
                + virtualNetworkFunctionRecord.getName()
                + "/"
                + vnfcInstance1.getHostname()
                + "-error.log";
      }
      File f = new File(path);

      if (!f.exists()) {
        f.getParentFile().mkdirs();
        f.createNewFile();
      }

      if (!error) {
        Files.write(
            Paths.get(path),
            ("Output of Script : " + script + "\n\n").getBytes(),
            StandardOpenOption.APPEND);
        Files.write(
            Paths.get(path),
            this.parser
                .fromJson(output, JsonObject.class)
                .get("output")
                .getAsString()
                .replaceAll("\\\\n", "\n")
                .getBytes(),
            StandardOpenOption.APPEND);
      } else {
        Files.write(
            Paths.get(path),
            ("Error log of Script : " + script + "\n\n").getBytes(),
            StandardOpenOption.APPEND);
        Files.write(
            Paths.get(path),
            this.parser
                .fromJson(output, JsonObject.class)
                .get("err")
                .getAsString()
                .replaceAll("\\\\n", "\n")
                .getBytes(),
            StandardOpenOption.APPEND);
      }
      Files.write(
          Paths.get(path),
          "\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~\n#########################\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n"
              .getBytes(),
          StandardOpenOption.APPEND);
    }
  }
Example #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;
  }