ONOS-1323 Upgrading environment to use Apache Karaf 3.0.3

Requires restart of any dev shell sessions that may have KARAF_VERSION=3.0.2 already set.

Developers that have their own local Karaf will have to run 'onos-setup-karaf <ip-address>' command

Change-Id: Iba234b3cd5af89de6dd249c97cac97525364cc34
diff --git a/tools/build/envDefaults b/tools/build/envDefaults
index d6893d0..24cab2f 100644
--- a/tools/build/envDefaults
+++ b/tools/build/envDefaults
@@ -5,7 +5,7 @@
 
 # M2 repository and Karaf gold bits
 export M2_REPO=${M2_REPO:-~/.m2/repository}
-export KARAF_VERSION=${KARAF_VERSION:-3.0.2}
+export KARAF_VERSION=${KARAF_VERSION:-3.0.3}
 export KARAF_ZIP=${KARAF_ZIP:-~/Downloads/apache-karaf-$KARAF_VERSION.zip}
 export KARAF_TAR=${KARAF_TAR:-~/Downloads/apache-karaf-$KARAF_VERSION.tar.gz}
 export KARAF_DIST=$(basename $KARAF_ZIP .zip)
diff --git a/tools/dev/bash_profile b/tools/dev/bash_profile
index 057e7d3..2b05f1b 100644
--- a/tools/dev/bash_profile
+++ b/tools/dev/bash_profile
@@ -18,7 +18,7 @@
 
 export MAVEN=${MAVEN:-~/Applications/apache-maven-3.3.1}
 
-export KARAF_VERSION=${KARAF_VERSION:-3.0.2}
+export KARAF_VERSION=${KARAF_VERSION:-3.0.3}
 export KARAF_ROOT=${KARAF_ROOT:-~/Applications/apache-karaf-$KARAF_VERSION}
 export KARAF_LOG=$KARAF_ROOT/data/log/karaf.log
 
diff --git a/tools/dev/bin/onos-local-log b/tools/dev/bin/onos-local-log
index ea93d7f..a17d3b9 100755
--- a/tools/dev/bin/onos-local-log
+++ b/tools/dev/bin/onos-local-log
@@ -2,7 +2,7 @@
 # ----------------------------------------------------------------------------
 # Continuously watches the Apache Karaf log; survives 'karaf clean'
 # ----------------------------------------------------------------------------
-KARAF_LOG=${KARAF_LOG:-~/apache-karaf-3.0.2/data/log/karaf.log}
+KARAF_LOG=${KARAF_LOG:-~/apache-karaf-$KARAF_VERSION/data/log/karaf.log}
 
 while true; do
     [ ! -f $KARAF_LOG ] && sleep 2 && continue
diff --git a/tools/dev/onos.cshrc b/tools/dev/onos.cshrc
index 5dc3298..e5d8e62 100644
--- a/tools/dev/onos.cshrc
+++ b/tools/dev/onos.cshrc
@@ -27,7 +27,7 @@
     setenv MAVEN $HOME/Applications/apache-maven-3.3.1
 endif
 if ( ! $?KARAF_VERSION ) then
-    setenv KARAF_VERSION 3.0.2
+    setenv KARAF_VERSION 3.0.3
 endif
 if ( ! $?KARAF_ROOT ) then
     setenv KARAF_ROOT $HOME/Applications/apache-karaf-$KARAF_VERSION
diff --git a/tools/package/archetypes/cli/src/main/resources/archetype-resources/pom.xml b/tools/package/archetypes/cli/src/main/resources/archetype-resources/pom.xml
index fbd618e..bb5262e 100644
--- a/tools/package/archetypes/cli/src/main/resources/archetype-resources/pom.xml
+++ b/tools/package/archetypes/cli/src/main/resources/archetype-resources/pom.xml
@@ -79,7 +79,7 @@
         <dependency>
             <groupId>org.apache.karaf.shell</groupId>
             <artifactId>org.apache.karaf.shell.console</artifactId>
-            <version>3.0.2</version>
+            <version>3.0.3</version>
             <scope>provided</scope>
         </dependency>
     </dependencies>
diff --git a/tools/package/etc/org.ops4j.pax.url.mvn.cfg b/tools/package/etc/org.ops4j.pax.url.mvn.cfg
index 24010da..15167a3 100644
--- a/tools/package/etc/org.ops4j.pax.url.mvn.cfg
+++ b/tools/package/etc/org.ops4j.pax.url.mvn.cfg
@@ -81,7 +81,7 @@
 #    http://repository.springsource.com/maven/bundles/external@id=spring.ebr.external
 #    http://zodiac.springsource.com/maven/bundles/release@id=gemini
 #    http://repository.apache.org/content/groups/snapshots-group@id=apache@snapshots@noreleases
-#    https://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@norelease
+#    https://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@noreleases
 #    https://oss.sonatype.org/content/repositories/ops4j-snapshots@id=ops4j.sonatype.snapshots.deploy@snapshots@noreleases
 # To add repositories to the default ones, prepend '+' to the list of repositories
 # to add.
@@ -97,5 +97,5 @@
     http://repository.springsource.com/maven/bundles/external@id=spring.ebr.external, \
     http://zodiac.springsource.com/maven/bundles/release@id=gemini, \
     http://repository.apache.org/content/groups/snapshots-group@id=apache@snapshots@noreleases, \
-    https://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@norelease, \
+    https://oss.sonatype.org/content/repositories/snapshots@id=sonatype.snapshots.deploy@snapshots@noreleases, \
     https://oss.sonatype.org/content/repositories/ops4j-snapshots@id=ops4j.sonatype.snapshots.deploy@snapshots@noreleases
