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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.Leadership; |
| 24 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 25 | import org.onosproject.distributedprimitives.DistributedPrimitivesTest; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 26 | import org.onosproject.store.service.LeaderElector; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 27 | |
| 28 | import com.google.common.base.Joiner; |
| 29 | |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 30 | @Service |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 31 | @Command(scope = "onos", name = "leader-test", |
| 32 | description = "LeaderElector test cli fixture") |
| 33 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 53 | protected void doExecute() { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 54 | ClusterService clusterService = get(ClusterService.class); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 55 | DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class); |
| 56 | leaderElector = test.getLeaderElector(name); |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 57 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 58 | if ("run".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 59 | print(leaderElector.run(topic, localNodeId)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 60 | } else if ("withdraw".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 61 | leaderElector.withdraw(topic); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 62 | } else if ("show".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 63 | 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 | } |