tmf: Make only one CustomOutputAnnotationProvider return annotations

The CustomOutputAnnotationProvider instances are associated with a
trace/experiment by a constructor parameter.

When annotations are being fetched, only the provider associated with
the 'requested_trace' fetch parameter will respond with a model. The
other providers will return a null model. If the 'requested_trace'
parameter is absent, only the provider associated with the first element
of the opened trace/experiment trace set will return a model. The other
providers will return a null model.

When annotation categories are being fetched, only the provider
associated with the first element of the opened trace/experiment trace
set will return a model. The other providers will return a null model.

The 'requested_trace' fetch parameter is no longer included when
fetching annotation categories, it is not required. When fetching
annotations, it is not included to allow for the default (first trace)
to be used, as there is currently no UI control to select a requested
trace.

The 'requested_marker_set' and 'requested_trace' fetch parameters are
removed from the requests sent to specific view output annotation data
providers. These parameters are only relevant to the
CustomOutputAnnotationProvider trace adapter output annotation provider.

The classes CustomDefinedOutputAnnotationProvider/Factory are renamed to
CustomOutputAnnotationProvider/Factory

[Fixed] Only one CustomOutputAnnotationProvider return annotations
[Fixed] Remove unnecessary parameters when fetching annotations
[Changed] CustomDefinedOutputAnnotationProvider/Factory renamed

Change-Id: I89ecf729fa958532ad6d80bbe85cae8ee5b8824d
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/182716
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java
index 7d6597b..41757a7 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java
@@ -19,7 +19,7 @@
 import org.eclipse.core.runtime.Plugin;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.tracecompass.internal.tmf.core.annotations.CustomDefinedOutputAnnotationProviderFactory;
+import org.eclipse.tracecompass.internal.tmf.core.annotations.CustomOutputAnnotationProviderFactory;
 import org.eclipse.tracecompass.internal.tmf.core.annotations.LostEventsOutputAnnotationProviderFactory;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
 import org.eclipse.tracecompass.tmf.core.dataprovider.DataProviderManager;
@@ -53,7 +53,7 @@
      */
     private static Activator fPlugin;
     private static final LostEventsOutputAnnotationProviderFactory LOST_EVENTS_ANNOTATION_PROVIDER_FACTORY = new LostEventsOutputAnnotationProviderFactory();
-    private static final CustomDefinedOutputAnnotationProviderFactory CUSTOM_DEFINED_OUTPUT_ANNOTATION_PROVIDER_FACTORY = new CustomDefinedOutputAnnotationProviderFactory();
+    private static final CustomOutputAnnotationProviderFactory CUSTOM_DEFINED_OUTPUT_ANNOTATION_PROVIDER_FACTORY = new CustomOutputAnnotationProviderFactory();
 
     // ------------------------------------------------------------------------
     // Constructors
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomDefinedOutputAnnotationProvider.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomOutputAnnotationProvider.java
similarity index 63%
rename from tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomDefinedOutputAnnotationProvider.java
rename to tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomOutputAnnotationProvider.java
index defa69e..dd22321 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomDefinedOutputAnnotationProvider.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomOutputAnnotationProvider.java
@@ -31,56 +31,69 @@
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 
 /**
- * Trace Annotation provider for providing custom (frame) annotations. This one
- * is a singleton for all of trace compass, it encapsulates the individual
- * annotation providers per trace.
+ * Trace Annotation provider for providing custom (frame) annotations. It
+ * encapsulates the individual annotation providers per marker set.
  */
