blob: ff9e2e6b9bb3450ae03b3e203bef49f87d3925c7 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Pier Luigib29144d2018-01-15 18:06:43 +010021import org.onlab.packet.IpAddress;
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000022import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart1d006392016-02-11 09:12:56 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.mcast.McastRoute;
Julian Lawrencefa790f62016-01-07 17:18:30 -080025import org.onosproject.net.mcast.MulticastRouteService;
Jonathan Hart1d006392016-02-11 09:12:56 -080026
Pier Luigib29144d2018-01-15 18:06:43 +010027import java.util.Comparator;
Jonathan Hart1d006392016-02-11 09:12:56 -080028import java.util.Set;
Pier Luigib29144d2018-01-15 18:06:43 +010029import java.util.stream.Collectors;
30
31import static com.google.common.base.Strings.isNullOrEmpty;
alshabibeff00542015-09-23 13:22:33 -070032
33/**
34 * Displays the source, multicast group flows entries.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
alshabibeff00542015-09-23 13:22:33 -070037@Command(scope = "onos", name = "mcast-show", description = "Displays the source, multicast group flows")
38public class McastShowCommand extends AbstractShellCommand {
39
Pier Luigib29144d2018-01-15 18:06:43 +010040 // Format for group line
41 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source=%s, sinks=%s";
42
43 @Argument(index = 0, name = "mcastIp", description = "mcast Ip",
44 required = false, multiValued = false)
45 String mcastIp;
alshabibeff00542015-09-23 13:22:33 -070046
47 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 protected void doExecute() {
Pier Luigib29144d2018-01-15 18:06:43 +010049 // Get the service
Jonathan Hart1d006392016-02-11 09:12:56 -080050 MulticastRouteService mcastService = get(MulticastRouteService.class);
Pier Luigib29144d2018-01-15 18:06:43 +010051 // Get the routes
Jonathan Hart1d006392016-02-11 09:12:56 -080052 Set<McastRoute> routes = mcastService.getRoutes();
Pier Luigib29144d2018-01-15 18:06:43 +010053 // Verify mcast group
54 if (!isNullOrEmpty(mcastIp)) {
55 // Let's find the group
56 IpAddress mcastGroup = IpAddress.valueOf(mcastIp);
57 McastRoute mcastRoute = routes.stream()
58 .filter(route -> route.group().equals(mcastGroup))
59 .findAny().orElse(null);
60 // If it exists
61 if (mcastRoute != null) {
62 // Get the sinks and print info
63 Set<ConnectPoint> sinks = mcastService.fetchSinks(mcastRoute);
64 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
65 mcastRoute.source(), sinks);
66 }
67 return;
Jonathan Hart1d006392016-02-11 09:12:56 -080068 }
Pier Luigib29144d2018-01-15 18:06:43 +010069 // Filter ipv4
70 Set<McastRoute> ipv4Routes = routes.stream()
71 .filter(mcastRoute -> mcastRoute.group().isIp4())
72 .collect(Collectors.toSet());
73 // Print ipv4 first
74 ipv4Routes.stream()
75 .sorted(Comparator.comparing(McastRoute::group))
76 .forEach(route -> {
77 // Get sinks
78 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
79 print(FORMAT_MAPPING, route.type(), route.group(),
80 route.source(), sinks);
81 });
82 // Filter ipv6
83 Set<McastRoute> ipv6Routes = routes.stream()
84 .filter(mcastRoute -> mcastRoute.group().isIp6())
85 .collect(Collectors.toSet());
86 // Then print ipv6
87 ipv6Routes.stream()
88 .sorted(Comparator.comparing(McastRoute::group))
89 .forEach(route -> {
90 // Get sinks
91 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
92 print(FORMAT_MAPPING, route.type(), route.group(),
93 route.source(), sinks);
94 });
alshabibeff00542015-09-23 13:22:33 -070095 }
96
alshabibeff00542015-09-23 13:22:33 -070097}