Use BrokerFacade non-statically 95/70995/1
authorTom Pantelis <tompantelis@gmail.com>
Mon, 16 Apr 2018 18:24:59 +0000 (14:24 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Mon, 16 Apr 2018 20:58:08 +0000 (16:58 -0400)
Similar to the previous patch that was done for ControllerContext.
keeps the static BrokerFacade instance temporarily
for the RestconfApplication but converts the rest of the code to
reference it non-statically.

Change-Id: Iafdb26200f8d645a8ed61c7006edd0b106d7d976
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/BrokerFacade.java
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java
restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml
restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/BrokerFacadeTest.java
restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplNotificationSubscribingTest.java

index 3ab324393de837abf9c40bfef7b530f557a03557..8c1299e0cd50a072854d624391e716a8fa8c6c82 100644 (file)
@@ -17,6 +17,7 @@ import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.MoreExecutors;
+import java.io.Closeable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
@@ -88,7 +89,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class BrokerFacade {
+@SuppressWarnings("checkstyle:FinalClass")
+public class BrokerFacade implements Closeable {
     private static final Logger LOG = LoggerFactory.getLogger(BrokerFacade.class);
     private static final BrokerFacade INSTANCE = new BrokerFacade();
 
@@ -98,24 +100,37 @@ public class BrokerFacade {
     private DOMNotificationService domNotification;
     private ControllerContext controllerContext;
 
-    BrokerFacade() {
-
+    // Temporary until the static instance is removed.
+    @Deprecated
+    private BrokerFacade() {
     }
 
-    public void setRpcService(final DOMRpcService router) {
-        this.rpcService = router;
+    private BrokerFacade(DOMRpcService rpcService, DOMDataBroker domDataBroker, DOMNotificationService domNotification,
+            ControllerContext controllerContext) {
+        this.rpcService = rpcService;
+        this.domDataBroker = domDataBroker;
+        this.domNotification = domNotification;
+        this.controllerContext = controllerContext;
     }
 
-    public void setDomNotificationService(final DOMNotificationService service) {
-        this.domNotification = service;
+    @Deprecated
+    public static BrokerFacade getInstance() {
+        return BrokerFacade.INSTANCE;
     }
 
-    public void setControllerContext(ControllerContext controllerContext) {
-        this.controllerContext = controllerContext;
+    public static BrokerFacade newInstance(DOMRpcService rpcService, DOMDataBroker domDataBroker,
+            DOMNotificationService domNotification, ControllerContext controllerContext) {
+        INSTANCE.rpcService = rpcService;
+        INSTANCE.domDataBroker = domDataBroker;
+        INSTANCE.controllerContext = controllerContext;
+        INSTANCE.domNotification = domNotification;
+        return INSTANCE;
+        //return new BrokerFacade(pcService, domDataBroker, controllerContext);
     }
 
-    public static BrokerFacade getInstance() {
-        return BrokerFacade.INSTANCE;
+    @Override
+    public void close() {
+        domDataBroker = null;
     }
 
     private void checkPreconditions() {
@@ -1211,10 +1226,6 @@ public class BrokerFacade {
         tx.merge(datastore, path, payload);
     }
 
-    public void setDomDataBroker(final DOMDataBroker domDataBroker) {
-        this.domDataBroker = domDataBroker;
-    }
-
     public void registerToListenNotification(final NotificationListenerAdapter listener) {
         checkPreconditions();
 
index 06e38484754d5ce7454787f8f8d8426a2cbb2e80..5870c5d5a787a0c3d4af4f74636d1545cf3fbb32 100644 (file)
@@ -10,9 +10,6 @@ package org.opendaylight.netconf.sal.restconf.impl;
 import com.google.common.base.Preconditions;
 import java.math.BigInteger;
 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.netconf.sal.rest.api.RestConnector;
 import org.opendaylight.netconf.sal.restconf.impl.jmx.Config;
 import org.opendaylight.netconf.sal.restconf.impl.jmx.Delete;
@@ -28,33 +25,18 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 
 public class RestconfProviderImpl extends AbstractMXBean
         implements AutoCloseable, RestConnector, RestConnectorRuntimeMXBean {
-    private final DOMDataBroker domDataBroker;
-    private final DOMRpcService rpcService;
-    private final DOMNotificationService notificationService;
     private final IpAddress websocketAddress;
     private final PortNumber websocketPort;
     private final StatisticsRestconfServiceWrapper stats = StatisticsRestconfServiceWrapper.getInstance();
-    private final ControllerContext controllerContext;
     private Thread webSocketServerThread;
 
-    public RestconfProviderImpl(DOMDataBroker domDataBroker, DOMRpcService rpcService,
-            DOMNotificationService notificationService, ControllerContext controllerContext,
-            IpAddress websocketAddress, PortNumber websocketPort) {
+    public RestconfProviderImpl(IpAddress websocketAddress, PortNumber websocketPort) {
         super("Draft02ProviderStatistics", "restconf-connector", null);
-        this.domDataBroker = Preconditions.checkNotNull(domDataBroker);
-        this.rpcService = Preconditions.checkNotNull(rpcService);
-        this.notificationService = Preconditions.checkNotNull(notificationService);
         this.websocketAddress = Preconditions.checkNotNull(websocketAddress);
         this.websocketPort = Preconditions.checkNotNull(websocketPort);
-        this.controllerContext = Preconditions.checkNotNull(controllerContext);
     }
 
     public void start() {
-        BrokerFacade.getInstance().setDomDataBroker(domDataBroker);
-        BrokerFacade.getInstance().setRpcService(rpcService);
-        BrokerFacade.getInstance().setDomNotificationService(notificationService);
-        BrokerFacade.getInstance().setControllerContext(controllerContext);
-
         this.webSocketServerThread = new Thread(WebSocketServer.createInstance(
                 new String(websocketAddress.getValue()), websocketPort.getValue()));
         this.webSocketServerThread.setName("Web socket server on port " + websocketPort);
@@ -65,8 +47,6 @@ public class RestconfProviderImpl extends AbstractMXBean
 
     @Override
     public void close() {
-        BrokerFacade.getInstance().setDomDataBroker(null);
-
         WebSocketServer.destroyInstance();
         if (this.webSocketServerThread != null) {
             this.webSocketServerThread.interrupt();
index 0e523d0d4f55676fda6a2bb74982555dd99950be..4286306c21907b8171ce1b9b071d95779853aabf 100644 (file)
     <argument ref="domSchemaService"/>
   </bean>
 
-  <bean id="restconfProviderDraft02" class="org.opendaylight.netconf.sal.restconf.impl.RestconfProviderImpl"
-          init-method="start" destroy-method="close">
-    <argument ref="domDataBroker"/>
+  <bean id="brokerFacade" class="org.opendaylight.netconf.sal.restconf.impl.BrokerFacade"
+      factory-method="newInstance" destroy-method="close">
     <argument ref="domRpcService"/>
+    <argument ref="domDataBroker"/>
     <argument ref="domNotificationService"/>
     <argument ref="controllerContext"/>
+  </bean>
+
+  <bean id="restconfProviderDraft02" class="org.opendaylight.netconf.sal.restconf.impl.RestconfProviderImpl"
+          init-method="start" destroy-method="close">
     <argument ref="webSocketAddress"/>
     <argument ref="webSocketPort"/>
   </bean>
index 287598868ff5582e3355060d6defd43b7db6058f..286db87835b6f57fb30a00dd5e3ab831e24ea72a 100644 (file)
@@ -100,7 +100,7 @@ public class BrokerFacadeTest {
     @Mock
     private DOMDataReadWriteTransaction rwTransaction;
 
-    private final BrokerFacade brokerFacade = BrokerFacade.getInstance();
+    private BrokerFacade brokerFacade;
     private final NormalizedNode<?, ?> dummyNode = createDummyNode("test:module", "2014-01-09", "interfaces");
     private final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> dummyNodeInFuture =
             wrapDummyNode(this.dummyNode);
@@ -116,10 +116,7 @@ public class BrokerFacadeTest {
         controllerContext = TestRestconfUtils.newControllerContext(
                 TestUtils.loadSchemaContext("/full-versions/test-module", "/modules"));
 
-        this.brokerFacade.setDomDataBroker(this.domDataBroker);
-        this.brokerFacade.setDomNotificationService(this.domNotification);
-        this.brokerFacade.setRpcService(this.mockRpcService);
-        this.brokerFacade.setControllerContext(controllerContext);
+        brokerFacade = BrokerFacade.newInstance(mockRpcService, domDataBroker, domNotification, controllerContext);
 
         when(this.domDataBroker.newReadOnlyTransaction()).thenReturn(this.readTransaction);
         when(this.domDataBroker.newWriteOnlyTransaction()).thenReturn(this.writeTransaction);
@@ -169,7 +166,7 @@ public class BrokerFacadeTest {
 
     @Test(expected = RestconfDocumentedException.class)
     public void testReadOperationalDataWithNoDataBroker() {
-        this.brokerFacade.setDomDataBroker(null);
+        this.brokerFacade.close();
 
         this.brokerFacade.readOperationalData(this.instanceID);
     }
@@ -209,7 +206,7 @@ public class BrokerFacadeTest {
 
     @Test(expected = RestconfDocumentedException.class)
     public void testInvokeRpcWithNoConsumerSession() {
-        brokerFacade.setDomDataBroker(null);
+        brokerFacade.close();
         this.brokerFacade.invokeRpc(this.type, this.dummyNode);
     }
 
index 6e644b9ca145d4815351d94b669ef5ac1e7534cf..54742ce0d866fb0ac3a7371561d317177d531504 100644 (file)
@@ -68,7 +68,6 @@ public class RestconfImplNotificationSubscribingTest {
     public void setup() throws Exception {
         MockitoAnnotations.initMocks(this);
 
-        this.broker.setDomDataBroker(this.domDataBroker);
         RestconfImpl.getInstance().setBroker(this.broker);
 
         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);