Example #1
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 #2
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 #3
0
  private Iterable<String> executeScriptsForEvent(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      VNFCInstance vnfcInstance,
      Event event,
      String cause)
      throws Exception {
    Map<String, String> env = getMap(virtualNetworkFunctionRecord);
    List<String> res = new LinkedList<>();
    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());
        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());
        // Add cause to the environment variables
        tempEnv.put("cause", cause);

        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 #4
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;
  }
Example #5
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 #6
0
  private String executeActionOnEMS(
      String vduHostname,
      String command,
      VirtualNetworkFunctionRecord vnfr,
      VNFCInstance vnfcInstance)
      throws Exception {
    log.trace("Sending message and waiting: " + command + " to " + vduHostname);
    log.info("Waiting answer from EMS - " + vduHostname);

    String response =
        this.vnfmHelper.sendAndReceive(command, "vnfm." + vduHostname.toLowerCase() + ".actions");

    log.debug("Received from EMS (" + vduHostname + "): " + response);

    if (response == null) {
      throw new NullPointerException("Response from EMS is null");
    }

    JsonObject jsonObject = this.parser.fromJson(response, JsonObject.class);

    if (jsonObject.get("status").getAsInt() == 0) {
      try {
        log.debug("Output from EMS (" + vduHostname + ") is: " + jsonObject.get("output"));
      } catch (Exception e) {
        e.printStackTrace();
        throw e;
      }
    } else {
      String err = jsonObject.get("err").getAsString();
      log.error(err);
      vnfcInstance.setState("error");
      saveLogToFile(
          vnfr,
          parser.fromJson(command, JsonObject.class).get("payload").getAsString(),
          vnfcInstance,
          response,
          true);
      throw new VnfmSdkException("EMS (" + vduHostname + ") had the following error: " + err);
    }
    return response;
  }
Example #7
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 #8
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 #9
0
  private Iterable<? extends String> executeScriptsForEventOnVnfr(
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      VNFCInstance vnfcInstanceRemote,
      Event event)
      throws Exception {
    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 (VirtualDeploymentUnit virtualDeploymentUnit : virtualNetworkFunctionRecord.getVdu()) {
        for (VNFCInstance vnfcInstanceLocal : virtualDeploymentUnit.getVnfc_instance()) {
          for (String script : le.getLifecycle_events()) {
            log.info(
                "Sending script: "
                    + script
                    + " to VirtualNetworkFunctionRecord: "
                    + virtualNetworkFunctionRecord.getName()
                    + " on VNFCInstance: "
                    + vnfcInstanceLocal.getId());
            Map<String, String> tempEnv = new HashMap<>();
            for (Ip ip : vnfcInstanceLocal.getIps()) {
              log.debug("Adding net: " + ip.getNetName() + " with value: " + ip.getIp());
              tempEnv.put(ip.getNetName(), ip.getIp());
            }
            log.debug("adding floatingIp: " + vnfcInstanceLocal.getFloatingIps());
            for (Ip fip : vnfcInstanceLocal.getFloatingIps()) {
              tempEnv.put(fip.getNetName() + "_floatingIp", fip.getIp());
            }

            tempEnv.put("hostname", vnfcInstanceLocal.getHostname());

            if (vnfcInstanceRemote != null) {
              // TODO what should i put here?
              for (Ip ip : vnfcInstanceRemote.getIps()) {
                log.debug("Adding net: " + ip.getNetName() + " with value: " + ip.getIp());
                tempEnv.put("removing_" + ip.getNetName(), ip.getIp());
              }
              log.debug("adding floatingIp: " + vnfcInstanceRemote.getFloatingIps());
              for (Ip fip : vnfcInstanceRemote.getFloatingIps()) {
                tempEnv.put("removing_" + fip.getNetName() + "_floatingIp", fip.getIp());
              }

              tempEnv.put("removing_" + "hostname", vnfcInstanceRemote.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(
                    vnfcInstanceLocal.getHostname(),
                    command,
                    virtualNetworkFunctionRecord,
                    vnfcInstanceLocal);
            res.add(output);

            saveLogToFile(virtualNetworkFunctionRecord, script, vnfcInstanceLocal, output);
            for (String key : tempEnv.keySet()) {
              env.remove(key);
            }
          }
        }
      }
    }
    return res;
  }
Example #10
0
  @Override
  public VirtualNetworkFunctionRecord scale(
      Action scaleInOrOut,
      VirtualNetworkFunctionRecord virtualNetworkFunctionRecord,
      VNFComponent component,
      Object scripts,
      VNFRecordDependency dependency)
      throws Exception {
    VNFCInstance vnfcInstance = (VNFCInstance) component;
    if (scaleInOrOut.ordinal() == Action.SCALE_OUT.ordinal()) {
      log.info("Created VNFComponent");
      saveScriptOnEms(vnfcInstance, scripts, virtualNetworkFunctionRecord);
      String output = "\n--------------------\n--------------------\n";
      for (String result :
          executeScriptsForEvent(virtualNetworkFunctionRecord, vnfcInstance, 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);

      if (dependency != null) {
        output = "\n--------------------\n--------------------\n";
        for (String result :
            executeScriptsForEvent(
                virtualNetworkFunctionRecord, vnfcInstance, Event.CONFIGURE, dependency)) {
          output +=
              this.parser
                  .fromJson(result, JsonObject.class)
                  .get("output")
                  .getAsString()
                  .replaceAll("\\\\n", "\n");
          output += "\n--------------------\n";
        }
        output += "\n--------------------\n";
        log.info("Executed script for CONFIGURE. Output was: \n\n" + output);
      }

      if ((vnfcInstance.getState() == null) || !vnfcInstance.getState().equals("STANDBY")) {
        if (VnfmUtils.getLifecycleEvent(
                virtualNetworkFunctionRecord.getLifecycle_event(), Event.START)
            != null) {
          output = "\n--------------------\n--------------------\n";
          for (String result :
              executeScriptsForEvent(virtualNetworkFunctionRecord, vnfcInstance, Event.START)) {
            output +=
                this.parser
                    .fromJson(result, JsonObject.class)
                    .get("output")
                    .getAsString()
                    .replaceAll("\\\\n", "\n");
            output += "\n--------------------\n";
          }
          output += "\n--------------------\n";
          log.info("Executed script for START. Output was: \n\n" + output);
        }
      }

      log.trace("HB_VERSION == " + virtualNetworkFunctionRecord.getHb_version());
      return virtualNetworkFunctionRecord;
    } else { // SCALE_IN

      String output = "\n--------------------\n--------------------\n";
      for (String result :
          executeScriptsForEventOnVnfr(
              virtualNetworkFunctionRecord, vnfcInstance, Event.SCALE_IN)) {
        output +=
            this.parser
                .fromJson(result, JsonObject.class)
                .get("output")
                .getAsString()
                .replaceAll("\\\\n", "\n");
        output += "\n--------------------\n";
      }
      output += "\n--------------------\n";
      log.info("Executed script for SCALE_IN. Output was: \n\n" + output);

      return virtualNetworkFunctionRecord;
    }
  }
Example #11
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;
  }