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 | |
Andrea Campanella | 37f9e0e | 2021-05-06 13:00:29 +0200 | [diff] [blame^] | 61 | /** |
| 62 | * Checks for mastership or falls back to leadership on deviceId. |
| 63 | * If the device is available use mastership, |
| 64 | * otherwise fallback on leadership. |
| 65 | * Leadership on the device topic is needed because the master can be NONE |
| 66 | * in case the device went away, we still need to handle events |
| 67 | * consistently |
| 68 | */ |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 69 | @Override |
| 70 | public boolean isLocalLeader(DeviceId deviceId) { |
Andrea Campanella | 37f9e0e | 2021-05-06 13:00:29 +0200 | [diff] [blame^] | 71 | if (deviceService.isAvailable(deviceId)) { |
| 72 | return mastershipService.isLocalMaster(deviceId); |
| 73 | } else { |
| 74 | // Fallback with Leadership service - device id is used as topic |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 75 | NodeId leader = leadershipService.runForLeadership( |
| 76 | deviceId.toString()).leaderNodeId(); |
Andrea Campanella | 37f9e0e | 2021-05-06 13:00:29 +0200 | [diff] [blame^] | 77 | // Verify if this node is the leader |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 78 | return clusterService.getLocalNode().id().equals(leader); |
| 79 | } |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 80 | } |
Ilayda Ozdemir | 0872abd | 2020-06-03 20:20:20 +0300 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public NodeId getLocalNodeId() { |
| 84 | return clusterService.getLocalNode().id(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public NodeId getLeader(String topic) { |
| 89 | return leadershipService.getLeader(topic); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Leadership runForLeadership(String topic) { |
| 94 | return leadershipService.runForLeadership(topic); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void withdraw(String topic) { |
| 99 | leadershipService.withdraw(topic); |
| 100 | } |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 101 | } |