Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.mastership.MastershipService; |
| 26 | import org.onosproject.net.DeviceId; |
Yuta HIGUCHI | d934003 | 2017-01-25 09:25:44 -0800 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 28 | import org.onosproject.utils.Comparators; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.Collections; |
| 31 | import java.util.List; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 32 | import 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") |
| 39 | public class MastersListCommand extends AbstractShellCommand { |
| 40 | |
| 41 | @Override |
| 42 | protected void execute() { |
| 43 | ClusterService service = get(ClusterService.class); |
| 44 | MastershipService mastershipService = get(MastershipService.class); |
Yuta HIGUCHI | d934003 | 2017-01-25 09:25:44 -0800 | [diff] [blame] | 45 | DeviceService deviceService = get(DeviceService.class); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 46 | List<ControllerNode> nodes = newArrayList(service.getNodes()); |
| 47 | Collections.sort(nodes, Comparators.NODE_COMPARATOR); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 48 | |
| 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 HIGUCHI | d934003 | 2017-01-25 09:25:44 -0800 | [diff] [blame] | 54 | ids.removeIf(did -> deviceService.getDevice(did) == null); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 55 | 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(); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 69 | for (ControllerNode node : nodes) { |
| 70 | List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 71 | result.add(mapper.createObjectNode() |
| 72 | .put("id", node.id().toString()) |
| 73 | .put("size", ids.size()) |
| 74 | .set("devices", json(mapper, ids))); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 75 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 76 | 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; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } |