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