Convert programming-impl to OSGi DS 27/93127/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 17 Oct 2020 01:55:25 +0000 (03:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Jun 2021 15:38:12 +0000 (17:38 +0200)
This is a rather simple blueprint, convert it to OSGi DS.

JIRA: BGPCEP-922
Change-Id: I33b455490123cad6d20fb735ab72177b62d6cc09
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
programming/impl/pom.xml
programming/impl/src/main/java/org/opendaylight/bgpcep/programming/impl/DefaultInstructionSchedulerFactory.java [moved from programming/impl/src/main/java/org/opendaylight/bgpcep/programming/impl/InstructionSchedulerFactoryImpl.java with 55% similarity]
programming/impl/src/main/java/org/opendaylight/bgpcep/programming/impl/ProgrammingServiceImpl.java
programming/impl/src/main/resources/OSGI-INF/blueprint/programming.xml [deleted file]

index f233bdeb9be6f8c6a54aeac10bb91cfcbe8b908d..bf3f03dc7a1742a4730b24d4bce8ddbe8d569a44 100644 (file)
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>concepts</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.checkerframework</groupId>
+            <artifactId>checker-qual</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
-            <artifactId>osgi.core</artifactId>
+            <artifactId>osgi.cmpn</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.checkerframework</groupId>
-            <artifactId>checker-qual</artifactId>
+            <groupId>javax.annotation</groupId>
+            <artifactId>javax.annotation-api</artifactId>
+            <scope>provided</scope>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>com.guicedee.services</groupId>
+            <artifactId>javax.inject</artifactId>
+            <optional>true</optional>
         </dependency>
 
         <!-- Testing dependencies -->
similarity index 55%
rename from programming/impl/src/main/java/org/opendaylight/bgpcep/programming/impl/InstructionSchedulerFactoryImpl.java
rename to programming/impl/src/main/java/org/opendaylight/bgpcep/programming/impl/DefaultInstructionSchedulerFactory.java
index 54878ed1d350d3e81638f3877f3e8baf4bfbaac1..3921c62967765fb0162b1923e76e1035d08d412a 100644 (file)
@@ -5,7 +5,6 @@
  * 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.bgpcep.programming.impl;
 
 import static java.util.Objects.requireNonNull;
@@ -13,60 +12,60 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
 import io.netty.util.Timer;
-import java.util.Dictionary;
-import java.util.Hashtable;
 import java.util.concurrent.Executors;
-import org.gaul.modernizer_maven_annotations.SuppressModernizer;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
 import org.opendaylight.bgpcep.programming.spi.InstructionSchedulerFactory;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
+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;
 
-public final class InstructionSchedulerFactoryImpl implements InstructionSchedulerFactory {
-
-    private static final Logger LOG = LoggerFactory.getLogger(InstructionSchedulerFactoryImpl.class);
+@Component(immediate = true, service = InstructionSchedulerFactory.class)
+@Singleton
+public final class DefaultInstructionSchedulerFactory implements InstructionSchedulerFactory, AutoCloseable {
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultInstructionSchedulerFactory.class);
 
     private final DataBroker dataProvider;
     private final NotificationPublishService notifs;
     private final Timer timer;
     private final RpcProviderService rpcProviderRegistry;
-    private final BundleContext bundleContext;
     private final ClusterSingletonServiceProvider cssp;
     private final ListeningExecutorService exec = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
 
-    public InstructionSchedulerFactoryImpl(
-            final DataBroker dataProvider,
-            final RpcProviderService rpcProviderRegistry,
-            final NotificationPublishService notifs,
-            final Timer timer,
-            final ClusterSingletonServiceProvider cssp,
-            final BundleContext bundleContext) {
+    @Inject
+    @Activate
+    public DefaultInstructionSchedulerFactory(@Reference final DataBroker dataProvider,
+            @Reference final RpcProviderService rpcProviderRegistry,
+            @Reference final NotificationPublishService notifs,
+            @Reference(target = "(type=global-timer)") final Timer timer,
+            @Reference final ClusterSingletonServiceProvider cssp) {
         this.dataProvider = requireNonNull(dataProvider);
         this.notifs = requireNonNull(notifs);
         this.timer = requireNonNull(timer);
         this.rpcProviderRegistry = requireNonNull(rpcProviderRegistry);
-        this.bundleContext = requireNonNull(bundleContext);
         this.cssp = requireNonNull(cssp);
     }
 
     @Override
-    @SuppressModernizer
     public InstructionScheduler createInstructionScheduler(final String instructionId) {
         LOG.info("Creating Instruction Scheduler {}.", instructionId);
+        return new ProgrammingServiceImpl(dataProvider, notifs, exec, rpcProviderRegistry, cssp, timer, instructionId);
+    }
 
-        final ProgrammingServiceImpl programmingInst = new ProgrammingServiceImpl(this.dataProvider, this.notifs,
-                this.exec, this.rpcProviderRegistry, this.cssp, this.timer, instructionId);
-        final Dictionary<String, String> properties = new Hashtable<>();
-        properties.put(InstructionScheduler.class.getName(), instructionId);
-        final ServiceRegistration<?> serviceRegistration = this.bundleContext
-                .registerService(InstructionScheduler.class.getName(), programmingInst, properties);
-        programmingInst.setServiceRegistration(serviceRegistration);
-        return programmingInst;
+    @Deactivate
+    @PreDestroy
+    @Override
+    public void close() {
+        // FIXME: This can have weird effects: should we keep track of all schedulers and refcount?
+        exec.shutdown();
     }
 }
index 68aa257c3b2f16177fa9a5cb193b19e2f7dc754f..fbfe0c5821edfdbb751e1e4baa5a0f5afa480e8a 100644 (file)
@@ -70,12 +70,10 @@ import org.opendaylight.yangtools.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public final class ProgrammingServiceImpl implements ClusterSingletonService, InstructionScheduler,
-        ProgrammingService {
+final class ProgrammingServiceImpl implements ClusterSingletonService, InstructionScheduler, ProgrammingService {
     private static final Logger LOG = LoggerFactory.getLogger(ProgrammingServiceImpl.class);
 
     private final Map<InstructionId, InstructionImpl> insns = new HashMap<>();
@@ -90,8 +88,6 @@ public final class ProgrammingServiceImpl implements ClusterSingletonService, In
     private final RpcProviderService rpcProviderRegistry;
     @GuardedBy("this")
     private ObjectRegistration<ProgrammingService> reg;
-    @GuardedBy("this")
-    private ServiceRegistration<?> serviceRegistration;
 
     private final class InstructionPusher implements QueueInstruction {
         private final InstructionBuilder builder = new InstructionBuilder();
@@ -446,22 +442,7 @@ public final class ProgrammingServiceImpl implements ClusterSingletonService, In
     }
 
     @Override
-    @SuppressWarnings("checkstyle:IllegalCatch")
     public synchronized void close() {
-        if (this.csspReg != null) {
-            try {
-                this.csspReg.close();
-            } catch (final Exception e) {
-                LOG.error("Failed to close Instruction Scheduler service", e);
-            }
-        }
-        if (this.serviceRegistration != null) {
-            this.serviceRegistration.unregister();
-            this.serviceRegistration = null;
-        }
-    }
-
-    void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
-        this.serviceRegistration = serviceRegistration;
+        this.csspReg.close();
     }
 }
diff --git a/programming/impl/src/main/resources/OSGI-INF/blueprint/programming.xml b/programming/impl/src/main/resources/OSGI-INF/blueprint/programming.xml
deleted file mode 100644 (file)
index 02c4ede..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- vi: set et smarttab sw=4 tabstop=4: -->
-<!--
-      Copyright (c) 2017 Pantheon Technologies 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
--->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
-           xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0">
-
-    <reference id="dataBroker" interface="org.opendaylight.mdsal.binding.api.DataBroker"/>
-    <reference id="rpcRegistry" interface="org.opendaylight.mdsal.binding.api.RpcProviderService"/>
-    <reference id="notificationService"
-               interface="org.opendaylight.mdsal.binding.api.NotificationPublishService"/>
-    <reference id="timer" interface="io.netty.util.Timer" odl:type="global-timer"/>
-    <reference id="clusterSingletonServiceProvider"
-               interface="org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider"/>
-
-    <bean id="IntructionFactory" class="org.opendaylight.bgpcep.programming.impl.InstructionSchedulerFactoryImpl">
-        <argument ref="dataBroker"/>
-        <argument ref="rpcRegistry"/>
-        <argument ref="notificationService"/>
-        <argument ref="timer"/>
-        <argument ref="clusterSingletonServiceProvider"/>
-        <argument ref="blueprintBundleContext"/>
-    </bean>
-
-    <service ref="IntructionFactory"
-             interface="org.opendaylight.bgpcep.programming.spi.InstructionSchedulerFactory"/>
-</blueprint>