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