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