blob: dcbdb4a4a7c29275ffe8294fa29d43eceb280205 [file] [log] [blame]
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -07001package org.onlab.onos.store.flow.impl;
2
Yuta HIGUCHI4d187952014-10-16 19:59:35 -07003import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -07004import static org.slf4j.LoggerFactory.getLogger;
5import static org.onlab.onos.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
6
7import java.util.Collections;
8import java.util.List;
9
10import org.apache.felix.scr.annotations.Activate;
11import org.apache.felix.scr.annotations.Component;
12import org.apache.felix.scr.annotations.Deactivate;
13import org.apache.felix.scr.annotations.Reference;
14import org.apache.felix.scr.annotations.ReferenceCardinality;
15import org.apache.felix.scr.annotations.Service;
16import org.onlab.onos.cluster.NodeId;
17import org.onlab.onos.event.AbstractListenerRegistry;
18import org.onlab.onos.event.EventDeliveryService;
19import org.onlab.onos.mastership.MastershipEvent;
20import org.onlab.onos.mastership.MastershipListener;
21import org.onlab.onos.mastership.MastershipService;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.store.flow.ReplicaInfo;
24import org.onlab.onos.store.flow.ReplicaInfoEvent;
25import org.onlab.onos.store.flow.ReplicaInfoEventListener;
26import org.onlab.onos.store.flow.ReplicaInfoService;
27import org.slf4j.Logger;
28
29/**
30 * Manages replica placement information.
31 */
32@Component(immediate = true)
33@Service
34public class ReplicaInfoManager implements ReplicaInfoService {
35
36 private final Logger log = getLogger(getClass());
37
38 private final MastershipListener mastershipListener = new InternalMastershipListener();
39
40 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 protected EventDeliveryService eventDispatcher;
42
43 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44 protected MastershipService mastershipService;
45
46 protected final AbstractListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
47 listenerRegistry = new AbstractListenerRegistry<>();
48
49 @Activate
50 public void activate() {
51 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
52 mastershipService.addListener(mastershipListener);
53 log.info("Started");
54 }
55
56 @Deactivate
57 public void deactivate() {
58 eventDispatcher.removeSink(ReplicaInfoEvent.class);
59 mastershipService.removeListener(mastershipListener);
60 log.info("Stopped");
61 }
62
63 @Override
64 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
65 // TODO: populate backup List when we reach the point we need them.
66 return new ReplicaInfo(mastershipService.getMasterFor(deviceId),
67 Collections.<NodeId>emptyList());
68 }
69
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070070 @Override
71 public void addListener(ReplicaInfoEventListener listener) {
72 listenerRegistry.addListener(checkNotNull(listener));
73 }
74
75 @Override
76 public void removeListener(ReplicaInfoEventListener listener) {
77 listenerRegistry.removeListener(checkNotNull(listener));
78 }
79
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070080 final class InternalMastershipListener implements MastershipListener {
81
82 @Override
83 public void event(MastershipEvent event) {
84 // TODO: distinguish stby list update, when MastershipService,
85 // start publishing them
86 final List<NodeId> standbyList = Collections.<NodeId>emptyList();
87 eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED,
88 event.subject(),
89 new ReplicaInfo(event.master(), standbyList)));
90 }
91 }
92
93}