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