[WIP] Upgrade ONOS to karaf version 4.2.1

Change-Id: I7cd40c995bdf1c80f94b1895fb3344e32404c7fa
diff --git a/tools/package/archetypes/bundle/src/main/resources/archetype-resources/src/main/java/AppComponent.java b/tools/package/archetypes/bundle/src/main/resources/archetype-resources/src/main/java/AppComponent.java
index 047561b..6ccc181 100644
--- a/tools/package/archetypes/bundle/src/main/resources/archetype-resources/src/main/java/AppComponent.java
+++ b/tools/package/archetypes/bundle/src/main/resources/archetype-resources/src/main/java/AppComponent.java
@@ -18,10 +18,10 @@
  */
 package ${package};
 
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Service;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Service;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
diff --git a/tools/package/archetypes/ui/src/main/resources/archetype-resources/src/main/java/AppUiComponent.java b/tools/package/archetypes/ui/src/main/resources/archetype-resources/src/main/java/AppUiComponent.java
index c59fb64..4522692 100644
--- a/tools/package/archetypes/ui/src/main/resources/archetype-resources/src/main/java/AppUiComponent.java
+++ b/tools/package/archetypes/ui/src/main/resources/archetype-resources/src/main/java/AppUiComponent.java
@@ -19,11 +19,11 @@
 package ${package};
 
 import com.google.common.collect.ImmutableList;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
 import org.onosproject.ui.UiMessageHandlerFactory;
@@ -44,7 +44,7 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected UiExtensionService uiExtensionService;
 
     // List of application views
diff --git a/tools/package/archetypes/uitab/src/main/resources/archetype-resources/src/main/java/AppUiTableComponent.java b/tools/package/archetypes/uitab/src/main/resources/archetype-resources/src/main/java/AppUiTableComponent.java
index 2e82cb6..655fd03 100644
--- a/tools/package/archetypes/uitab/src/main/resources/archetype-resources/src/main/java/AppUiTableComponent.java
+++ b/tools/package/archetypes/uitab/src/main/resources/archetype-resources/src/main/java/AppUiTableComponent.java
@@ -19,11 +19,11 @@
 package ${package};
 
 import com.google.common.collect.ImmutableList;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
 import org.onosproject.ui.UiMessageHandlerFactory;
@@ -44,7 +44,7 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected UiExtensionService uiExtensionService;
 
     // List of application views
diff --git a/tools/package/archetypes/uitopo/src/main/resources/archetype-resources/src/main/java/AppUiTopovComponent.java b/tools/package/archetypes/uitopo/src/main/resources/archetype-resources/src/main/java/AppUiTopovComponent.java
index 55b1e6f..193e41f 100644
--- a/tools/package/archetypes/uitopo/src/main/resources/archetype-resources/src/main/java/AppUiTopovComponent.java
+++ b/tools/package/archetypes/uitopo/src/main/resources/archetype-resources/src/main/java/AppUiTopovComponent.java
@@ -19,11 +19,11 @@
 package ${package};
 
 import com.google.common.collect.ImmutableList;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
 import org.onosproject.ui.UiMessageHandlerFactory;
@@ -46,7 +46,7 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected UiExtensionService uiExtensionService;
 
     // List of application views
diff --git a/tools/package/bin/onos-service b/tools/package/bin/onos-service
index d3717ac..467a04a 100755
--- a/tools/package/bin/onos-service
+++ b/tools/package/bin/onos-service
@@ -19,7 +19,7 @@
 # If ONOS_HOME is not set (e.g. in the init or service environment),
 # set it based on this script's path.
 ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)}
-KARAF_ARGS=
+KARAF_ARGS=server
 SYS_APPS=drivers
 ONOS_APPS=${ONOS_APPS:-}  # Empty means don't activate any new apps
 
@@ -52,4 +52,6 @@
     fi
 done
 
