// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.distributor.httpd;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import org.openqa.selenium.BuildInfo;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.grid.TemplateGridCommand;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.Role;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.local.LocalDistributor;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.Tracer;

import java.util.Collections;
import java.util.Set;
import java.util.logging.Logger;

import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_MAP_ROLE;

@AutoService(CliCommand.class)
public class DistributorServer extends TemplateGridCommand {

  private static final Logger LOG = Logger.getLogger(DistributorServer.class.getName());

  @Override
  public String getName() {
    return "distributor";
  }

  @Override
  public String getDescription() {
    return "Adds this server as the distributor in a selenium grid.";
  }

  @Override
  public Set<Role> getConfigurableRoles() {
    return ImmutableSet.of(EVENT_BUS_ROLE, HTTPD_ROLE, SESSION_MAP_ROLE);
  }

  @Override
  public Set<Object> getFlagObjects() {
    return Collections.emptySet();
  }

  @Override
  protected String getSystemPropertiesConfigPrefix() {
    return "distributor";
  }

  @Override
  protected Config getDefaultConfig() {
    return new DefaultDistributorConfig();
  }

  @Override
  protected void execute(Config config) {
    LoggingOptions loggingOptions = new LoggingOptions(config);
    Tracer tracer = loggingOptions.getTracer();

    EventBusOptions events = new EventBusOptions(config);
    EventBus bus = events.getEventBus();

    NetworkOptions networkOptions = new NetworkOptions(config);
    HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer);

    SessionMap sessions = new SessionMapOptions(config).getSessionMap();

    BaseServerOptions serverOptions = new BaseServerOptions(config);

    Distributor distributor = new LocalDistributor(
      tracer,
      bus,
      clientFactory,
      sessions,
      serverOptions.getRegistrationSecret());

    Server<?> server = new NettyServer(serverOptions, distributor);
    server.start();

    BuildInfo info = new BuildInfo();
    LOG.info(String.format(
      "Started Selenium distributor %s (revision %s): %s",
      info.getReleaseLabel(),
      info.getBuildRevision(),
      server.getUrl()));
  }
}

@@ -894,16 +894,63 @@
 ervice;%0A
+import com.google.common.collect.ImmutableMap;%0A
 import c
@@ -984,24 +984,24 @@
 mutableSet;%0A
-
 import org.o
@@ -1820,32 +1820,81 @@
 er.NettyServer;%0A
+import org.openqa.selenium.remote.http.Contents;%0A
 import org.openq
@@ -1928,16 +1928,115 @@
 Client;%0A
+import org.openqa.selenium.remote.http.HttpResponse;%0Aimport org.openqa.selenium.remote.http.Route;%0A
 import o
@@ -2401,16 +2401,78 @@
 AP_ROLE;
+%0Aimport static org.openqa.selenium.remote.http.HttpMethod.GET;
 %0A%0A@AutoS
@@ -3983,24 +3983,391 @@
 Secret());%0A%0A
+    Route handler = Route.combine(%0A      distributor,%0A      Route.matching(req -%3E GET.equals(req.getMethod()) && %22/status%22.equals(req.getUri()))%0A        .to(() -%3E req -%3E new HttpResponse()%0A          .setContent(Contents.asJson(%0A            ImmutableMap.of(%22value%22, ImmutableMap.of(%0A              %22ready%22, true,%0A              %22message%22, %22Distributor is ready%22))))));%0A%0A
     Server%3C?
@@ -4408,26 +4408,22 @@
 ptions, 
-distributo
+handle
 r);%0A    

Add `/status` end point to DistributorServer