blob: 305304b7cfbce7f04885547c9a29b4e5fbb6f38d [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
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.mastership.MastershipAdminService;
25import org.onosproject.net.Device;
26import org.onosproject.net.MastershipRole;
27import org.onosproject.net.device.DeviceService;
28
29/**
30 * Re-configure mastership.
31 */
32@Command(scope = "onos", name = "openstack-reset-mastership",
33 description = "Reconfigure the mastership")
34public class ResetMastershipCommand extends AbstractShellCommand {
35
36 @Option(name = "-c", aliases = "--concentrate",
37 description = "enforce all switches to move to one controller",
38 required = false, multiValued = false)
39 private boolean isConcentrate = false;
40
41 @Option(name = "-b", aliases = "--balance",
42 description = "enforce all switches to be evenly distributed",
43 required = false, multiValued = false)
44 private boolean isBalance = false;
45
46 @Override
47 protected void execute() {
48 MastershipAdminService mastershipService = get(MastershipAdminService.class);
49 ClusterService clusterService = get(ClusterService.class);
50 DeviceService deviceService = get(DeviceService.class);
51
52 if ((isConcentrate && isBalance) || (!isConcentrate && !isBalance)) {
53 print("Please specify either -b or -c option only");
54 return;
55 }
56
57 NodeId localId = clusterService.getLocalNode().id();
58
59 if (isConcentrate) {
60 deviceService.getAvailableDevices(Device.Type.SWITCH).forEach(d ->
61 mastershipService.setRole(localId, d.id(), MastershipRole.MASTER));
62 }
63
64 if (isBalance) {
65 mastershipService.balanceRoles();
66 }
67 }
68}