Configurable clustername

- Configurable clustername for ZooKeeper, Hazelcast, RAMCloud

Change-Id: I2862c7667ece21733f95b25fad3840777314170d
diff --git a/conf/hazelcast.default.xml b/conf/hazelcast.default.xml
index b61fc9f..a65edce 100644
--- a/conf/hazelcast.default.xml
+++ b/conf/hazelcast.default.xml
@@ -3,6 +3,10 @@
 	xmlns="http://www.hazelcast.com/schema/config"
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
+  <group>
+    <name>onos</name>
+  </group>
+
   <properties>
     <property name="hazelcast.logging.type">slf4j</property>
     <property name="hazelcast.version.check.enabled">false</property>
diff --git a/conf/onos_node.conf b/conf/onos_node.conf
index 5688ce1..19c6ae6 100644
--- a/conf/onos_node.conf
+++ b/conf/onos_node.conf
@@ -1,3 +1,8 @@
+
+# Name of ONOS cluster ("onos" by default)
+# used as namespace, etc. in ZooKeeper, Hazelcast, RAMCloud
+#onos.cluster.name = onos
+
 # Name of this host (`hostname` by default)
 #host.name = onosdev1
 
@@ -19,8 +24,8 @@
 # myid will be assigned incrementally according to order of list
 #zookeeper.hosts = onosdev1,onosdev2,onosdev3,onosdev4
 
-# RAMCloud clusterName (ONOS-RC by default)
-#ramcloud.clusterName = ONOS-RC
+# RAMCloud clusterName (same as onos.cluster.name by default)
+#ramcloud.clusterName = onos
 
 # RAMCloud transport timeout (1000 by ONOS default)
 #ramcloud.timeout = 1000
@@ -82,4 +87,3 @@
 
 # Multicast port used by Hazelcast. (54327 by default)
 #hazelcast.multicast.port = 54327
-
diff --git a/conf/template/hazelcast.xml.template b/conf/template/hazelcast.xml.template
index ce0e53e..0865194 100644
--- a/conf/template/hazelcast.xml.template
+++ b/conf/template/hazelcast.xml.template
@@ -9,6 +9,10 @@
       try to follow the schema defined in the xsd when possible.
   -->
 
+  <group>
+    <name>__HC_CLUSTER__</name>
+  </group>
+
   <properties>
     <property name="hazelcast.logging.type">slf4j</property>
     <property name="hazelcast.version.check.enabled">false</property>
diff --git a/onos.sh b/onos.sh
index 67c1070..30a7d8a 100755
--- a/onos.sh
+++ b/onos.sh
@@ -37,6 +37,7 @@
 fi
 
 ### Variables read from ONOS config file ###
+ONOS_CLUSTER_NAME=$(read-conf ${ONOS_CONF}  onos.cluster.name             "onos")
 ONOS_HOST_NAME=$(read-conf ${ONOS_CONF}     host.name                     `hostname`)
 ONOS_HOST_IP=$(read-conf ${ONOS_CONF}       host.ip)
 ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF}     host.role)
@@ -355,6 +356,8 @@
       fi
     elif [[ $line =~ __HC_PORT__ ]]; then
       echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
+    elif [[ $line =~ __HC_CLUSTER__ ]]; then
+      echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
     else
       echo "${line}"
     fi
@@ -370,7 +373,7 @@
 
   local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
 
-  local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
+  local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
 
   # TODO make ZooKeeper address configurable.
   echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
@@ -677,7 +680,7 @@
   local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
 
   # RAMCloud cluster name
-  local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName "ONOS-RC")
+  local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
 
   # RAMCloud transport timeout
   local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
@@ -727,7 +730,7 @@
   # Configuration for ZK address, port
   local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
   # RAMCloud cluster name
-  local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName "ONOS-RC")
+  local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
   # RAMCloud option deadServerTimeout
   # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
   local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
@@ -809,7 +812,7 @@
   # Configuration for ZK address, port
   local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
   # RAMCloud cluster name
-  local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName "ONOS-RC")
+  local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
   # RAMCloud transport timeout
   local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
   # replication factor (-r) config
@@ -933,6 +936,9 @@
     exit 1
     fi
 
+  # specify ZooKeeper(curator) namespace
+  JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
+
   # specify hazelcast.xml to datagrid
   JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
 
diff --git a/src/main/java/net/onrc/onos/core/registry/ZookeeperRegistry.java b/src/main/java/net/onrc/onos/core/registry/ZookeeperRegistry.java
index 38ca110..7bc2a75 100644
--- a/src/main/java/net/onrc/onos/core/registry/ZookeeperRegistry.java
+++ b/src/main/java/net/onrc/onos/core/registry/ZookeeperRegistry.java
@@ -68,7 +68,12 @@
     // configuration parameter
     private String connectionString = "localhost:2181";
 
-    private static final String NAMESPACE = "onos";
+    /**
+     * JVM Option to specify ZooKeeper namespace.
+     */
+    public static final String ZK_NAMESPACE_KEY = "zookeeper.namespace";
+    private static final String DEFAULT_NAMESPACE = "onos";
+    private String namespace = DEFAULT_NAMESPACE;
     private static final String SWITCH_LATCHES_PATH = "/switches";
     private static final String CLUSTER_LEADER_PATH = "/cluster/leader";
 
@@ -570,6 +575,12 @@
         }
         log.info("Setting Zookeeper connection string to {}", this.connectionString);
 
+        namespace = System.getProperty(ZK_NAMESPACE_KEY, DEFAULT_NAMESPACE).trim();
+        if (namespace.isEmpty()) {
+            namespace = DEFAULT_NAMESPACE;
+        }
+        log.info("Setting Zookeeper namespace to {}", namespace);
+
         restApi = context.getServiceImpl(IRestApiService.class);
 
         switches = new ConcurrentHashMap<String, SwitchLeadershipData>();
@@ -581,7 +592,7 @@
                 SESSION_TIMEOUT, CONNECTION_TIMEOUT, retryPolicy);
 
         curatorFrameworkClient.start();
-        curatorFrameworkClient = curatorFrameworkClient.usingNamespace(NAMESPACE);
+        curatorFrameworkClient = curatorFrameworkClient.usingNamespace(namespace);
 
         distributedIdCounter = new DistributedAtomicLong(
                 curatorFrameworkClient,