blob: 91ab0c4bc42e361cfa8dbec5fce3d363bb33cf10 [file] [log] [blame]
alshabibeff00542015-09-23 13:22:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
alshabibeff00542015-09-23 13:22:33 -07003 *
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 */
Jonathan Hart1d006392016-02-11 09:12:56 -080016package org.onosproject.cli.net;
alshabibeff00542015-09-23 13:22:33 -070017
Pier Luigib29144d2018-01-15 18:06:43 +010018import org.apache.karaf.shell.commands.Argument;
alshabibeff00542015-09-23 13:22:33 -070019import org.apache.karaf.shell.commands.Command;
Pier Luigib29144d2018-01-15 18:06:43 +010020import org.onlab.packet.IpAddress;
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000021import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart1d006392016-02-11 09:12:56 -080022import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.mcast.McastRoute;
Julian Lawrencefa790f62016-01-07 17:18:30 -080024import org.onosproject.net.mcast.MulticastRouteService;
Jonathan Hart1d006392016-02-11 09:12:56 -080025
Pier Luigib29144d2018-01-15 18:06:43 +010026import java.util.Comparator;
Jonathan Hart1d006392016-02-11 09:12:56 -080027import java.util.Set;
Pier Luigib29144d2018-01-15 18:06:43 +010028import java.util.stream.Collectors;
29
30import static com.google.common.base.Strings.isNullOrEmpty;
alshabibeff00542015-09-23 13:22:33 -070031
32/**
33 * Displays the source, multicast group flows entries.
34 */
35@Command(scope = "onos", name = "mcast-show", description = "Displays the source, multicast group flows")
36public class McastShowCommand extends AbstractShellCommand {
37
Pier Luigib29144d2018-01-15 18:06:43 +010038 // Format for group line
39 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source=%s, sinks=%s";
40
41 @Argument(index = 0, name = "mcastIp", description = "mcast Ip",
42 required = false, multiValued = false)
43 String mcastIp;
alshabibeff00542015-09-23 13:22:33 -070044
45 @Override
46 protected void execute() {
Pier Luigib29144d2018-01-15 18:06:43 +010047 // Get the service
Jonathan Hart1d006392016-02-11 09:12:56 -080048 MulticastRouteService mcastService = get(MulticastRouteService.class);
Pier Luigib29144d2018-01-15 18:06:43 +010049 // Get the routes
Jonathan Hart1d006392016-02-11 09:12:56 -080050 Set<McastRoute> routes = mcastService.getRoutes();
Pier Luigib29144d2018-01-15 18:06:43 +010051 // Verify mcast group
52 if (!isNullOrEmpty(mcastIp)) {
53 // Let's find the group
54 IpAddress mcastGroup = IpAddress.valueOf(mcastIp);
55 McastRoute mcastRoute = routes.stream()
56 .filter(route -> route.group().equals(mcastGroup))
57 .findAny().orElse(null);
58 // If it exists
59 if (mcastRoute != null) {
60 // Get the sinks and print info
61 Set<ConnectPoint> sinks = mcastService.fetchSinks(mcastRoute);
62 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
63 mcastRoute.source(), sinks);
64 }
65 return;
Jonathan Hart1d006392016-02-11 09:12:56 -080066 }
Pier Luigib29144d2018-01-15 18:06:43 +010067 // Filter ipv4
68 Set<McastRoute> ipv4Routes = routes.stream()
69 .filter(mcastRoute -> mcastRoute.group().isIp4())
70 .collect(Collectors.toSet());
71 // Print ipv4 first
72 ipv4Routes.stream()
73 .sorted(Comparator.comparing(McastRoute::group))
74 .forEach(route -> {
75 // Get sinks
76 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
77 print(FORMAT_MAPPING, route.type(), route.group(),
78 route.source(), sinks);
79 });
80 // Filter ipv6
81 Set<McastRoute> ipv6Routes = routes.stream()
82 .filter(mcastRoute -> mcastRoute.group().isIp6())
83 .collect(Collectors.toSet());
84 // Then print ipv6
85 ipv6Routes.stream()
86 .sorted(Comparator.comparing(McastRoute::group))
87 .forEach(route -> {
88 // Get sinks
89 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
90 print(FORMAT_MAPPING, route.type(), route.group(),
91 route.source(), sinks);
92 });
alshabibeff00542015-09-23 13:22:33 -070093 }
94
alshabibeff00542015-09-23 13:22:33 -070095}