Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.flow.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | import static org.onlab.onos.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED; |
| 5 | |
| 6 | import java.util.Collections; |
| 7 | import java.util.List; |
| 8 | |
| 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 14 | import org.apache.felix.scr.annotations.Service; |
| 15 | import org.onlab.onos.cluster.NodeId; |
| 16 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 17 | import org.onlab.onos.event.EventDeliveryService; |
| 18 | import org.onlab.onos.mastership.MastershipEvent; |
| 19 | import org.onlab.onos.mastership.MastershipListener; |
| 20 | import org.onlab.onos.mastership.MastershipService; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.store.flow.ReplicaInfo; |
| 23 | import org.onlab.onos.store.flow.ReplicaInfoEvent; |
| 24 | import org.onlab.onos.store.flow.ReplicaInfoEventListener; |
| 25 | import org.onlab.onos.store.flow.ReplicaInfoService; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | /** |
| 29 | * Manages replica placement information. |
| 30 | */ |
| 31 | @Component(immediate = true) |
| 32 | @Service |
| 33 | public 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 | } |