Pier | db27b8d | 2018-04-17 16:29:56 +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 org.apache.karaf.shell.commands.Command; |
| 20 | import org.apache.karaf.shell.commands.Option; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.mcast.cli.McastGroupCompleter; |
| 25 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | |
| 29 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 30 | |
| 31 | /** |
| 32 | * Command to show the mcast leaders of the groups. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "sr-mcast-leader", |
| 35 | description = "Lists all mcast leaders") |
| 36 | public class McastLeaderListCommand extends AbstractShellCommand { |
| 37 | |
| 38 | // OSGi workaround to introduce package dependency |
| 39 | McastGroupCompleter completer; |
| 40 | |
| 41 | // Format for group line |
| 42 | private static final String G_FORMAT_MAPPING = "group=%s, leader=%s"; |
| 43 | |
| 44 | @Option(name = "-gAddr", aliases = "--groupAddress", |
| 45 | description = "IP Address of the multicast group", |
| 46 | valueToShowInHelp = "224.0.0.0", |
| 47 | required = false, multiValued = false) |
| 48 | String gAddr = null; |
| 49 | |
| 50 | @Override |
| 51 | protected void execute() { |
| 52 | // Verify mcast group |
| 53 | IpAddress mcastGroup = null; |
| 54 | if (!isNullOrEmpty(gAddr)) { |
| 55 | mcastGroup = IpAddress.valueOf(gAddr); |
| 56 | } |
| 57 | // Get SR service |
| 58 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 59 | // Get the mapping |
| 60 | Map<IpAddress, NodeId> keyToRole = srService.getMcastLeaders(mcastGroup); |
| 61 | // And print local cache |
| 62 | keyToRole.forEach(this::printMcastLeder); |
| 63 | } |
| 64 | |
| 65 | private void printMcastLeder(IpAddress mcastGroup, |
| 66 | NodeId nodeId) { |
| 67 | print(G_FORMAT_MAPPING, mcastGroup, nodeId); |
| 68 | } |
| 69 | |
| 70 | } |