Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [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 | */ |
| 16 | package org.onosproject.election; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.core.CoreService; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 27 | import org.onosproject.cluster.LeadershipEvent; |
| 28 | import org.onosproject.cluster.LeadershipEventListener; |
| 29 | import org.onosproject.cluster.LeadershipService; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Simple application to test leadership election. |
| 37 | */ |
| 38 | @Component(immediate = true) |
| 39 | public class ElectionTest { |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | private static final String ELECTION_APP = "org.onosproject.election"; |
| 44 | private ApplicationId appId; |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected ClusterService clusterService; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected CoreService coreService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected LeadershipService leadershipService; |
| 54 | |
| 55 | private LeadershipEventListener leadershipEventListener = |
| 56 | new InnerLeadershipEventListener(); |
| 57 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 58 | private NodeId localNodeId; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | @Activate |
| 62 | protected void activate() { |
| 63 | log.info("Election-test app started"); |
| 64 | |
| 65 | appId = coreService.registerApplication(ELECTION_APP); |
| 66 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 67 | localNodeId = clusterService.getLocalNode().id(); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 68 | |
| 69 | leadershipService.addListener(leadershipEventListener); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | protected void deactivate() { |
| 74 | |
| 75 | leadershipService.withdraw(appId.name()); |
| 76 | leadershipService.removeListener(leadershipEventListener); |
| 77 | |
| 78 | log.info("Election-test app Stopped"); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * A listener for Leadership Events. |
| 83 | */ |
| 84 | private class InnerLeadershipEventListener |
| 85 | implements LeadershipEventListener { |
| 86 | |
| 87 | @Override |
| 88 | public void event(LeadershipEvent event) { |
| 89 | |
| 90 | |
Ayaka Koshibe | e3bfcaf | 2015-04-21 14:29:58 -0700 | [diff] [blame] | 91 | if (event.type().equals(LeadershipEvent.Type.CANDIDATES_CHANGED)) { |
| 92 | return; |
| 93 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 94 | if (!event.subject().topic().equals(appId.name())) { |
| 95 | return; // Not our topic: ignore |
| 96 | } |
| 97 | |
| 98 | //only log what pertains to us |
| 99 | log.debug("Leadership Event: time = {} type = {} event = {}", |
| 100 | event.time(), event.type(), event); |
| 101 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 102 | switch (event.type()) { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 103 | case LEADER_CHANGED: |
| 104 | case LEADER_AND_CANDIDATES_CHANGED: |
| 105 | log.info("Election-test app leader changed. New leadership: {}", event.subject()); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } |