Keep retrying until Database table is ready.

Change-Id: Idd696e3807435c65a422064e078cf5993a216df1
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/resource/impl/DistributedLinkResourceStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/resource/impl/DistributedLinkResourceStore.java
index ab167be..1078728 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/resource/impl/DistributedLinkResourceStore.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/resource/impl/DistributedLinkResourceStore.java
@@ -104,33 +104,32 @@
     private StoreSerializer serializer;
 
 
+    void createTable(String tableName) {
+        boolean tableReady = false;
+        do {
+            try {
+                if (!databaseAdminService.listTables().contains(tableName)) {
+                    databaseAdminService.createTable(tableName);
+                }
+                tableReady = true;
+            } catch (DatabaseException e) {
+                log.debug("Failed creating table, retrying", e);
+                try {
+                    Thread.sleep(200);
+                } catch (InterruptedException e1) {
+                    throw new DatabaseException(e1);
+                }
+            }
+        } while (!tableReady);
+    }
+
     @Activate
     public void activate() {
 
         serializer = new KryoSerializer();
 
-        Set<String> tables = null;
-        int retries = 0;
-        do {
-            try {
-                tables = databaseAdminService.listTables();
-            } catch (DatabaseException e) {
-                log.debug("DatabaseException", e);
-                retries++;
-                if (retries > 10) {
-                    log.error("Failed to list tables, moving on", e);
-                    tables = new HashSet<>();
-                }
-            }
-        } while (tables == null);
-
-        if (!tables.contains(LINK_RESOURCE_ALLOCATIONS)) {
-            databaseAdminService.createTable(LINK_RESOURCE_ALLOCATIONS);
-        }
-        if (!tables.contains(INTENT_ALLOCATIONS)) {
-            databaseAdminService.createTable(INTENT_ALLOCATIONS);
-        }
-
+        createTable(LINK_RESOURCE_ALLOCATIONS);
+        createTable(INTENT_ALLOCATIONS);
 
         log.info("Started");
     }
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/CMap.java b/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/CMap.java
index 2c390c5..a3f907e 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/CMap.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/CMap.java
@@ -23,6 +23,7 @@
 
 import org.onlab.onos.store.serializers.StoreSerializer;
 import org.onlab.onos.store.service.DatabaseAdminService;
+import org.onlab.onos.store.service.DatabaseException;
 import org.onlab.onos.store.service.DatabaseService;
 import org.onlab.onos.store.service.VersionedValue;
 
@@ -70,9 +71,21 @@
         this.tableName = checkNotNull(tableName);
         this.serializer = checkNotNull(serializer);
 
-        if (!dbAdminService.listTables().contains(tableName)) {
-            dbAdminService.createTable(tableName);
-        }
+        boolean tableReady = false;
+        do {
+            try {
+                if (!dbAdminService.listTables().contains(tableName)) {
+                    dbAdminService.createTable(tableName);
+                }
+                tableReady = true;
+            } catch (DatabaseException e) {
+                try {
+                    Thread.sleep(200);
+                } catch (InterruptedException e1) {
+                    throw new DatabaseException(e1);
+                }
+            }
+        } while (!tableReady);
 
         keyCache = CacheBuilder.newBuilder()
                     .softValues()