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