blob: d1de73c47f2393592b4bea27dceed10db192d76b [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 */
16
17package org.onosproject.segmentrouting.cli;
18
19import com.google.common.collect.ArrayListMultimap;
20import com.google.common.collect.Multimap;
Pier Luigib29144d2018-01-15 18:06:43 +010021import org.apache.karaf.shell.commands.Command;
Pier3ee24552018-03-14 16:47:32 -070022import org.apache.karaf.shell.commands.Option;
Pier Luigib29144d2018-01-15 18:06:43 +010023import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
Pier3ee24552018-03-14 16:47:32 -070025import org.onosproject.mcast.cli.McastGroupCompleter;
Pier Luigib29144d2018-01-15 18:06:43 +010026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
Pier Luigib29144d2018-01-15 18:06:43 +010028import org.onosproject.segmentrouting.SegmentRoutingService;
Pier Luigi96fe0772018-02-28 12:10:50 +010029import org.onosproject.segmentrouting.mcast.McastRole;
Pier Luigib29144d2018-01-15 18:06:43 +010030import org.onosproject.segmentrouting.storekey.McastStoreKey;
31
32import java.util.Collection;
33import java.util.List;
34import java.util.Map;
35import java.util.Set;
36import java.util.stream.Collectors;
37
38import static com.google.common.base.Strings.isNullOrEmpty;
Pier Luigi96fe0772018-02-28 12:10:50 +010039import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS;
40import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS;
41import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT;
Pier Luigib29144d2018-01-15 18:06:43 +010042
43/**
44 * Command to show the list of mcast trees.
45 */
46@Command(scope = "onos", name = "sr-mcast-tree",
47 description = "Lists all mcast trees")
48public class McastTreeListCommand extends AbstractShellCommand {
49
Pier3ee24552018-03-14 16:47:32 -070050 // OSGi workaround to introduce package dependency
51 McastGroupCompleter completer;
52
Pier Luigib29144d2018-01-15 18:06:43 +010053 // Format for group line
54 private static final String G_FORMAT_MAPPING = "group=%s, ingress=%s, transit=%s, egress=%s";
55 // Format for sink line
56 private static final String S_FORMAT_MAPPING = "\tsink=%s\tpath=%s";
57
Pier3ee24552018-03-14 16:47:32 -070058 @Option(name = "-gAddr", aliases = "--groupAddress",
59 description = "IP Address of the multicast group",
60 valueToShowInHelp = "224.0.0.0",
Pier Luigib29144d2018-01-15 18:06:43 +010061 required = false, multiValued = false)
Pier3ee24552018-03-14 16:47:32 -070062 String gAddr = null;
Pier Luigib29144d2018-01-15 18:06:43 +010063
64 @Override
65 protected void execute() {
66 // Verify mcast group
67 IpAddress mcastGroup = null;
Pier3ee24552018-03-14 16:47:32 -070068 if (!isNullOrEmpty(gAddr)) {
69 mcastGroup = IpAddress.valueOf(gAddr);
Pier Luigib29144d2018-01-15 18:06:43 +010070
71 }
72 // Get SR service
73 SegmentRoutingService srService = get(SegmentRoutingService.class);
74 // Get the mapping
75 Map<McastStoreKey, McastRole> keyToRole = srService.getMcastRoles(mcastGroup);
76 // Reduce to the set of mcast groups
77 Set<IpAddress> mcastGroups = keyToRole.keySet().stream()
78 .map(McastStoreKey::mcastIp)
79 .collect(Collectors.toSet());
80 // Print the trees for each group
81 mcastGroups.forEach(group -> {
82 // Create a new map for the group
83 Multimap<McastRole, DeviceId> roleDeviceIdMap = ArrayListMultimap.create();
84 keyToRole.entrySet()
85 .stream()
86 // Filter only the elements related to this group
87 .filter(entry -> entry.getKey().mcastIp().equals(group))
88 // For each create a new entry in the group related map
89 .forEach(entry -> roleDeviceIdMap.put(entry.getValue(), entry.getKey().deviceId()));
90 // Print the map
91 printMcastRole(group,
92 roleDeviceIdMap.get(INGRESS),
93 roleDeviceIdMap.get(TRANSIT),
94 roleDeviceIdMap.get(EGRESS)
95 );
96 // Get sinks paths
97 Map<ConnectPoint, List<ConnectPoint>> mcastPaths = srService.getMcastPaths(group);
98 // Print the paths
99 mcastPaths.forEach(this::printMcastSink);
100 });
101 }
102
103 private void printMcastRole(IpAddress mcastGroup,
104 Collection<DeviceId> ingress,
105 Collection<DeviceId> transit,
106 Collection<DeviceId> egress) {
107 print(G_FORMAT_MAPPING, mcastGroup, ingress, transit, egress);
108 }
109
110 private void printMcastSink(ConnectPoint sink, List<ConnectPoint> path) {
111 print(S_FORMAT_MAPPING, sink, path);
112 }
113}