Pier | e99511d | 2018-04-19 16:47:06 +0200 | [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.Maps; |
| 21 | import com.google.common.collect.Multimap; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.apache.karaf.shell.commands.Option; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.mcast.cli.McastGroupCompleter; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 30 | import org.onosproject.segmentrouting.mcast.McastRole; |
| 31 | import org.onosproject.segmentrouting.mcast.McastRoleStoreKey; |
| 32 | |
| 33 | import java.util.Collection; |
| 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; |
| 39 | |
| 40 | /** |
| 41 | * Command to show the list of mcast roles. |
| 42 | */ |
| 43 | @Command(scope = "onos", name = "sr-mcast-role", |
| 44 | description = "Lists all mcast roles") |
| 45 | public class McastRoleListCommand extends AbstractShellCommand { |
| 46 | |
| 47 | // OSGi workaround to introduce package dependency |
| 48 | McastGroupCompleter completer; |
| 49 | |
| 50 | // Format for group line |
| 51 | private static final String FORMAT_MAPPING = "%s,%s ingress=%s\ttransit=%s\tegress=%s"; |
| 52 | |
| 53 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 54 | description = "IP Address of the multicast group", |
| 55 | valueToShowInHelp = "224.0.0.0", |
| 56 | required = false, multiValued = false) |
| 57 | String gAddr = null; |
| 58 | |
| 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 | |
| 65 | @Override |
| 66 | protected void execute() { |
| 67 | // Verify mcast group |
| 68 | IpAddress mcastGroup = null; |
| 69 | // We want to use source cp only for a specific group |
| 70 | ConnectPoint sourcecp = null; |
| 71 | if (!isNullOrEmpty(gAddr)) { |
| 72 | mcastGroup = IpAddress.valueOf(gAddr); |
| 73 | if (!isNullOrEmpty(source)) { |
| 74 | sourcecp = ConnectPoint.deviceConnectPoint(source); |
| 75 | } |
| 76 | } |
| 77 | // Get SR service, the roles and the groups |
| 78 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 79 | Map<McastRoleStoreKey, McastRole> keyToRole = srService.getMcastRoles(mcastGroup, sourcecp); |
| 80 | Set<IpAddress> mcastGroups = keyToRole.keySet().stream() |
| 81 | .map(McastRoleStoreKey::mcastIp) |
| 82 | .collect(Collectors.toSet()); |
| 83 | // Print the trees for each group |
| 84 | mcastGroups.forEach(group -> { |
| 85 | // Create a new map for the group |
| 86 | Map<ConnectPoint, Multimap<McastRole, DeviceId>> roleDeviceIdMap = Maps.newHashMap(); |
| 87 | keyToRole.entrySet() |
| 88 | .stream() |
| 89 | .filter(entry -> entry.getKey().mcastIp().equals(group)) |
| 90 | .forEach(entry -> roleDeviceIdMap.compute(entry.getKey().source(), (gsource, map) -> { |
| 91 | map = map == null ? ArrayListMultimap.create() : map; |
| 92 | map.put(entry.getValue(), entry.getKey().deviceId()); |
| 93 | return map; |
| 94 | })); |
| 95 | roleDeviceIdMap.forEach((gsource, map) -> { |
| 96 | // Print the map |
| 97 | printMcastRole(group, gsource, |
| 98 | map.get(McastRole.INGRESS), |
| 99 | map.get(McastRole.TRANSIT), |
| 100 | map.get(McastRole.EGRESS)); |
| 101 | }); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | private void printMcastRole(IpAddress mcastGroup, ConnectPoint source, |
| 106 | Collection<DeviceId> ingress, |
| 107 | Collection<DeviceId> transit, |
| 108 | Collection<DeviceId> egress) { |
| 109 | print(FORMAT_MAPPING, mcastGroup, source, ingress, transit, egress); |
| 110 | } |
| 111 | } |