blob: 00a70e6f51b2b09084346be875f464b1ce7a02b3 [file] [log] [blame]
Jian Li43d04282018-08-22 14:31:21 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.openstacktroubleshoot.cli;
18
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Li43d04282018-08-22 14:31:21 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.mastership.MastershipAdminService;
26import org.onosproject.net.Device;
27import org.onosproject.net.MastershipRole;
28import org.onosproject.net.device.DeviceService;
29
30/**
31 * Re-configure mastership.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
Jian Li43d04282018-08-22 14:31:21 +090034@Command(scope = "onos", name = "openstack-reset-mastership",
35 description = "Reconfigure the mastership")
36public class ResetMastershipCommand extends AbstractShellCommand {
37
38 @Option(name = "-c", aliases = "--concentrate",
39 description = "enforce all switches to move to one controller",
40 required = false, multiValued = false)
41 private boolean isConcentrate = false;
42
43 @Option(name = "-b", aliases = "--balance",
44 description = "enforce all switches to be evenly distributed",
45 required = false, multiValued = false)
46 private boolean isBalance = false;
47
48 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070049 protected void doExecute() {
Jian Li43d04282018-08-22 14:31:21 +090050 MastershipAdminService mastershipService = get(MastershipAdminService.class);
51 ClusterService clusterService = get(ClusterService.class);
52 DeviceService deviceService = get(DeviceService.class);
53
54 if ((isConcentrate && isBalance) || (!isConcentrate && !isBalance)) {
55 print("Please specify either -b or -c option only");
56 return;
57 }
58
59 NodeId localId = clusterService.getLocalNode().id();
60
61 if (isConcentrate) {
62 deviceService.getAvailableDevices(Device.Type.SWITCH).forEach(d ->
63 mastershipService.setRole(localId, d.id(), MastershipRole.MASTER));
64 }
65
66 if (isBalance) {
67 mastershipService.balanceRoles();
68 }
69 }
70}