blob: ed5e6dad01c666bf77f7e1d11be085e1e190d217 [file] [log] [blame]
Pier3e793752018-04-19 16:47:06 +02001/*
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 */
16
17package org.onosproject.segmentrouting.cli;
18
19import com.google.common.collect.ArrayListMultimap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.Option;
Pier3e793752018-04-19 16:47:06 +020024import org.onlab.packet.IpAddress;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.mcast.cli.McastGroupCompleter;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.segmentrouting.SegmentRoutingService;
30import org.onosproject.segmentrouting.mcast.McastRole;
31import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
32
33import java.util.Collection;
34import java.util.Map;
35import java.util.Set;
36import java.util.stream.Collectors;
37
38import static com.google.common.base.Strings.isNullOrEmpty;
39
40/**
41 * Command to show the list of mcast roles.
42 */
43@Command(scope = "onos", name = "sr-mcast-role",
44 description = "Lists all mcast roles")
45public class McastRoleListCommand extends AbstractShellCommand {
46
47 // OSGi workaround to introduce package dependency
48 McastGroupCompleter completer;
49
50 // Format for group line
51 private static final String FORMAT_MAPPING = "%s,%s ingress=%s\ttransit=%s\tegress=%s";
52
53 @Option(name = "-gAddr", aliases = "--groupAddress",
54 description = "IP Address of the multicast group",
55 valueToShowInHelp = "224.0.0.0",
56 required = false, multiValued = false)
57 String gAddr = null;
58
59 @Option(name = "-src", aliases = "--connectPoint",
60 description = "Source port of:XXXXXXXXXX/XX",
61 valueToShowInHelp = "of:0000000000000001/1",
62 required = false, multiValued = false)
63 String source = null;
64
65 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070066 protected void doExecute() {
Pier3e793752018-04-19 16:47:06 +020067 // Verify mcast group
68 IpAddress mcastGroup = null;
69 // We want to use source cp only for a specific group
70 ConnectPoint sourcecp = null;
71 if (!isNullOrEmpty(gAddr)) {
72 mcastGroup = IpAddress.valueOf(gAddr);
73 if (!isNullOrEmpty(source)) {
74 sourcecp = ConnectPoint.deviceConnectPoint(source);
75 }
76 }
77 // Get SR service, the roles and the groups
78 SegmentRoutingService srService = get(SegmentRoutingService.class);
79 Map<McastRoleStoreKey, McastRole> keyToRole = srService.getMcastRoles(mcastGroup, sourcecp);
80 Set<IpAddress> mcastGroups = keyToRole.keySet().stream()
81 .map(McastRoleStoreKey::mcastIp)
82 .collect(Collectors.toSet());
83 // Print the trees for each group
84 mcastGroups.forEach(group -> {
85 // Create a new map for the group
86 Map<ConnectPoint, Multimap<McastRole, DeviceId>> roleDeviceIdMap = Maps.newHashMap();
87 keyToRole.entrySet()
88 .stream()
89 .filter(entry -> entry.getKey().mcastIp().equals(group))
90 .forEach(entry -> roleDeviceIdMap.compute(entry.getKey().source(), (gsource, map) -> {
91 map = map == null ? ArrayListMultimap.create() : map;
92 map.put(entry.getValue(), entry.getKey().deviceId());
93 return map;
94 }));
95 roleDeviceIdMap.forEach((gsource, map) -> {
96 // Print the map
97 printMcastRole(group, gsource,
98 map.get(McastRole.INGRESS),
99 map.get(McastRole.TRANSIT),
100 map.get(McastRole.EGRESS));
101 });
102 });
103 }
104
105 private void printMcastRole(IpAddress mcastGroup, ConnectPoint source,
106 Collection<DeviceId> ingress,
107 Collection<DeviceId> transit,
108 Collection<DeviceId> egress) {
109 print(FORMAT_MAPPING, mcastGroup, source, ingress, transit, egress);
110 }
111}