blob: 128f9b5f098e76bae537b327490b1a2d679dbfb2 [file] [log] [blame]
Pier Luigib29144d2018-01-15 18:06:43 +01001/*
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 */
Pier3ee24552018-03-14 16:47:32 -070016
Pier Luigib29144d2018-01-15 18:06:43 +010017package org.onosproject.segmentrouting.cli;
18
19import com.google.common.collect.Maps;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Pier Luigib29144d2018-01-15 18:06:43 +010022import org.onlab.packet.IpAddress;
Pier3e793752018-04-19 16:47:06 +020023import org.onlab.packet.VlanId;
Pier Luigib29144d2018-01-15 18:06:43 +010024import org.onosproject.cli.AbstractShellCommand;
Pier3ee24552018-03-14 16:47:32 -070025import org.onosproject.mcast.cli.McastGroupCompleter;
Pier Luigib29144d2018-01-15 18:06:43 +010026import org.onosproject.net.DeviceId;
27import org.onosproject.segmentrouting.SegmentRoutingService;
Pier3e793752018-04-19 16:47:06 +020028import org.onosproject.segmentrouting.mcast.McastStoreKey;
29import org.apache.commons.lang3.tuple.Pair;
Pier Luigib29144d2018-01-15 18:06:43 +010030
31import java.util.Map;
32import java.util.Set;
33import java.util.stream.Collectors;
34
35import static com.google.common.base.Strings.isNullOrEmpty;
36
37/**
38 * Command to show the list of mcast nextids.
39 */
Charles Chand5814aa2018-08-19 19:21:46 -070040@Command(scope = "onos", name = "sr-next-mcast",
Pier Luigib29144d2018-01-15 18:06:43 +010041 description = "Lists all mcast nextids")
42public class McastNextListCommand extends AbstractShellCommand {
43
Pier3ee24552018-03-14 16:47:32 -070044 // OSGi workaround to introduce package dependency
45 McastGroupCompleter completer;
46
Pier Luigib29144d2018-01-15 18:06:43 +010047 // Format for group line
48 private static final String FORMAT_MAPPING = "group=%s, deviceIds-nextIds=%s";
49
Pier3ee24552018-03-14 16:47:32 -070050 @Option(name = "-gAddr", aliases = "--groupAddress",
51 description = "IP Address of the multicast group",
52 valueToShowInHelp = "224.0.0.0",
Pier Luigib29144d2018-01-15 18:06:43 +010053 required = false, multiValued = false)
Pier3ee24552018-03-14 16:47:32 -070054 String gAddr = null;
Pier Luigib29144d2018-01-15 18:06:43 +010055
56 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070057 protected void doExecute() {
Pier Luigib29144d2018-01-15 18:06:43 +010058 // Verify mcast group
59 IpAddress mcastGroup = null;
Pier3ee24552018-03-14 16:47:32 -070060 if (!isNullOrEmpty(gAddr)) {
61 mcastGroup = IpAddress.valueOf(gAddr);
Pier Luigib29144d2018-01-15 18:06:43 +010062 }
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
Pier3e793752018-04-19 16:47:06 +020074 Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap = Maps.newHashMap();
Pier Luigib29144d2018-01-15 18:06:43 +010075 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
Pier3e793752018-04-19 16:47:06 +020080 .forEach(entry -> deviceIdNextMap.put(Pair.of(entry.getKey().deviceId(),
81 entry.getKey().vlanId()), entry.getValue()));
Pier Luigib29144d2018-01-15 18:06:43 +010082 // Print the map
83 printMcastNext(group, deviceIdNextMap);
84 });
85 }
86
Pier3e793752018-04-19 16:47:06 +020087 private void printMcastNext(IpAddress mcastGroup, Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap) {
Pier Luigib29144d2018-01-15 18:06:43 +010088 print(FORMAT_MAPPING, mcastGroup, deviceIdNextMap);
89 }
90}