blob: 63fb015659cc902aa0e55f8c3a8e46e550e2940d [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella545edb42018-03-20 16:37:29 -070021import org.onlab.packet.IpAddress;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.mcast.api.McastRoute;
24import org.onosproject.mcast.api.MulticastRouteService;
25import org.onosproject.net.ConnectPoint;
Pier139babb2018-03-23 14:59:49 -070026import org.onosproject.net.HostId;
Andrea Campanella545edb42018-03-20 16:37:29 -070027
28import java.util.Comparator;
Pier139babb2018-03-23 14:59:49 -070029import java.util.Map;
Andrea Campanella545edb42018-03-20 16:37:29 -070030import java.util.Set;
31import java.util.stream.Collectors;
32
33import static com.google.common.base.Strings.isNullOrEmpty;
34
35/**
36 * Displays the source, multicast group flows entries.
37 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Andrea Campanella545edb42018-03-20 16:37:29 -070039@Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows")
40public class McastShowHostCommand extends AbstractShellCommand {
41
42 // Format for group line
Pier139babb2018-03-23 14:59:49 -070043 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source IP=%s, sources=%s, sinks=%s";
Andrea Campanella545edb42018-03-20 16:37:29 -070044
Pier139babb2018-03-23 14:59:49 -070045 @Option(name = "-gAddr", aliases = "--groupAddress",
46 description = "IP Address of the multicast group",
47 valueToShowInHelp = "224.0.0.0",
Andrea Campanella545edb42018-03-20 16:37:29 -070048 required = false, multiValued = false)
Pier139babb2018-03-23 14:59:49 -070049 String gAddr = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070050
51 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070052 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070053 // Get the service
54 MulticastRouteService mcastService = get(MulticastRouteService.class);
55 // Get the routes
56 Set<McastRoute> routes = mcastService.getRoutes();
57 // Verify mcast group
Pier139babb2018-03-23 14:59:49 -070058 if (!isNullOrEmpty(gAddr)) {
Andrea Campanella545edb42018-03-20 16:37:29 -070059 // Let's find the group
Pier139babb2018-03-23 14:59:49 -070060 IpAddress mcastGroup = IpAddress.valueOf(gAddr);
Andrea Campanella545edb42018-03-20 16:37:29 -070061 McastRoute mcastRoute = routes.stream()
62 .filter(route -> route.group().equals(mcastGroup))
63 .findAny().orElse(null);
64 // If it exists
65 if (mcastRoute != null) {
Pier139babb2018-03-23 14:59:49 -070066 printRoute(mcastService, mcastRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070067 }
68 return;
69 }
70 // Filter ipv4
71 Set<McastRoute> ipv4Routes = routes.stream()
72 .filter(mcastRoute -> mcastRoute.group().isIp4())
73 .collect(Collectors.toSet());
74 // Print ipv4 first
75 ipv4Routes.stream()
76 .sorted(Comparator.comparing(McastRoute::group))
77 .forEach(route -> {
Pier139babb2018-03-23 14:59:49 -070078 printRoute(mcastService, route);
Andrea Campanella545edb42018-03-20 16:37:29 -070079 });
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 -> {
Pier139babb2018-03-23 14:59:49 -070088 printRoute(mcastService, route);
Andrea Campanella545edb42018-03-20 16:37:29 -070089 });
90 }
91
Pier139babb2018-03-23 14:59:49 -070092 private void printRoute(MulticastRouteService mcastService, McastRoute route) {
93 Map<HostId, Set<ConnectPoint>> sinks = mcastService.routeData(route).sinks();
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020094 Map<HostId, Set<ConnectPoint>> sources = mcastService.routeData(route).sources();
Pier139babb2018-03-23 14:59:49 -070095 String srcIp = "*";
96 if (route.source().isPresent()) {
97 srcIp = route.source().get().toString();
98 }
99
100 print(FORMAT_MAPPING, route.type(), route.group(), srcIp, sources, sinks);
101 }
102
Andrea Campanella545edb42018-03-20 16:37:29 -0700103}