private <T extends Enum<T>> String getMessage(Map<T, String> map, T type) {
   String message = map.get(type);
   if (message == null) {
     log.warnFormat(
         "The message key {0} is missing from {1}", type.name(), EXECUTION_MESSAGES_FILE_PATH);
     message = type.name();
   }
   return message;
 }
  /**
   * This method checks if the virtual machine is running in some host. It also has the side effect
   * of storing the reference to the host inside the command.
   *
   * @return <code>true</code> if the virtual machine is running in a any host, <code>false</code>
   *     otherwise
   */
  protected boolean GetRunningOnVds() {
    // We will need the virtual machine and the status, so it is worth saving references:
    final VM vm = getVm();
    final VMStatus status = vm.getstatus();

    // If the status of the machine implies that it is not running in a host then
    // there is no need to find the id of the host:
    if (!VM.isStatusUpOrPaused(status) && status != VMStatus.NotResponding) {
      return false;
    }

    // Find the id of the host where the machine is running:
    NGuid hostId = vm.getrun_on_vds();
    if (hostId == null) {
      log.warnFormat(
          "Strange, according to the status \"{0}\" virtual machine \"{1}\" should be running in a host but it isn't.",
          status, vm.getId());
      return false;
    }

    // Find the reference to the host using the id that we got before (setting
    // the host to null is required in order to make sure that the host is
    // reloaded from the database):
    setVdsId(new Guid(hostId.toString()));
    setVds(null);
    if (getVds() == null) {
      log.warnFormat(
          "Strange, virtual machine \"{0}\" is is running in host \"{1}\" but that host can't be found.",
          vm.getId(), hostId);
      return false;
    }

    // If we are here everything went right, so the machine is running in
    // a host:
    return true;
  }
  /**
   * Adds a pair of {@code Enum} and message to the messages map. If the key is not valid, an error
   * message is logged. The key should be resolvable as an {@code Enum}, once its prefix is trimmed
   * and the searched for an {@code Enum} match by name. Possible entries of (key,value) from the
   * resource bundle:
   *
   * <pre>
   * job.ChangeVMCluster=Change VM ${VM} Cluster to ${VdsGroups}
   * step.VALIDATING=Validating
   * </pre>
   *
   * @param key The key of the pair to be added, by which the enum is searched.
   * @param value The message of the pair to be added
   * @param enumClass The enum class search for an instance which match the key
   * @param messagesMap The map whic the message should be added to
   * @param prefixLength The length of the key prefix
   */
  private <T extends Enum<T>> void addMessage(
      String key, String value, Map<T, String> messagesMap, Class<T> enumClass, int prefixLength) {

    T enumKey = null;

    try {
      enumKey = T.valueOf(enumClass, key.substring(prefixLength));
    } catch (IllegalArgumentException e) {
      log.errorFormat("Message key {0} is not valid for enum {1}", key, enumClass.getSimpleName());
      return;
    }

    if (!messagesMap.containsKey(key)) {
      messagesMap.put(enumKey, value);
    } else {
      log.warnFormat(
          "Code {0} appears more then once in {1} table.", key, enumClass.getSimpleName());
    }
  }
  private Map<String, Object> generateNetworks() {
    Map<String, Object> networks = new HashMap<String, Object>();
    NetworkQoSDao qosDao = getDbFacade().getQosDao();
    for (Network network : getParameters().getNetworks()) {
      Map<String, Object> opts = new HashMap<String, Object>();
      VdsNetworkInterface iface =
          findNetworkInterface(
              network.getName(), getParameters().getInterfaces(), getParameters().getBonds());
      String ifaceNameWithoutVlan = NetworkUtils.stripVlan(iface);
      Boolean bonded = findInterfaceByName(ifaceNameWithoutVlan).getBonded();
      String type = (bonded != null && bonded) ? "bonding" : "nic";
      opts.put(type, ifaceNameWithoutVlan);
      if (NetworkUtils.isVlan(network)) {
        opts.put("vlan", network.getVlanId().toString());
      }

      if (iface.getBootProtocol() != null) {
        addBootProtocol(opts, iface);
      }

      if (network.getMtu() == 0) {
        opts.put("mtu", NetworkUtils.getDefaultMtu().toString());
      } else {
        opts.put("mtu", String.valueOf(network.getMtu()));
      }

      opts.put("bridged", Boolean.toString(network.isVmNetwork()));
      if (network.isVmNetwork()) {
        opts.put(VdsProperties.STP, network.getStp() ? "yes" : "no");
      }

      VDS host = getDbFacade().getVdsDao().get(getParameters().getVdsId());
      Version version = host.getVdsGroupCompatibilityVersion();
      if (qosConfiguredOnInterface(iface, network) && FeatureSupported.hostNetworkQos(version)) {
        NetworkQosMapper qosMapper =
            new NetworkQosMapper(
                opts, VdsProperties.HOST_QOS_INBOUND, VdsProperties.HOST_QOS_OUTBOUND);
        qosMapper.serialize(
            iface.isQosOverridden() ? iface.getQos() : qosDao.get(network.getQosId()));
      }

      Set<Version> supportedClusterVersionsSet = host.getSupportedClusterVersionsSet();
      if (supportedClusterVersionsSet == null || supportedClusterVersionsSet.isEmpty()) {
        log.warnFormat(
            "Host {0} ({1}) doesn't contain Supported Cluster Versions, therefore 'defaultRoute'"
                + " will not be sent via the SetupNetworks",
            host.getName(), host.getId());
      } else if (FeatureSupported.defaultRoute(Collections.max(supportedClusterVersionsSet))
          && NetworkUtils.isManagementNetwork(network)
          && (iface.getBootProtocol() == NetworkBootProtocol.DHCP
              || (iface.getBootProtocol() == NetworkBootProtocol.STATIC_IP
                  && StringUtils.isNotEmpty(iface.getGateway())))) {
        opts.put(DEFAULT_ROUTE, Boolean.TRUE);
      }

      if (iface.hasCustomProperties()) {
        opts.put(VdsProperties.NETWORK_CUSTOM_PROPERTIES, iface.getCustomProperties());
      }

      networks.put(network.getName(), opts);
    }

    for (String net : getParameters().getRemovedNetworks()) {
      networks.put(net, REMOVE_OBJ);
    }

    return networks;
  }