blob: b28f0adf59439162e9ea6fb2b5cf2a7d4c6297bc [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.Leadership;
24import org.onosproject.cluster.NodeId;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070025import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070026import org.onosproject.store.service.LeaderElector;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070027
28import com.google.common.base.Joiner;
29
Ray Milkey7a2dee52018-09-28 10:58:28 -070030@Service
Madan Jampani9e1a8c12016-06-27 11:19:10 -070031@Command(scope = "onos", name = "leader-test",
32description = "LeaderElector test cli fixture")
33public class LeaderElectorTestCommand extends AbstractShellCommand {
34 @Argument(index = 0, name = "name",
35 description = "leader elector name",
36 required = true, multiValued = false)
37 String name = null;
38
39 @Argument(index = 1, name = "operation",
40 description = "operation",
41 required = true, multiValued = false)
42 String operation = null;
43
44
45 @Argument(index = 2, name = "topic",
46 description = "topic name",
47 required = false, multiValued = false)
48 String topic = null;
49
50 LeaderElector leaderElector;
51
52 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070053 protected void doExecute() {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070054 ClusterService clusterService = get(ClusterService.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070055 DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
56 leaderElector = test.getLeaderElector(name);
Madan Jampani9e1a8c12016-06-27 11:19:10 -070057 NodeId localNodeId = clusterService.getLocalNode().id();
Jon Hall41e6cd72017-03-28 13:57:59 -070058 if ("run".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070059 print(leaderElector.run(topic, localNodeId));
Jon Hall41e6cd72017-03-28 13:57:59 -070060 } else if ("withdraw".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070061 leaderElector.withdraw(topic);
Jon Hall41e6cd72017-03-28 13:57:59 -070062 } else if ("show".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070063 print(leaderElector.getLeadership(topic));
64 }
65 }
66
67 private void print(Leadership leadership) {
68 if (leadership.leader() != null) {
69 print("leader=%s#term=%d#candidates=%s",
70 leadership.leaderNodeId(),
71 leadership.leader().term(),
72 leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
73 } else {
74 print("leader=none#candidates=%s",
75 leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
76 }
77 }
78}