Remove publishing of stream events

From version 3.5.0-alpha-202107290338 events-broker library
provides an implementation of StreamEventPublisher which is
bound by multi-site directly in [1], removing the need for registering
any event listener in events-gcloud-pubsub.

[1] https://gerrit-review.googlesource.com/c/plugins/multi-site/+/312871

Bug: Issue 14841
Change-Id: I79a059deb8da1328d547b185e6b22f3c08119f47
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java
index 01bd857..1d5f649 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java
@@ -26,16 +26,11 @@
 
   private final Set<TopicSubscriber> consumers;
   private final BrokerApi brokerApi;
-  private final PubSubEventListener pubSubEventListener;
 
   @Inject
-  public Manager(
-      Set<TopicSubscriber> consumers,
-      BrokerApi brokerApi,
-      PubSubEventListener pubSubEventListener) {
+  public Manager(Set<TopicSubscriber> consumers, BrokerApi brokerApi) {
     this.consumers = consumers;
     this.brokerApi = brokerApi;
-    this.pubSubEventListener = pubSubEventListener;
   }
 
   @Override
@@ -48,6 +43,5 @@
   @Override
   public void stop() {
     brokerApi.disconnect();
-    pubSubEventListener.disconnect();
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
index 50fad61..d1723c7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
@@ -18,7 +18,6 @@
 import com.google.gerrit.extensions.config.FactoryModule;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.extensions.registration.DynamicSet;
-import com.google.gerrit.server.events.EventListener;
 import com.google.inject.Inject;
 import com.google.inject.Scopes;
 import com.googlesource.gerrit.plugins.pubsub.local.EnvironmentChecker;
@@ -40,7 +39,6 @@
   @Override
   protected void configure() {
     DynamicSet.bind(binder(), LifecycleListener.class).to(Manager.class);
-    DynamicSet.bind(binder(), EventListener.class).to(PubSubEventListener.class);
     factory(PubSubPublisher.Factory.class);
     factory(PubSubEventSubscriber.Factory.class);
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
index 614d0aa..465c913 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
@@ -28,7 +28,6 @@
   private static final String DEFAULT_ACK_DEADLINE_SECONDS = "10";
   private static final String DEFAULT_SUBSCTIPRION_TIMEOUT = "10";
   private static final String DEFAULT_SHUTDOWN_TIMEOUT = "10";
-  private static final String DEFAULT_STREAM_EVENTS_TOPIC = "gerrit";
 
   private final String gcloudProject;
   private final String subscriptionId;
@@ -37,7 +36,6 @@
   private final Integer ackDeadlineSeconds;
   private final Long subscribtionTimeoutInSeconds;
   private final Long shutdownTimeoutInSeconds;
-  private final String streamEventsTopic;
   private final PluginConfig fromGerritConfig;
 
   @Inject
@@ -49,8 +47,6 @@
     this.gcloudProject = getMandatoryString("gcloudProject");
     this.subscriptionId = getMandatoryString("subscriptionId", instanceId);
     this.privateKeyLocation = getMandatoryString("privateKeyLocation");
-    this.streamEventsTopic =
-        fromGerritConfig.getString("streamEventsTopic", DEFAULT_STREAM_EVENTS_TOPIC);
     this.numberOfSubscribers =
         Integer.parseInt(
             fromGerritConfig.getString("numberOfSubscribers", DEFAULT_NUMBER_OF_SUBSCRIBERS));
@@ -94,10 +90,6 @@
     return shutdownTimeoutInSeconds;
   }
 
-  public String getStreamEventsTopic() {
-    return streamEventsTopic;
-  }
-
   private String getMandatoryString(String name) throws IllegalStateException {
     return getMandatoryString(name, null);
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubEventListener.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubEventListener.java
deleted file mode 100644
index 29ec263..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubEventListener.java
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright (C) 2021 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.googlesource.gerrit.plugins.pubsub;
-
-import com.google.common.flogger.FluentLogger;
-import com.google.gerrit.server.events.Event;
-import com.google.gerrit.server.events.EventListener;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-
-@Singleton
-public class PubSubEventListener implements EventListener {
-  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
-
-  private final PubSubPublisher publisher;
-
-  @Inject
-  public PubSubEventListener(
-      PubSubPublisher.Factory publisherFactory, PubSubConfiguration configuration) {
-    this.publisher = publisherFactory.create(configuration.getStreamEventsTopic());
-  }
-
-  @Override
-  public void onEvent(Event event) {
-    publisher.publish(event);
-  }
-
-  public void disconnect() {
-    try {
-      publisher.close();
-    } catch (InterruptedException e) {
-      logger.atSevere().withCause(e).log("Disconnect failed");
-    }
-  }
-}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index f3263d3..d5f9643 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -42,8 +42,3 @@
 `plugin.events-gcloud-pubsub.subscribtionTimeoutInSeconds`
 :   Optional. Maximum time in seconds to wait for the subscriber to connect to GCloud PubSub topic.
     Default: 10
-
-`plugin.events-gcloud-pubsub.streamEventsTopic`
-:   Optional. Name of the GCloud PubSub topic for stream events. events-gcloud-pubsub plugin exposes
-    all stream events under this topic name.
-    Default: gerrit
diff --git a/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java b/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
index d202f25..4a68b8b 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
@@ -15,8 +15,6 @@
 package com.googlesource.gerrit.plugins.pubsub;
 
 import static com.google.common.truth.Truth.assertThat;
-import static java.util.stream.Collectors.counting;
-import static java.util.stream.Collectors.groupingBy;
 
 import com.gerritforge.gerrit.eventbroker.BrokerApi;
 import com.google.api.gax.core.NoCredentialsProvider;
@@ -38,22 +36,18 @@
 import com.google.gerrit.server.events.Event;
 import com.google.gerrit.server.events.EventGson;
 import com.google.gerrit.server.events.ProjectCreatedEvent;
-import com.google.gerrit.server.events.RefUpdatedEvent;
 import com.google.gson.Gson;
 import com.google.inject.Inject;
 import com.google.pubsub.v1.ProjectSubscriptionName;
 import com.google.pubsub.v1.PullRequest;
 import com.google.pubsub.v1.PullResponse;
 import com.google.pubsub.v1.PushConfig;
-import com.google.pubsub.v1.ReceivedMessage;
 import com.google.pubsub.v1.TopicName;
 import com.googlesource.gerrit.plugins.pubsub.local.EnvironmentChecker;
 import io.grpc.ManagedChannel;
 import io.grpc.ManagedChannelBuilder;
 import java.io.IOException;
 import java.time.Duration;
-import java.util.List;
-import java.util.Map;
 import java.util.function.Consumer;
 import org.junit.Test;
 import org.testcontainers.containers.PubSubEmulatorContainer;
@@ -135,36 +129,6 @@
   @GerritConfig(
       name = "plugin.events-gcloud-pubsub.privateKeyLocation",
       value = PRIVATE_KEY_LOCATION)
-  public void shouldProduceStreamEvents() throws Exception {
-    String subscriptionId = "gerrit-subscription-id";
-    String topicId = "gerrit";
-    createSubscription(subscriptionId, topicId, channelProvider, credentialsProvider);
-
-    createChange();
-
-    readMessageAndValidate(
-        (pullResponse) -> {
-          List<ReceivedMessage> messages = pullResponse.getReceivedMessagesList();
-          assertThat(messages).hasSize(4);
-          Map<String, Long> messageTypeCount =
-              messages.stream()
-                  .map(m -> gson.fromJson(m.getMessage().getData().toStringUtf8(), Map.class))
-                  .map(m -> m.get("type").toString())
-                  .collect(groupingBy(t -> t, counting()));
-
-          assertThat(messageTypeCount.get(RefUpdatedEvent.TYPE)).isEqualTo(3);
-          assertThat(messageTypeCount.get("patchset-created")).isEqualTo(1);
-        },
-        PROJECT_ID,
-        subscriptionId);
-  }
-
-  @Test
-  @GerritConfig(name = "plugin.events-gcloud-pubsub.gcloudProject", value = PROJECT_ID)
-  @GerritConfig(name = "plugin.events-gcloud-pubsub.subscriptionId", value = SUBSCRIPTION_ID)
-  @GerritConfig(
-      name = "plugin.events-gcloud-pubsub.privateKeyLocation",
-      value = PRIVATE_KEY_LOCATION)
   public void shouldConsumeEvent() throws InterruptedException {
     Event event = new ProjectCreatedEvent();
     event.instanceId = DEFAULT_INSTANCE_ID;