initial sketch of ReplicaInfoService.
Change-Id: I364b023aa6be69cabb184b92fd7a0dadd5215ec9
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfo.java b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfo.java
new file mode 100644
index 0000000..47706f9
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfo.java
@@ -0,0 +1,54 @@
+package org.onlab.onos.store.flow;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.onlab.onos.cluster.NodeId;
+
+import com.google.common.base.Optional;
+
+/**
+ * Class to represent placement information about Master/Backup copy.
+ */
+public final class ReplicaInfo {
+
+ private final Optional<NodeId> master;
+ private final Collection<NodeId> backups;
+
+ /**
+ * Creates a ReplicaInfo instance.
+ *
+ * @param master NodeId of the node where the master copy should be
+ * @param backups collection of NodeId, where backup copies should be placed
+ */
+ public ReplicaInfo(NodeId master, Collection<NodeId> backups) {
+ this.master = Optional.fromNullable(master);
+ this.backups = checkNotNull(backups);
+ }
+
+ /**
+ * Returns the NodeId, if there is a Node where the master copy should be.
+ *
+ * @return NodeId, where the master copy should be placed
+ */
+ public Optional<NodeId> master() {
+ return master;
+ }
+
+ /**
+ * Returns the collection of NodeId, where backup copies should be placed.
+ *
+ * @return collection of NodeId, where backup copies should be placed
+ */
+ public Collection<NodeId> backups() {
+ return backups;
+ }
+
+ // for Serializer
+ private ReplicaInfo() {
+ this.master = Optional.absent();
+ this.backups = Collections.emptyList();
+ }
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoEvent.java b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoEvent.java
new file mode 100644
index 0000000..7cba127
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoEvent.java
@@ -0,0 +1,49 @@
+package org.onlab.onos.store.flow;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.onlab.onos.event.AbstractEvent;
+import org.onlab.onos.net.DeviceId;
+
+/**
+ * Describes a device replicainfo event.
+ */
+public class ReplicaInfoEvent extends AbstractEvent<ReplicaInfoEvent.Type, DeviceId> {
+
+ private final ReplicaInfo replicaInfo;
+
+ /**
+ * Types of Replica info event.
+ */
+ public enum Type {
+ /**
+ * Event to notify that master placement should be changed.
+ */
+ MASTER_CHANGED,
+ //
+ // BACKUPS_CHANGED?
+ }
+
+
+ /**
+ * Creates an event of a given type and for the specified device,
+ * and replica info.
+ *
+ * @param type replicainfo event type
+ * @param device event device subject
+ * @param replicaInfo replicainfo
+ */
+ public ReplicaInfoEvent(Type type, DeviceId device, ReplicaInfo replicaInfo) {
+ super(type, device);
+ this.replicaInfo = checkNotNull(replicaInfo);
+ }
+
+ /**
+ * Returns the current replica information for the subject.
+ *
+ * @return replica information for the subject
+ */
+ public ReplicaInfo replicaInfo() {
+ return replicaInfo;
+ };
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoEventListener.java b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoEventListener.java
new file mode 100644
index 0000000..44239dc
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoEventListener.java
@@ -0,0 +1,11 @@
+package org.onlab.onos.store.flow;
+
+import org.onlab.onos.event.EventListener;
+
+/**
+ * Entity capable of receiving Replica placement information-related events.
+ */
+public interface ReplicaInfoEventListener extends EventListener<ReplicaInfoEvent> {
+
+}
+
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoService.java b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoService.java
new file mode 100644
index 0000000..c3f1bbf
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/flow/ReplicaInfoService.java
@@ -0,0 +1,18 @@
+package org.onlab.onos.store.flow;
+
+import org.onlab.onos.net.DeviceId;
+
+/**
+ * Service to return where the Replica should be placed.
+ */
+public interface ReplicaInfoService {
+
+ // returns where it should be.
+ /**
+ * Returns the placement information for given Device.
+ *
+ * @param deviceId identifier of the device
+ * @return placement information
+ */
+ ReplicaInfo getReplicaInfoFor(DeviceId deviceId);
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/ReplicaInfoManager.java b/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/ReplicaInfoManager.java
new file mode 100644
index 0000000..0d4cd08
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/ReplicaInfoManager.java
@@ -0,0 +1,82 @@
+package org.onlab.onos.store.flow.impl;
+
+import static org.slf4j.LoggerFactory.getLogger;
+import static org.onlab.onos.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.onos.cluster.NodeId;
+import org.onlab.onos.event.AbstractListenerRegistry;
+import org.onlab.onos.event.EventDeliveryService;
+import org.onlab.onos.mastership.MastershipEvent;
+import org.onlab.onos.mastership.MastershipListener;
+import org.onlab.onos.mastership.MastershipService;
+import org.onlab.onos.net.DeviceId;
+import org.onlab.onos.store.flow.ReplicaInfo;
+import org.onlab.onos.store.flow.ReplicaInfoEvent;
+import org.onlab.onos.store.flow.ReplicaInfoEventListener;
+import org.onlab.onos.store.flow.ReplicaInfoService;
+import org.slf4j.Logger;
+
+/**
+ * Manages replica placement information.
+ */
+@Component(immediate = true)
+@Service
+public class ReplicaInfoManager implements ReplicaInfoService {
+
+ private final Logger log = getLogger(getClass());
+
+ private final MastershipListener mastershipListener = new InternalMastershipListener();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected EventDeliveryService eventDispatcher;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected MastershipService mastershipService;
+
+ protected final AbstractListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
+ listenerRegistry = new AbstractListenerRegistry<>();
+
+ @Activate
+ public void activate() {
+ eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
+ mastershipService.addListener(mastershipListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ eventDispatcher.removeSink(ReplicaInfoEvent.class);
+ mastershipService.removeListener(mastershipListener);
+ log.info("Stopped");
+ }
+
+ @Override
+ public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
+ // TODO: populate backup List when we reach the point we need them.
+ return new ReplicaInfo(mastershipService.getMasterFor(deviceId),
+ Collections.<NodeId>emptyList());
+ }
+
+ final class InternalMastershipListener implements MastershipListener {
+
+ @Override
+ public void event(MastershipEvent event) {
+ // TODO: distinguish stby list update, when MastershipService,
+ // start publishing them
+ final List<NodeId> standbyList = Collections.<NodeId>emptyList();
+ eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED,
+ event.subject(),
+ new ReplicaInfo(event.master(), standbyList)));
+ }
+ }
+
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/package-info.java b/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/package-info.java
index 38d4efc..cedf68e 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/package-info.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/flow/impl/package-info.java
@@ -2,4 +2,4 @@
* Implementation of the distributed flow rule store using p2p synchronization
* protocol.
*/
-package org.onlab.onos.store.flow.impl;
\ No newline at end of file
+package org.onlab.onos.store.flow.impl;