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