Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 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 | */ |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 16 | package org.onosproject.distributedprimitives.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.Leadership; |
| 23 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 24 | import org.onosproject.distributedprimitives.DistributedPrimitivesTest; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.LeaderElector; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 26 | |
| 27 | import com.google.common.base.Joiner; |
| 28 | |
| 29 | @Command(scope = "onos", name = "leader-test", |
| 30 | description = "LeaderElector test cli fixture") |
| 31 | public 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 |
| 51 | protected void execute() { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 52 | ClusterService clusterService = get(ClusterService.class); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 53 | DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class); |
| 54 | leaderElector = test.getLeaderElector(name); |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 55 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 56 | if ("run".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 57 | print(leaderElector.run(topic, localNodeId)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 58 | } else if ("withdraw".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 59 | leaderElector.withdraw(topic); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 60 | } else if ("show".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 61 | 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 | } |