Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.mcast.cli; |
| 17 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Command; |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Option; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.mcast.api.McastRoute; |
| 23 | import org.onosproject.mcast.api.MulticastRouteService; |
| 24 | import org.onosproject.net.ConnectPoint; |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 25 | import org.onosproject.net.HostId; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Comparator; |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 28 | import java.util.Map; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
| 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 33 | |
| 34 | /** |
| 35 | * Displays the source, multicast group flows entries. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows") |
| 38 | public class McastShowHostCommand extends AbstractShellCommand { |
| 39 | |
| 40 | // Format for group line |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 41 | private static final String FORMAT_MAPPING = "origin=%s, group=%s, source IP=%s, sources=%s, sinks=%s"; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 42 | |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 43 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 44 | description = "IP Address of the multicast group", |
| 45 | valueToShowInHelp = "224.0.0.0", |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 46 | required = false, multiValued = false) |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 47 | String gAddr = null; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 48 | |
| 49 | @Override |
| 50 | protected void execute() { |
| 51 | // Get the service |
| 52 | MulticastRouteService mcastService = get(MulticastRouteService.class); |
| 53 | // Get the routes |
| 54 | Set<McastRoute> routes = mcastService.getRoutes(); |
| 55 | // Verify mcast group |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 56 | if (!isNullOrEmpty(gAddr)) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 57 | // Let's find the group |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 58 | IpAddress mcastGroup = IpAddress.valueOf(gAddr); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 59 | McastRoute mcastRoute = routes.stream() |
| 60 | .filter(route -> route.group().equals(mcastGroup)) |
| 61 | .findAny().orElse(null); |
| 62 | // If it exists |
| 63 | if (mcastRoute != null) { |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 64 | printRoute(mcastService, mcastRoute); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 65 | } |
| 66 | return; |
| 67 | } |
| 68 | // Filter ipv4 |
| 69 | Set<McastRoute> ipv4Routes = routes.stream() |
| 70 | .filter(mcastRoute -> mcastRoute.group().isIp4()) |
| 71 | .collect(Collectors.toSet()); |
| 72 | // Print ipv4 first |
| 73 | ipv4Routes.stream() |
| 74 | .sorted(Comparator.comparing(McastRoute::group)) |
| 75 | .forEach(route -> { |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 76 | printRoute(mcastService, route); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 77 | }); |
| 78 | // Filter ipv6 |
| 79 | Set<McastRoute> ipv6Routes = routes.stream() |
| 80 | .filter(mcastRoute -> mcastRoute.group().isIp6()) |
| 81 | .collect(Collectors.toSet()); |
| 82 | // Then print ipv6 |
| 83 | ipv6Routes.stream() |
| 84 | .sorted(Comparator.comparing(McastRoute::group)) |
| 85 | .forEach(route -> { |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 86 | printRoute(mcastService, route); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 87 | }); |
| 88 | } |
| 89 | |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 90 | private void printRoute(MulticastRouteService mcastService, McastRoute route) { |
| 91 | Map<HostId, Set<ConnectPoint>> sinks = mcastService.routeData(route).sinks(); |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 92 | Map<HostId, Set<ConnectPoint>> sources = mcastService.routeData(route).sources(); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 93 | String srcIp = "*"; |
| 94 | if (route.source().isPresent()) { |
| 95 | srcIp = route.source().get().toString(); |
| 96 | } |
| 97 | |
| 98 | print(FORMAT_MAPPING, route.type(), route.group(), srcIp, sources, sinks); |
| 99 | } |
| 100 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 101 | } |