Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 18 | import static com.google.common.collect.Lists.newArrayList; |
| 19 | |
| 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | import java.util.SortedMap; |
| 23 | import java.util.TreeMap; |
| 24 | |
| 25 | import org.apache.karaf.shell.commands.Argument; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Command; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 28 | import org.onosproject.cli.Comparators; |
| 29 | import org.onosproject.net.Device; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.group.Group; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 33 | import org.onosproject.net.group.Group.GroupState; |
| 34 | import org.onosproject.net.group.GroupBucket; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 35 | import org.onosproject.net.group.GroupService; |
| 36 | |
| 37 | /** |
| 38 | * Lists all groups in the system. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "groups", |
| 41 | description = "Lists all groups in the system") |
| 42 | public class GroupsListCommand extends AbstractShellCommand { |
| 43 | |
| 44 | private static final String FORMAT = |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 45 | " id=%s, state=%s, bytes=%s, packets=%s, appId=%s"; |
| 46 | private static final String BUCKET_FORMAT = |
| 47 | " id=%s, bucket=%s, bytes=%s, packets=%s, actions=%s"; |
| 48 | |
| 49 | @Argument(index = 1, name = "uri", description = "Device ID", |
| 50 | required = false, multiValued = false) |
| 51 | String uri = null; |
| 52 | |
| 53 | @Argument(index = 0, name = "state", description = "Group state", |
| 54 | required = false, multiValued = false) |
| 55 | String state; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 56 | |
| 57 | @Override |
| 58 | protected void execute() { |
| 59 | DeviceService deviceService = get(DeviceService.class); |
| 60 | GroupService groupService = get(GroupService.class); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 61 | SortedMap<Device, List<Group>> sortedGroups = |
| 62 | getSortedGroups(deviceService, groupService); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 63 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 64 | sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 67 | /** |
| 68 | * Returns the list of devices sorted using the device ID URIs. |
| 69 | * |
| 70 | * @param deviceService device service |
| 71 | * @param groupService group service |
| 72 | * @return sorted device list |
| 73 | */ |
| 74 | protected SortedMap<Device, List<Group>> |
| 75 | getSortedGroups(DeviceService deviceService, |
| 76 | GroupService groupService) { |
| 77 | SortedMap<Device, List<Group>> sortedGroups = |
| 78 | new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
| 79 | List<Group> groups; |
| 80 | GroupState s = null; |
| 81 | if (state != null && !state.equals("any")) { |
| 82 | s = GroupState.valueOf(state.toUpperCase()); |
| 83 | } |
| 84 | Iterable<Device> devices = (uri == null) ? deviceService.getDevices() : |
| 85 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 86 | for (Device d : devices) { |
| 87 | if (s == null) { |
| 88 | groups = newArrayList(groupService.getGroups(d.id())); |
| 89 | } else { |
| 90 | groups = newArrayList(); |
| 91 | for (Group g : groupService.getGroups(d.id())) { |
| 92 | if (g.state().equals(s)) { |
| 93 | groups.add(g); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | groups.sort(Comparators.GROUP_COMPARATOR); |
| 98 | sortedGroups.put(d, groups); |
| 99 | } |
| 100 | return sortedGroups; |
| 101 | } |
| 102 | |
| 103 | private void printGroups(DeviceId deviceId, List<Group> groups) { |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 104 | print("deviceId=%s", deviceId); |
| 105 | for (Group group : groups) { |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame^] | 106 | print(FORMAT, group.id().id(), group.state(), |
| 107 | group.bytes(), group.packets(), group.appId().name()); |
| 108 | int i = 0; |
| 109 | for (GroupBucket bucket:group.buckets().buckets()) { |
| 110 | print(BUCKET_FORMAT, group.id().id(), ++i, |
| 111 | bucket.bytes(), bucket.packets(), |
| 112 | bucket.treatment().allInstructions()); |
| 113 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |