blob: dbf2ca664d35c6c7cee2c11ae04d034913b3a34f [file] [log] [blame]
Andrea Campanella545edb42018-03-20 16:37:29 -07001/*
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 */
16package org.onosproject.mcast.cli;
17
Andrea Campanella545edb42018-03-20 16:37:29 -070018import org.apache.karaf.shell.commands.Command;
Pier139babb2018-03-23 14:59:49 -070019import org.apache.karaf.shell.commands.Option;
Andrea Campanella545edb42018-03-20 16:37:29 -070020import org.onlab.packet.IpAddress;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.mcast.api.McastRoute;
23import org.onosproject.mcast.api.MulticastRouteService;
24import org.onosproject.net.ConnectPoint;
Pier139babb2018-03-23 14:59:49 -070025import org.onosproject.net.HostId;
Andrea Campanella545edb42018-03-20 16:37:29 -070026
27import java.util.Comparator;
Pier139babb2018-03-23 14:59:49 -070028import java.util.Map;
Andrea Campanella545edb42018-03-20 16:37:29 -070029import java.util.Set;
30import java.util.stream.Collectors;
31
32import 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")
38public class McastShowHostCommand extends AbstractShellCommand {
39
40 // Format for group line
Pier139babb2018-03-23 14:59:49 -070041 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source IP=%s, sources=%s, sinks=%s";
Andrea Campanella545edb42018-03-20 16:37:29 -070042
Pier139babb2018-03-23 14:59:49 -070043 @Option(name = "-gAddr", aliases = "--groupAddress",
44 description = "IP Address of the multicast group",
45 valueToShowInHelp = "224.0.0.0",
Andrea Campanella545edb42018-03-20 16:37:29 -070046 required = false, multiValued = false)
Pier139babb2018-03-23 14:59:49 -070047 String gAddr = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070048
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
Pier139babb2018-03-23 14:59:49 -070056 if (!isNullOrEmpty(gAddr)) {
Andrea Campanella545edb42018-03-20 16:37:29 -070057 // Let's find the group
Pier139babb2018-03-23 14:59:49 -070058 IpAddress mcastGroup = IpAddress.valueOf(gAddr);
Andrea Campanella545edb42018-03-20 16:37:29 -070059 McastRoute mcastRoute = routes.stream()
60 .filter(route -> route.group().equals(mcastGroup))
61 .findAny().orElse(null);
62 // If it exists
63 if (mcastRoute != null) {
Pier139babb2018-03-23 14:59:49 -070064 printRoute(mcastService, mcastRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070065 }
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 -> {
Pier139babb2018-03-23 14:59:49 -070076 printRoute(mcastService, route);
Andrea Campanella545edb42018-03-20 16:37:29 -070077 });
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 -> {
Pier139babb2018-03-23 14:59:49 -070086 printRoute(mcastService, route);
Andrea Campanella545edb42018-03-20 16:37:29 -070087 });
88 }
89
Pier139babb2018-03-23 14:59:49 -070090 private void printRoute(MulticastRouteService mcastService, McastRoute route) {
91 Map<HostId, Set<ConnectPoint>> sinks = mcastService.routeData(route).sinks();
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020092 Map<HostId, Set<ConnectPoint>> sources = mcastService.routeData(route).sources();
Pier139babb2018-03-23 14:59:49 -070093 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 Campanella545edb42018-03-20 16:37:29 -0700101}