Eliminate blueprint from openflow-protocol-impl 79/94679/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 20 Jan 2021 21:33:06 +0000 (22:33 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 20 Jan 2021 21:33:06 +0000 (22:33 +0100)
We have simple services here, to the point of one being ServiceLoader
kind. Use OSGi DS instead of blueprint, unblocking dependencies and
making things neater.

Change-Id: Ifab9ba92e1ac6d73854093b5dce4768bd722984e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowjava/openflow-protocol-impl/pom.xml
openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/DefaultOpenflowDiagStatusProvider.java [moved from openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OpenflowDiagStatusProviderImpl.java with 76% similarity]
openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OSGiOpenflowDiagStatusProvider.java [new file with mode: 0644]
openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/SwitchConnectionProviderFactoryImpl.java

index 0c997388f2e4a43611c62767b828cae0e10f4988..512053bfccfa564a62ef090c25fb8326fc8fa010 100644 (file)
 
     <build>
         <plugins>
-            <plugin>
-              <groupId>org.apache.aries.blueprint</groupId>
-              <artifactId>blueprint-maven-plugin</artifactId>
-              <configuration>
-                <scanPaths>
-                  <!-- This is code migrated from openflowjava project and the java package
-                       does not match artifact groupId, we therefore need to override it.
-                  -->
-                  <scanPath>org.opendaylight.openflowjava</scanPath>
-                </scanPaths>
-              </configuration>
-
-            </plugin>
             <plugin>
               <groupId>org.apache.felix</groupId>
               <artifactId>maven-bundle-plugin</artifactId>
             <groupId>io.netty</groupId>
             <artifactId>netty-handler</artifactId>
         </dependency>
+
         <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.core</artifactId>
+            <groupId>org.kohsuke.metainf-services</groupId>
+            <artifactId>metainf-services</artifactId>
         </dependency>
         <dependency>
-            <groupId>javax.inject</groupId>
+            <groupId>com.guicedee.services</groupId>
             <artifactId>javax.inject</artifactId>
             <optional>true</optional>
         </dependency>
         <dependency>
-            <groupId>javax.annotation</groupId>
-            <artifactId>javax.annotation-api</artifactId>
-            <optional>true</optional>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component.annotations</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.aries.blueprint</groupId>
-            <artifactId>blueprint-maven-plugin-annotation</artifactId>
+            <groupId>javax.annotation</groupId>
+            <artifactId>javax.annotation-api</artifactId>
+            <scope>provided</scope>
             <optional>true</optional>
         </dependency>
+
         <dependency>
+            <!-- FIXME: configure slf4j-simple instead -->
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <scope>test</scope>
         </dependency>
+
         <dependency>
             <groupId>org.opendaylight.infrautils</groupId>
             <artifactId>infrautils-util</artifactId>
             <groupId>io.netty</groupId>
             <artifactId>netty-transport-native-epoll</artifactId>
             <!-- Explicitly bring in the linux classifier, test may fail on 32-bit linux -->
+            <!--
+                FIXME: I don't think this is a good idea. We should be platform-agnostic
+                       for build, so this works on Mac and others as well.
+                       x86-64 Linux is tested in CSIT, where this library is present
+             -->
             <classifier>linux-x86_64</classifier>
         </dependency>
     </dependencies>
@@ -11,38 +11,48 @@ import static org.opendaylight.infrautils.diagstatus.ServiceState.ERROR;
 import static org.opendaylight.infrautils.diagstatus.ServiceState.OPERATIONAL;
 import static org.opendaylight.infrautils.diagstatus.ServiceState.STARTING;
 
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.apache.aries.blueprint.annotation.service.Reference;
-import org.apache.aries.blueprint.annotation.service.Service;
 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
 import org.opendaylight.infrautils.diagstatus.ServiceDescriptor;
+import org.opendaylight.infrautils.diagstatus.ServiceRegistration;
 import org.opendaylight.infrautils.diagstatus.ServiceState;
 import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-@Service(classes = OpenflowDiagStatusProvider.class)
-public class OpenflowDiagStatusProviderImpl implements OpenflowDiagStatusProvider {
-    private static final Logger LOG = LoggerFactory.getLogger(OpenflowDiagStatusProviderImpl.class);
+public final class DefaultOpenflowDiagStatusProvider implements OpenflowDiagStatusProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultOpenflowDiagStatusProvider.class);
     private static final String OPENFLOW_SERVICE = "OPENFLOW";
     private static final String OPENFLOW_SERVER_6633 = "OPENFLOW_SERVER_6633";
     private static final String OPENFLOW_SERVER_6653 = "OPENFLOW_SERVER_6653";
     private static final String OPENFLOW_SERVICE_AGGREGATE = OPENFLOW_SERVICE;
 
-    private final DiagStatusService diagStatusService;
-    private volatile Map<String, ServiceState> statusMap = new HashMap<>(Map.of(
+    private final ConcurrentMap<String, ServiceState> statusMap = new ConcurrentHashMap<>(Map.of(
         OPENFLOW_SERVICE, STARTING,
         OPENFLOW_SERVER_6633, STARTING,
         OPENFLOW_SERVER_6653, STARTING));
+    private final DiagStatusService diagStatusService;
+
+    private ServiceRegistration reg;
 
     @Inject
-    public OpenflowDiagStatusProviderImpl(final @Reference DiagStatusService diagStatusService) {
+    public DefaultOpenflowDiagStatusProvider(final DiagStatusService diagStatusService) {
         this.diagStatusService = diagStatusService;
-        diagStatusService.register(OPENFLOW_SERVICE_AGGREGATE);
+        reg = diagStatusService.register(OPENFLOW_SERVICE_AGGREGATE);
+    }
+
+    @PreDestroy
+    public void close() {
+        if (reg != null) {
+            reg.unregister();
+            reg = null;
+        }
     }
 
     @Override
@@ -73,8 +83,7 @@ public class OpenflowDiagStatusProviderImpl implements OpenflowDiagStatusProvide
     }
 
     public void reportStatus() {
-        boolean state = statusMap.values().stream().allMatch(serviceState -> serviceState.equals(OPERATIONAL));
-        if (state) {
+        if (statusMap.values().stream().allMatch(OPERATIONAL::equals)) {
             reportStatus(OPERATIONAL);
         }
     }
diff --git a/openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OSGiOpenflowDiagStatusProvider.java b/openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/OSGiOpenflowDiagStatusProvider.java
new file mode 100644 (file)
index 0000000..33207ca
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2020 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.openflowjava.protocol.impl.core;
+
+import static com.google.common.base.Verify.verifyNotNull;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.infrautils.diagstatus.DiagStatusService;
+import org.opendaylight.infrautils.diagstatus.ServiceState;
+import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(immediate = true)
+// FIXME: integrate with DefaultOpenflowDiagStatusProvider once we have OSGi R7
+public final class OSGiOpenflowDiagStatusProvider implements OpenflowDiagStatusProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(OSGiOpenflowDiagStatusProvider.class);
+
+    @Reference
+    DiagStatusService diagStatus;
+
+    private DefaultOpenflowDiagStatusProvider delegate = null;
+
+    @Override
+    public void reportStatus(final ServiceState serviceState) {
+        delegate().reportStatus(serviceState);
+    }
+
+    @Override
+    public void reportStatus(final String diagStatusService, final Throwable throwable) {
+        delegate().reportStatus(diagStatusService, throwable);
+    }
+
+    @Override
+    public void reportStatus(final String diagStatusIdentifier, final ServiceState serviceState,
+            final String description) {
+        delegate().reportStatus(diagStatusIdentifier, serviceState, description);
+    }
+
+    @Override
+    public void reportStatus(final String diagStatusIdentifier, final ServiceState serviceState) {
+        delegate().reportStatus(diagStatusIdentifier, serviceState);
+    }
+
+    @Activate
+    void activate() {
+        delegate = new DefaultOpenflowDiagStatusProvider(diagStatus);
+        LOG.info("OpenFlow diagnostic status provider activated");
+    }
+
+    @Deactivate
+    void deactivate() {
+        delegate.close();
+        delegate = null;
+        LOG.info("OpenFlow diagnostic status provider deactivated");
+    }
+
+    private @NonNull DefaultOpenflowDiagStatusProvider delegate() {
+        return verifyNotNull(delegate);
+    }
+}
index 2ea22ad9950da3cdbff0ba6c51f94ea677f793e7..50eb8ffd60fd805a2712c731a79b0c6885d44877 100644 (file)
@@ -12,8 +12,9 @@ import static java.util.Objects.requireNonNull;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.List;
+import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.apache.aries.blueprint.annotation.service.Service;
+import org.kohsuke.MetaInfServices;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
 import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
@@ -26,19 +27,24 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.T
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506.SwitchConnectionConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506._switch.connection.config.Threads;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow._switch.connection.config.rev160506._switch.connection.config.Tls;
+import org.osgi.service.component.annotations.Component;
 
 /**
  * Implementation of the SwitchConnectionProviderFactory interface.
  */
+@MetaInfServices
 @Singleton
-@Service(classes = SwitchConnectionProviderFactory.class)
+@Component(immediate = true)
 public class SwitchConnectionProviderFactoryImpl implements SwitchConnectionProviderFactory {
+    @Inject
+    public SwitchConnectionProviderFactoryImpl() {
+        // Exposed for DI
+    }
 
     @Override
     public SwitchConnectionProvider newInstance(final SwitchConnectionConfig config,
-                                                final OpenflowDiagStatusProvider openflowPluginDiagStatusProvider) {
-        return new SwitchConnectionProviderImpl(new ConnectionConfigurationImpl(config),
-                openflowPluginDiagStatusProvider);
+                                                final OpenflowDiagStatusProvider diagStatus) {
+        return new SwitchConnectionProviderImpl(new ConnectionConfigurationImpl(config), diagStatus);
     }
 
     private static class ConnectionConfigurationImpl implements ConnectionConfiguration {