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