+KARAF_ARGS+=" server"
+
 exec ${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf $KARAF_ARGS
diff --git a/tools/package/branding/src/main/resources/org/apache/karaf/branding/branding.properties b/tools/package/branding/src/main/resources/org/apache/karaf/branding/branding.properties
index 69a74ba..e04d327 100644
--- a/tools/package/branding/src/main/resources/org/apache/karaf/branding/branding.properties
+++ b/tools/package/branding/src/main/resources/org/apache/karaf/branding/branding.properties
@@ -27,5 +27,4 @@
 \r\n\
   Hit '\u001B[1m<tab>\u001B[0m' for a list of available commands\r\n\
      and '\u001B[1m[cmd] --help\u001B[0m' for help on a specific command.\r\n\
-  Hit '\u001B[1m<ctrl-d>\u001B[0m' or type '\u001B[1msystem:shutdown\u001B[0m' or '\u001B[1mlogout\u001B[0m' to shutdown ONOS.\r\n
-prompt = \u001B[32monos> \u001B[0m
+  Hit '\u001B[1m<ctrl-d>\u001B[0m' or type '\u001B[1mlogout\u001B[0m' to exit ONOS session.\r\n
diff --git a/tools/package/etc/keys.properties b/tools/package/etc/keys.properties
new file mode 100644
index 0000000..463a0bb
--- /dev/null
+++ b/tools/package/etc/keys.properties
@@ -0,0 +1,35 @@
+################################################################################
+#
+#    Licensed to the Apache Software Foundation (ASF) under one or more
+#    contributor license agreements.  See the NOTICE file distributed with
+#    this work for additional information regarding copyright ownership.
+#    The ASF licenses this file to You under the Apache License, Version 2.0
+#    (the "License"); you may not use this file except in compliance with
+#    the License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+################################################################################
+
+#
+# This file contains the valid users who can log into Karaf. Each line have to be of
+# the format:
+#
+# USER=KEY,ROLE1,ROLE2,...
+#
+# All users and roles entered in this file are available after Karaf startup
+# and modifiable via the JAAS command group. These users reside in a JAAS domain
+# with the name "karaf"..
+#
+
+#
+# For security reason, the default auto-signed key is disabled.
+# The user guide describes how to generate/update the key.
+#
+_g_\:admingroup = group,admin,manager,viewer,systembundles,ssh
diff --git a/tools/package/etc/org.apache.karaf.features.cfg b/tools/package/etc/org.apache.karaf.features.cfg
new file mode 100644
index 0000000..d986a57
--- /dev/null
+++ b/tools/package/etc/org.apache.karaf.features.cfg
@@ -0,0 +1,107 @@
+################################################################################
+#
+#    Licensed to the Apache Software Foundation (ASF) under one or more
+#    contributor license agreements.  See the NOTICE file distributed with
+#    this work for additional information regarding copyright ownership.
+#    The ASF licenses this file to You under the Apache License, Version 2.0
+#    (the "License"); you may not use this file except in compliance with
+#    the License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+################################################################################
+
+#
+# Comma separated list of features repositories to register by default
+#
+featuresRepositories = \
+    mvn:org.apache.karaf.features/standard/4.2.1/xml/features, \
+    mvn:org.onosproject/onos-features/$ONOS_VERSION/xml/features, \
+    mvn:org.apache.karaf.features/enterprise/4.2.1/xml/features, \
+    mvn:org.apache.karaf.features/spring/4.2.1/xml/features, \
+    mvn:org.apache.karaf.features/standard/4.2.1/xml/features, \
+    mvn:org.apache.karaf.features/framework/4.2.1/xml/features
+
+#
+# Comma separated list of features to install at startup
+#
+featuresBoot = \
+    instance/4.2.1, \
+    package/4.2.1, \
+    log/4.2.1, \
+    ssh/4.2.1, \
+    framework/4.2.1, \
+    system/4.2.1, \
+    eventadmin/4.2.1, \
+    feature/4.2.1, \
+    shell/4.2.1, \
+    management/4.2.1, \
+    service/4.2.1, \
+    jaas/4.2.1, \
+    deployer/4.2.1, \
+    diagnostic/4.2.1, \
+    (wrap/2.5.4), \
+    bundle/4.2.1, \
+    config/4.2.1, \
+    kar/4.2.1, \
+    webconsole/4.2.1, \
+    scr/4.2.1, \
+    war/4.2.1, \
+    onos-api, \
+    onos-core, \
+    onos-incubator
+    #onos-cli,
+    #onos-gui,
+    #onos-rest
+
+
+#
+# Resource repositories (OBR) that the features resolver can use
+# to resolve requirements/capabilities
+#
+# The format of the resourceRepositories is
+# resourceRepositories=[xml:url|json:url],...
+# for Instance:
+#
+#resourceRepositories=xml:http://host/path/to/index.xml
+# or
+#resourceRepositories=json:http://host/path/to/index.json
+#
+
+#
+# Defines if the boot features are started in asynchronous mode (in a dedicated thread)
+#
+featuresBootAsynchronous=false
+
+#
+# Service requirements enforcement
+#
+# By default, the feature resolver checks the service requirements/capabilities of
+# bundles for new features (xml schema >= 1.3.0) in order to automatically installs
+# the required bundles.
+# The following flag can have those values:
+#   - disable: service requirements are completely ignored
+#   - default: service requirements are ignored for old features
+#   - enforce: service requirements are always verified
+#
+#serviceRequirements=default
+
+#
+# Store cfg file for config element in feature
+#
+#configCfgStore=true
+
+#
+# Configuration of features processing mechanism (overrides, blacklisting, modification of features)
+# XML file defines instructions related to features processing
+# versions.properties may declare properties to resolve placeholders in XML file
+# both files are relative to ${karaf.etc}
+#
+#featureProcessing=org.apache.karaf.features.xml
+#featureProcessingVersions=versions.properties
diff --git a/tools/package/etc/shell.init.script b/tools/package/etc/shell.init.script
new file mode 100644
index 0000000..ac55755
--- /dev/null
+++ b/tools/package/etc/shell.init.script
@@ -0,0 +1,65 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// This script is run each time a shell is created.
+// You can define here closures or variables that will be available
+// in each session.
+//
+ld = { log:display $args } ;
+lde = { log:exception-display $args } ;
+la = { bundle:list -t 0 $args } ;
+ls = { service:list $args } ;
+cl = { config:list "(service.pid=$args)" } ;
+halt = { system:shutdown -h -f $args } ;
+help = { *:help $args | more } ;
+man = { help $args } ;
+log:list = { log:get ALL } ;
+service:get = { $.context getService ($.context getServiceReference $args) };
+
+env = { shell:set $args }
+edit = { shell:nano $args }
+more = { shell:less -F $args }
+
+\#prompt = { "${USER}@${APPLICATION} ${SUBSHELL}> "?};
+
+__load_class = {
+  (($.reader class) classLoader) loadClass $1
+}
+
+// make sure that we catch exceptions
+// as they do occur if the session is headless / non-interactive
+jlineReader = $.reader
+if { %(jlineReader != null) } {
+
+  # On 256 colors terminal, add a right prompt
+  max_colors = ($.jline.terminal getNumericCapability max_colors)
+  if { %(max_colors >= 256) } {
+    __rprompt_formatter = (((__load_class java.text.SimpleDateFormat) getConstructor (__load_class java.lang.String)) newInstance \'$'\u001B\[90m'\'HH:mm:ss)
+    __date_class = (__load_class java.util.Date)
+    \#rprompt = { $__rprompt_formatter format ($__date_class newInstance) }
+  }
+
+  setopt group
+  setopt auto-fresh-line
+  unsetopt insert-tab
+  keymap "^[OA" up-line-or-search
+  keymap "^[[A" up-line-or-search
+  keymap "^[OB" down-line-or-search
+  keymap "^[[B" down-line-or-search
+
+}
diff --git a/tools/package/etc/users.properties b/tools/package/etc/users.properties
index 95a97bc..cacc254 100644
--- a/tools/package/etc/users.properties
+++ b/tools/package/etc/users.properties
@@ -28,10 +28,11 @@
 # All users, groups, and roles entered in this file are available after Karaf startup
 # and modifiable via the JAAS command group. These users reside in a JAAS domain
 # with the name "karaf".
-#
 karaf = karaf,_g_:admingroup
 onos = rocks,_g_:admingroup
 guest = guest,_g_:guestgroup
-_g_\:admingroup = group,admin,manager,viewer,webconsole
+
+_g_\:admingroup = group,admin,manager,viewer,systembundles,ssh,webconsole
 _g_\:guestgroup = group,viewer
 
+
diff --git a/tools/package/features/BUILD b/tools/package/features/BUILD
index f8355c0..d9ae32b 100644
--- a/tools/package/features/BUILD
+++ b/tools/package/features/BUILD
@@ -45,10 +45,11 @@
         "@mapdb//jar",
         "@bcpkix_jdk15on//jar",
         "@bcprov_jdk15on//jar",
-        "@commons_jxpath//jar",
         "@commons_beanutils//jar",
         "@jdom//jar",
         "@sigar//jar",
+        "@org_osgi_util_function//jar",
+        "@org_osgi_util_promise//jar",
     ],
     required_features = [],
     visibility = ["//visibility:public"],
@@ -154,6 +155,16 @@
 )
 
 osgi_feature(
+    name = "onos-cli2",
+    description = "stripped down ONOS admin command console components",
+    included_bundles = [
+        "//cli2:onos-cli2",
+    ],
+    required_features = ["onos-api"],
+    visibility = ["//visibility:public"],
+)
+
+osgi_feature(
     name = "onos-gui",
     description = "ONOS GUI console components",
     included_bundles = [
diff --git a/tools/package/onos-prep-karaf b/tools/package/onos-prep-karaf
index 1cc21e2..b77d7e6 100755
--- a/tools/package/onos-prep-karaf
+++ b/tools/package/onos-prep-karaf
@@ -3,17 +3,17 @@
 # Packages ONOS distributable into onos.tar.gz, onos.zip or a deb file
 # -----------------------------------------------------------------------------
 
-set -e
+set -eu -o pipefail
 
 OUT=$1
-KARAF_TAR=$2
+#export HOME=/Users/tom
+KARAF_TAR="$HOME/Downloads/apache-karaf-4.2.1.tar.gz"
 ONOS_VERSION=$3
 BRANDING=$4
 KARAF_PATCHES=$5
 SANDBOX=${6:-.}
 
-#FIXME karaf version
-KARAF_VERSION="3.0.8"
+KARAF_VERSION="4.2.1"
 ONOS_SECURITY_MODE="false"
 
 PREFIX="onos-$ONOS_VERSION"
@@ -22,9 +22,9 @@
 tar xf $KARAF_TAR
 
 # rename path name to match what was distributed with vicci
-mv "$(ls -d apache*)" "apache-karaf-$KARAF_VERSION" || true
+# mv "$(ls -d apache*)" "apache-karaf-$KARAF_VERSION" || true
 
-tar xf $KARAF_PATCHES
+#tar xf $KARAF_PATCHES
 
 # Unroll the Apache Karaf bits, prune them and make ONOS top-level directories.
 KARAF_DIR=$(ls -d apache*)
@@ -43,17 +43,6 @@
 rm -f bin/*.bk
 chmod a+x $SANDBOX/bin/onos-service $SANDBOX/bin/onos
 
-export BOOT_FEATURES="standard,ssh,scr,war,webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
-
-# Patch the Apache Karaf distribution file to add ONOS features repository
-perl -pi.old -e "s|^(featuresRepositories=).*|\1mvn:org.apache.karaf.features/standard/$KARAF_VERSION/xml/features,mvn:org.onosproject/onos-features/$ONOS_VERSION/xml/features|" \
-    $KARAF_DIR/etc/org.apache.karaf.features.cfg
-
-# Patch the Apache Karaf distribution file to load default ONOS boot features
-perl -pi.old -e "s|^(featuresBoot ?= ?).*|\1$BOOT_FEATURES|" \
-    $KARAF_DIR/etc/org.apache.karaf.features.cfg
-
-
 # Patch the Apache Karaf distribution with ONOS branding bundle
 cp $BRANDING $KARAF_DIR/lib/onos-tools-package-branding.jar
 
@@ -67,6 +56,9 @@
 cp -r $SANDBOX/init $PREFIX
 cp -r $SANDBOX/etc/* $PREFIX/$KARAF_DIR/etc/
 
+# Fix the onos version string in the features config
+sed -i".VERBACK" -E "s/.ONOS_VERSION/${ONOS_VERSION}/" $PREFIX/$KARAF_DIR/etc/org.apache.karaf.features.cfg
+
 if [ "$ONOS_SECURITY_MODE" = true ]
 then
     # ONOS Patching ----------------------------------------------------------------
diff --git a/tools/package/onos-run-karaf b/tools/package/onos-run-karaf
index b0c371f..7068802 100755
--- a/tools/package/onos-run-karaf
+++ b/tools/package/onos-run-karaf
@@ -7,8 +7,14 @@
 
 [ -f $ONOS_TAR ] || (echo "$ONOS_TAR not found" && exit 1)
 
+
+function killServer() {
+    echo "Killing ONOS server..."
+    ps -e | grep apache.karaf.main.Main | grep -v grep | cut -d\  -f1 | xargs kill -9 &>/dev/null
+}
+
 # Kill any running instances
-[ -f /tmp/onos.pid ] && kill -9 $(cat /tmp/onos.pid) &>/dev/null
+killServer
 
 ONOS_DIR=/tmp/$(tar tf $ONOS_TAR | head -n 1 | cut -d/ -f1)
 ONOS_MD5=$ONOS_DIR/CHECKSUM
@@ -75,15 +81,15 @@
 cd $ONOS_DIR
 export ONOS_HOME=$PWD
 
+# FIXME
+#export HOME=/Users/tom
+cp $HOME/Downloads/reflectasm-1.11.7.jar \
+    $ONOS_DIR/apache-karaf-4.2.1/system/com/esotericsoftware/reflectasm/1.11.7/reflectasm-1.11.7.jar
+
 # Start ONOS as a server, but include any specified options
 ./bin/onos-service server "$@" &>onos.log &
 echo "$!" > /tmp/onos.pid
 
-function killServer() {
-    echo "Killing ONOS server..."
-    cat /tmp/onos.pid | xargs kill -9
-}
-
 # Hang-on a bit and then start tailing the ONOS log output
 MAX_RETRY=30
 echo "Waiting for karaf.log"
diff --git a/tools/package/onos_stage.py b/tools/package/onos_stage.py
index a9f519f..a6b46d4 100755
--- a/tools/package/onos_stage.py
+++ b/tools/package/onos_stage.py
@@ -27,6 +27,8 @@
 
 written_files = set()
 now = time.time()
+karaf_version = "4.2.1"
+karaf_system = "apache-karaf-" + karaf_version + "/system/"
 
 def addFile(tar, dest, file, file_size):
     if dest not in written_files:
@@ -69,7 +71,7 @@
                     for f in zip_part.infolist():
                         dest = f.filename
                         if base not in dest:
-                            dest = base + 'apache-karaf-3.0.8/system/' + f.filename
+                            dest = base + karaf_system + f.filename
                         addFile(output, dest, zip_part.open(f), f.file_size)
             elif '.oar' in file:
                 with ZipFile(file, 'r') as oar:
@@ -82,12 +84,12 @@
                     for f in oar.infolist():
                         filename = f.filename
                         if 'm2' in filename:
-                            dest = base + 'apache-karaf-3.0.8/system/' + filename[3:]
+                            dest = base + karaf_system + filename[3:]
                             if dest not in written_files:
                                 addFile(output, dest, oar.open(f), f.file_size)
                                 written_files.add(dest)
             elif 'features.xml' in file:
-                dest = base + 'apache-karaf-3.0.8/system/org/onosproject/onos-features/%s/' % version
+                dest = base + karaf_system + 'org/onosproject/onos-features/%s/' % version
                 dest += 'onos-features-%s-features.xml' % version
                 with open(file, 'rb') as f:
                     addFile(output, dest, f, os.stat(file).st_size)