blob: c21ef93f38c9f3e9ba26cabd0f28b24fb4d105e6 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
tom1380eee2014-09-24 09:22:02 -070017
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
tom1380eee2014-09-24 09:22:02 -070021import com.google.common.collect.Lists;
22import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.mastership.MastershipService;
26import org.onosproject.net.DeviceId;
Yuta HIGUCHId9340032017-01-25 09:25:44 -080027import org.onosproject.net.device.DeviceService;
Ray Milkeyc7477292016-03-11 10:53:43 -080028import org.onosproject.utils.Comparators;
tom1380eee2014-09-24 09:22:02 -070029
30import java.util.Collections;
31import java.util.List;
tom1380eee2014-09-24 09:22:02 -070032import static com.google.common.collect.Lists.newArrayList;
33
34/**
35 * Lists device mastership information.
36 */
37@Command(scope = "onos", name = "masters",
38 description = "Lists device mastership information")
39public class MastersListCommand extends AbstractShellCommand {
40
41 @Override
42 protected void execute() {
43 ClusterService service = get(ClusterService.class);
44 MastershipService mastershipService = get(MastershipService.class);
Yuta HIGUCHId9340032017-01-25 09:25:44 -080045 DeviceService deviceService = get(DeviceService.class);
tom1380eee2014-09-24 09:22:02 -070046 List<ControllerNode> nodes = newArrayList(service.getNodes());
47 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070048
49 if (outputJson()) {
50 print("%s", json(service, mastershipService, nodes));
51 } else {
52 for (ControllerNode node : nodes) {
53 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
Yuta HIGUCHId9340032017-01-25 09:25:44 -080054 ids.removeIf(did -> deviceService.getDevice(did) == null);
tom32085cf2014-10-16 00:04:33 -070055 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
56 print("%s: %d devices", node.id(), ids.size());
57 for (DeviceId deviceId : ids) {
58 print(" %s", deviceId);
59 }
60 }
61 }
62 }
63
64 // Produces JSON structure.
65 private JsonNode json(ClusterService service, MastershipService mastershipService,
66 List<ControllerNode> nodes) {
67 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
tom1380eee2014-09-24 09:22:02 -070069 for (ControllerNode node : nodes) {
70 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
tom32085cf2014-10-16 00:04:33 -070071 result.add(mapper.createObjectNode()
72 .put("id", node.id().toString())
73 .put("size", ids.size())
74 .set("devices", json(mapper, ids)));
tom1380eee2014-09-24 09:22:02 -070075 }
tom32085cf2014-10-16 00:04:33 -070076 return result;
77 }
78
79 /**
80 * Produces a JSON array containing the specified device identifiers.
81 *
82 * @param mapper object mapper
83 * @param ids collection of device identifiers
84 * @return JSON array
85 */
86 public static JsonNode json(ObjectMapper mapper, Iterable<DeviceId> ids) {
87 ArrayNode result = mapper.createArrayNode();
88 for (DeviceId deviceId : ids) {
89 result.add(deviceId.toString());
90 }
91 return result;
tom1380eee2014-09-24 09:22:02 -070092 }
93
94}