blob: 633745c09267534282f91cbe225c0f39f27f319f [file] [log] [blame]
Pier96f63cb2018-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 Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Command;
Ray Milkeyb52acf32018-10-05 10:17:34 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Pier96f63cb2018-04-17 16:29:56 +020023import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.mcast.cli.McastGroupCompleter;
27import org.onosproject.segmentrouting.SegmentRoutingService;
28
29import java.util.Map;
30
31import static com.google.common.base.Strings.isNullOrEmpty;
32
33/**
34 * Command to show the mcast leaders of the groups.
35 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Pier96f63cb2018-04-17 16:29:56 +020037@Command(scope = "onos", name = "sr-mcast-leader",
38 description = "Lists all mcast leaders")
39public 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 Milkeyb52acf32018-10-05 10:17:34 -070051 @Completion(McastGroupCompleter.class)
Pier96f63cb2018-04-17 16:29:56 +020052 String gAddr = null;
53
54 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
Pier96f63cb2018-04-17 16:29:56 +020056 // 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}