ONOS-7207 Fix for -  Flows exists even if the devices are not available

Change-Id: Icf7ceac9c2a5ed52a9abea03c0070e50d93c0b48
diff --git a/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DistributedFlowRuleStore.java b/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DistributedFlowRuleStore.java
index 68ba068..2fd7076 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DistributedFlowRuleStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/flow/impl/DistributedFlowRuleStore.java
@@ -338,19 +338,33 @@
     @Override
     @SuppressWarnings("unchecked")
     public FlowEntry getFlowEntry(FlowRule rule) {
-        DocumentPath path = getPathFor(rule.deviceId(), rule.id());
-        Versioned<Map<StoredFlowEntry, StoredFlowEntry>> flowEntries = flows.get(path);
-        return flowEntries != null ? flowEntries.value().get(rule) : null;
+        DeviceId deviceId = rule.deviceId();
+        if (mastershipService.getMasterFor(deviceId) != null) {
+            DocumentPath path = getPathFor(deviceId, rule.id());
+            Versioned<Map<StoredFlowEntry, StoredFlowEntry>> flowEntries = flows.get(path);
+            return flowEntries != null ? flowEntries.value().get(rule) : null;
+        } else {
+            log.debug("Failed to getFlowEntries: No master for {}", deviceId);
+            return null;
+        }
+
+
     }
 
     @Override
     public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
-        DocumentPath path = getPathFor(deviceId);
-        try {
-            return getFlowEntries(path);
-        } catch (NoSuchDocumentPathException e) {
+        if (mastershipService.getMasterFor(deviceId) != null) {
+            DocumentPath path = getPathFor(deviceId);
+            try {
+                return getFlowEntries(path);
+            } catch (NoSuchDocumentPathException e) {
+                return Collections.emptyList();
+            }
+        } else {
+            log.debug("Failed to getFlowEntries: No master for {}", deviceId);
             return Collections.emptyList();
         }
+
     }
 
     @SuppressWarnings("unchecked")
@@ -703,18 +717,29 @@
 
     @Override
     public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
-        List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
-        if (tableStats == null) {
+        if (mastershipService.getMasterFor(deviceId) != null) {
+            List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
+            if (tableStats == null) {
+                return Collections.emptyList();
+            }
+            return ImmutableList.copyOf(tableStats);
+        } else {
+            log.debug("Failed to getTableStatistics: No master for {}", deviceId);
             return Collections.emptyList();
         }
-        return ImmutableList.copyOf(tableStats);
+
     }
 
     @Override
     public long getActiveFlowRuleCount(DeviceId deviceId) {
-        return Streams.stream(getTableStatistics(deviceId))
-                .mapToLong(TableStatisticsEntry::activeFlowEntries)
-                .sum();
+        if (mastershipService.getMasterFor(deviceId) != null) {
+            return Streams.stream(getTableStatistics(deviceId))
+                    .mapToLong(TableStatisticsEntry::activeFlowEntries)
+                    .sum();
+        } else {
+            log.debug("Failed to getActiveFlowRuleCount: No master for {}", deviceId);
+            return 0;
+        }
     }
 
     private class InternalTableStatsListener