Split out OSGiBGPStateConsumer 00/96700/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 28 Jun 2021 17:25:17 +0000 (19:25 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 28 Jun 2021 17:30:39 +0000 (19:30 +0200)
BGPStateCollectorImpl is a weird bit of code. We essentially have an
OSGi whiteboard pattern going on here, except we drive it through an
otherwise-unused BGPStateProducer interface.

Turn GPStateCollectorImpl into a @Singleton for things like Guice et al,
but it will need some more integration work to get it operation.

Add OSGiBGPStateConsumer which is a trivial OSGi DS whiteboard consumer
and thus eliminate a chunk of blueprint wiring.

JIRA: BGPCEP-958
Change-Id: I92fd2ecb0ef3f1ecb1069536c8b2a4d6261f23c4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/AbstractBGPStateConsumer.java [new file with mode: 0644]
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/BGPStateCollectorImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/OSGiBGPStateConsumer.java [new file with mode: 0644]
bgp/rib-impl/src/main/resources/OSGI-INF/blueprint/bgp-rib.xml

diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/AbstractBGPStateConsumer.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/AbstractBGPStateConsumer.java
new file mode 100644 (file)
index 0000000..90ee118
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.rib.impl.state;
+
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import java.util.Objects;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibState;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibStateConsumer;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateConsumer;
+
+abstract class AbstractBGPStateConsumer implements BGPStateConsumer {
+    @Override
+    public final List<BGPRibState> getRibStats() {
+        return bgpRibStates().stream()
+            .map(BGPRibStateConsumer::getRIBState)
+            .filter(Objects::nonNull)
+            .collect(ImmutableList.toImmutableList());
+    }
+
+    @Override
+    public final List<BGPPeerState> getPeerStats() {
+        return bgpPeerStates().stream()
+            .map(BGPPeerStateConsumer::getPeerState)
+            .filter(Objects::nonNull)
+            .collect(ImmutableList.toImmutableList());
+    }
+
+    abstract List<BGPRibStateConsumer> bgpRibStates();
+
+    abstract List<BGPPeerStateConsumer> bgpPeerStates();
+}
index 3b246dae1d5a03ae3dc7324ff61aebeedca71e0e..933f141856f874f3d437ee4f4e671054d18c9348 100644 (file)
@@ -7,51 +7,52 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl.state;
 
-import com.google.common.collect.ImmutableList;
 import java.util.List;
-import java.util.Objects;
 import java.util.concurrent.CopyOnWriteArrayList;
-import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
-import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibState;
 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibStateConsumer;
-import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateConsumer;
 import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateProvider;
 
+@Singleton
 // This class is thread-safe
-public class BGPStateCollectorImpl implements BGPStateProvider, BGPStateConsumer {
+public final class BGPStateCollectorImpl extends AbstractBGPStateConsumer implements BGPStateProvider {
     private final List<BGPRibStateConsumer> bgpRibStates = new CopyOnWriteArrayList<>();
     private final List<BGPPeerStateConsumer> bgpPeerStates = new CopyOnWriteArrayList<>();
 
-    @Override
-    public List<BGPRibState> getRibStats() {
-        return this.bgpRibStates.stream().map(BGPRibStateConsumer::getRIBState).filter(Objects::nonNull)
-                .collect(ImmutableList.toImmutableList());
-    }
-
-    @Override
-    public List<BGPPeerState> getPeerStats() {
-        return this.bgpPeerStates.stream().map(BGPPeerStateConsumer::getPeerState).filter(Objects::nonNull)
-                .collect(ImmutableList.toImmutableList());
+    @Inject
+    public BGPStateCollectorImpl() {
+        // Exposed for DI
     }
 
     @Override
     public void bind(final BGPRibStateConsumer bgpState) {
-        this.bgpRibStates.add(bgpState);
+        bgpRibStates.add(bgpState);
     }
 
     @Override
     public void bind(final BGPPeerStateConsumer bgpState) {
-        this.bgpPeerStates.add(bgpState);
+        bgpPeerStates.add(bgpState);
     }
 
     @Override
     public void unbind(final BGPRibStateConsumer bgpState) {
-        this.bgpRibStates.remove(bgpState);
+        bgpRibStates.remove(bgpState);
     }
 
     @Override
     public void unbind(final BGPPeerStateConsumer bgpState) {
-        this.bgpPeerStates.remove(bgpState);
+        bgpPeerStates.remove(bgpState);
+    }
+
+    @Override
+    List<BGPRibStateConsumer> bgpRibStates() {
+        return bgpRibStates;
+    }
+
+    @Override
+    List<BGPPeerStateConsumer> bgpPeerStates() {
+        return bgpPeerStates;
     }
 }
diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/OSGiBGPStateConsumer.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/OSGiBGPStateConsumer.java
new file mode 100644 (file)
index 0000000..878666b
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.rib.impl.state;
+
+import java.util.List;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibStateConsumer;
+import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateConsumer;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+
+@Component(immediate = true, service = BGPStateConsumer.class)
+public final class OSGiBGPStateConsumer extends AbstractBGPStateConsumer {
+    @Reference(policyOption = ReferencePolicyOption.GREEDY)
+    volatile List<BGPRibStateConsumer> ribStates;
+    @Reference(policyOption = ReferencePolicyOption.GREEDY)
+    volatile List<BGPPeerStateConsumer> peerStates;
+
+    @Override
+    List<BGPRibStateConsumer> bgpRibStates() {
+        return ribStates;
+    }
+
+    @Override
+    List<BGPPeerStateConsumer> bgpPeerStates() {
+        return peerStates;
+    }
+}
index 9a29802161c29816761590943b7fd826039a9d5b..2f4b6247fdaab4e7eee775a0a645e673a9978329 100644 (file)
   </bean>
 
   <bean id="appPeer" class="org.opendaylight.protocol.bgp.rib.impl.config.AppPeer" scope="prototype"/>
-
-  <bean id="bgpStateCollector" class="org.opendaylight.protocol.bgp.rib.impl.state.BGPStateCollectorImpl"/>
-  <service ref="bgpStateCollector" interface="org.opendaylight.protocol.bgp.rib.spi.state.BGPStateConsumer"/>
-
-  <reference-list id="ribStatsListener" interface="org.opendaylight.protocol.bgp.rib.spi.state.BGPRibStateConsumer"
-    availability="optional">
-      <reference-listener bind-method="bind" unbind-method="unbind">
-          <ref component-id="bgpStateCollector"/>
-      </reference-listener>
-  </reference-list>
-
-  <reference-list id="peersStatsListener" interface="org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer"
-                  availability="optional">
-      <reference-listener bind-method="bind" unbind-method="unbind">
-          <ref component-id="bgpStateCollector"/>
-      </reference-listener>
-  </reference-list>
 </blueprint>