Convert pcep-server-provider to OSGi DS 91/96691/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 28 Jun 2021 10:31:33 +0000 (12:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 28 Jun 2021 10:32:15 +0000 (12:32 +0200)
The blueprint here is a very simple single component, refactor it to use
OSGi Declarative Services instead.

JIRA: BGPCEP-963
Change-Id: Id396abaf268fce5ec01cea0e96a1d834551cb852
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/server/server-provider/pom.xml
pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/DefaultPceServerProvider.java [moved from pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/PceServerFactory.java with 55% similarity]
pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/PathComputationImpl.java
pcep/server/server-provider/src/main/resources/OSGI-INF/blueprint/pce-server.xml [deleted file]

index fbe1e1bb2d2f67a3cc101baf0cce4bfea04c71d0..5a730b47d525ed745dc46357e45c9030c5cf8a9c 100644 (file)
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>yang-common</artifactId>
         </dependency>
-        <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-api</artifactId>
-        </dependency>
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
             <groupId>org.opendaylight.mdsal.binding.model.ietf</groupId>
             <artifactId>rfc6991-ietf-inet-types</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.guicedee.services</groupId>
+            <artifactId>javax.inject</artifactId>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
 </project>
@@ -5,50 +5,43 @@
  * 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.pcep.server.provider;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.algo.PathComputationProvider;
 import org.opendaylight.bgpcep.pcep.server.PceServerProvider;
 import org.opendaylight.graph.ConnectedGraph;
 import org.opendaylight.graph.ConnectedGraphProvider;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
-public class PceServerFactory implements PceServerProvider {
-
+@Singleton
+@Component(immediate = true)
+public final class DefaultPceServerProvider implements PceServerProvider {
     private final ConnectedGraphProvider graphProvider;
     private final PathComputationProvider algoProvider;
-    private ConnectedGraph tedGraph = null;
 
-    public PceServerFactory(ConnectedGraphProvider graphProvider, PathComputationProvider pathComputationProvider) {
-        Preconditions.checkArgument(graphProvider != null);
-        this.graphProvider = graphProvider;
-        this.algoProvider = pathComputationProvider;
-        setTedGraph();
-    }
+    private volatile ConnectedGraph tedGraph;
 
-    /**
-     * Set Traffic Engineering Graph. This method is necessary as the TedGraph could be available
-     * after the PathComputationFactory start e.g. manual insertion of a ted Graph, or late tedGraph fulfillment
-     * from BGP Link State.
-     */
-    private void setTedGraph() {
-        for (ConnectedGraph cgraph : this.graphProvider.getConnectedGraphs()) {
-            if (cgraph.getGraph().getName().startsWith("ted://")) {
-                this.tedGraph = cgraph;
-                break;
-            }
-        }
+    @Inject
+    @Activate
+    public DefaultPceServerProvider(@Reference final ConnectedGraphProvider graphProvider,
+            @Reference final PathComputationProvider pathComputationProvider) {
+        this.graphProvider = requireNonNull(graphProvider);
+        this.algoProvider = requireNonNull(pathComputationProvider);
+        setTedGraph();
     }
 
     @Override
     public @Nullable PathComputationImpl getPathComputation() {
         /* Check that we have a valid graph */
-        if (getTedGraph() == null) {
-            return null;
-        }
-        return new PathComputationImpl(tedGraph, algoProvider);
+        final ConnectedGraph graph = getTedGraph();
+        return graph == null ? null : new PathComputationImpl(tedGraph, algoProvider);
     }
 
     @Override
@@ -57,6 +50,18 @@ public class PceServerFactory implements PceServerProvider {
         if (tedGraph == null) {
             setTedGraph();
         }
-        return this.tedGraph;
+        return tedGraph;
+    }
+
+    /**
+     * Set Traffic Engineering Graph. This method is necessary as the TedGraph could be available
+     * after the PathComputationFactory start e.g. manual insertion of a ted Graph, or late tedGraph fulfillment
+     * from BGP Link State.
+     */
+    private void setTedGraph() {
+        tedGraph = graphProvider.getConnectedGraphs().stream()
+            .filter(graph -> graph.getGraph().getName().startsWith("ted://"))
+            .findFirst()
+            .orElse(null);
     }
 }
index 1b6428a48a5ace6269a3e35fc2d74d3468a35d9b..68eace1e8d76d99a212477980b8aa7107a0af854 100644 (file)
@@ -5,10 +5,10 @@
  * 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.pcep.server.provider;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import java.math.BigDecimal;
 import java.nio.ByteBuffer;
 import java.util.List;
@@ -52,9 +52,8 @@ public class PathComputationImpl implements PathComputation {
     private final PathComputationProvider algoProvider;
 
     public PathComputationImpl(final ConnectedGraph tedGraph, final PathComputationProvider algoProvider) {
-        Preconditions.checkArgument(tedGraph != null);
-        this.tedGraph = tedGraph;
-        this.algoProvider = algoProvider;
+        this.tedGraph = requireNonNull(tedGraph);
+        this.algoProvider = requireNonNull(algoProvider);
     }
 
     @Override
diff --git a/pcep/server/server-provider/src/main/resources/OSGI-INF/blueprint/pce-server.xml b/pcep/server/server-provider/src/main/resources/OSGI-INF/blueprint/pce-server.xml
deleted file mode 100644 (file)
index 7b3439d..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  Copyright (c) 2020 Orange. 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">
-
-    <reference id="pathComputationProvider" interface="org.opendaylight.algo.PathComputationProvider"/>
-    <reference id="connectedGraphProvider" interface="org.opendaylight.graph.ConnectedGraphProvider" />
-
-    <bean id="pceServerProvider" class="org.opendaylight.bgpcep.pcep.server.provider.PceServerFactory">
-        <argument ref="connectedGraphProvider"/>
-        <argument ref="pathComputationProvider"/>
-    </bean>
-
-    <service ref="pceServerProvider" interface="org.opendaylight.bgpcep.pcep.server.PceServerProvider"/>
-
-</blueprint>