blob: ae244841d41d97557fe2d4de335d842f4a966fcf [file] [log] [blame]
Ray Milkeyd84f89b2018-08-17 14:54:17 -07001/*
2 * Copyright 2014-present Open Networking Foundation
3 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.cli2;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.google.common.collect.Lists;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Command;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.mastership.MastershipService;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.utils.Comparators;
30
31import java.util.Collections;
32import java.util.List;
33import static com.google.common.collect.Lists.newArrayList;
34
35/**
36 * Lists device mastership information.
37 */
38@Command(scope = "onos", name = "masters",
39 description = "Lists device mastership information")
40@Service
41public class MastersListCommand extends AbstractShellCommand {
42
43 @Override
44 protected void doExecute() {
45 ClusterService service = get(ClusterService.class);
46 MastershipService mastershipService = get(MastershipService.class);
47 DeviceService deviceService = get(DeviceService.class);
48 List<ControllerNode> nodes = newArrayList(service.getNodes());
49 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
50
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()));
56 ids.removeIf(did -> deviceService.getDevice(did) == null);
57 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();
71 for (ControllerNode node : nodes) {
72 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
73 result.add(mapper.createObjectNode()
74 .put("id", node.id().toString())
75 .put("size", ids.size())
76 .set("devices", json(mapper, ids)));
77 }
78 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;
94 }
95
96}