blob: b31091bd91552a4d891ab5552bc7a71ff650afb3 [file] [log] [blame]
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +00001/*
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 */
16package org.opencord.igmpproxy.impl;
17
18import org.onosproject.cluster.ClusterService;
19import org.onosproject.cluster.LeadershipService;
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +030020import org.onosproject.cluster.Leadership;
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000021import org.onosproject.cluster.NodeId;
22import org.onosproject.mastership.MastershipService;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.device.DeviceService;
25import org.opencord.igmpproxy.IgmpLeadershipService;
26import org.osgi.service.component.ComponentContext;
27import org.osgi.service.component.annotations.Component;
28import org.osgi.service.component.annotations.Reference;
29import org.osgi.service.component.annotations.ReferenceCardinality;
30import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Deactivate;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35/**
36 * Leadership service implementation.
37 */
38@Component(immediate = true, service = IgmpLeadershipService.class)
39public 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 Campanella37f9e0e2021-05-06 13:00:29 +020061 /**
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 Ozdemir4c5947c2020-05-05 13:14:32 +000069 @Override
70 public boolean isLocalLeader(DeviceId deviceId) {
Andrea Campanella37f9e0e2021-05-06 13:00:29 +020071 if (deviceService.isAvailable(deviceId)) {
72 return mastershipService.isLocalMaster(deviceId);
73 } else {
74 // Fallback with Leadership service - device id is used as topic
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000075 NodeId leader = leadershipService.runForLeadership(
76 deviceId.toString()).leaderNodeId();
Andrea Campanella37f9e0e2021-05-06 13:00:29 +020077 // Verify if this node is the leader
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000078 return clusterService.getLocalNode().id().equals(leader);
79 }
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000080 }
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +030081
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 Ozdemir4c5947c2020-05-05 13:14:32 +0000101}