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