blob: e5d8b120ebb6a5ac9550d42af186fe7a29b5a032 [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;
Pier Luigib29144d2018-01-15 18:06:43 +010020import org.apache.karaf.shell.commands.Command;
Pier3ee24552018-03-14 16:47:32 -070021import org.apache.karaf.shell.commands.Option;
Pier Luigib29144d2018-01-15 18:06:43 +010022import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
Pier3ee24552018-03-14 16:47:32 -070024import org.onosproject.mcast.cli.McastGroupCompleter;
Pier Luigib29144d2018-01-15 18:06:43 +010025import org.onosproject.net.DeviceId;
26import org.onosproject.segmentrouting.SegmentRoutingService;
27import org.onosproject.segmentrouting.storekey.McastStoreKey;
28
29import java.util.Map;
30import java.util.Set;
31import java.util.stream.Collectors;
32
33import 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")
40public class McastNextListCommand extends AbstractShellCommand {
41
Pier3ee24552018-03-14 16:47:32 -070042 // OSGi workaround to introduce package dependency
43 McastGroupCompleter completer;
44
Pier Luigib29144d2018-01-15 18:06:43 +010045 // Format for group line
46 private static final String FORMAT_MAPPING = "group=%s, deviceIds-nextIds=%s";
47
Pier3ee24552018-03-14 16:47:32 -070048 @Option(name = "-gAddr", aliases = "--groupAddress",
49 description = "IP Address of the multicast group",
50 valueToShowInHelp = "224.0.0.0",
Pier Luigib29144d2018-01-15 18:06:43 +010051 required = false, multiValued = false)
Pier3ee24552018-03-14 16:47:32 -070052 String gAddr = null;
Pier Luigib29144d2018-01-15 18:06:43 +010053
54 @Override
55 protected void execute() {
56 // Verify mcast group
57 IpAddress mcastGroup = null;
Pier3ee24552018-03-14 16:47:32 -070058 if (!isNullOrEmpty(gAddr)) {
59 mcastGroup = IpAddress.valueOf(gAddr);
Pier Luigib29144d2018-01-15 18:06:43 +010060 }
61 // Get SR service
62 SegmentRoutingService srService = get(SegmentRoutingService.class);
63 // Get the mapping
64 Map<McastStoreKey, Integer> keyToNextId = srService.getMcastNextIds(mcastGroup);
65 // Reduce to the set of mcast groups
66 Set<IpAddress> mcastGroups = keyToNextId.keySet().stream()
67 .map(McastStoreKey::mcastIp)
68 .collect(Collectors.toSet());
69 // Print the nextids for each group
70 mcastGroups.forEach(group -> {
71 // Create a new map for the group
72 Map<DeviceId, Integer> deviceIdNextMap = Maps.newHashMap();
73 keyToNextId.entrySet()
74 .stream()
75 // Filter only the elements related to this group
76 .filter(entry -> entry.getKey().mcastIp().equals(group))
77 // For each create a new entry in the group related map
78 .forEach(entry -> deviceIdNextMap.put(entry.getKey().deviceId(), entry.getValue()));
79 // Print the map
80 printMcastNext(group, deviceIdNextMap);
81 });
82 }
83
84 private void printMcastNext(IpAddress mcastGroup, Map<DeviceId, Integer> deviceIdNextMap) {
85 print(FORMAT_MAPPING, mcastGroup, deviceIdNextMap);
86 }
87}