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 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Argument; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Command; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.mcast.McastRoute; |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 24 | import org.onosproject.net.mcast.MulticastRouteService; |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 25 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 26 | import java.util.Comparator; |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 27 | import java.util.Set; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 28 | import java.util.stream.Collectors; |
| 29 | |
| 30 | import static com.google.common.base.Strings.isNullOrEmpty; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Displays the source, multicast group flows entries. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "mcast-show", description = "Displays the source, multicast group flows") |
| 36 | public class McastShowCommand extends AbstractShellCommand { |
| 37 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 38 | // 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; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 47 | // Get the service |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 48 | MulticastRouteService mcastService = get(MulticastRouteService.class); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 49 | // Get the routes |
Jonathan Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 50 | Set<McastRoute> routes = mcastService.getRoutes(); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 51 | // 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 Hart | 1d00639 | 2016-02-11 09:12:56 -0800 | [diff] [blame] | 66 | } |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 67 | // 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 | }); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 93 | } |
| 94 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 95 | } |