alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 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 | */ |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.mcast.McastRoute; |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 26 | import org.onosproject.net.mcast.MulticastRouteService; |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 27 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 28 | import java.util.Comparator; |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 29 | import java.util.Set; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
| 31 | |
| 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Displays the source, multicast group flows entries. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "mcast-show", description = "Displays the source, multicast group flows") |
| 39 | public class McastShowCommand extends AbstractShellCommand { |
| 40 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 41 | // 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 Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 46 | @Completion(McastGroupCompleter.class) |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 47 | String mcastIp; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 48 | |
| 49 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 51 | // Get the service |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 52 | MulticastRouteService mcastService = get(MulticastRouteService.class); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 53 | // Get the routes |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 54 | Set<McastRoute> routes = mcastService.getRoutes(); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 55 | // 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 Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 70 | } |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 71 | // 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 | }); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 97 | } |
| 98 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 99 | } |