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