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