blob: 3d9b9b5af5aa19370201a88a09627c50156de840 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Pier Luigib29144d2018-01-15 18:06:43 +010022import org.onlab.packet.IpAddress;
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000023import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart1d006392016-02-11 09:12:56 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.mcast.McastRoute;
Julian Lawrencefa790f62016-01-07 17:18:30 -080026import org.onosproject.net.mcast.MulticastRouteService;
Jonathan Hart1d006392016-02-11 09:12:56 -080027
Pier Luigib29144d2018-01-15 18:06:43 +010028import java.util.Comparator;
Jonathan Hart1d006392016-02-11 09:12:56 -080029import java.util.Set;
Pier Luigib29144d2018-01-15 18:06:43 +010030import java.util.stream.Collectors;
31
32import static com.google.common.base.Strings.isNullOrEmpty;
alshabibeff00542015-09-23 13:22:33 -070033
34/**
35 * Displays the source, multicast group flows entries.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
alshabibeff00542015-09-23 13:22:33 -070038@Command(scope = "onos", name = "mcast-show", description = "Displays the source, multicast group flows")
39public class McastShowCommand extends AbstractShellCommand {
40
Pier Luigib29144d2018-01-15 18:06:43 +010041 // Format for group line
42 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source=%s, sinks=%s";
43
44 @Argument(index = 0, name = "mcastIp", description = "mcast Ip",
45 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070046 @Completion(McastGroupCompleter.class)
Pier Luigib29144d2018-01-15 18:06:43 +010047 String mcastIp;
alshabibeff00542015-09-23 13:22:33 -070048
49 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 protected void doExecute() {
Pier Luigib29144d2018-01-15 18:06:43 +010051 // Get the service
Jonathan Hart1d006392016-02-11 09:12:56 -080052 MulticastRouteService mcastService = get(MulticastRouteService.class);
Pier Luigib29144d2018-01-15 18:06:43 +010053 // Get the routes
Jonathan Hart1d006392016-02-11 09:12:56 -080054 Set<McastRoute> routes = mcastService.getRoutes();
Pier Luigib29144d2018-01-15 18:06:43 +010055 // Verify mcast group
56 if (!isNullOrEmpty(mcastIp)) {
57 // Let's find the group
58 IpAddress mcastGroup = IpAddress.valueOf(mcastIp);
59 McastRoute mcastRoute = routes.stream()
60 .filter(route -> route.group().equals(mcastGroup))
61 .findAny().orElse(null);
62 // If it exists
63 if (mcastRoute != null) {
64 // Get the sinks and print info
65 Set<ConnectPoint> sinks = mcastService.fetchSinks(mcastRoute);
66 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
67 mcastRoute.source(), sinks);
68 }
69 return;
Jonathan Hart1d006392016-02-11 09:12:56 -080070 }
Pier Luigib29144d2018-01-15 18:06:43 +010071 // Filter ipv4
72 Set<McastRoute> ipv4Routes = routes.stream()
73 .filter(mcastRoute -> mcastRoute.group().isIp4())
74 .collect(Collectors.toSet());
75 // Print ipv4 first
76 ipv4Routes.stream()
77 .sorted(Comparator.comparing(McastRoute::group))
78 .forEach(route -> {
79 // Get sinks
80 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
81 print(FORMAT_MAPPING, route.type(), route.group(),
82 route.source(), sinks);
83 });
84 // Filter ipv6
85 Set<McastRoute> ipv6Routes = routes.stream()
86 .filter(mcastRoute -> mcastRoute.group().isIp6())
87 .collect(Collectors.toSet());
88 // Then print ipv6
89 ipv6Routes.stream()
90 .sorted(Comparator.comparing(McastRoute::group))
91 .forEach(route -> {
92 // Get sinks
93 Set<ConnectPoint> sinks = mcastService.fetchSinks(route);
94 print(FORMAT_MAPPING, route.type(), route.group(),
95 route.source(), sinks);
96 });
alshabibeff00542015-09-23 13:22:33 -070097 }
98
alshabibeff00542015-09-23 13:22:33 -070099}