Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 16 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 17 | package org.onosproject.segmentrouting.cli; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Pier | 7631c49 | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 25 | import org.onosproject.mcast.cli.McastGroupCompleter; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.segmentrouting.SegmentRoutingService; |
Pier | 7631c49 | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 28 | import org.onosproject.segmentrouting.mcast.McastStoreKey; |
| 29 | import org.apache.commons.lang3.tuple.Pair; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 30 | |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 36 | |
| 37 | /** |
| 38 | * Command to show the list of mcast nextids. |
| 39 | */ |
Charles Chan | 1fa010c | 2018-08-19 19:21:46 -0700 | [diff] [blame^] | 40 | @Command(scope = "onos", name = "sr-next-mcast", |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 41 | description = "Lists all mcast nextids") |
| 42 | public class McastNextListCommand extends AbstractShellCommand { |
| 43 | |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 44 | // OSGi workaround to introduce package dependency |
| 45 | McastGroupCompleter completer; |
| 46 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 47 | // Format for group line |
| 48 | private static final String FORMAT_MAPPING = "group=%s, deviceIds-nextIds=%s"; |
| 49 | |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 50 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 51 | description = "IP Address of the multicast group", |
| 52 | valueToShowInHelp = "224.0.0.0", |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 53 | required = false, multiValued = false) |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 54 | String gAddr = null; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | protected void execute() { |
| 58 | // Verify mcast group |
| 59 | IpAddress mcastGroup = null; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 60 | if (!isNullOrEmpty(gAddr)) { |
| 61 | mcastGroup = IpAddress.valueOf(gAddr); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 62 | } |
| 63 | // Get SR service |
| 64 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 65 | // Get the mapping |
| 66 | Map<McastStoreKey, Integer> keyToNextId = srService.getMcastNextIds(mcastGroup); |
| 67 | // Reduce to the set of mcast groups |
| 68 | Set<IpAddress> mcastGroups = keyToNextId.keySet().stream() |
| 69 | .map(McastStoreKey::mcastIp) |
| 70 | .collect(Collectors.toSet()); |
| 71 | // Print the nextids for each group |
| 72 | mcastGroups.forEach(group -> { |
| 73 | // Create a new map for the group |
Pier | 7631c49 | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 74 | Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap = Maps.newHashMap(); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 75 | keyToNextId.entrySet() |
| 76 | .stream() |
| 77 | // Filter only the elements related to this group |
| 78 | .filter(entry -> entry.getKey().mcastIp().equals(group)) |
| 79 | // For each create a new entry in the group related map |
Pier | 7631c49 | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 80 | .forEach(entry -> deviceIdNextMap.put(Pair.of(entry.getKey().deviceId(), |
| 81 | entry.getKey().vlanId()), entry.getValue())); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 82 | // Print the map |
| 83 | printMcastNext(group, deviceIdNextMap); |
| 84 | }); |
| 85 | } |
| 86 | |
Pier | 7631c49 | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 87 | private void printMcastNext(IpAddress mcastGroup, Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap) { |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 88 | print(FORMAT_MAPPING, mcastGroup, deviceIdNextMap); |
| 89 | } |
| 90 | } |