Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.opencord.igmpproxy.impl; |
| 17 | |
| 18 | import org.onosproject.cluster.ClusterService; |
| 19 | import org.onosproject.cluster.LeadershipService; |
Ilayda Ozdemir | 0872abd | 2020-06-03 20:20:20 +0300 | [diff] [blame] | 20 | import org.onosproject.cluster.Leadership; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.mastership.MastershipService; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.opencord.igmpproxy.IgmpLeadershipService; |
| 26 | import org.osgi.service.component.ComponentContext; |
| 27 | import org.osgi.service.component.annotations.Component; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | /** |
| 36 | * Leadership service implementation. |
| 37 | */ |
| 38 | @Component(immediate = true, service = IgmpLeadershipService.class) |
| 39 | public class IgmpLeadershipManager implements IgmpLeadershipService { |
| 40 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 41 | |
| 42 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 43 | protected DeviceService deviceService; |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 45 | protected LeadershipService leadershipService; |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 47 | protected ClusterService clusterService; |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 49 | protected MastershipService mastershipService; |
| 50 | |
| 51 | @Activate |
| 52 | public void activate(ComponentContext context) { |
| 53 | log.info("Started."); |
| 54 | } |
| 55 | |
| 56 | @Deactivate |
| 57 | public void deactivate(ComponentContext context) { |
| 58 | log.info("Stopped."); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean isLocalLeader(DeviceId deviceId) { |
| 63 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 64 | if (deviceService.isAvailable(deviceId)) { |
| 65 | return false; |
| 66 | } |
| 67 | NodeId leader = leadershipService.runForLeadership( |
| 68 | deviceId.toString()).leaderNodeId(); |
| 69 | return clusterService.getLocalNode().id().equals(leader); |
| 70 | } |
| 71 | return true; |
| 72 | } |
Ilayda Ozdemir | 0872abd | 2020-06-03 20:20:20 +0300 | [diff] [blame] | 73 | |
| 74 | @Override |
| 75 | public NodeId getLocalNodeId() { |
| 76 | return clusterService.getLocalNode().id(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public NodeId getLeader(String topic) { |
| 81 | return leadershipService.getLeader(topic); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Leadership runForLeadership(String topic) { |
| 86 | return leadershipService.runForLeadership(topic); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void withdraw(String topic) { |
| 91 | leadershipService.withdraw(topic); |
| 92 | } |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 93 | } |