-public class CustomDefinedOutputAnnotationProvider implements IOutputAnnotationProvider {
+public class CustomOutputAnnotationProvider implements IOutputAnnotationProvider {
 
     private static final String INVALID_MARKER_ID = "Invalid marker ID %s"; //$NON-NLS-1$
     private static final String NO_MARKER_ID = "no markerID"; //$NON-NLS-1$
-    private final Map<String, Map<String, CustomAnnotationProvider>> fTraceMarkers = Collections.synchronizedMap(new HashMap<>());
+    private final Map<String, CustomAnnotationProvider> fProviders = Collections.synchronizedMap(new HashMap<>());
+    private final ITmfTrace fTrace;
+
+    static {
+        MarkerConfigXmlParser.initMarkerSets();
+    }
 
     /**
      * Constructor
      */
-    public CustomDefinedOutputAnnotationProvider() {
-        MarkerConfigXmlParser.initMarkerSets();
+    public CustomOutputAnnotationProvider(ITmfTrace trace) {
+        fTrace = trace;
     }
 
     @Override
     public TmfModelResponse<AnnotationCategoriesModel> fetchAnnotationCategories(Map<String, Object> fetchParameters, @Nullable IProgressMonitor monitor) {
         Object markerID = fetchParameters.get(DataProviderParameterUtils.REQUESTED_MARKER_SET_KEY);
-        Object hostID = fetchParameters.get(DataProviderParameterUtils.REQUESTED_TRACE_KEY);
-        Optional<ITmfTrace> activeTrace = getTrace(hostID);
+        /* Ignore if trace is not the first element of its trace set. */
+        if (!isFirstTrace()) {
+            return new TmfModelResponse<>(null, Status.COMPLETED, ""); //$NON-NLS-1$
+        }
         if (markerID == null) {
             return new TmfModelResponse<>(null, Status.FAILED, NO_MARKER_ID);
         }
         MarkerSet ms = getMarkerSet(markerID);
-        if (ms != null) {
-            return getAnnotationProvider(String.valueOf(hostID), activeTrace.get(), ms).fetchAnnotationCategories(fetchParameters, monitor);
+        if (ms == null) {
+            return new TmfModelResponse<>(null, Status.FAILED, formatError(markerID));
         }
-        return new TmfModelResponse<>(null, Status.FAILED, formatError(markerID));
+        return getAnnotationProvider(null, ms).fetchAnnotationCategories(fetchParameters, monitor);
     }
 
     @Override
     public TmfModelResponse<AnnotationModel> fetchAnnotations(Map<String, Object> fetchParameters, @Nullable IProgressMonitor monitor) {
         Object markerID = fetchParameters.get(DataProviderParameterUtils.REQUESTED_MARKER_SET_KEY);
         Object hostID = fetchParameters.get(DataProviderParameterUtils.REQUESTED_TRACE_KEY);
-        Optional<ITmfTrace> activeTrace = getTrace(hostID);
+        Optional<ITmfTrace> requestedTrace = getTrace(hostID);
+        /*
+         * Ignore if trace is not the requested trace, or if no requested trace,
+         * if the trace is not the first element of its trace set.
+         */
+        if ((requestedTrace.isPresent() && !requestedTrace.get().equals(fTrace)) ||
+                (requestedTrace.isEmpty() && !isFirstTrace())) {
+            return new TmfModelResponse<>(null, Status.COMPLETED, ""); //$NON-NLS-1$
+        }
         if (markerID == null) {
             return new TmfModelResponse<>(null, Status.FAILED, NO_MARKER_ID);
         }
         MarkerSet ms = getMarkerSet(markerID);
-        if (ms != null) {
-            return getAnnotationProvider(String.valueOf(hostID), activeTrace.isPresent() ? activeTrace.get() : null, ms).fetchAnnotations(fetchParameters, monitor);
+        if (ms == null) {
+            return new TmfModelResponse<>(null, Status.FAILED, formatError(markerID));
         }
-        return new TmfModelResponse<>(null, Status.FAILED, formatError(markerID));
+        return getAnnotationProvider(requestedTrace.isPresent() ? requestedTrace.get() : null, ms).fetchAnnotations(fetchParameters, monitor);
     }
 
-    private CustomAnnotationProvider getAnnotationProvider(String hostID, @Nullable ITmfTrace activeTrace, MarkerSet ms) {
-        Map<String, CustomAnnotationProvider> capMap = fTraceMarkers.computeIfAbsent(hostID, e -> Collections.synchronizedMap(new HashMap<>()));
-        return capMap.computeIfAbsent(ms.getId(), a -> new CustomAnnotationProvider(activeTrace, ms));
+    private CustomAnnotationProvider getAnnotationProvider(@Nullable ITmfTrace trace, MarkerSet ms) {
+        return fProviders.computeIfAbsent(ms.getId(), msId -> new CustomAnnotationProvider(trace, ms));
     }
 
     private static @Nullable MarkerSet getMarkerSet(Object markerID) {
@@ -97,6 +110,15 @@
         return TmfTraceManager.getInstance().getOpenedTraces().stream().flatMap(trace -> TmfTraceManager.getTraceSetWithExperiment(trace).stream()).filter(t -> Objects.equals(t.getHostId(), hostID)).findAny();
     }
 
+    /*
+     * Returns true if, for any opened trace, this provider's trace is the first
+     * element in the opened trace's trace set.
+     */
+    private boolean isFirstTrace() {
+        return TmfTraceManager.getInstance().getOpenedTraces().stream()
+                .anyMatch(t -> TmfTraceManager.getTraceSet(t).stream().findFirst().get().equals(fTrace));
+    }
+
     private static String formatError(Object markerID) {
         return String.format(INVALID_MARKER_ID, markerID);
     }
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomDefinedOutputAnnotationProviderFactory.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomOutputAnnotationProviderFactory.java
similarity index 85%
rename from tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomDefinedOutputAnnotationProviderFactory.java
rename to tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomOutputAnnotationProviderFactory.java
index 0f81ee4..2b2c040 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomDefinedOutputAnnotationProviderFactory.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/CustomOutputAnnotationProviderFactory.java
@@ -17,14 +17,15 @@
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 
 /**
- * Output annotation provider factory for trace level annotations for custom annotations (frames).
+ * Output annotation provider factory for trace level annotations for custom
+ * annotations (frames).
  */
-public class CustomDefinedOutputAnnotationProviderFactory extends AbstractTmfTraceAdapterFactory {
+public class CustomOutputAnnotationProviderFactory extends AbstractTmfTraceAdapterFactory {
 
     @Override
     protected @Nullable <T> T getTraceAdapter(ITmfTrace trace, @Nullable Class<T> adapterType) {
         if (null != adapterType && IOutputAnnotationProvider.class.equals(adapterType)) {
-            return adapterType.cast(new CustomDefinedOutputAnnotationProvider());
+            return adapterType.cast(new CustomOutputAnnotationProvider(trace));
         }
         return null;
     }
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/AbstractTimeGraphView.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/AbstractTimeGraphView.java
index 275a3f4..403e6fb 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/AbstractTimeGraphView.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/AbstractTimeGraphView.java
@@ -247,7 +247,6 @@
                 parameters.put(DataProviderParameterUtils.REQUESTED_MARKER_SET_KEY, defaultMarkerSet.getId());
             }
             parameters.put(DataProviderParameterUtils.REQUESTED_MARKER_CATEGORIES_KEY, Collections.singletonList(category));
-            parameters.put(DataProviderParameterUtils.REQUESTED_TRACE_KEY, fTrace.getHostId());
             parameters.put(DataProviderParameterUtils.REQUESTED_TIME_KEY, StateSystemUtils.getTimes(startTime, endTime, resolution));
             TmfModelResponse<@NonNull AnnotationModel> response = fProvider.fetchAnnotations(parameters, new NullProgressMonitor());
             AnnotationModel model = response.getModel();
@@ -277,7 +276,6 @@
             if (defaultMarkerSet != null) {
                 parameters.put(DataProviderParameterUtils.REQUESTED_MARKER_SET_KEY, defaultMarkerSet.getId());
             }
-            parameters.put(DataProviderParameterUtils.REQUESTED_TRACE_KEY, fTrace.getHostId());
             TmfModelResponse<@NonNull AnnotationCategoriesModel> cats = fProvider.fetchAnnotationCategories(parameters, new NullProgressMonitor());
             AnnotationCategoriesModel model = cats.getModel();
             if (model != null) {
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/BaseDataProviderTimeGraphView.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/BaseDataProviderTimeGraphView.java
index 01aa8fd..9519f5e 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/BaseDataProviderTimeGraphView.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/BaseDataProviderTimeGraphView.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2018, 2019 Ericsson
+ * Copyright (c) 2018, 2021 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License 2.0 which
@@ -44,9 +44,7 @@
 import org.eclipse.tracecompass.internal.provisional.tmf.core.model.annotations.IOutputAnnotationProvider;
 import org.eclipse.tracecompass.internal.provisional.tmf.ui.widgets.ViewFilterDialog;
 import org.eclipse.tracecompass.internal.provisional.tmf.ui.widgets.timegraph.BaseDataProviderTimeGraphPresentationProvider;
-import org.eclipse.tracecompass.internal.tmf.core.markers.MarkerSet;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
-import org.eclipse.tracecompass.internal.tmf.ui.markers.MarkerUtils;
 import org.eclipse.tracecompass.internal.tmf.ui.views.timegraph.Messages;
 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
 import org.eclipse.tracecompass.tmf.core.TmfStrings;
@@ -719,21 +717,9 @@
      */
     protected @NonNull Map<@NonNull String, @NonNull Object> getFetchAnnotationCategoriesParameters() {
         HashMap<@NonNull String, @NonNull Object> categoriesParameters = new HashMap<>();
-        putMarkerSetParameters(categoriesParameters, getTrace());
         return categoriesParameters;
     }
 
-    private static void putMarkerSetParameters(Map<@NonNull String, @NonNull Object> parameters, ITmfTrace trace) {
-        MarkerSet markerSet = MarkerUtils.getDefaultMarkerSet();
-        if (markerSet != null) {
-            String markerSetID = markerSet.getId();
-            if (markerSetID != null) {
-                parameters.put(DataProviderParameterUtils.REQUESTED_MARKER_SET_KEY, markerSetID);
-                parameters.put(DataProviderParameterUtils.REQUESTED_TRACE_KEY, trace.getHostId());
-            }
-        }
-    }
-
     /**
      * Get the fetch parameters to pass to a fetchAnnotations call
      *
@@ -748,7 +734,6 @@
         Map<@NonNull String, @NonNull Object> parameters = new HashMap<>();
         parameters.put(DataProviderParameterUtils.REQUESTED_TIME_KEY, times);
         parameters.put(DataProviderParameterUtils.REQUESTED_ITEMS_KEY, items);
-        putMarkerSetParameters(parameters, getTrace());
         return parameters;
     }