From 45503ce0b25bd5f9e1358980c005c20665a5e01f Mon Sep 17 00:00:00 2001 From: Ayaka Koshibe Date: Tue, 14 Oct 2014 11:26:45 -0700 Subject: [PATCH] list mastership roles CLI command Change-Id: I54dc296f90c4b8ceebe4e86816c3796da4d2d714 --- .../OSGI-INF/blueprint/shell-config.xml | 4 +++ .../onos/mastership/MastershipService.java | 10 +++++++ .../onos/mastership/MastershipStore.java | 11 ++++++++ .../mastership/MastershipServiceAdapter.java | 6 +++++ .../onos/cluster/impl/MastershipManager.java | 6 +++++ .../cluster/impl/DistributedClusterStore.java | 4 +-- .../impl/DistributedMastershipStore.java | 26 +++++++++++++++++++ .../trivial/impl/SimpleMastershipStore.java | 6 +++++ 8 files changed, 71 insertions(+), 2 deletions(-) diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml index a0b9f50c0b..101cc82664 100644 --- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml +++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml @@ -13,6 +13,10 @@ + + + + diff --git a/core/api/src/main/java/org/onlab/onos/mastership/MastershipService.java b/core/api/src/main/java/org/onlab/onos/mastership/MastershipService.java index 029e357ba0..224bc05ceb 100644 --- a/core/api/src/main/java/org/onlab/onos/mastership/MastershipService.java +++ b/core/api/src/main/java/org/onlab/onos/mastership/MastershipService.java @@ -1,5 +1,6 @@ package org.onlab.onos.mastership; +import java.util.List; import java.util.Set; import org.onlab.onos.cluster.NodeId; @@ -49,6 +50,15 @@ public interface MastershipService { */ NodeId getMasterFor(DeviceId deviceId); + /** + * Returns controllers connected to a given device, in order of + * preference. The first entry in the list is the current master. + * + * @param deviceId the identifier of the device + * @return a list of controller IDs + */ + List getNodesFor(DeviceId deviceId); + /** * Returns the devices for which a controller is master. * diff --git a/core/api/src/main/java/org/onlab/onos/mastership/MastershipStore.java b/core/api/src/main/java/org/onlab/onos/mastership/MastershipStore.java index 0117d0d06e..5e7b0e4932 100644 --- a/core/api/src/main/java/org/onlab/onos/mastership/MastershipStore.java +++ b/core/api/src/main/java/org/onlab/onos/mastership/MastershipStore.java @@ -1,5 +1,6 @@ package org.onlab.onos.mastership; +import java.util.List; import java.util.Set; import org.onlab.onos.cluster.NodeId; @@ -40,6 +41,15 @@ public interface MastershipStore extends Store getNodes(DeviceId deviceId); + /** * Returns the devices that a controller instance is master of. * @@ -48,6 +58,7 @@ public interface MastershipStore extends Store getDevices(NodeId nodeId); + /** * Sets a device's role for a specified controller instance. * diff --git a/core/api/src/test/java/org/onlab/onos/mastership/MastershipServiceAdapter.java b/core/api/src/test/java/org/onlab/onos/mastership/MastershipServiceAdapter.java index 97b57e566e..af376e8738 100644 --- a/core/api/src/test/java/org/onlab/onos/mastership/MastershipServiceAdapter.java +++ b/core/api/src/test/java/org/onlab/onos/mastership/MastershipServiceAdapter.java @@ -4,6 +4,7 @@ import org.onlab.onos.cluster.NodeId; import org.onlab.onos.net.DeviceId; import org.onlab.onos.net.MastershipRole; +import java.util.List; import java.util.Set; /** @@ -46,4 +47,9 @@ public class MastershipServiceAdapter implements MastershipService { public MastershipTermService requestTermService() { return null; } + + @Override + public List getNodesFor(DeviceId deviceId) { + return null; + } } diff --git a/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java b/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java index 59614daf5e..4bcaff4a1e 100644 --- a/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java +++ b/core/net/src/main/java/org/onlab/onos/cluster/impl/MastershipManager.java @@ -3,6 +3,7 @@ package org.onlab.onos.cluster.impl; import static com.google.common.base.Preconditions.checkNotNull; import static org.slf4j.LoggerFactory.getLogger; +import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; @@ -127,6 +128,11 @@ implements MastershipService, MastershipAdminService { return store.getDevices(nodeId); } + @Override + public List getNodesFor(DeviceId deviceId) { + checkNotNull(deviceId, DEVICE_ID_NULL); + return store.getNodes(deviceId); + } @Override public MastershipTermService requestTermService() { diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java index 5e64a39c6c..8f48890259 100644 --- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java +++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java @@ -54,7 +54,7 @@ public class DistributedClusterStore @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) private ClusterCommunicationAdminService clusterCommunicationAdminService; - private final ClusterNodesDelegate nodesDelegate = new InnerNodesDelegate(); + private final ClusterNodesDelegate nodesDelegate = new InternalNodesDelegate(); @Activate public void activate() throws IOException { @@ -151,7 +151,7 @@ public class DistributedClusterStore } // Entity to handle back calls from the connection manager. - private class InnerNodesDelegate implements ClusterNodesDelegate { + private class InternalNodesDelegate implements ClusterNodesDelegate { @Override public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) { DefaultControllerNode node = nodes.get(nodeId); diff --git a/core/store/hz/cluster/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java b/core/store/hz/cluster/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java index e073b636fc..1def9f9fa0 100644 --- a/core/store/hz/cluster/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java +++ b/core/store/hz/cluster/src/main/java/org/onlab/onos/store/mastership/impl/DistributedMastershipStore.java @@ -2,6 +2,8 @@ package org.onlab.onos.store.mastership.impl; import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; @@ -143,6 +145,30 @@ implements MastershipStore { return deserialize(masters.get(serialize(deviceId))); } + + @Override + public List getNodes(DeviceId deviceId) { + byte [] did = serialize(deviceId); + List nodes = new LinkedList<>(); + + //add current master to head - if there is one + ILock lock = theInstance.getLock(LOCK); + lock.lock(); + try { + byte [] master = masters.get(did); + if (master != null) { + nodes.add((NodeId) deserialize(master)); + } + + for (byte [] el : standbys.get(serialize(deviceId))) { + nodes.add((NodeId) deserialize(el)); + } + return nodes; + } finally { + lock.unlock(); + } + } + @Override public Set getDevices(NodeId nodeId) { ImmutableSet.Builder builder = ImmutableSet.builder(); diff --git a/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleMastershipStore.java b/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleMastershipStore.java index aba77d0ad4..a6cfe8ba91 100644 --- a/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleMastershipStore.java +++ b/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleMastershipStore.java @@ -5,6 +5,7 @@ import static org.slf4j.LoggerFactory.getLogger; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; @@ -94,6 +95,11 @@ public class SimpleMastershipStore return masterMap.get(deviceId); } + @Override + public List getNodes(DeviceId deviceId) { + return null; + } + @Override public Set getDevices(NodeId nodeId) { Set ids = new HashSet<>();