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 | package org.onosproject.segmentrouting.cli; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 25 | import org.onosproject.segmentrouting.storekey.McastStoreKey; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | import java.util.stream.Collectors; |
| 30 | |
| 31 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 32 | |
| 33 | /** |
| 34 | * Command to show the list of mcast nextids. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "sr-mcast-next", |
| 37 | description = "Lists all mcast nextids") |
| 38 | public class McastNextListCommand extends AbstractShellCommand { |
| 39 | |
| 40 | // Format for group line |
| 41 | private static final String FORMAT_MAPPING = "group=%s, deviceIds-nextIds=%s"; |
| 42 | |
| 43 | @Argument(index = 0, name = "mcastIp", description = "mcast Ip", |
| 44 | required = false, multiValued = false) |
| 45 | String mcastIp; |
| 46 | |
| 47 | @Override |
| 48 | protected void execute() { |
| 49 | // Verify mcast group |
| 50 | IpAddress mcastGroup = null; |
| 51 | if (!isNullOrEmpty(mcastIp)) { |
| 52 | mcastGroup = IpAddress.valueOf(mcastIp); |
| 53 | |
| 54 | } |
| 55 | // Get SR service |
| 56 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 57 | // Get the mapping |
| 58 | Map<McastStoreKey, Integer> keyToNextId = srService.getMcastNextIds(mcastGroup); |
| 59 | // Reduce to the set of mcast groups |
| 60 | Set<IpAddress> mcastGroups = keyToNextId.keySet().stream() |
| 61 | .map(McastStoreKey::mcastIp) |
| 62 | .collect(Collectors.toSet()); |
| 63 | // Print the nextids for each group |
| 64 | mcastGroups.forEach(group -> { |
| 65 | // Create a new map for the group |
| 66 | Map<DeviceId, Integer> deviceIdNextMap = Maps.newHashMap(); |
| 67 | keyToNextId.entrySet() |
| 68 | .stream() |
| 69 | // Filter only the elements related to this group |
| 70 | .filter(entry -> entry.getKey().mcastIp().equals(group)) |
| 71 | // For each create a new entry in the group related map |
| 72 | .forEach(entry -> deviceIdNextMap.put(entry.getKey().deviceId(), entry.getValue())); |
| 73 | // Print the map |
| 74 | printMcastNext(group, deviceIdNextMap); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | private void printMcastNext(IpAddress mcastGroup, Map<DeviceId, Integer> deviceIdNextMap) { |
| 79 | print(FORMAT_MAPPING, mcastGroup, deviceIdNextMap); |
| 80 | } |
| 81 | } |