@VisibleForTesting
  void validateServicesAndComponents() {
    Stack stack = state.getStack();
    Map<String, Map<String, Set<String>>> newServices = new LinkedHashMap<>();
    Map<String, Map<String, Multiset<String>>> withAllHosts = new LinkedHashMap<>();

    Set<String> existingServices = cluster.getServices().keySet();

    // process service declarations
    for (Service service : request.getServices()) {
      String serviceName = service.getName();

      CHECK.checkArgument(stack.getServices().contains(serviceName),
        "Unknown service %s in %s", service, stack);
      CHECK.checkArgument(!existingServices.contains(serviceName),
        "Service %s already exists in cluster %s", serviceName, cluster.getClusterName());

      newServices.computeIfAbsent(serviceName, __ -> new HashMap<>());
    }

    // process component declarations
    for (Component requestedComponent : request.getComponents()) {
      String componentName = requestedComponent.getName();
      String serviceName = stack.getServiceForComponent(componentName);

      CHECK.checkArgument(serviceName != null,
        "No service found for component %s in %s", componentName, stack);
      CHECK.checkArgument(!existingServices.contains(serviceName),
        "Service %s (for component %s) already exists in cluster %s", serviceName, componentName, cluster.getClusterName());

      List<String> hosts = requestedComponent.getHosts().stream().map(Host::getFqdn).collect(toList());
      newServices.computeIfAbsent(serviceName, __ -> new HashMap<>())
        .computeIfAbsent(componentName, __ -> new HashSet<>())
        .addAll(hosts);
      withAllHosts.computeIfAbsent(serviceName, __ -> new HashMap<>())
        .computeIfAbsent(componentName, __ -> HashMultiset.create())
        .addAll(hosts);
    }

    CHECK.checkArgument(!newServices.isEmpty(), "Request should have at least one new service or component to be added");

    newServices.forEach(
      (service, components) -> components.forEach(
        (component, hosts) -> {
          Multiset<String> allHosts = withAllHosts.get(service).get(component);
          Multisets.removeOccurrences(allHosts, hosts);
          CHECK.checkArgument(allHosts.isEmpty(), "Some hosts appear multiple times for the same component (%s) in the request: %s", component, allHosts);
        }
      )
    );

    state = state.withNewServices(newServices);
  }
Create an {@link AddServiceInfo} based on the validated request.
ambari-server/src/main/java/org/apache/ambari/server/topology/addservice/RequestValidator.java