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 | |
| 19 | import com.google.common.collect.ArrayListMultimap; |
| 20 | import com.google.common.collect.Multimap; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame^] | 22 | import org.apache.karaf.shell.commands.Option; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame^] | 25 | import org.onosproject.mcast.cli.McastGroupCompleter; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 28 | import org.onosproject.segmentrouting.SegmentRoutingService; |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 29 | import org.onosproject.segmentrouting.mcast.McastRole; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 30 | import org.onosproject.segmentrouting.storekey.McastStoreKey; |
| 31 | |
| 32 | import java.util.Collection; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.Set; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import static com.google.common.base.Strings.isNullOrEmpty; |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 39 | import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS; |
| 40 | import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS; |
| 41 | import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Command to show the list of mcast trees. |
| 45 | */ |
| 46 | @Command(scope = "onos", name = "sr-mcast-tree", |
| 47 | description = "Lists all mcast trees") |
| 48 | public class McastTreeListCommand extends AbstractShellCommand { |
| 49 | |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame^] | 50 | // OSGi workaround to introduce package dependency |
| 51 | McastGroupCompleter completer; |
| 52 | |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 53 | // Format for group line |
| 54 | private static final String G_FORMAT_MAPPING = "group=%s, ingress=%s, transit=%s, egress=%s"; |
| 55 | // Format for sink line |
| 56 | private static final String S_FORMAT_MAPPING = "\tsink=%s\tpath=%s"; |
| 57 | |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame^] | 58 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 59 | description = "IP Address of the multicast group", |
| 60 | valueToShowInHelp = "224.0.0.0", |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 61 | required = false, multiValued = false) |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame^] | 62 | String gAddr = null; |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | protected void execute() { |
| 66 | // Verify mcast group |
| 67 | IpAddress mcastGroup = null; |
Pier | 3ee2455 | 2018-03-14 16:47:32 -0700 | [diff] [blame^] | 68 | if (!isNullOrEmpty(gAddr)) { |
| 69 | mcastGroup = IpAddress.valueOf(gAddr); |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 70 | |
| 71 | } |
| 72 | // Get SR service |
| 73 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 74 | // Get the mapping |
| 75 | Map<McastStoreKey, McastRole> keyToRole = srService.getMcastRoles(mcastGroup); |
| 76 | // Reduce to the set of mcast groups |
| 77 | Set<IpAddress> mcastGroups = keyToRole.keySet().stream() |
| 78 | .map(McastStoreKey::mcastIp) |
| 79 | .collect(Collectors.toSet()); |
| 80 | // Print the trees for each group |
| 81 | mcastGroups.forEach(group -> { |
| 82 | // Create a new map for the group |
| 83 | Multimap<McastRole, DeviceId> roleDeviceIdMap = ArrayListMultimap.create(); |
| 84 | keyToRole.entrySet() |
| 85 | .stream() |
| 86 | // Filter only the elements related to this group |
| 87 | .filter(entry -> entry.getKey().mcastIp().equals(group)) |
| 88 | // For each create a new entry in the group related map |
| 89 | .forEach(entry -> roleDeviceIdMap.put(entry.getValue(), entry.getKey().deviceId())); |
| 90 | // Print the map |
| 91 | printMcastRole(group, |
| 92 | roleDeviceIdMap.get(INGRESS), |
| 93 | roleDeviceIdMap.get(TRANSIT), |
| 94 | roleDeviceIdMap.get(EGRESS) |
| 95 | ); |
| 96 | // Get sinks paths |
| 97 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = srService.getMcastPaths(group); |
| 98 | // Print the paths |
| 99 | mcastPaths.forEach(this::printMcastSink); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | private void printMcastRole(IpAddress mcastGroup, |
| 104 | Collection<DeviceId> ingress, |
| 105 | Collection<DeviceId> transit, |
| 106 | Collection<DeviceId> egress) { |
| 107 | print(G_FORMAT_MAPPING, mcastGroup, ingress, transit, egress); |
| 108 | } |
| 109 | |
| 110 | private void printMcastSink(ConnectPoint sink, List<ConnectPoint> path) { |
| 111 | print(S_FORMAT_MAPPING, sink, path); |
| 112 | } |
| 113 | } |