blob: a71313011fd98f79990439acfbc5085fc893bc8f [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 }
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}