Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.segmentrouting.cli; |
| 18 | |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.ImmutableSet; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 23 | import com.google.common.collect.Multimap; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Command; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Option; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onosproject.cli.AbstractShellCommand; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 28 | import org.onosproject.mcast.cli.McastGroupCompleter; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 30 | import org.onosproject.segmentrouting.SegmentRoutingService; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 31 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 32 | import java.util.List; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 33 | import java.util.Set; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
| 36 | import static com.google.common.base.Strings.isNullOrEmpty; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Command to show the list of mcast trees. |
| 40 | */ |
| 41 | @Command(scope = "onos", name = "sr-mcast-tree", |
| 42 | description = "Lists all mcast trees") |
| 43 | public class McastTreeListCommand extends AbstractShellCommand { |
| 44 | |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 45 | // OSGi workaround to introduce package dependency |
| 46 | McastGroupCompleter completer; |
| 47 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 48 | // Format for group line |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 49 | private static final String G_FORMAT_MAPPING = "group=%s"; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 50 | // Format for sink line |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 51 | private static final String S_FORMAT_MAPPING = " sink=%s\tpath=%s"; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 52 | |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 53 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 54 | description = "IP Address of the multicast group", |
| 55 | valueToShowInHelp = "224.0.0.0", |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 56 | required = false, multiValued = false) |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 57 | String gAddr = null; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 58 | |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 59 | @Option(name = "-src", aliases = "--connectPoint", |
| 60 | description = "Source port of:XXXXXXXXXX/XX", |
| 61 | valueToShowInHelp = "of:0000000000000001/1", |
| 62 | required = false, multiValued = false) |
| 63 | String source = null; |
| 64 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 65 | @Override |
| 66 | protected void execute() { |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 67 | // Get SR service and the handled mcast groups |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 68 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 69 | Set<IpAddress> mcastGroups = ImmutableSet.copyOf(srService.getMcastLeaders(null) |
| 70 | .keySet()); |
| 71 | |
| 72 | if (!isNullOrEmpty(gAddr)) { |
| 73 | mcastGroups = mcastGroups.stream() |
| 74 | .filter(mcastIp -> mcastIp.equals(IpAddress.valueOf(gAddr))) |
| 75 | .collect(Collectors.toSet()); |
| 76 | } |
| 77 | |
| 78 | ObjectMapper mapper = new ObjectMapper(); |
| 79 | ObjectNode root = mapper.createObjectNode(); |
| 80 | |
| 81 | // Print the trees for each group or build json objects |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 82 | mcastGroups.forEach(group -> { |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 83 | // We want to use source cp only for a specific group |
| 84 | ConnectPoint sourcecp = null; |
| 85 | if (!isNullOrEmpty(source) && |
| 86 | !isNullOrEmpty(gAddr)) { |
| 87 | sourcecp = ConnectPoint.deviceConnectPoint(source); |
| 88 | } |
| 89 | Multimap<ConnectPoint, List<ConnectPoint>> mcastTree = srService.getMcastTrees(group, |
| 90 | sourcecp); |
| 91 | // Build a json object for each group |
| 92 | if (outputJson()) { |
| 93 | root.putPOJO(group.toString(), json(mcastTree)); |
| 94 | } else { |
| 95 | // Banner and then the trees |
| 96 | printMcastGroup(group); |
| 97 | mcastTree.forEach(this::printMcastSink); |
| 98 | } |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 99 | }); |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 100 | |
| 101 | // Print the json object at the end |
| 102 | if (outputJson()) { |
| 103 | print("%s", root); |
| 104 | } |
| 105 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 108 | private void printMcastGroup(IpAddress mcastGroup) { |
| 109 | print(G_FORMAT_MAPPING, mcastGroup); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private void printMcastSink(ConnectPoint sink, List<ConnectPoint> path) { |
| 113 | print(S_FORMAT_MAPPING, sink, path); |
| 114 | } |
Pier | b1fe738 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 115 | |
| 116 | private ObjectNode json(Multimap<ConnectPoint, List<ConnectPoint>> mcastTree) { |
| 117 | ObjectMapper mapper = new ObjectMapper(); |
| 118 | ObjectNode jsonSinks = mapper.createObjectNode(); |
| 119 | mcastTree.asMap().forEach((sink, paths) -> { |
| 120 | ArrayNode jsonPaths = mapper.createArrayNode(); |
| 121 | paths.forEach(path -> { |
| 122 | ArrayNode jsonPath = mapper.createArrayNode(); |
| 123 | path.forEach(connectPoint -> jsonPath.add(connectPoint.toString())); |
| 124 | jsonPaths.addPOJO(jsonPath); |
| 125 | }); |
| 126 | jsonSinks.putPOJO(sink.toString(), jsonPaths); |
| 127 | }); |
| 128 | return jsonSinks; |
| 129 | } |
| 130 | |
| 131 | } |