Fixed incorrect "Env vars" description in onos.sh.
Modified onos.sh so that ZooKeeper logdir can be configurable.
Fixed onos.py so that Hazelcast properly works.
 - Made onos_node.conf, hazelcast.xml and logback.xml to be specific for each instance.
Added note about Hazelcast config file to onos.properties.

Change-Id: I0fd3367fa38c0a7ee1b774977ca52e5202e02b04
diff --git a/conf/onos.properties b/conf/onos.properties
index 0f7256e..20d90c4 100644
--- a/conf/onos.properties
+++ b/conf/onos.properties
@@ -11,4 +11,5 @@
 net.floodlightcontroller.core.FloodlightProvider.workerthreads = 16
 net.floodlightcontroller.forwarding.Forwarding.idletimeout = 5
 net.floodlightcontroller.forwarding.Forwarding.hardtimeout = 0
-net.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig = conf/hazelcast.xml
+# NOTE: Do NOT modify or remove the line below. This value will be overwritten by onos.sh script.
+net.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig = 
diff --git a/onos.sh b/onos.sh
index dbf2739..9bc7a3d 100755
--- a/onos.sh
+++ b/onos.sh
@@ -6,12 +6,13 @@
 # $ONOS_CONF       : path of ONOS node config file (~/ONOS/conf/onos_node.`hostname`.conf or onos_node.conf)
 # $ONOS_PROPS      : path of ONOS properties file (~/ONOS/conf/onos.properties)
 # $ONOS_LOGBACK    : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
-# $LOGDIR          : path of log output directory (~/ONOS/onos-logs)
-# $LOGBASE         : base name of log output file (onos.`hostname`)
+# $ONOS_LOGDIR     : path of log output directory (~/ONOS/onos-logs)
+# $ONOS_LOGBASE    : base name of log output file (onos.`hostname`)
 # $RAMCLOUD_HOME   : path of root directory of RAMCloud repository (~/ramcloud)
 # $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
 # $ZK_HOME         : path of root directory of ZooKeeper (~/zookeeper-3.4.5)
 # $ZK_LIB_DIR      : path of ZooKeeper library (/var/lib/zookeeper)
+# $ZK_LOG_DIR      : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
 # $JVM_OPTS        : JVM options ONOS starts with
 # $ZK_CONF         : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
 # $HC_CONF         : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
@@ -62,7 +63,7 @@
 ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
 ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
 # Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
-ZK_LOG_DIR=${ONOS_HOME}/onos-logs/${ONOS_HOST_NAME}
+ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
 ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
 ZK_MY_ID=${ZK_LIB_DIR}/myid
 
@@ -124,6 +125,8 @@
 
 JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
 
+JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
+
 # Uncomment to dump final JVM flags to stdout
 #JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
 
diff --git a/vm-utils/onos.py b/vm-utils/onos.py
index 119df36..9a34c5b 100755
--- a/vm-utils/onos.py
+++ b/vm-utils/onos.py
@@ -61,6 +61,7 @@
     ofbase = 6633
     restbase = 8080
     jmxbase = 7189
+    hcbase = 5701
 
     fc = 'net.floodlightcontroller.'
 
@@ -194,10 +195,13 @@
         "Stop Zookeeper"
         self.cmd( 'onos.sh zk stop' )
 
+    def getPropsFilename( self, id, path ):
+        return path + '/onos-%s.properties' % id
+        
     def genProperties( self, id, path='/tmp' ):
         "Generate ONOS properties file and return its full pathname"
         defaultProps = self.onosDir + '/conf/onos.properties'
-        propsFile = path + '/onos-%s.properties' % id
+        propsFile = self.getPropsFilename( id, path )
         with open( propsFile, 'w' ) as f:
             with open( defaultProps ) as d:
                 for line in d.readlines():
@@ -216,7 +220,24 @@
                                 f.write( '%s,\\\n' % module )
         return propsFile
 
-    def setVars( self, id, propsFile ):
+    def getConfsFilename( self, id, path ):
+        return path + '/onos-%s.conf' % id
+        
+    def genConfig( self, id, path='/tmp' ):
+        "Generate ONOS node config file and return its full pathname"
+        confsFile = self.getConfsFilename( id, path )
+        with open( confsFile, 'w' ) as f:
+            f.write( 'host.ip = 127.0.0.1\n' )
+            f.write( 'host.backend = ramcloud\n' )
+            f.write( 'hazelcast.host.port = %s\n' % ( self.hcbase + 10 * id ) )
+        return confsFile
+
+    def setVarsGlobal( self, path='/tmp'):
+        logdir = self.logDir
+        self.cmd( 'export ONOS_LOGDIR=%s' % logdir )
+        self.cmd( 'export ZK_LOG_DIR=%s' % logdir )
+
+    def setVarsLocal( self, id, path='/tmp' ):
         """Set and return environment vars
            id: ONOS instance number
            propsFile: properties file name"""
@@ -224,22 +245,29 @@
         logback = self.logbackFile % id
         jmxport = self.jmxbase + id
         logdir = self.logDir
-        self.cmd( 'export ONOS_LOGDIR=%s' % logdir )
         self.cmd( 'export ONOS_LOGBASE=onos-%d.`hostname`' % id)
-        self.cmd( 'export ZOO_LOG_DIR="%s"' % logdir )
         self.cmd( 'export ONOS_LOGBACK="%s"' % logback )
         self.cmd( 'export JMX_PORT=%s' % jmxport )
         self.cmd( 'export JVM_OPTS="-D%s=%s"' % (
             self.proctag, id ) )
+        propsFile = self.getPropsFilename( id, path )
         self.cmd( 'export ONOS_PROPS="%s"' % propsFile )
+        confsFile = self.getConfsFilename( id, path )
+        self.cmd( 'export ONOS_CONF="%s"' % confsFile )
+        self.cmd( 'export HC_CONF="%s/hazelcast.%s.conf"' % ( path, id ) )
 
+    def setupONOS (self, id):
+        propsFile = self.genProperties( id )
+        confFile = self.genConfig( id )
+        self.setVarsLocal( id )
+        self.cmd( 'onos.sh setup -f' )
+        
     def startONOS( self, id ):
         """Start ONOS
            id: new instance number"""
         start = time.time()
+        self.setVarsLocal( id )
         self.stopONOS( id )
-        propsFile = self.genProperties( id )
-        self.setVars( id, propsFile )
         self.cmd( 'onos.sh core startnokill' )
         # start-onos.sh waits for ONOS startup
         elapsed = time.time() - start
@@ -255,6 +283,11 @@
 
     def start( self, *args ):
         "Start ONOS instances"
+        self.setVarsGlobal()
+        # TODO: use onos-cluster.sh to setup/start/stop ONOS cluster
+        for id in self.ids:
+            info( '* Setting up ONOS %s\n' % id )
+            self.setupONOS( id )
         info( '* Starting Zookeeper\n' )
         self.startZookeeper()
         info( '* Starting Ramcloud\n' )