blob: 743e3f26f688ebe77edaf98a61009a8d8bb9a8c8 [file] [log] [blame]
Ray Milkey85267002016-11-16 11:06:35 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey85267002016-11-16 11:06:35 -08003 *
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 */
Madan Jampani9e1a8c12016-06-27 11:19:10 -070016package org.onosproject.distributedprimitives.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.cluster.ClusterService;
22import org.onosproject.cluster.Leadership;
23import org.onosproject.cluster.NodeId;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070024import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070025import org.onosproject.store.service.LeaderElector;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070026
27import com.google.common.base.Joiner;
28
29@Command(scope = "onos", name = "leader-test",
30description = "LeaderElector test cli fixture")
31public class LeaderElectorTestCommand extends AbstractShellCommand {
32 @Argument(index = 0, name = "name",
33 description = "leader elector name",
34 required = true, multiValued = false)
35 String name = null;
36
37 @Argument(index = 1, name = "operation",
38 description = "operation",
39 required = true, multiValued = false)
40 String operation = null;
41
42
43 @Argument(index = 2, name = "topic",
44 description = "topic name",
45 required = false, multiValued = false)
46 String topic = null;
47
48 LeaderElector leaderElector;
49
50 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 protected void doExecute() {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070052 ClusterService clusterService = get(ClusterService.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070053 DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
54 leaderElector = test.getLeaderElector(name);
Madan Jampani9e1a8c12016-06-27 11:19:10 -070055 NodeId localNodeId = clusterService.getLocalNode().id();
Jon Hall41e6cd72017-03-28 13:57:59 -070056 if ("run".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070057 print(leaderElector.run(topic, localNodeId));
Jon Hall41e6cd72017-03-28 13:57:59 -070058 } else if ("withdraw".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070059 leaderElector.withdraw(topic);
Jon Hall41e6cd72017-03-28 13:57:59 -070060 } else if ("show".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070061 print(leaderElector.getLeadership(topic));
62 }
63 }
64
65 private void print(Leadership leadership) {
66 if (leadership.leader() != null) {
67 print("leader=%s#term=%d#candidates=%s",
68 leadership.leaderNodeId(),
69 leadership.leader().term(),
70 leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
71 } else {
72 print("leader=none#candidates=%s",
73 leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
74 }
75 }
76}