blob: 73cbb1a6d3f159c3049a4c397ef60300e0b8d8d6 [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 */
16
17package org.onosproject.segmentrouting.cli;
18
19import com.google.common.collect.ArrayListMultimap;
20import com.google.common.collect.Multimap;
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
23import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.segmentrouting.McastHandler.McastRole;
28import org.onosproject.segmentrouting.SegmentRoutingService;
29import org.onosproject.segmentrouting.storekey.McastStoreKey;
30
31import java.util.Collection;
32import java.util.List;
33import java.util.Map;
34import java.util.Set;
35import java.util.stream.Collectors;
36
37import static com.google.common.base.Strings.isNullOrEmpty;
38import static org.onosproject.segmentrouting.McastHandler.McastRole.EGRESS;
39import static org.onosproject.segmentrouting.McastHandler.McastRole.INGRESS;
40import static org.onosproject.segmentrouting.McastHandler.McastRole.TRANSIT;
41
42/**
43 * Command to show the list of mcast trees.
44 */
45@Command(scope = "onos", name = "sr-mcast-tree",
46 description = "Lists all mcast trees")
47public class McastTreeListCommand extends AbstractShellCommand {
48
49 // Format for group line
50 private static final String G_FORMAT_MAPPING = "group=%s, ingress=%s, transit=%s, egress=%s";
51 // Format for sink line
52 private static final String S_FORMAT_MAPPING = "\tsink=%s\tpath=%s";
53
54 @Argument(index = 0, name = "mcastIp", description = "mcast Ip",
55 required = false, multiValued = false)
56 String mcastIp;
57
58 @Override
59 protected void execute() {
60 // Verify mcast group
61 IpAddress mcastGroup = null;
62 if (!isNullOrEmpty(mcastIp)) {
63 mcastGroup = IpAddress.valueOf(mcastIp);
64
65 }
66 // Get SR service
67 SegmentRoutingService srService = get(SegmentRoutingService.class);
68 // Get the mapping
69 Map<McastStoreKey, McastRole> keyToRole = srService.getMcastRoles(mcastGroup);
70 // Reduce to the set of mcast groups
71 Set<IpAddress> mcastGroups = keyToRole.keySet().stream()
72 .map(McastStoreKey::mcastIp)
73 .collect(Collectors.toSet());
74 // Print the trees for each group
75 mcastGroups.forEach(group -> {
76 // Create a new map for the group
77 Multimap<McastRole, DeviceId> roleDeviceIdMap = ArrayListMultimap.create();
78 keyToRole.entrySet()
79 .stream()
80 // Filter only the elements related to this group
81 .filter(entry -> entry.getKey().mcastIp().equals(group))
82 // For each create a new entry in the group related map
83 .forEach(entry -> roleDeviceIdMap.put(entry.getValue(), entry.getKey().deviceId()));
84 // Print the map
85 printMcastRole(group,
86 roleDeviceIdMap.get(INGRESS),
87 roleDeviceIdMap.get(TRANSIT),
88 roleDeviceIdMap.get(EGRESS)
89 );
90 // Get sinks paths
91 Map<ConnectPoint, List<ConnectPoint>> mcastPaths = srService.getMcastPaths(group);
92 // Print the paths
93 mcastPaths.forEach(this::printMcastSink);
94 });
95 }
96
97 private void printMcastRole(IpAddress mcastGroup,
98 Collection<DeviceId> ingress,
99 Collection<DeviceId> transit,
100 Collection<DeviceId> egress) {
101 print(G_FORMAT_MAPPING, mcastGroup, ingress, transit, egress);
102 }
103
104 private void printMcastSink(ConnectPoint sink, List<ConnectPoint> path) {
105 print(S_FORMAT_MAPPING, sink, path);
106 }
107}