// Copyright (C) 2018 The Android Open Source Project
//
// Licensed 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 com.google.gerrit.server.git;

import static com.google.common.base.Preconditions.checkState;

import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.gerrit.server.logging.TraceContext;
import java.util.List;
import java.util.Optional;
import org.eclipse.jgit.transport.FetchV2Request;
import org.eclipse.jgit.transport.LsRefsV2Request;
import org.eclipse.jgit.transport.ProtocolV2Hook;

/**
 * Git hook for ls-refs and fetch that enables Gerrit request tracing if the user sets the 'trace'
 * server option.
 *
 * <p>This hook is only invoked if Git protocol v2 is used.
 *
 * <p>If the 'trace' server option is specified without value, this means without providing a trace
 * ID, a trace ID is generated, but it's not returned to the client. Hence users are advised to
 * always provide a trace ID.
 */
public class TracingHook implements ProtocolV2Hook, AutoCloseable {
  private TraceContext traceContext;

  @Override
  public void onLsRefs(LsRefsV2Request req) {
    maybeStartTrace(req.getServerOptions());
  }

  @Override
  public void onFetch(FetchV2Request req) {
    maybeStartTrace(req.getServerOptions());
  }

  @Override
  public void close() {
    if (traceContext != null) {
      traceContext.close();
    }
  }

  /**
   * Starts request tracing if 'trace' server option is set.
   *
   * @param serverOptionList list of provided server options
   */
  private void maybeStartTrace(List<String> serverOptionList) {
    checkState(traceContext == null, "Trace was already started.");

    Optional<String> traceOption = parseTraceOption(serverOptionList);
    traceContext =
        TraceContext.newTrace(
            traceOption.isPresent(),
            traceOption.orElse(null),
            (tagName, traceId) -> {
              // TODO(ekempin): Return trace ID to client
            });
  }

  private Optional<String> parseTraceOption(List<String> serverOptionList) {
    if (serverOptionList == null || serverOptionList.isEmpty()) {
      return Optional.empty();
    }

    ListMultimap<String, String> serverOptions = LinkedListMultimap.create();
    for (String option : serverOptionList) {
      int e = option.indexOf('=');
      if (e > 0) {
        serverOptions.put(option.substring(0, e), option.substring(e + 1));
      } else {
        serverOptions.put(option, "");
      }
    }

    List<String> traceValues = serverOptions.get("trace");
    if (!traceValues.isEmpty()) {
      return Optional.of(Iterables.getLast(traceValues));
    }

    return Optional.empty();
  }
}

@@ -709,152 +709,8 @@
 e;%0A%0A
-import com.google.common.collect.Iterables;%0Aimport com.google.common.collect.LinkedListMultimap;%0Aimport com.google.common.collect.ListMultimap;%0A
 impo
@@ -2577,86 +2577,123 @@
     
-ListMultimap%3CString, String%3E serverOptions = LinkedListMultimap.create
+Optional%3CString%3E traceOption =%0A        serverOptionList.stream().filter(o -%3E o.startsWith(%22trace%22)).findAny
 ();%0A    
 for 
@@ -2692,63 +2692,90 @@
     
+i
 f
-or
  (
-String option : serverOptionList) %7B%0A   
+!traceOption.isPresent()) %7B%0A      return Optional.empty();%0A    %7D%0A%0A
    
+ 
 int e = 
 opti
@@ -2770,22 +2770,33 @@
 int e = 
-option
+traceOption.get()
 .indexOf
@@ -2802,18 +2802,16 @@
 f('=');%0A
-  
     if (
@@ -2829,58 +2829,116 @@
     
-  serverOptions.put(option.substring(0, e), option
+// trace option was specified with trace ID: %22--trace=%3Ctrace-ID%3E%22%0A      return Optional.of(traceOption.get()
 .sub
@@ -2957,234 +2957,139 @@
 ));%0A
-  
     %7D
- else %7B%0A        serverOptions.put(option, %22%22);%0A      %7D%0A    %7D%0A%0A    List%3CString%3E traceValues = serverOptions.get(%22
+%0A%0A    // trace option was specified without trace ID: %22--
 trace%22
-);
+,
 %0A    
-if (!traceValues.isEmpty()) %7B%0A      return Optional.of(Iterables.getLast(traceValues));%0A    %7D%0A
+// return an empty string so that a trace ID is generated
 %0A   
@@ -3101,27 +3101,26 @@
 rn Optional.
-empty(
+of(%22%22
 );%0A  %7D%0A%7D%0A

Simplify parsing of trace option