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