/**
   * It validates the security groups.
   *
   * @param claudiaData
   * @param tierDto
   * @throws InfrastructureException
   * @throws QuotaExceededException
   */
  public void validateSecurityGroups(ClaudiaData claudiaData, TierDto tierDto)
      throws InfrastructureException, QuotaExceededException {

    Map<String, Limits> limits = new HashMap<String, Limits>();

    String region = tierDto.getRegion();
    if (!limits.containsKey(region)) {
      try {
        limits.put(region, quotaClient.getLimits(claudiaData, region));
      } catch (InfrastructureException e) {
        throw new InfrastructureException("Failed in getLimits " + e.getMessage());
      }
    }

    Limits limitsRegion = limits.get(region);

    if (limitsRegion.checkTotalSecurityGroupsUsed()) {
      if (1 + limitsRegion.getTotalSecurityGroups() > limitsRegion.getMaxSecurityGroups()) {
        throw new QuotaExceededException(
            "max number of security groups exceeded: " + limitsRegion.getMaxSecurityGroups());
      }
    }
  }
  /**
   * It deploys the required networks.
   *
   * @param data
   * @param tierInstance
   * @throws InvalidEntityException
   * @throws InfrastructureException
   * @throws EntityNotFoundException
   */
  public void deployNetworks(ClaudiaData data, TierInstance tierInstance)
      throws InvalidEntityException, InfrastructureException, EntityNotFoundException,
          AlreadyExistsEntityException {
    Tier tier = tierInstance.getTier();
    tier =
        tierManager.loadTierWithNetworks(tier.getName(), data.getVdc(), tier.getEnviromentName());
    // Creating networks...
    log.info(
        "Deploying network for tier instance "
            + tierInstance.getName()
            + " "
            + tier.getNetworks().size()
            + " region "
            + tier.getRegion());
    List<Network> networkToBeDeployed = new ArrayList<Network>();
    for (Network network : tier.getNetworks()) {
      log.info("Network to be added " + network.getNetworkName());
      if (network.getNetworkName().equals("Internet")) {

        tier.setFloatingip("true");
        tier = tierManager.update(tier);
        tierInstance.update(tier);
      } else {
        networkToBeDeployed.add(network);
      }
    }

    for (Network network : networkToBeDeployed) {
      log.info(
          "Network instance to be deployed: "
              + network.getNetworkName()
              + " vdc "
              + data.getVdc()
              + " "
              + network.getRegion()
              + " and federated "
              + network.getfederatedNetwork());
      network = networkManager.load(network.getNetworkName(), data.getVdc(), network.getRegion());
      NetworkInstance networkInst = network.toNetworkInstance();
      log.info(
          "Network instance to be deployed: "
              + network.getNetworkName()
              + " vdc "
              + data.getVdc()
              + " region "
              + networkInst.getRegionName()
              + " and networkInst "
              + network.getfederatedNetwork());

      if (networkInstanceManager.exists(data, networkInst, tier.getRegion())) {
        log.info("the network inst " + networkInst.getNetworkName() + " already exists");
        networkInst =
            networkInstanceManager.load(networkInst.getNetworkName(), data, tier.getRegion());
      } else {
        try {
          log.info("the network inst " + networkInst.getNetworkName() + " do not exists");
          networkInst =
              networkInstanceManager.create(data, networkInst, tierInstance.getTier().getRegion());
        } catch (AlreadyExistsEntityException e2) {
          throw new InvalidEntityException(network);
        } catch (InfrastructureException e) {
          String mens =
              "Error to deploy a network " + network.getNetworkName() + " :" + e.getMessage();
          throw new InfrastructureException(mens);
        }
      }
      log.info("Adding network to tier isntance " + networkInst.getNetworkName());
      tierInstance.addNetworkInstance(networkInst);
    }
  }