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