blob: d5b05045b1e45bbea55381e065f2cf5357575ac4 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Ayaka Koshibee8e45352014-10-16 00:37:19 -070016package org.onlab.onos.cli;
17
Thomas Vachuska444eda62014-10-28 13:09:42 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070022import org.apache.karaf.shell.commands.Command;
23import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070024import org.onlab.onos.cluster.RoleInfo;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070025import org.onlab.onos.mastership.MastershipService;
26import org.onlab.onos.net.Device;
27import org.onlab.onos.net.DeviceId;
28import org.onlab.onos.net.device.DeviceService;
29
Thomas Vachuska444eda62014-10-28 13:09:42 -070030import java.util.List;
31
32import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070033
34/**
35 * Lists mastership roles of nodes for each device.
36 */
37@Command(scope = "onos", name = "roles",
Thomas Vachuska444eda62014-10-28 13:09:42 -070038 description = "Lists mastership roles of nodes for each device.")
Ayaka Koshibee8e45352014-10-16 00:37:19 -070039public class RolesCommand extends AbstractShellCommand {
40
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070041 private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]";
Ayaka Koshibee8e45352014-10-16 00:37:19 -070042
43 @Override
44 protected void execute() {
45 DeviceService deviceService = get(DeviceService.class);
46 MastershipService roleService = get(MastershipService.class);
47
Thomas Vachuska444eda62014-10-28 13:09:42 -070048 if (outputJson()) {
49 print("%s", json(roleService, getSortedDevices(deviceService)));
50 } else {
51 for (Device d : getSortedDevices(deviceService)) {
52 DeviceId did = d.id();
53 printRoles(roleService, did);
54 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070055 }
56 }
57
Thomas Vachuska444eda62014-10-28 13:09:42 -070058 // Produces JSON structure with role information for the given devices.
59 private JsonNode json(MastershipService service, List<Device> sortedDevices) {
60 ObjectMapper mapper = new ObjectMapper();
61 ArrayNode results = mapper.createArrayNode();
62 for (Device device : sortedDevices) {
63 results.add(json(service, mapper, device));
64 }
65 return results;
66 }
67
68 // Produces JSON structure with role information for the given device.
69 private JsonNode json(MastershipService service, ObjectMapper mapper,
70 Device device) {
71 NodeId master = service.getMasterFor(device.id());
72 ObjectNode result = mapper.createObjectNode()
73 .put("id", device.id().toString())
74 .put("master", master != null ? master.toString() : "none");
75 RoleInfo nodes = service.getNodesFor(device.id());
76 ArrayNode standbys = mapper.createArrayNode();
77 for (NodeId nid : nodes.backups()) {
78 standbys.add(nid.toString());
79 }
80 result.set("standbys", standbys);
81 return result;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070082 }
83
84 /**
85 * Prints the role information for a device.
86 *
Thomas Vachuska444eda62014-10-28 13:09:42 -070087 * @param service mastership service
Ayaka Koshibee8e45352014-10-16 00:37:19 -070088 * @param deviceId the ID of the device
Ayaka Koshibee8e45352014-10-16 00:37:19 -070089 */
90 protected void printRoles(MastershipService service, DeviceId deviceId) {
Ayaka Koshibeabedb092014-10-20 17:01:31 -070091 RoleInfo nodes = service.getNodesFor(deviceId);
92 StringBuilder builder = new StringBuilder();
93 for (NodeId nid : nodes.backups()) {
94 builder.append(nid).append(" ");
95 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070096
Ayaka Koshibeabedb092014-10-20 17:01:31 -070097 print(FMT_HDR, deviceId,
Thomas Vachuska444eda62014-10-28 13:09:42 -070098 nodes.master() == null ? "NONE" : nodes.master(),
99 builder.toString());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700100 }
101}