Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 18 | import org.apache.karaf.shell.api.action.Command; |
Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.mcast.api.McastRoute; |
| 22 | import org.onosproject.mcast.api.MulticastRouteService; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | |
| 25 | import java.util.Comparator; |
| 26 | import java.util.Optional; |
| 27 | import java.util.Set; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
| 30 | /** |
| 31 | * Displays the source, multicast group flows entries. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "mcast-host-routes", |
| 34 | description = "Lists routes in the mcast route store") |
| 35 | public class McastRoutesListCommand extends AbstractShellCommand { |
| 36 | |
| 37 | // Format for total |
| 38 | private static final String FORMAT_TOTAL = " Total: %d"; |
| 39 | |
| 40 | // Format for ipv4 |
| 41 | private static final String FORMAT_ROUTE = "%-1s %-18s %-15s %s %s %s"; |
| 42 | // Format for ipv6 |
| 43 | private static final String FORMAT_ROUTE6 = "%-1s %-40s %-36s %s %s %s"; |
| 44 | |
| 45 | // Table header |
| 46 | private static final String FORMAT_TABLE = "Table: %s"; |
| 47 | private static final String GROUP = "Group"; |
| 48 | private static final String SOURCE = "Source"; |
| 49 | private static final String ORIGIN = "Origin"; |
| 50 | private static final String SOURCES = "Sources"; |
| 51 | private static final String SINKS = "Sinks"; |
| 52 | |
| 53 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 54 | protected void doExecute() { |
Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame] | 55 | // Get the service |
| 56 | MulticastRouteService mcastService = get(MulticastRouteService.class); |
| 57 | // Get the routes |
| 58 | Set<McastRoute> routes = mcastService.getRoutes(); |
| 59 | // Filter ipv4 |
| 60 | Set<McastRoute> ipv4Routes = routes.stream() |
| 61 | .filter(mcastRoute -> mcastRoute.group().isIp4()) |
| 62 | .collect(Collectors.toSet()); |
| 63 | // Filter ipv6 |
| 64 | Set<McastRoute> ipv6Routes = routes.stream() |
| 65 | .filter(mcastRoute -> mcastRoute.group().isIp6()) |
| 66 | .collect(Collectors.toSet()); |
| 67 | // Print header |
| 68 | print(FORMAT_TABLE, "ipv4"); |
| 69 | print(FORMAT_ROUTE, "", GROUP, SOURCE, ORIGIN, SOURCES, SINKS); |
| 70 | // Print ipv4 mcast routing entries |
| 71 | ipv4Routes.stream() |
| 72 | .sorted(Comparator.comparing(McastRoute::group)) |
| 73 | .forEach(route -> { |
| 74 | // Get sinks and sources |
| 75 | Set<ConnectPoint> sources = mcastService.sources(route); |
| 76 | Set<ConnectPoint> sinks = mcastService.sinks(route); |
| 77 | Optional<IpAddress> sourceIp = route.source(); |
| 78 | String src = "* "; |
| 79 | if (sourceIp.isPresent()) { |
| 80 | src = sourceIp.get().toString(); |
| 81 | } |
| 82 | print(FORMAT_ROUTE, "", route.group(), src, |
| 83 | route.type(), sources.size(), " " + sinks.size()); |
| 84 | }); |
| 85 | print(FORMAT_TOTAL, ipv4Routes.size()); |
| 86 | print(""); |
| 87 | |
| 88 | // Print header |
| 89 | print(FORMAT_TABLE, "ipv6"); |
| 90 | print(FORMAT_ROUTE6, "", GROUP, SOURCE, ORIGIN, SOURCES, SINKS); |
| 91 | // Print ipv6 mcast routing entries |
| 92 | ipv6Routes.stream() |
| 93 | .sorted(Comparator.comparing(McastRoute::group)) |
| 94 | .forEach(route -> { |
| 95 | // Get sinks and sources |
| 96 | Set<ConnectPoint> sources = mcastService.sources(route); |
| 97 | Set<ConnectPoint> sinks = mcastService.sinks(route); |
| 98 | Optional<IpAddress> sourceIp = route.source(); |
| 99 | String src = "* "; |
| 100 | if (sourceIp.isPresent()) { |
| 101 | src = sourceIp.get().toString(); |
| 102 | } |
| 103 | print(FORMAT_ROUTE6, "", route.group(), src, |
| 104 | route.type(), sources.size(), " " + sinks.size()); |
| 105 | }); |
| 106 | print(FORMAT_TOTAL, ipv6Routes.size()); |
| 107 | print(""); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | } |