Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 20 | import com.google.common.collect.Lists; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Argument; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Device; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.group.Group; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 29 | import org.onosproject.net.group.Group.GroupState; |
| 30 | import org.onosproject.net.group.GroupBucket; |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 31 | import org.onosproject.net.group.GroupDescription; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 32 | import org.onosproject.net.group.GroupService; |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 33 | import org.onosproject.utils.Comparators; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 34 | |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 35 | import java.util.Collections; |
| 36 | import java.util.List; |
| 37 | import java.util.Map; |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 38 | import java.util.Optional; |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 39 | import java.util.SortedMap; |
| 40 | import java.util.TreeMap; |
| 41 | import java.util.stream.Collectors; |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 42 | import java.util.stream.Stream; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Lists all groups in the system. |
| 46 | */ |
| 47 | @Command(scope = "onos", name = "groups", |
| 48 | description = "Lists all groups in the system") |
| 49 | public class GroupsListCommand extends AbstractShellCommand { |
| 50 | |
Charles Chan | f38aca7 | 2016-02-18 13:40:18 -0800 | [diff] [blame] | 51 | public static final String ANY = "any"; |
| 52 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 53 | private static final String FORMAT = |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 54 | " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 55 | private static final String BUCKET_FORMAT = |
Andreas Pantelopoulos | 250ad71 | 2017-10-02 17:44:59 -0400 | [diff] [blame] | 56 | " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 57 | |
| 58 | @Argument(index = 1, name = "uri", description = "Device ID", |
| 59 | required = false, multiValued = false) |
| 60 | String uri = null; |
| 61 | |
| 62 | @Argument(index = 0, name = "state", description = "Group state", |
| 63 | required = false, multiValued = false) |
| 64 | String state; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 65 | |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 66 | @Option(name = "-c", aliases = "--count", |
| 67 | description = "Print group count only", |
| 68 | required = false, multiValued = false) |
| 69 | private boolean countOnly = false; |
| 70 | |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 71 | @Option(name = "-r", aliases = "--referenced", |
| 72 | description = "Print referenced groups only", |
| 73 | required = false, multiValued = false) |
| 74 | private boolean referencedOnly = false; |
| 75 | |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 76 | @Option(name = "-t", aliases = "--type", |
| 77 | description = "Print groups with specified type", |
| 78 | required = false, multiValued = false) |
| 79 | private String type = null; |
| 80 | |
| 81 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 82 | private JsonNode json(Map<Device, List<Group>> sortedGroups) { |
| 83 | ArrayNode result = mapper().createArrayNode(); |
| 84 | |
| 85 | sortedGroups.forEach((device, groups) -> |
| 86 | groups.forEach(group -> |
| 87 | result.add(jsonForEntity(group, Group.class)))); |
| 88 | |
| 89 | return result; |
| 90 | } |
| 91 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 92 | @Override |
| 93 | protected void execute() { |
| 94 | DeviceService deviceService = get(DeviceService.class); |
| 95 | GroupService groupService = get(GroupService.class); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 96 | SortedMap<Device, List<Group>> sortedGroups = |
| 97 | getSortedGroups(deviceService, groupService); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 98 | if (outputJson()) { |
| 99 | print("%s", json(sortedGroups)); |
| 100 | } else { |
| 101 | sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); |
| 102 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 103 | } |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Returns the list of devices sorted using the device ID URIs. |
| 106 | * |
| 107 | * @param deviceService device service |
| 108 | * @param groupService group service |
| 109 | * @return sorted device list |
| 110 | */ |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 111 | protected SortedMap<Device, List<Group>> getSortedGroups(DeviceService deviceService, GroupService groupService) { |
| 112 | final GroupState groupsState = (this.state != null && !"any".equals(this.state)) ? |
| 113 | GroupState.valueOf(this.state.toUpperCase()) : |
| 114 | null; |
| 115 | final Iterable<Device> devices = Optional.ofNullable(uri) |
| 116 | .map(DeviceId::deviceId) |
| 117 | .map(deviceService::getDevice) |
| 118 | .map(dev -> (Iterable<Device>) Collections.singletonList(dev)) |
| 119 | .orElse(deviceService.getDevices()); |
| 120 | |
| 121 | SortedMap<Device, List<Group>> sortedGroups = new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 122 | for (Device d : devices) { |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 123 | Stream<Group> groupStream = Lists.newArrayList(groupService.getGroups(d.id())).stream(); |
| 124 | if (groupsState != null) { |
| 125 | groupStream = groupStream.filter(g -> g.state().equals(groupsState)); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 126 | } |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 127 | if (referencedOnly) { |
| 128 | groupStream = groupStream.filter(g -> g.referenceCount() != 0); |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 129 | } |
Charles Chan | 81e7417 | 2018-04-26 15:45:04 -0400 | [diff] [blame^] | 130 | if (type != null && !"any".equals(type)) { |
| 131 | groupStream = groupStream.filter(g -> |
| 132 | g.type().equals(GroupDescription.Type.valueOf(type.toUpperCase()))); |
| 133 | } |
| 134 | sortedGroups.put(d, groupStream.sorted(Comparators.GROUP_COMPARATOR).collect(Collectors.toList())); |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 135 | } |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 136 | return sortedGroups; |
| 137 | } |
| 138 | |
| 139 | private void printGroups(DeviceId deviceId, List<Group> groups) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 140 | print("deviceId=%s, groupCount=%s", deviceId, groups.size()); |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 141 | |
| 142 | if (countOnly) { |
| 143 | return; |
| 144 | } |
| 145 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 146 | for (Group group : groups) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 147 | print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(), |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 148 | group.bytes(), group.packets(), group.appId().name(), group.referenceCount()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 149 | int i = 0; |
| 150 | for (GroupBucket bucket:group.buckets().buckets()) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 151 | print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i, |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 152 | bucket.bytes(), bucket.packets(), |
| 153 | bucket.treatment().allInstructions()); |
| 154 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |