Fix NPE in flow store

Change-Id: I310b9bf45ba7f59c4f5cc96d302ede9b652af623
diff --git a/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DeviceFlowTable.java b/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DeviceFlowTable.java
index 53b3cd6..722ebfa 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DeviceFlowTable.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DeviceFlowTable.java
@@ -122,8 +122,7 @@
         this.scheduler = scheduler;
         this.executor = executor;
         this.localNodeId = clusterService.getLocalNode().id();
-
-        addListeners();
+        this.replicaInfo = lifecycleManager.getReplicaInfo();
 
         for (int i = 0; i < NUM_BUCKETS; i++) {
             flowBuckets.put(i, new FlowBucket(new BucketId(deviceId, i)));
@@ -133,12 +132,15 @@
         getBucketSubject = new MessageSubject(String.format("flow-store-%s-bucket", deviceId));
         backupSubject = new MessageSubject(String.format("flow-store-%s-backup", deviceId));
 
+        addListeners();
+
         setBackupPeriod(backupPeriod);
         setAntiEntropyPeriod(antiEntropyPeriod);
         registerSubscribers();
 
-        startTerm(lifecycleManager.getReplicaInfo());
         scheduleBackups();
+
+        activateMaster(replicaInfo);
     }
 
     /**
@@ -686,12 +688,14 @@
      * @param replicaInfo the new replica info
      */
     private void activateMaster(DeviceReplicaInfo replicaInfo) {
-        log.debug("Activating term {} for device {}", replicaInfo.term(), deviceId);
-        for (int i = 0; i < NUM_BUCKETS; i++) {
-            activateBucket(i);
+        if (replicaInfo.isMaster(localNodeId)) {
+            log.debug("Activating term {} for device {}", replicaInfo.term(), deviceId);
+            for (int i = 0; i < NUM_BUCKETS; i++) {
+                activateBucket(i);
+            }
+            lifecycleManager.activate(replicaInfo.term());
+            activeTerm = replicaInfo.term();
         }
-        lifecycleManager.activate(replicaInfo.term());
-        activeTerm = replicaInfo.term();
     }
 
     /**