diff --git a/tools/test/bin/onos-setup-karaf b/tools/test/bin/onos-setup-karaf
new file mode 100755
index 0000000..c22a53a
--- /dev/null
+++ b/tools/test/bin/onos-setup-karaf
@@ -0,0 +1,90 @@
+#!/bin/bash
+# -----------------------------------------------------------------------------
+# Downloads and sets-up Apache Karaf as a basis for running ONOS locally
+# as a single-instance.
+# -----------------------------------------------------------------------------
+
+[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
+. $ONOS_ROOT/tools/build/envDefaults
+
+# TODO: consider putting this under ~/Applications/onos/apache-karaf-...
+export KARAF_ROOT=${KARAF_ROOT:-~/Applications/apache-karaf-$KARAF_VERSION}
+export STAGE=$(dirname $KARAF_ROOT)
+
+# Clean the previous Karaf directory if requested and if it exists.
+if [ "$1" = "clean" ]; then
+    shift
+    [ -d $KARAF_ROOT ] && rm -fr $KARAF_ROOT $STAGE/apps $STAGE/config
+fi
+
+if [ -z $1 ]; then
+    echo "usage: $(basename $0) [clean] <ip-address>"
+    echo "Available IP addresses are:"
+    ifconfig | grep 'inet ' | cut -d\  -f2 | grep -v "127.0.0.1"
+    exit 1
+fi
+
+IP="$1"
+SUBNET="$(echo $IP | cut -d. -f1-3)"
+
+# Bail on any errors
+set -e
+
+# Check if Apache Karaf is already installed.
+if [ ! -d $KARAF_ROOT ]; then
+    # Check if Apache Karaf bits are available and if not, fetch them.
+    if [ ! -f $KARAF_TAR ]; then
+        echo "Downloading $KARAF_TAR..."
+        curl -sL http://downloads.onosproject.org/third-party/apache-karaf-$KARAF_VERSION.tar.gz > $KARAF_TAR
+    fi
+    [ ! -f $KARAF_ZIP -a ! -f $KARAF_TAR ] && \
+        echo "Apache Karaf bits $KARAF_ZIP or $KARAF_TAR not found" && exit 1
+
+    echo "Unpacking $KARAF_TAR to $STAGE..."
+    mkdir -p $STAGE
+    cd $STAGE
+    tar zxf $KARAF_TAR
+    rm -rf $KARAF_ROOT/demos
+fi
+
+if ! grep -q "/onos-features/" $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
+    # Patch the Apache Karaf distribution file to add ONOS features repository
+    echo "Adding ONOS feature repository..."
+    perl -pi.old -e "s|^(featuresRepositories=.*)|\1,mvn:org.onosproject/onos-features/$ONOS_POM_VERSION/xml/features|" \
+        $KARAF_ROOT/etc/org.apache.karaf.features.cfg
+fi
+
+if ! grep -q ",onos-api," $KARAF_ROOT/etc/org.apache.karaf.features.cfg; then
+    # Patch the Apache Karaf distribution file to load default ONOS boot features
+    export BOOT_FEATURES="webconsole,onos-api,onos-core,onos-cli,onos-rest,onos-gui"
+    echo "Adding ONOS boot features $BOOT_FEATURES..."
+    perl -pi.old -e "s|^(featuresBoot=.*)|\1,$BOOT_FEATURES|" \
+        $KARAF_ROOT/etc/org.apache.karaf.features.cfg
+fi
+
+if [ ! -f $KARAF_ROOT/lib/onos-branding-$ONOS_POM_VERSION.jar ]; then
+    # Patch the Apache Karaf distribution with ONOS branding bundle
+    echo "Branding as ONOS..."
+    rm -f $KARAF_ROOT/lib/onos-branding-*.jar
+    cp $M2_REPO/org/onosproject/onos-branding/$ONOS_POM_VERSION/onos-branding-$ONOS_POM_VERSION.jar \
+        $KARAF_ROOT/lib
+fi
+
+if [ ! -d $STAGE/apps -o ! -d $STAGE/config ]; then
+    echo "Creating local cluster configs for IP $IP..."
+    mkdir -p $STAGE/apps $STAGE/config
+    cat > $STAGE/config/cluster.json <<EOF
+    { "ipPrefix": "$SUBNET.*",
+      "nodes":[ { "id": "$IP", "ip": "$IP", "tcpPort": 9876 }]}
+EOF
+
+    cat > $STAGE/config/tablets.json <<EOF
+    { "nodes": [ { "ip": "$IP", "id": "$IP", "tcpPort": 7238 }],
+      "partitions": { "p1": [ { "ip": "$IP", "id": "$IP", "tcpPort": 7238 }]}}
+EOF
+fi
+
+echo "Setting up hazelcast.xml for subnet $SUBNET..."
+cp $ONOS_ROOT/tools/package/etc/hazelcast.xml $KARAF_ROOT/etc/hazelcast.xml
+perl -pi.old -e "s/192.168.56/$SUBNET/" $KARAF_ROOT/etc/hazelcast.xml
+perl -pi.old -e "s/        <name>onos</        <name>$IP</" $KARAF_ROOT/etc/hazelcast.xml
diff --git a/tools/test/topos/onos.py b/tools/test/topos/onos.py
index dac37c4..cdad6da 100755
--- a/tools/test/topos/onos.py
+++ b/tools/test/topos/onos.py
@@ -36,7 +36,7 @@
         #self.checkListening()
 
         self.onosDir = onosDir
-        self.karafDir = onosDir + 'apache-karaf-3.0.2/'
+        self.karafDir = onosDir + 'apache-karaf-3.0.3/'
         self.instanceDir = self.karafDir
 
         # add default modules