blob: 88fa8fe73481e6120de08a126c21adf72d1d5755 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070017
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cluster.NodeId;
25import org.onosproject.cluster.RoleInfo;
26import org.onosproject.mastership.MastershipService;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.device.DeviceService;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070030
Thomas Vachuska444eda62014-10-28 13:09:42 -070031import java.util.List;
32
Brian O'Connorabafb502014-12-02 22:26:20 -080033import static org.onosproject.cli.net.DevicesListCommand.getSortedDevices;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070034
35/**
36 * Lists mastership roles of nodes for each device.
37 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038@Service
Ayaka Koshibee8e45352014-10-16 00:37:19 -070039@Command(scope = "onos", name = "roles",
Thomas Vachuska444eda62014-10-28 13:09:42 -070040 description = "Lists mastership roles of nodes for each device.")
Ayaka Koshibee8e45352014-10-16 00:37:19 -070041public class RolesCommand extends AbstractShellCommand {
42
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070043 private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]";
Ayaka Koshibee8e45352014-10-16 00:37:19 -070044
45 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046 protected void doExecute() {
Ayaka Koshibee8e45352014-10-16 00:37:19 -070047 DeviceService deviceService = get(DeviceService.class);
48 MastershipService roleService = get(MastershipService.class);
49
Thomas Vachuska444eda62014-10-28 13:09:42 -070050 if (outputJson()) {
51 print("%s", json(roleService, getSortedDevices(deviceService)));
52 } else {
53 for (Device d : getSortedDevices(deviceService)) {
54 DeviceId did = d.id();
55 printRoles(roleService, did);
56 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070057 }
58 }
59
Thomas Vachuska444eda62014-10-28 13:09:42 -070060 // Produces JSON structure with role information for the given devices.
61 private JsonNode json(MastershipService service, List<Device> sortedDevices) {
62 ObjectMapper mapper = new ObjectMapper();
63 ArrayNode results = mapper.createArrayNode();
64 for (Device device : sortedDevices) {
65 results.add(json(service, mapper, device));
66 }
67 return results;
68 }
69
70 // Produces JSON structure with role information for the given device.
71 private JsonNode json(MastershipService service, ObjectMapper mapper,
72 Device device) {
73 NodeId master = service.getMasterFor(device.id());
74 ObjectNode result = mapper.createObjectNode()
75 .put("id", device.id().toString())
76 .put("master", master != null ? master.toString() : "none");
77 RoleInfo nodes = service.getNodesFor(device.id());
78 ArrayNode standbys = mapper.createArrayNode();
79 for (NodeId nid : nodes.backups()) {
80 standbys.add(nid.toString());
81 }
82 result.set("standbys", standbys);
83 return result;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070084 }
85
86 /**
87 * Prints the role information for a device.
88 *
Thomas Vachuska444eda62014-10-28 13:09:42 -070089 * @param service mastership service
Ayaka Koshibee8e45352014-10-16 00:37:19 -070090 * @param deviceId the ID of the device
Ayaka Koshibee8e45352014-10-16 00:37:19 -070091 */
92 protected void printRoles(MastershipService service, DeviceId deviceId) {
Ayaka Koshibeabedb092014-10-20 17:01:31 -070093 RoleInfo nodes = service.getNodesFor(deviceId);
94 StringBuilder builder = new StringBuilder();
95 for (NodeId nid : nodes.backups()) {
96 builder.append(nid).append(" ");
97 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070098
Ayaka Koshibeabedb092014-10-20 17:01:31 -070099 print(FMT_HDR, deviceId,
Thomas Vachuska444eda62014-10-28 13:09:42 -0700100 nodes.master() == null ? "NONE" : nodes.master(),
101 builder.toString());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700102 }
103}