blob: 4ba11b424348ee8c7236af85336922c28e5daaaf [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Ayaka Koshibee8e45352014-10-16 00:37:19 -070019package org.onlab.onos.cli;
20
Thomas Vachuska444eda62014-10-28 13:09:42 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070025import org.apache.karaf.shell.commands.Command;
26import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070027import org.onlab.onos.cluster.RoleInfo;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070028import org.onlab.onos.mastership.MastershipService;
29import org.onlab.onos.net.Device;
30import org.onlab.onos.net.DeviceId;
31import org.onlab.onos.net.device.DeviceService;
32
Thomas Vachuska444eda62014-10-28 13:09:42 -070033import java.util.List;
34
35import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070036
37/**
38 * Lists mastership roles of nodes for each device.
39 */
40@Command(scope = "onos", name = "roles",
Thomas Vachuska444eda62014-10-28 13:09:42 -070041 description = "Lists mastership roles of nodes for each device.")
Ayaka Koshibee8e45352014-10-16 00:37:19 -070042public class RolesCommand extends AbstractShellCommand {
43
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070044 private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]";
Ayaka Koshibee8e45352014-10-16 00:37:19 -070045
46 @Override
47 protected void execute() {
48 DeviceService deviceService = get(DeviceService.class);
49 MastershipService roleService = get(MastershipService.class);
50
Thomas Vachuska444eda62014-10-28 13:09:42 -070051 if (outputJson()) {
52 print("%s", json(roleService, getSortedDevices(deviceService)));
53 } else {
54 for (Device d : getSortedDevices(deviceService)) {
55 DeviceId did = d.id();
56 printRoles(roleService, did);
57 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070058 }
59 }
60
Thomas Vachuska444eda62014-10-28 13:09:42 -070061 // Produces JSON structure with role information for the given devices.
62 private JsonNode json(MastershipService service, List<Device> sortedDevices) {
63 ObjectMapper mapper = new ObjectMapper();
64 ArrayNode results = mapper.createArrayNode();
65 for (Device device : sortedDevices) {
66 results.add(json(service, mapper, device));
67 }
68 return results;
69 }
70
71 // Produces JSON structure with role information for the given device.
72 private JsonNode json(MastershipService service, ObjectMapper mapper,
73 Device device) {
74 NodeId master = service.getMasterFor(device.id());
75 ObjectNode result = mapper.createObjectNode()
76 .put("id", device.id().toString())
77 .put("master", master != null ? master.toString() : "none");
78 RoleInfo nodes = service.getNodesFor(device.id());
79 ArrayNode standbys = mapper.createArrayNode();
80 for (NodeId nid : nodes.backups()) {
81 standbys.add(nid.toString());
82 }
83 result.set("standbys", standbys);
84 return result;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070085 }
86
87 /**
88 * Prints the role information for a device.
89 *
Thomas Vachuska444eda62014-10-28 13:09:42 -070090 * @param service mastership service
Ayaka Koshibee8e45352014-10-16 00:37:19 -070091 * @param deviceId the ID of the device
Ayaka Koshibee8e45352014-10-16 00:37:19 -070092 */
93 protected void printRoles(MastershipService service, DeviceId deviceId) {
Ayaka Koshibeabedb092014-10-20 17:01:31 -070094 RoleInfo nodes = service.getNodesFor(deviceId);
95 StringBuilder builder = new StringBuilder();
96 for (NodeId nid : nodes.backups()) {
97 builder.append(nid).append(" ");
98 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070099
Ayaka Koshibeabedb092014-10-20 17:01:31 -0700100 print(FMT_HDR, deviceId,
Thomas Vachuska444eda62014-10-28 13:09:42 -0700101 nodes.master() == null ? "NONE" : nodes.master(),
102 builder.toString());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700103 }
104}