Support rebalance the mastership for running openstack troubleshoot

Change-Id: Ibe7e8bbc60211451f57bea9e5de6a1575b03d36b
diff --git a/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackEastWestProbeCommand.java b/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackEastWestProbeCommand.java
index 0b832da..918bff6 100644
--- a/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackEastWestProbeCommand.java
+++ b/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackEastWestProbeCommand.java
@@ -21,8 +21,13 @@
 import org.apache.karaf.shell.commands.Option;
 import org.onlab.packet.IpAddress;
 import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.mastership.MastershipService;
 import org.onosproject.openstacknetworking.api.InstancePort;
 import org.onosproject.openstacknetworking.api.InstancePortService;
+import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackNodeService;
 import org.onosproject.openstacktroubleshoot.api.OpenstackTroubleshootService;
 import org.onosproject.openstacktroubleshoot.api.Reachability;
 
@@ -31,6 +36,7 @@
 import java.util.stream.Collectors;
 
 import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
+import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
 
 /**
  * Checks the east-west VMs connectivity.
@@ -55,10 +61,11 @@
 
     @Override
     protected void execute() {
-        OpenstackTroubleshootService tsService =
-                get(OpenstackTroubleshootService.class);
-
+        OpenstackTroubleshootService tsService = get(OpenstackTroubleshootService.class);
         InstancePortService instPortService = get(InstancePortService.class);
+        MastershipService mastershipService = get(MastershipService.class);
+        ClusterService clusterService = get(ClusterService.class);
+        OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
 
         if (tsService == null) {
             error("Failed to troubleshoot openstack networking.");
@@ -70,6 +77,16 @@
             return;
         }
 
+        NodeId localNodeId = clusterService.getLocalNode().id();
+
+        for (OpenstackNode node : osNodeService.completeNodes(COMPUTE)) {
+            if (!localNodeId.equals(mastershipService.getMasterFor(node.intgBridge()))) {
+                error("Current node is not the master for all compute nodes. " +
+                        "Please enforce mastership first using openstack-reset-mastership -c !");
+                return;
+            }
+        }
+
         if (isAll) {
             printHeader();
             // send ICMP PACKET_OUT to all connect VMs whose instance port state is ACTIVE
diff --git a/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackNorthSouthProbeCommand.java b/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackNorthSouthProbeCommand.java
index 77c24bb..c409641 100644
--- a/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackNorthSouthProbeCommand.java
+++ b/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/OpenstackNorthSouthProbeCommand.java
@@ -79,7 +79,8 @@
 
         for (OpenstackNode gw : osNodeService.completeNodes(GATEWAY)) {
             if (!localNodeId.equals(mastershipService.getMasterFor(gw.intgBridge()))) {
-                error("Current node is not the master for all gateway nodes. Please enforce mastership first!");
+                error("Current node is not the master for all gateway nodes. " +
+                        "Please enforce mastership first using openstack-reset-mastership -c !");
                 return;
             }
         }
diff --git a/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/ResetMastershipCommand.java b/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/ResetMastershipCommand.java
new file mode 100644
index 0000000..305304b
--- /dev/null
+++ b/apps/openstacktroubleshoot/app/src/main/java/org/onosproject/openstacktroubleshoot/cli/ResetMastershipCommand.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.openstacktroubleshoot.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.mastership.MastershipAdminService;
+import org.onosproject.net.Device;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.device.DeviceService;
+
+/**
+ * Re-configure mastership.
+ */
+@Command(scope = "onos", name = "openstack-reset-mastership",
+        description = "Reconfigure the mastership")
+public class ResetMastershipCommand extends AbstractShellCommand {
+
+    @Option(name = "-c", aliases = "--concentrate",
+            description = "enforce all switches to move to one controller",
+            required = false, multiValued = false)
+    private boolean isConcentrate = false;
+
+    @Option(name = "-b", aliases = "--balance",
+            description = "enforce all switches to be evenly distributed",
+            required = false, multiValued = false)
+    private boolean isBalance = false;
+
+    @Override
+    protected void execute() {
+        MastershipAdminService mastershipService = get(MastershipAdminService.class);
+        ClusterService clusterService = get(ClusterService.class);
+        DeviceService deviceService = get(DeviceService.class);
+
+        if ((isConcentrate && isBalance) || (!isConcentrate && !isBalance)) {
+            print("Please specify either -b or -c option only");
+            return;
+        }
+
+        NodeId localId = clusterService.getLocalNode().id();
+
+        if (isConcentrate) {
+            deviceService.getAvailableDevices(Device.Type.SWITCH).forEach(d ->
+                    mastershipService.setRole(localId, d.id(), MastershipRole.MASTER));
+        }
+
+        if (isBalance) {
+            mastershipService.balanceRoles();
+        }
+    }
+}
diff --git a/apps/openstacktroubleshoot/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/openstacktroubleshoot/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index fa3dabd..791a58d 100644
--- a/apps/openstacktroubleshoot/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/openstacktroubleshoot/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -27,6 +27,9 @@
                 <ref component-id="activeFloatingIpCompleter"/>
             </completers>
         </command>
+        <command>
+            <action class="org.onosproject.openstacktroubleshoot.cli.ResetMastershipCommand"/>
+        </command>
     </command-bundle>
 
     <bean id="activeVmIpCompleter" class="org.onosproject.openstacktroubleshoot.cli.ActiveVmIpCompleter"/>