/**
   * It desploys the VM.
   *
   * @param claudiaData
   * @param tierInstance
   * @param replica
   * @param vm
   * @throws InfrastructureException
   */
  public void deployVM(ClaudiaData claudiaData, TierInstance tierInstance, int replica, VM vm)
      throws InfrastructureException {

    log.info(
        "Deploy VM for tier "
            + tierInstance.getTier().getName()
            + " with networks "
            + tierInstance.getNetworkInstances().size()
            + " and public ip "
            + tierInstance.getTier().getFloatingip());

    claudiaClient.deployVM(claudiaData, tierInstance, replica, vm);

    List<String> ips =
        claudiaClient.getIP(
            claudiaData,
            tierInstance.getTier().getName(),
            replica,
            vm,
            tierInstance.getTier().getRegion());
    if (ips != null) {
      for (String ip : ips) {
        log.info("Ip " + ip);
      }
    } else {
      log.warn("ips null");
    }
    log.info("VM ");

    vm.setIp(ips.get(0));
  }
  public EnvironmentInstance createInfrasctuctureEnvironmentInstance(
      EnvironmentInstance environmentInstance, Set<Tier> tiers, ClaudiaData claudiaData)
      throws InfrastructureException, InvalidEntityException, EntityNotFoundException,
          AlreadyExistsEntityException {

    // Deploy MVs
    log.info(
        "Creating infrastructure for environment instance "
            + environmentInstance.getBlueprintName());

    for (Tier tier : tiers) {
      for (int numReplica = 1; numReplica <= tier.getInitialNumberInstances(); numReplica++) {
        // claudiaData.setVm(tier.getName());
        log.info("Deploying tier instance for tier " + tier.getName() + " " + tier.getRegion());
        Tier tierDB =
            tierManager.loadTierWithProductReleaseAndMetadata(
                tier.getName(), tier.getEnviromentName(), tier.getVdc());

        TierInstance tierInstance = new TierInstance();
        String name =
            generateVMName(
                environmentInstance.getBlueprintName(),
                tier.getName(),
                numReplica,
                claudiaData.getVdc());

        tierInstance.setName(name);
        tierInstance.setNumberReplica(numReplica);
        tierInstance.setVdc(claudiaData.getVdc());
        tierInstance.setStatus(Status.DEPLOYING);
        tierInstance.setTier(tierDB);
        VM vm = new VM();
        String fqn =
            claudiaData.getOrg().replace("_", ".")
                + ".customers."
                + claudiaData.getVdc()
                + ".services."
                + claudiaData.getService()
                + ".vees."
                + tier.getName()
                + ".replicas."
                + numReplica;

        String hostname =
            generateVMName(
                    claudiaData.getService(), tier.getName(), numReplica, claudiaData.getVdc())
                .toLowerCase();
        log.info("fqn " + fqn + " hostname " + hostname);
        vm.setFqn(fqn);
        vm.setHostname(hostname);
        tierInstance.setVM(vm);

        log.info("Deploy networks if required");

        log.info("Deploying tier instance for tier " + tier.getName() + " " + tier.getRegion());
        this.deployNetworks(claudiaData, tierInstance);
        log.info(
            "Number of networks "
                + tierInstance.getNetworkInstances().size()
                + " floatin ip "
                + tierInstance.getTier().getFloatingip());

        log.info("Inserting in database ");
        tierInstance =
            insertTierInstanceBD(
                claudiaData, environmentInstance.getEnvironment().getName(), tierInstance);
        log.info(
            "Return: Number of networks "
                + tierInstance.getNetworkInstances().size()
                + " floating ip "
                + tierInstance.getTier().getFloatingip());
        environmentInstance.addTierInstance(tierInstance);
        environmentInstanceDao.update(environmentInstance);

        try {
          tierInstanceManager.update(
              claudiaData, environmentInstance.getEnvironment().getName(), tierInstance);
        } catch (Exception e) {
          log.error("Error deploying a VM: " + e.getMessage());
          environmentInstance.setStatus(Status.ERROR);
          throw new InfrastructureException(e.getMessage());
        }

        log.info(
            "Tier instance name "
                + environmentInstance.getBlueprintName()
                + "-"
                + tier.getName()
                + "-"
                + numReplica);
        deployVM(claudiaData, tierInstance, numReplica, vm);

        tierInstance.setVM(vm);

        try {
          log.info("Inserting in database ");
          // tierInstance = insertTierInstanceBD(tierInstance);
          tierInstance.setStatus(Status.DEPLOYED);
          tierInstanceManager.update(
              claudiaData, environmentInstance.getEnvironment().getName(), tierInstance);
        } catch (EntityNotFoundException e) {
          log.info(
              "Entitiy NOt found: Tier " + tierInstance.getTier().getName() + " " + e.getMessage());
          throw new InfrastructureException(e);
        } catch (InvalidEntityException e) {
          throw new InfrastructureException(e);
        } catch (AlreadyExistsEntityException e) {
          throw new InfrastructureException(e);
        } catch (Exception e) {
          throw new InfrastructureException(e);
        }
      }
    }
    return environmentInstance;
  }