blob: 7ab5da0ce6d38f5e3ee54ee7dad0c572096c6a5d [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;
Ray Milkey8a636bb2018-10-09 14:35:13 -070019import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella545edb42018-03-20 16:37:29 -070022import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.mcast.api.McastRoute;
25import org.onosproject.mcast.api.MulticastRouteService;
26import org.onosproject.net.ConnectPoint;
Pier139babb2018-03-23 14:59:49 -070027import org.onosproject.net.HostId;
Andrea Campanella545edb42018-03-20 16:37:29 -070028
29import java.util.Comparator;
Pier139babb2018-03-23 14:59:49 -070030import java.util.Map;
Andrea Campanella545edb42018-03-20 16:37:29 -070031import java.util.Set;
32import java.util.stream.Collectors;
33
34import static com.google.common.base.Strings.isNullOrEmpty;
35
36/**
37 * Displays the source, multicast group flows entries.
38 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070039@Service
Andrea Campanella545edb42018-03-20 16:37:29 -070040@Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows")
41public class McastShowHostCommand extends AbstractShellCommand {
42
43 // Format for group line
Pier139babb2018-03-23 14:59:49 -070044 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source IP=%s, sources=%s, sinks=%s";
Andrea Campanella545edb42018-03-20 16:37:29 -070045
Pier139babb2018-03-23 14:59:49 -070046 @Option(name = "-gAddr", aliases = "--groupAddress",
47 description = "IP Address of the multicast group",
48 valueToShowInHelp = "224.0.0.0",
Andrea Campanella545edb42018-03-20 16:37:29 -070049 required = false, multiValued = false)
Ray Milkey8a636bb2018-10-09 14:35:13 -070050 @Completion(McastGroupCompleter.class)
Pier139babb2018-03-23 14:59:49 -070051 String gAddr = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070052
53 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070054 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070055 // Get the service
56 MulticastRouteService mcastService = get(MulticastRouteService.class);
57 // Get the routes
58 Set<McastRoute> routes = mcastService.getRoutes();
59 // Verify mcast group
Pier139babb2018-03-23 14:59:49 -070060 if (!isNullOrEmpty(gAddr)) {
Andrea Campanella545edb42018-03-20 16:37:29 -070061 // Let's find the group
Pier139babb2018-03-23 14:59:49 -070062 IpAddress mcastGroup = IpAddress.valueOf(gAddr);
Andrea Campanella545edb42018-03-20 16:37:29 -070063 McastRoute mcastRoute = routes.stream()
64 .filter(route -> route.group().equals(mcastGroup))
65 .findAny().orElse(null);
66 // If it exists
67 if (mcastRoute != null) {
Pier139babb2018-03-23 14:59:49 -070068 printRoute(mcastService, mcastRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070069 }
70 return;
71 }
72 // Filter ipv4
73 Set<McastRoute> ipv4Routes = routes.stream()
74 .filter(mcastRoute -> mcastRoute.group().isIp4())
75 .collect(Collectors.toSet());
76 // Print ipv4 first
77 ipv4Routes.stream()
78 .sorted(Comparator.comparing(McastRoute::group))
79 .forEach(route -> {
Pier139babb2018-03-23 14:59:49 -070080 printRoute(mcastService, route);
Andrea Campanella545edb42018-03-20 16:37:29 -070081 });
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 -> {
Pier139babb2018-03-23 14:59:49 -070090 printRoute(mcastService, route);
Andrea Campanella545edb42018-03-20 16:37:29 -070091 });
92 }
93
Pier139babb2018-03-23 14:59:49 -070094 private void printRoute(MulticastRouteService mcastService, McastRoute route) {
95 Map<HostId, Set<ConnectPoint>> sinks = mcastService.routeData(route).sinks();
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020096 Map<HostId, Set<ConnectPoint>> sources = mcastService.routeData(route).sources();
Pier139babb2018-03-23 14:59:49 -070097 String srcIp = "*";
98 if (route.source().isPresent()) {
99 srcIp = route.source().get().toString();
100 }
101
102 print(FORMAT_MAPPING, route.type(), route.group(), srcIp, sources, sinks);
103 }
104
Andrea Campanella545edb42018-03-20 16:37:29 -0700105}