class AddComponentAction extends AbstractUpgradeServerAction {

  @Override
  public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext)
      throws AmbariException, InterruptedException {

    Map<String, String> commandParameters = getCommandParameters();
    if (null == commandParameters || commandParameters.isEmpty()) {
      return createCommandReport(0, HostRoleStatus.FAILED, "{}", "",
          "Unable to add a new component to the cluster as there is no information on what to add.");
    }

    String clusterName = commandParameters.get("clusterName");
    Cluster cluster = getClusters().getCluster(clusterName);
    UpgradeContext upgradeContext = getUpgradeContext(cluster);

    // guard against downgrade until there is such a thing as removal of a
    // component on downgrade
    if (upgradeContext.isDowngradeAllowed() || upgradeContext.isPatchRevert()) {
      return createCommandReport(0, HostRoleStatus.SKIPPED_FAILED, "{}", "",
          "Unable to add a component during an upgrade which can be downgraded.");
    }

    String serializedJson = commandParameters.get(
        AddComponentTask.PARAMETER_SERIALIZED_ADD_COMPONENT_TASK);

    Gson gson = getGson();
    AddComponentTask task = gson.fromJson(serializedJson, AddComponentTask.class);

    final Service service;
    try {
      service = cluster.getService(task.service);
    } catch (ServiceNotFoundException snfe) {
      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", "",
          String.format(
              "%s was not installed in this cluster since %s is not an installed service.",
              task.component, task.service));
    }


    // build the list of candidate hosts
    Collection<Host> candidates = MasterHostResolver.getCandidateHosts(cluster, task.hosts,
        task.hostService, task.hostComponent);

    if (candidates.isEmpty()) {
      return createCommandReport(0, HostRoleStatus.FAILED, "{}", "", String.format(
          "Unable to add a new component to the cluster as there are no hosts which contain %s's %s",
          task.hostService, task.hostComponent));
    }

    // create the component if it doesn't exist in the service yet
    ServiceComponent serviceComponent;
    try {
      serviceComponent = service.getServiceComponent(task.component);
    } catch (ServiceComponentNotFoundException scnfe) {
      serviceComponent = service.addServiceComponent(task.component);
      serviceComponent.setDesiredState(State.INSTALLED);
    }

    StringBuilder buffer = new StringBuilder(String.format(
        "Successfully added %s's %s to the cluster", task.service, task.component)).append(
            System.lineSeparator());

    Map<String, ServiceComponentHost> existingSCHs = serviceComponent.getServiceComponentHosts();

    for (Host host : candidates) {
      if (existingSCHs.containsKey(host.getHostName())) {
        buffer.append("  ")
        .append(host.getHostName())
        .append(": ")
        .append("Already Installed")
        .append(System.lineSeparator());

        continue;
      }

      ServiceComponentHost sch = serviceComponent.addServiceComponentHost(host.getHostName());
      sch.setDesiredState(State.INSTALLED);
      sch.setState(State.INSTALLED);

      // for now, this is the easiest way to fire a topology event which
      // refreshes the information about the cluster (needed for restart
      // commands)
      sch.setVersion(StackVersionListener.UNKNOWN_VERSION);

      buffer.append("  ")
        .append(host.getHostName())
        .append(": ")
        .append("Installed")
        .append(System.lineSeparator());
    }

    Set<String> sortedHosts = candidates.stream().map(host -> host.getHostName()).collect(
        Collectors.toCollection(() -> new TreeSet<>()));

    Map<String, Object> structureOutMap = new LinkedHashMap<>();
    structureOutMap.put("service", task.service);
    structureOutMap.put("component", task.component);
    structureOutMap.put("hosts", sortedHosts);

    return createCommandReport(0, HostRoleStatus.COMPLETED, gson.toJson(structureOutMap),
        buffer.toString(), "");
  }

}