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 java.util.Collections; |
| 19 | import java.util.List; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 20 | import java.util.Map; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 21 | import java.util.SortedMap; |
| 22 | import java.util.TreeMap; |
| 23 | |
| 24 | import org.apache.karaf.shell.commands.Argument; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.Comparators; |
| 28 | import org.onosproject.net.Device; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.group.Group; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 32 | import org.onosproject.net.group.Group.GroupState; |
| 33 | import org.onosproject.net.group.GroupBucket; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 34 | import org.onosproject.net.group.GroupService; |
| 35 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 36 | import com.fasterxml.jackson.databind.JsonNode; |
| 37 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 38 | |
| 39 | import static com.google.common.collect.Lists.newArrayList; |
| 40 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Lists all groups in the system. |
| 43 | */ |
| 44 | @Command(scope = "onos", name = "groups", |
| 45 | description = "Lists all groups in the system") |
| 46 | public class GroupsListCommand extends AbstractShellCommand { |
| 47 | |
Charles Chan | f38aca7 | 2016-02-18 13:40:18 -0800 | [diff] [blame^] | 48 | public static final String ANY = "any"; |
| 49 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 50 | private static final String FORMAT = |
Charles Chan | ccd59a4 | 2015-11-10 17:52:59 -0800 | [diff] [blame] | 51 | " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 52 | private static final String BUCKET_FORMAT = |
Charles Chan | ccd59a4 | 2015-11-10 17:52:59 -0800 | [diff] [blame] | 53 | " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 54 | |
| 55 | @Argument(index = 1, name = "uri", description = "Device ID", |
| 56 | required = false, multiValued = false) |
| 57 | String uri = null; |
| 58 | |
| 59 | @Argument(index = 0, name = "state", description = "Group state", |
| 60 | required = false, multiValued = false) |
| 61 | String state; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 62 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 63 | private JsonNode json(Map<Device, List<Group>> sortedGroups) { |
| 64 | ArrayNode result = mapper().createArrayNode(); |
| 65 | |
| 66 | sortedGroups.forEach((device, groups) -> |
| 67 | groups.forEach(group -> |
| 68 | result.add(jsonForEntity(group, Group.class)))); |
| 69 | |
| 70 | return result; |
| 71 | } |
| 72 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 73 | @Override |
| 74 | protected void execute() { |
| 75 | DeviceService deviceService = get(DeviceService.class); |
| 76 | GroupService groupService = get(GroupService.class); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 77 | SortedMap<Device, List<Group>> sortedGroups = |
| 78 | getSortedGroups(deviceService, groupService); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 79 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 80 | if (outputJson()) { |
| 81 | print("%s", json(sortedGroups)); |
| 82 | } else { |
| 83 | sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); |
| 84 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Returns the list of devices sorted using the device ID URIs. |
| 89 | * |
| 90 | * @param deviceService device service |
| 91 | * @param groupService group service |
| 92 | * @return sorted device list |
| 93 | */ |
| 94 | protected SortedMap<Device, List<Group>> |
| 95 | getSortedGroups(DeviceService deviceService, |
| 96 | GroupService groupService) { |
| 97 | SortedMap<Device, List<Group>> sortedGroups = |
| 98 | new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
| 99 | List<Group> groups; |
| 100 | GroupState s = null; |
| 101 | if (state != null && !state.equals("any")) { |
| 102 | s = GroupState.valueOf(state.toUpperCase()); |
| 103 | } |
| 104 | Iterable<Device> devices = (uri == null) ? deviceService.getDevices() : |
| 105 | Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri))); |
| 106 | for (Device d : devices) { |
| 107 | if (s == null) { |
| 108 | groups = newArrayList(groupService.getGroups(d.id())); |
| 109 | } else { |
| 110 | groups = newArrayList(); |
| 111 | for (Group g : groupService.getGroups(d.id())) { |
| 112 | if (g.state().equals(s)) { |
| 113 | groups.add(g); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | groups.sort(Comparators.GROUP_COMPARATOR); |
| 118 | sortedGroups.put(d, groups); |
| 119 | } |
| 120 | return sortedGroups; |
| 121 | } |
| 122 | |
| 123 | private void printGroups(DeviceId deviceId, List<Group> groups) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 124 | print("deviceId=%s, groupCount=%s", deviceId, groups.size()); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 125 | for (Group group : groups) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 126 | print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(), |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 127 | group.bytes(), group.packets(), group.appId().name()); |
| 128 | int i = 0; |
| 129 | for (GroupBucket bucket:group.buckets().buckets()) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 130 | print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i, |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 131 | bucket.bytes(), bucket.packets(), |
| 132 | bucket.treatment().allInstructions()); |
| 133 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |