blob: aad9a7e1ec64d01d5176cbc6f826d6e07139cd78 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Pier Luigib29144d2018-01-15 18:06:43 +010023import org.onlab.packet.IpAddress;
Pier3e793752018-04-19 16:47:06 +020024import org.onlab.packet.VlanId;
Pier Luigib29144d2018-01-15 18:06:43 +010025import org.onosproject.cli.AbstractShellCommand;
Pier3ee24552018-03-14 16:47:32 -070026import org.onosproject.mcast.cli.McastGroupCompleter;
Pier Luigib29144d2018-01-15 18:06:43 +010027import org.onosproject.net.DeviceId;
28import org.onosproject.segmentrouting.SegmentRoutingService;
Pier3e793752018-04-19 16:47:06 +020029import org.onosproject.segmentrouting.mcast.McastStoreKey;
30import org.apache.commons.lang3.tuple.Pair;
Pier Luigib29144d2018-01-15 18:06:43 +010031
32import java.util.Map;
33import java.util.Set;
34import java.util.stream.Collectors;
35
36import static com.google.common.base.Strings.isNullOrEmpty;
37
38/**
39 * Command to show the list of mcast nextids.
40 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070041@Service
Charles Chand5814aa2018-08-19 19:21:46 -070042@Command(scope = "onos", name = "sr-next-mcast",
Pier Luigib29144d2018-01-15 18:06:43 +010043 description = "Lists all mcast nextids")
44public class McastNextListCommand extends AbstractShellCommand {
45
Pier3ee24552018-03-14 16:47:32 -070046 // OSGi workaround to introduce package dependency
47 McastGroupCompleter completer;
48
Pier Luigib29144d2018-01-15 18:06:43 +010049 // Format for group line
50 private static final String FORMAT_MAPPING = "group=%s, deviceIds-nextIds=%s";
51
Pier3ee24552018-03-14 16:47:32 -070052 @Option(name = "-gAddr", aliases = "--groupAddress",
53 description = "IP Address of the multicast group",
54 valueToShowInHelp = "224.0.0.0",
Pier Luigib29144d2018-01-15 18:06:43 +010055 required = false, multiValued = false)
Pier3ee24552018-03-14 16:47:32 -070056 String gAddr = null;
Pier Luigib29144d2018-01-15 18:06:43 +010057
58 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070059 protected void doExecute() {
Pier Luigib29144d2018-01-15 18:06:43 +010060 // Verify mcast group
61 IpAddress mcastGroup = null;
Pier3ee24552018-03-14 16:47:32 -070062 if (!isNullOrEmpty(gAddr)) {
63 mcastGroup = IpAddress.valueOf(gAddr);
Pier Luigib29144d2018-01-15 18:06:43 +010064 }
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
Pier3e793752018-04-19 16:47:06 +020076 Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap = Maps.newHashMap();
Pier Luigib29144d2018-01-15 18:06:43 +010077 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
Pier3e793752018-04-19 16:47:06 +020082 .forEach(entry -> deviceIdNextMap.put(Pair.of(entry.getKey().deviceId(),
83 entry.getKey().vlanId()), entry.getValue()));
Pier Luigib29144d2018-01-15 18:06:43 +010084 // Print the map
85 printMcastNext(group, deviceIdNextMap);
86 });
87 }
88
Pier3e793752018-04-19 16:47:06 +020089 private void printMcastNext(IpAddress mcastGroup, Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap) {
Pier Luigib29144d2018-01-15 18:06:43 +010090 print(FORMAT_MAPPING, mcastGroup, deviceIdNextMap);
91 }
92}