DistributedLockManger creates a locks table if one does not exist

Change-Id: Ifa766ad441f677a4071b68d8f6caa564cf320869
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/DistributedLockManager.java b/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/DistributedLockManager.java
index a8941aa..ea0f7d5 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/DistributedLockManager.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/service/impl/DistributedLockManager.java
@@ -5,6 +5,7 @@
 
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -20,6 +21,8 @@
 import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
 import org.onlab.onos.store.cluster.messaging.ClusterMessage;
 import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
+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.Lock;
 import org.onlab.onos.store.service.LockEventListener;
@@ -41,6 +44,8 @@
 
     public static final String ONOS_LOCK_TABLE_NAME = "onos-locks";
 
+    public static final int DEAD_LOCK_TIMEOUT_MS = 5000;
+
     private final ListMultimap<String, LockRequest> locksToAcquire =
                 Multimaps.synchronizedListMultimap(LinkedListMultimap.<String, LockRequest>create());
 
@@ -48,6 +53,9 @@
     private ClusterCommunicationService clusterCommunicator;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private DatabaseAdminService databaseAdminService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     private DatabaseService databaseService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -55,9 +63,22 @@
 
     @Activate
     public void activate() {
+        try {
+            Set<String> tableNames = databaseAdminService.listTables();
+            if (!tableNames.contains(ONOS_LOCK_TABLE_NAME)) {
+                if (databaseAdminService.createTable(ONOS_LOCK_TABLE_NAME, DEAD_LOCK_TIMEOUT_MS)) {
+                    log.info("Created {} table.", ONOS_LOCK_TABLE_NAME);
+                }
+            }
+        } catch (DatabaseException e) {
+            log.error("DistributedLockManager#activate failed.", e);
+            throw e;
+        }
+
         clusterCommunicator.addSubscriber(
                 DatabaseStateMachine.DATABASE_UPDATE_EVENTS,
                 new LockEventMessageListener());
+
         log.info("Started.");
 
     }