Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.mcast.cli; |
| 17 | |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.mcast.api.McastRoute; |
| 27 | import org.onosproject.mcast.api.MulticastRouteService; |
| 28 | import org.onosproject.net.ConnectPoint; |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 29 | import org.onosproject.net.HostId; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.Comparator; |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 32 | import java.util.Map; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 33 | import java.util.Set; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 34 | |
| 35 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 36 | |
| 37 | /** |
| 38 | * Displays the source, multicast group flows entries. |
| 39 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 40 | @Service |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 41 | @Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows") |
| 42 | public class McastShowHostCommand extends AbstractShellCommand { |
| 43 | |
| 44 | // Format for group line |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 45 | 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 48 | |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 49 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 50 | description = "IP Address of the multicast group", |
| 51 | valueToShowInHelp = "224.0.0.0", |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 52 | required = false, multiValued = false) |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 53 | @Completion(McastGroupCompleter.class) |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 54 | String gAddr = null; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 55 | |
| 56 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 57 | protected void doExecute() { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 58 | // Get the service |
| 59 | MulticastRouteService mcastService = get(MulticastRouteService.class); |
| 60 | // Get the routes |
| 61 | Set<McastRoute> routes = mcastService.getRoutes(); |
| 62 | // Verify mcast group |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 63 | if (!isNullOrEmpty(gAddr)) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 64 | // Let's find the group |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 65 | IpAddress mcastGroup = IpAddress.valueOf(gAddr); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 66 | McastRoute mcastRoute = routes.stream() |
| 67 | .filter(route -> route.group().equals(mcastGroup)) |
| 68 | .findAny().orElse(null); |
| 69 | // If it exists |
| 70 | if (mcastRoute != null) { |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 71 | prepareResult(mcastService, mcastRoute); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 72 | } |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 73 | } 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 Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 86 | } |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 87 | if (outputJson()) { |
| 88 | print("%s", routesNode); |
| 89 | } else { |
| 90 | print("%s", routesBuilder.toString()); |
| 91 | } |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Seyeon Jeong | 357bcec | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 94 | 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)); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 107 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 110 | } |