blob: 6debc1b70cb7f242fda66ee5cef3424c05262255 [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;
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;
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.ClusterService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.mastership.MastershipService;
27import org.onosproject.net.DeviceId;
Yuta HIGUCHId9340032017-01-25 09:25:44 -080028import org.onosproject.net.device.DeviceService;
Ray Milkeyc7477292016-03-11 10:53:43 -080029import org.onosproject.utils.Comparators;
tom1380eee2014-09-24 09:22:02 -070030
31import java.util.Collections;
32import java.util.List;
tom1380eee2014-09-24 09:22:02 -070033import static com.google.common.collect.Lists.newArrayList;
34
35/**
36 * Lists device mastership information.
37 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038@Service
tom1380eee2014-09-24 09:22:02 -070039@Command(scope = "onos", name = "masters",
40 description = "Lists device mastership information")
41public class MastersListCommand extends AbstractShellCommand {
42
43 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044 protected void doExecute() {
tom1380eee2014-09-24 09:22:02 -070045 ClusterService service = get(ClusterService.class);
46 MastershipService mastershipService = get(MastershipService.class);
Yuta HIGUCHId9340032017-01-25 09:25:44 -080047 DeviceService deviceService = get(DeviceService.class);
tom1380eee2014-09-24 09:22:02 -070048 List<ControllerNode> nodes = newArrayList(service.getNodes());
49 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070050
51 if (outputJson()) {
52 print("%s", json(service, mastershipService, nodes));
53 } else {
54 for (ControllerNode node : nodes) {
55 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
Yuta HIGUCHId9340032017-01-25 09:25:44 -080056 ids.removeIf(did -> deviceService.getDevice(did) == null);
tom32085cf2014-10-16 00:04:33 -070057 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
58 print("%s: %d devices", node.id(), ids.size());
59 for (DeviceId deviceId : ids) {
60 print(" %s", deviceId);
61 }
62 }
63 }
64 }
65
66 // Produces JSON structure.
67 private JsonNode json(ClusterService service, MastershipService mastershipService,
68 List<ControllerNode> nodes) {
69 ObjectMapper mapper = new ObjectMapper();
70 ArrayNode result = mapper.createArrayNode();
tom1380eee2014-09-24 09:22:02 -070071 for (ControllerNode node : nodes) {
72 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
tom32085cf2014-10-16 00:04:33 -070073 result.add(mapper.createObjectNode()
74 .put("id", node.id().toString())
75 .put("size", ids.size())
76 .set("devices", json(mapper, ids)));
tom1380eee2014-09-24 09:22:02 -070077 }
tom32085cf2014-10-16 00:04:33 -070078 return result;
79 }
80
81 /**
82 * Produces a JSON array containing the specified device identifiers.
83 *
84 * @param mapper object mapper
85 * @param ids collection of device identifiers
86 * @return JSON array
87 */
88 public static JsonNode json(ObjectMapper mapper, Iterable<DeviceId> ids) {
89 ArrayNode result = mapper.createArrayNode();
90 for (DeviceId deviceId : ids) {
91 result.add(deviceId.toString());
92 }
93 return result;
tom1380eee2014-09-24 09:22:02 -070094 }
95
96}