blob: 7dd6fd3555a1b391cbdf8595da991d1b11efba98 [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 */
Pier1f87aca2018-03-14 16:47:32 -070016
Pier Luigi0f9635b2018-01-15 18:06:43 +010017package org.onosproject.segmentrouting.cli;
18
19import com.google.common.collect.Maps;
Ray Milkeydb38bc82018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Ray Milkey5105ec32018-10-05 10:17:34 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeydb38bc82018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkey52ca4e92018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Pier Luigi0f9635b2018-01-15 18:06:43 +010024import org.onlab.packet.IpAddress;
Piere99511d2018-04-19 16:47:06 +020025import org.onlab.packet.VlanId;
Pier Luigi0f9635b2018-01-15 18:06:43 +010026import org.onosproject.cli.AbstractShellCommand;
Pier1f87aca2018-03-14 16:47:32 -070027import org.onosproject.mcast.cli.McastGroupCompleter;
Pier Luigi0f9635b2018-01-15 18:06:43 +010028import org.onosproject.net.DeviceId;
29import org.onosproject.segmentrouting.SegmentRoutingService;
Piere99511d2018-04-19 16:47:06 +020030import org.onosproject.segmentrouting.mcast.McastStoreKey;
31import org.apache.commons.lang3.tuple.Pair;
Pier Luigi0f9635b2018-01-15 18:06:43 +010032
33import java.util.Map;
34import java.util.Set;
35import java.util.stream.Collectors;
36
37import static com.google.common.base.Strings.isNullOrEmpty;
38
39/**
40 * Command to show the list of mcast nextids.
41 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070042@Service
Charles Chan0b1dd7e2018-08-19 19:21:46 -070043@Command(scope = "onos", name = "sr-next-mcast",
Pier Luigi0f9635b2018-01-15 18:06:43 +010044 description = "Lists all mcast nextids")
45public class McastNextListCommand extends AbstractShellCommand {
46
Pier1f87aca2018-03-14 16:47:32 -070047 // OSGi workaround to introduce package dependency
48 McastGroupCompleter completer;
49
Pier Luigi0f9635b2018-01-15 18:06:43 +010050 // Format for group line
51 private static final String FORMAT_MAPPING = "group=%s, deviceIds-nextIds=%s";
52
Pier1f87aca2018-03-14 16:47:32 -070053 @Option(name = "-gAddr", aliases = "--groupAddress",
54 description = "IP Address of the multicast group",
55 valueToShowInHelp = "224.0.0.0",
Pier Luigi0f9635b2018-01-15 18:06:43 +010056 required = false, multiValued = false)
Ray Milkey5105ec32018-10-05 10:17:34 -070057 @Completion(McastGroupCompleter.class)
Pier1f87aca2018-03-14 16:47:32 -070058 String gAddr = null;
Pier Luigi0f9635b2018-01-15 18:06:43 +010059
60 @Override
Ray Milkeydb38bc82018-09-27 12:32:28 -070061 protected void doExecute() {
Pier Luigi0f9635b2018-01-15 18:06:43 +010062 // Verify mcast group
63 IpAddress mcastGroup = null;
Pier1f87aca2018-03-14 16:47:32 -070064 if (!isNullOrEmpty(gAddr)) {
65 mcastGroup = IpAddress.valueOf(gAddr);
Pier Luigi0f9635b2018-01-15 18:06:43 +010066 }
67 // Get SR service
68 SegmentRoutingService srService = get(SegmentRoutingService.class);
69 // Get the mapping
70 Map<McastStoreKey, Integer> keyToNextId = srService.getMcastNextIds(mcastGroup);
71 // Reduce to the set of mcast groups
72 Set<IpAddress> mcastGroups = keyToNextId.keySet().stream()
73 .map(McastStoreKey::mcastIp)
74 .collect(Collectors.toSet());
75 // Print the nextids for each group
76 mcastGroups.forEach(group -> {
77 // Create a new map for the group
Piere99511d2018-04-19 16:47:06 +020078 Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap = Maps.newHashMap();
Pier Luigi0f9635b2018-01-15 18:06:43 +010079 keyToNextId.entrySet()
80 .stream()
81 // Filter only the elements related to this group
82 .filter(entry -> entry.getKey().mcastIp().equals(group))
83 // For each create a new entry in the group related map
Piere99511d2018-04-19 16:47:06 +020084 .forEach(entry -> deviceIdNextMap.put(Pair.of(entry.getKey().deviceId(),
85 entry.getKey().vlanId()), entry.getValue()));
Pier Luigi0f9635b2018-01-15 18:06:43 +010086 // Print the map
87 printMcastNext(group, deviceIdNextMap);
88 });
89 }
90
Piere99511d2018-04-19 16:47:06 +020091 private void printMcastNext(IpAddress mcastGroup, Map<Pair<DeviceId, VlanId>, Integer> deviceIdNextMap) {
Pier Luigi0f9635b2018-01-15 18:06:43 +010092 print(FORMAT_MAPPING, mcastGroup, deviceIdNextMap);
93 }
94}