blob: 562afad01fdb3bb6e4e998f083a2a5c94f5c3263 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import 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;
25
26import java.util.Comparator;
27import java.util.Set;
28import java.util.stream.Collectors;
29
30import static com.google.common.base.Strings.isNullOrEmpty;
31
32/**
33 * Displays the source, multicast group flows entries.
34 */
35@Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows")
36public class McastShowHostCommand extends AbstractShellCommand {
37
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;
44
45 @Override
46 protected void execute() {
47 // Get the service
48 MulticastRouteService mcastService = get(MulticastRouteService.class);
49 // Get the routes
50 Set<McastRoute> routes = mcastService.getRoutes();
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.sinks(mcastRoute);
62 Set<ConnectPoint> sources = mcastService.sources(mcastRoute);
63 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
64 sources, sinks);
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 -> {
76 // Get sinks
77 Set<ConnectPoint> sinks = mcastService.sinks(route);
78 Set<ConnectPoint> sources = mcastService.sources(route);
79 print(FORMAT_MAPPING, route.type(), route.group(),
80 sources, sinks);
81 });
82 // Filter ipv6
83 Set<McastRoute> ipv6Routes = routes.stream()
84 .filter(mcastRoute -> mcastRoute.group().isIp6())
85 .collect(Collectors.toSet());
86 // Then print ipv6
87 ipv6Routes.stream()
88 .sorted(Comparator.comparing(McastRoute::group))
89 .forEach(route -> {
90 // Get sinks
91 Set<ConnectPoint> sinks = mcastService.sinks(route);
92 Set<ConnectPoint> sources = mcastService.sources(route);
93 print(FORMAT_MAPPING, route.type(), route.group(),
94 sources, sinks);
95 });
96 }
97
98}