blob: 337a65dd3c7a1ae4ca28860bbf9362ee197e2e60 [file] [log] [blame]
Pierdb27b8d2018-04-17 16:29:56 +02001/*
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
17package org.onosproject.segmentrouting.cli;
18
Ray Milkeydb38bc82018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
Ray Milkey52ca4e92018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Pierdb27b8d2018-04-17 16:29:56 +020022import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.mcast.cli.McastGroupCompleter;
26import org.onosproject.segmentrouting.SegmentRoutingService;
27
28import java.util.Map;
29
30import static com.google.common.base.Strings.isNullOrEmpty;
31
32/**
33 * Command to show the mcast leaders of the groups.
34 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070035@Service
Pierdb27b8d2018-04-17 16:29:56 +020036@Command(scope = "onos", name = "sr-mcast-leader",
37 description = "Lists all mcast leaders")
38public 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 Milkeydb38bc82018-09-27 12:32:28 -070053 protected void doExecute() {
Pierdb27b8d2018-04-17 16:29:56 +020054 // 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}