blob: 22613788ddaadb748f40fc9bc94af27bdd8ee3e9 [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
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Ray Milkey8a636bb2018-10-09 14:35:13 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella545edb42018-03-20 16:37:29 -070024import org.onlab.packet.IpAddress;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.mcast.api.McastRoute;
27import org.onosproject.mcast.api.MulticastRouteService;
28import org.onosproject.net.ConnectPoint;
Pier139babb2018-03-23 14:59:49 -070029import org.onosproject.net.HostId;
Andrea Campanella545edb42018-03-20 16:37:29 -070030
31import java.util.Comparator;
Pier139babb2018-03-23 14:59:49 -070032import java.util.Map;
Andrea Campanella545edb42018-03-20 16:37:29 -070033import java.util.Set;
Andrea Campanella545edb42018-03-20 16:37:29 -070034
35import static com.google.common.base.Strings.isNullOrEmpty;
36
37/**
38 * Displays the source, multicast group flows entries.
39 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070040@Service
Andrea Campanella545edb42018-03-20 16:37:29 -070041@Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows")
42public class McastShowHostCommand extends AbstractShellCommand {
43
44 // Format for group line
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080045 private static final String FORMAT_MAPPING = "origin=%s, group=%s, source IP=%s, sources=%s, sinks=%s\n";
46 private StringBuilder routesBuilder = new StringBuilder();
47 private ArrayNode routesNode = mapper().createArrayNode();
Andrea Campanella545edb42018-03-20 16:37:29 -070048
Pier139babb2018-03-23 14:59:49 -070049 @Option(name = "-gAddr", aliases = "--groupAddress",
50 description = "IP Address of the multicast group",
51 valueToShowInHelp = "224.0.0.0",
Andrea Campanella545edb42018-03-20 16:37:29 -070052 required = false, multiValued = false)
Ray Milkey8a636bb2018-10-09 14:35:13 -070053 @Completion(McastGroupCompleter.class)
Pier139babb2018-03-23 14:59:49 -070054 String gAddr = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070055
56 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070057 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070058 // Get the service
59 MulticastRouteService mcastService = get(MulticastRouteService.class);
60 // Get the routes
61 Set<McastRoute> routes = mcastService.getRoutes();
62 // Verify mcast group
Pier139babb2018-03-23 14:59:49 -070063 if (!isNullOrEmpty(gAddr)) {
Andrea Campanella545edb42018-03-20 16:37:29 -070064 // Let's find the group
Pier139babb2018-03-23 14:59:49 -070065 IpAddress mcastGroup = IpAddress.valueOf(gAddr);
Andrea Campanella545edb42018-03-20 16:37:29 -070066 McastRoute mcastRoute = routes.stream()
67 .filter(route -> route.group().equals(mcastGroup))
68 .findAny().orElse(null);
69 // If it exists
70 if (mcastRoute != null) {
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080071 prepareResult(mcastService, mcastRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070072 }
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080073 } else {
74 routes.stream()
75 .filter(mcastRoute -> mcastRoute.group().isIp4())
76 .sorted(Comparator.comparing(McastRoute::group))
77 .forEach(route -> {
78 prepareResult(mcastService, route);
79 });
80 routes.stream()
81 .filter(mcastRoute -> mcastRoute.group().isIp6())
82 .sorted(Comparator.comparing(McastRoute::group))
83 .forEach(route -> {
84 prepareResult(mcastService, route);
85 });
Andrea Campanella545edb42018-03-20 16:37:29 -070086 }
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080087 if (outputJson()) {
88 print("%s", routesNode);
89 } else {
90 print("%s", routesBuilder.toString());
91 }
Andrea Campanella545edb42018-03-20 16:37:29 -070092 }
93
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080094 private void prepareResult(MulticastRouteService mcastService, McastRoute route) {
95 if (outputJson()) {
96 // McastHostRouteCodec is used to encode McastRoute
97 ObjectNode routeNode = jsonForEntity(route, McastRoute.class);
98 routesNode.add(routeNode);
99 } else {
100 Map<HostId, Set<ConnectPoint>> sinks = mcastService.routeData(route).sinks();
101 Map<HostId, Set<ConnectPoint>> sources = mcastService.routeData(route).sources();
102 String srcIp = "*";
103 if (route.source().isPresent()) {
104 srcIp = route.source().get().toString();
105 }
106 routesBuilder.append(String.format(FORMAT_MAPPING, route.type(), route.group(), srcIp, sources, sinks));
Pier139babb2018-03-23 14:59:49 -0700107 }
Pier139babb2018-03-23 14:59:49 -0700108 }
109
Andrea Campanella545edb42018-03-20 16:37:29 -0700110}