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