Merge "BUG 2676 : Introduce API for accessing akka dispatchers"
authorTom Pantelis <tpanteli@brocade.com>
Mon, 23 Feb 2015 23:13:35 +0000 (23:13 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 23 Feb 2015 23:13:36 +0000 (23:13 +0000)
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/Dispatchers.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/DispatchersTest.java [new file with mode: 0644]

diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/Dispatchers.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/Dispatchers.java
new file mode 100644 (file)
index 0000000..8de8a9d
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. 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.controller.cluster.datastore.utils;
+
+import com.google.common.base.Preconditions;
+import scala.concurrent.ExecutionContext;
+
+public class Dispatchers {
+    public static final String DEFAULT_DISPATCHER_PATH = "akka.actor.default-dispatcher";
+    public static final String CLIENT_DISPATCHER_PATH = "client-dispatcher";
+    public static final String TXN_DISPATCHER_PATH = "txn-dispatcher";
+    public static final String SHARD_DISPATCHER_PATH = "shard-dispatcher";
+    public static final String NOTIFICATION_DISPATCHER_PATH = "notification-dispatcher";
+
+    private final akka.dispatch.Dispatchers dispatchers;
+
+    public static enum DispatcherType {
+        Client(CLIENT_DISPATCHER_PATH),
+        Transaction(TXN_DISPATCHER_PATH),
+        Shard(SHARD_DISPATCHER_PATH),
+        Notification(NOTIFICATION_DISPATCHER_PATH);
+
+        private final String path;
+        private DispatcherType(String path){
+            this.path = path;
+        }
+        private String path(akka.dispatch.Dispatchers dispatchers){
+            if(dispatchers.hasDispatcher(path)){
+                return path;
+            }
+            return DEFAULT_DISPATCHER_PATH;
+        }
+
+        private ExecutionContext dispatcher(akka.dispatch.Dispatchers dispatchers){
+            if(dispatchers.hasDispatcher(path)){
+                return dispatchers.lookup(path);
+            }
+            return dispatchers.defaultGlobalDispatcher();
+        }
+    }
+
+    public Dispatchers(akka.dispatch.Dispatchers dispatchers){
+        Preconditions.checkNotNull(dispatchers, "dispatchers should not be null");
+        this.dispatchers = dispatchers;
+    }
+
+    public ExecutionContext getDispatcher(DispatcherType dispatcherType){
+        return dispatcherType.dispatcher(this.dispatchers);
+    }
+
+    public String getDispatcherPath(DispatcherType dispatcherType){
+        return dispatcherType.path(this.dispatchers);
+    }
+}
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/DispatchersTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/DispatchersTest.java
new file mode 100644 (file)
index 0000000..85a0cac
--- /dev/null
@@ -0,0 +1,81 @@
+package org.opendaylight.controller.cluster.datastore.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import akka.dispatch.MessageDispatcher;
+import org.junit.Test;
+
+public class DispatchersTest {
+
+    @Test
+    public void testGetDefaultDispatcherPath(){
+        akka.dispatch.Dispatchers mockDispatchers = mock(akka.dispatch.Dispatchers.class);
+        doReturn(false).when(mockDispatchers).hasDispatcher(anyString());
+        Dispatchers dispatchers = new Dispatchers(mockDispatchers);
+
+        for(Dispatchers.DispatcherType type : Dispatchers.DispatcherType.values()) {
+            assertEquals(Dispatchers.DEFAULT_DISPATCHER_PATH,
+                    dispatchers.getDispatcherPath(type));
+        }
+
+    }
+
+    @Test
+    public void testGetDefaultDispatcher(){
+        akka.dispatch.Dispatchers mockDispatchers = mock(akka.dispatch.Dispatchers.class);
+        MessageDispatcher mockGlobalDispatcher = mock(MessageDispatcher.class);
+        doReturn(false).when(mockDispatchers).hasDispatcher(anyString());
+        doReturn(mockGlobalDispatcher).when(mockDispatchers).defaultGlobalDispatcher();
+        Dispatchers dispatchers = new Dispatchers(mockDispatchers);
+
+        for(Dispatchers.DispatcherType type : Dispatchers.DispatcherType.values()) {
+            assertEquals(mockGlobalDispatcher,
+                    dispatchers.getDispatcher(type));
+        }
+
+    }
+
+    @Test
+    public void testGetDispatcherPath(){
+        akka.dispatch.Dispatchers mockDispatchers = mock(akka.dispatch.Dispatchers.class);
+        doReturn(true).when(mockDispatchers).hasDispatcher(anyString());
+        Dispatchers dispatchers = new Dispatchers(mockDispatchers);
+
+        assertEquals(Dispatchers.CLIENT_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Client));
+
+        assertEquals(Dispatchers.TXN_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Transaction));
+
+        assertEquals(Dispatchers.SHARD_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Shard));
+
+        assertEquals(Dispatchers.NOTIFICATION_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Notification));
+
+    }
+
+    @Test
+    public void testGetDispatcher(){
+        akka.dispatch.Dispatchers mockDispatchers = mock(akka.dispatch.Dispatchers.class);
+        MessageDispatcher mockDispatcher = mock(MessageDispatcher.class);
+        doReturn(true).when(mockDispatchers).hasDispatcher(anyString());
+        doReturn(mockDispatcher).when(mockDispatchers).lookup(anyString());
+        Dispatchers dispatchers = new Dispatchers(mockDispatchers);
+
+        assertEquals(Dispatchers.CLIENT_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Client));
+
+        assertEquals(Dispatchers.TXN_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Transaction));
+
+        assertEquals(Dispatchers.SHARD_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Shard));
+
+        assertEquals(Dispatchers.NOTIFICATION_DISPATCHER_PATH,
+                dispatchers.getDispatcherPath(Dispatchers.DispatcherType.Notification));
+
+    }
+}
\ No newline at end of file

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.