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