[SDFAB-1197] First stab to update ONOS to py3

Update has been performed only on the scripts and the
executables strictly necessary for the following use cases:

- Build (including Intellij)
- Tests (including Intellij)
- Docker build (including dev and yourkit)
- Upload snapshot (including local)
- Release

Finally, fix the ability to run ONOS using onos-local targets,
stc and docker stc (aka up4 stc env). Last but not least, updated
the azul image to a newer one which exports also the PYTHONENCODING

Change-Id: Ie96f3a9c76dbba83b1fc3896a372f1045d3d7ccc
diff --git a/.bazelrc b/.bazelrc
index 2b9ed65..5faf22a 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -12,7 +12,7 @@
 build --tool_java_language_version=11
 build --tool_java_runtime_version=remotejdk_11
 
-build --host_force_python=PY2
+build --host_force_python=PY3
 
 build --nouse_ijars
 build --experimental_strict_action_env
diff --git a/Dockerfile b/Dockerfile
index 129e7cf..6a168c5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -16,8 +16,8 @@
 
 ARG JOBS=2
 ARG PROFILE=default
-ARG TAG=11.0.8-11.41.23
-ARG JAVA_PATH=/usr/lib/jvm/zulu11-ca-amd64
+ARG TAG=11.0.13-11.52.13
+ARG JAVA_PATH=/usr/lib/jvm/zulu11
 
 # First stage is the build environment.
 # zulu-openjdk images are based on Ubuntu.
@@ -26,7 +26,6 @@
 ENV BUILD_DEPS \
     ca-certificates \
     zip \
-    python \
     python3 \
     git \
     bzip2 \
diff --git a/README.md b/README.md
index b3f066c..9d108dd 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,6 @@
 * zip
 * curl
 * unzip
-* python2.7
 * python3 (needed by Bazel)
 
 ### Build ONOS from source
diff --git a/WORKSPACE-docker b/WORKSPACE-docker
index 1b2b0d7..d9c9b46 100644
--- a/WORKSPACE-docker
+++ b/WORKSPACE-docker
@@ -7,7 +7,7 @@
 # local_java_repository for docker based builds
 load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository")
 
-ABSOLUTE_JAVABASE = "/usr/lib/jvm/zulu11-ca-amd64"
+ABSOLUTE_JAVABASE = "/usr/lib/jvm/zulu11"
 
 local_java_repository(
   name = "dockerjdk",
diff --git a/tools/build/bazel/dependencies_pom_generator.py b/tools/build/bazel/dependencies_pom_generator.py
index f2df0c2..8c6483f 100755
--- a/tools/build/bazel/dependencies_pom_generator.py
+++ b/tools/build/bazel/dependencies_pom_generator.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # Copyright 2019-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,7 +20,7 @@
 def resolve(mvn_coord):
     mvn_pieces = mvn_coord.split(":")
     if mvn_pieces[0] != "mvn":
-        raise ("Invalid Maven coordinate: %s" % mvn_coord)
+        raise ValueError("Invalid Maven coordinate: %s" % mvn_coord)
     return dict(
         groupId=mvn_pieces[1],
         artifactId=mvn_pieces[2],
@@ -79,7 +79,7 @@
                 dep_template.format(scope='test', **deps[x])
                 for x in test_deps])
         else:
-            for old, new in var_dict.items():
+            for old, new in list(var_dict.items()):
                 line = line.replace(old, new)
             new_lines.append(line)
 
@@ -111,7 +111,7 @@
     for var in args.vars:
         pieces = var.split('=')
         if len(pieces) != 2:
-            raise ("Invalid var '%s'" % var)
+            raise ValueError("Invalid var '%s'" % var)
         processed_vars["<!-- %s -->" % pieces[0]] = pieces[1]
 
     generate_pom(
diff --git a/tools/build/bazel/onos_app_bundler.py b/tools/build/bazel/onos_app_bundler.py
index 6c4ca68..5aa77a0 100755
--- a/tools/build/bazel/onos_app_bundler.py
+++ b/tools/build/bazel/onos_app_bundler.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
  Copyright 2018-present Open Networking Foundation
 
@@ -60,5 +60,5 @@
         print('There must be an even number of args: file mvn_coords')
         sys.exit(2)
 
-    files = zip(*[iter(args)]*2)
+    files = list(zip(*[iter(args)]*2))
     generateOar(output, files)
diff --git a/tools/build/bazel/onos_app_tools.py b/tools/build/bazel/onos_app_tools.py
index 9979ab5..43cab17 100755
--- a/tools/build/bazel/onos_app_tools.py
+++ b/tools/build/bazel/onos_app_tools.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
  Copyright 2018-present Open Networking Foundation
 
@@ -207,7 +207,7 @@
         values['category'] = options.category
         values['url'] = options.url
     elif options.write_app:
-        sys.stderr.write('ERROR: Feature Name, Version, and Title are required\n')
+        sys.stderr.write('ERROR: App Name, Origin, Category and URL are required\n')
         sys.stderr.flush()
         sys.exit(1)
 
diff --git a/tools/build/bazel/osgi_feature_bundler.py b/tools/build/bazel/osgi_feature_bundler.py
index a4550e0..f390c88 100755
--- a/tools/build/bazel/osgi_feature_bundler.py
+++ b/tools/build/bazel/osgi_feature_bundler.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
  Copyright 2018-present Open Networking Foundation
 
@@ -56,5 +56,5 @@
         print('There must be an even number of args: file mvn_coords')
         sys.exit(2)
 
-    files = zip(*[iter(args)]*2)
+    files = list(zip(*[iter(args)]*2))
     writeFeatureBundle(output, files)
diff --git a/tools/build/bazel/pom_generator.py b/tools/build/bazel/pom_generator.py
index 2e16f49..1ad54ec 100755
--- a/tools/build/bazel/pom_generator.py
+++ b/tools/build/bazel/pom_generator.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
  Copyright 2018-present Open Networking Foundation
 
@@ -49,7 +49,7 @@
         with open(name, "w") as file:
             file.write(msg)
     else:
-        print msg
+        print(msg)
 
 
 def write_pom(output, coords, deps):
@@ -70,7 +70,7 @@
     import sys
 
     if len(sys.argv) < 3:
-        print 'usage: pom_generator pom.xml maven_coords dep_coords1 dep_coords2 ...'
+        print('usage: pom_generator pom.xml maven_coords dep_coords1 dep_coords2 ...')
         sys.exit(1)
 
     output = sys.argv[1]
diff --git a/tools/build/onos-close-staging b/tools/build/onos-close-staging
index e70d119..1e37ef3 100755
--- a/tools/build/onos-close-staging
+++ b/tools/build/onos-close-staging
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # This script finds an open staging repository, checks that it contains an
 # expected artifact, attemps to close the repository, and checks that it is closed.
@@ -29,7 +29,7 @@
       resp.raise_for_status()
       return resp
     except requests.exceptions.HTTPError as e:
-      print 'Encountered error:', e
+      print('Encountered error:', e)
       error = e
       time.sleep(1)
   if error:
@@ -46,7 +46,7 @@
       resp.raise_for_status()
       return resp
     except requests.exceptions.HTTPError as e:
-      print 'Encountered error:', e
+      print('Encountered error:', e)
       error = e
       time.sleep(1)
   if error:
@@ -64,11 +64,11 @@
       repos.append(( entry['repositoryId'], entry['profileId'] ))
   
   if len(repos) > 1:
-    print 'Aborting... too many open staging repos'
-    print repos
+    print('Aborting... too many open staging repos')
+    print(repos)
     sys.exit(1)
   elif len(repos) == 0:
-    print 'Aborting... there are no open staging repos'
+    print('Aborting... there are no open staging repos')
     sys.exit(1)
 
   return repos[0]  
@@ -131,8 +131,8 @@
     if event['name'] == 'repositoryClosed':
       return True
     elif event['name'] == 'repositoryCloseFailed':
-      print 'Aborting... repository failed to close'
-      print json.dumps(activity, sort_keys=True, indent=2, separators=(',', ': '))
+      print('Aborting... repository failed to close')
+      print(json.dumps(activity, sort_keys=True, indent=2, separators=(',', ': ')))
       sys.exit(1)
   return False
 
@@ -144,12 +144,12 @@
     sys.stdout.write('.')
     sys.stdout.flush()     
     time.sleep(2)
-  print ' Closed.'
+  print(' Closed.')
 
 if __name__ == '__main__':
   repositoryId, profileId = getStagingRepo(GROUP_ID)
-  print 'Repository Id:', repositoryId
-  print 'Profile Id:', profileId
+  print('Repository Id:', repositoryId)
+  print('Profile Id:', profileId)
 
   checkStagingRepo(repositoryId, ARTIFACT, GROUP_ID, VERSION)
 
@@ -158,5 +158,5 @@
   waitClosed(repositoryId)
 
   if '-d' in sys.argv:
-    print 'Dropping repo %s' % repositoryId
+    print('Dropping repo %s' % repositoryId)
     dropRepo(repositoryId, profileId)
diff --git a/tools/build/onos-release-prerequisites b/tools/build/onos-release-prerequisites
index 92e201d..c1605ac 100755
--- a/tools/build/onos-release-prerequisites
+++ b/tools/build/onos-release-prerequisites
@@ -50,7 +50,7 @@
 
 # Tests availability of the required tools
 function testToolchain {
-    for tool in bash python git java javac mvn tar; do
+    for tool in bash python3 git java javac mvn tar; do
         testTool $tool;
     done
     testGerritTool
diff --git a/tools/build/onos-upload-artifacts.py b/tools/build/onos-upload-artifacts.py
index a9b24c5..d5f0079 100755
--- a/tools/build/onos-upload-artifacts.py
+++ b/tools/build/onos-upload-artifacts.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
  Copyright 2018-present Open Networking Foundation
 
@@ -147,8 +147,8 @@
         with open(file, 'rb') as f:
             r = requests.post(url, data=f.read(), headers=headers, auth=(SONATYPE_USER, SONATYPE_PASSWORD))
             if r.status_code != 201:
-                print (r.status_code)
-                print (r.text)
+                print((r.status_code))
+                print((r.text))
                 sys.exit(1)
     else:
         # deploy to local repo
@@ -165,7 +165,7 @@
 
 
 def upload_file(src, dest):
-    print ("publishing: " + dest.replace("org/onosproject", ""))
+    print(("publishing: " + dest.replace("org/onosproject", "")))
     files = generate_metadata_files(src, dest)
     stage_files(files, dest)
 
@@ -174,7 +174,7 @@
     import sys
 
     if len(sys.argv) < 2:
-        print 'USAGE: upload-maven-artifacts catalog-file-name [nexus root url]'
+        print('USAGE: upload-maven-artifacts catalog-file-name [nexus root url]')
         sys.exit(1)
 
     input_list_file = sys.argv[1]
@@ -191,20 +191,20 @@
 
     if destination_repo_url is not None:
         if SONATYPE_USER is None:
-            print "Environment variable SONATYPE_USER must be set"
+            print("Environment variable SONATYPE_USER must be set")
             sys.exit(1)
 
         if SONATYPE_PASSWORD is None:
-            print "Environment variable SONATYPE_PASSWORD must be set"
+            print("Environment variable SONATYPE_PASSWORD must be set")
             sys.exit(1)
 
         if SONATYPE_PROFILE is None:
-            print "Environment variable SONATYPE_PROFILE must be set"
+            print("Environment variable SONATYPE_PROFILE must be set")
             sys.exit(1)
 
-        print ("Uploading to remote repo: " + destination_repo_url)
+        print(("Uploading to remote repo: " + destination_repo_url))
     else:
-        print ("Installing in local repo: " + local_maven_repo)
+        print(("Installing in local repo: " + local_maven_repo))
 
     list_file = open(input_list_file, "r")
     lines = list_file.readlines()
diff --git a/tools/build/uploadToS3.py b/tools/build/uploadToS3.py
index 720878c..c5dc841 100755
--- a/tools/build/uploadToS3.py
+++ b/tools/build/uploadToS3.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
 Upload a file to S3
 """
@@ -20,7 +20,7 @@
         key = basename( filename )
     else:
         key = dest + basename( filename ) #FIXME add the /
-    print '* Uploading', filename, 'to bucket', bucket, 'as', key
+    print('* Uploading', filename, 'to bucket', bucket, 'as', key)
     stdout.flush()
     start = time()
     def callback( transmitted, size ):
@@ -28,9 +28,9 @@
         elapsed = time() - start
         percent = 100.0 * transmitted / size
         kbps = .001 * transmitted / elapsed
-        print ( '\r%d bytes transmitted of %d (%.2f%%),'
+        print(( '\r%d bytes transmitted of %d (%.2f%%),'
                 ' %.2f KB/sec ' %
-                ( transmitted, size, percent, kbps ) ),
+                ( transmitted, size, percent, kbps ) ), end='\n')
         stdout.flush()
     conn = S3Connection()
     bucket = conn.get_bucket( bucket )
@@ -38,11 +38,11 @@
     k.key = key
     if overwrite or not k.exists():
         k.set_contents_from_filename( filename, cb=callback, num_cb=100 )
-        print
+        print()
         elapsed = time() - start
-        print "* elapsed time: %.2f seconds" % elapsed
+        print("* elapsed time: %.2f seconds" % elapsed)
     else:
-        print 'file', basename( filename ), 'already exists in', bucket.name
+        print('file', basename( filename ), 'already exists in', bucket.name)
 
 def testAccess( bucket=None ):
     "Verify access to a bucket"
@@ -51,7 +51,7 @@
 
     conn = S3Connection()
     bucket = conn.get_bucket( bucket )
-    print bucket.get_acl()
+    print(bucket.get_acl())
 
 
 if __name__ == '__main__':
diff --git a/tools/dev/Dockerfile-bazel b/tools/dev/Dockerfile-bazel
index 4860e04..1eae2df 100644
--- a/tools/dev/Dockerfile-bazel
+++ b/tools/dev/Dockerfile-bazel
@@ -19,8 +19,8 @@
 # COPY command incompatibilities).
 # e.g. docker build -t onosproject/onos:latest -f $ONOS_ROOT/tools/dev/Dockerfile-bazel $ONOS_ROOT/bazel-bin
 
-ARG TAG=11.0.8-11.41.23
-ARG JAVA_PATH=/usr/lib/jvm/zulu11-ca-amd64
+ARG TAG=11.0.13-11.52.13
+ARG JAVA_PATH=/usr/lib/jvm/zulu11
 
 # Firts stage to copy and untar ONOS archive inside the container.
 FROM bitnami/minideb:jessie as untar
diff --git a/tools/dev/Dockerfile-yourkit b/tools/dev/Dockerfile-yourkit
index 176c3b3..61fbe4f 100644
--- a/tools/dev/Dockerfile-yourkit
+++ b/tools/dev/Dockerfile-yourkit
@@ -17,8 +17,8 @@
 
 ARG JOBS=2
 ARG PROFILE=default
-ARG TAG=11.0.8-11.41.23
-ARG JAVA_PATH=/usr/lib/jvm/zulu11-ca-amd64
+ARG TAG=11.0.13-11.52.13
+ARG JAVA_PATH=/usr/lib/jvm/zulu11
 
 # First stage is the build environment.
 # zulu-openjdk images are based on Ubuntu.
@@ -30,7 +30,6 @@
 ENV BUILD_DEPS \
     ca-certificates \
     zip \
-    python \
     python3 \
     git \
     bzip2 \
diff --git a/tools/dev/bash_profile b/tools/dev/bash_profile
index eaff4b3..f6a9543 100644
--- a/tools/dev/bash_profile
+++ b/tools/dev/bash_profile
@@ -77,7 +77,7 @@
 alias ole='olo "ERROR|WARN|Exception|Error"'
 
 # Pretty-print JSON output
-alias pp='python -m json.tool'
+alias pp='python3 -m json.tool'
 
 # Short-hand to launch Java API docs, REST API docs and ONOS GUI
 alias docs='open $ONOS_ROOT/docs/target/site/apidocs/index.html'
diff --git a/tools/dev/bin/onos-gen-bazel-project b/tools/dev/bin/onos-gen-bazel-project
index 355193a..007a13a 100755
--- a/tools/dev/bin/onos-gen-bazel-project
+++ b/tools/dev/bin/onos-gen-bazel-project
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/env python3
 import os
 
 import subprocess
@@ -31,8 +31,9 @@
 
 def get_library_targets():
     this_dir = os.path.dirname(os.path.realpath(__file__))
+    # NOTE that here we depends on bazel generating utf-8 bytes
     out = subprocess.check_output([
-        "bazel", "query", "kind(\"java_library\", //...:all)"], cwd=this_dir)
+        "bazel", "query", "kind(\"java_library\", //...:all)"], cwd=this_dir, encoding='UTF-8')
     return out.strip().split("\n")
 
 
@@ -43,12 +44,12 @@
         "script_name": os.path.basename(__file__),
         "language_level": JAVA_LANGUAGE_LEVEL,
         "exclude_dirs": "\n".join(
-            map(lambda x: "  -" + x, EXCLUDE_DIRECTORIES)),
-        "targets": "\n".join(map(lambda x: "  " + x, all_targets)),
-        "test_sources": "\n".join(map(lambda x: "  " + x, TEST_SOURCES)),
+            ["  -" + x for x in EXCLUDE_DIRECTORIES]),
+        "targets": "\n".join(["  " + x for x in all_targets]),
+        "test_sources": "\n".join(["  " + x for x in TEST_SOURCES]),
     }
     return TEMPLATE.format(**data)
 
 
 if __name__ == "__main__":
-    print gen_project()
+    print(gen_project())
diff --git a/tools/package/onos_stage.py b/tools/package/onos_stage.py
index 7a48650..ac66906 100755
--- a/tools/package/onos_stage.py
+++ b/tools/package/onos_stage.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
  Copyright 2017-present Open Networking Foundation
 
diff --git a/tools/package/runtime/bin/_check-json b/tools/package/runtime/bin/_check-json
index 1eef25b..0ebaf2c 100644
--- a/tools/package/runtime/bin/_check-json
+++ b/tools/package/runtime/bin/_check-json
@@ -22,7 +22,7 @@
 # -----------------------------------------------------------------------------
 
 checkJson() {
-    cat ${1} | python -m json.tool >> /dev/null
+    cat ${1} | python3 -m json.tool >> /dev/null
     if [ "$?" -ne "0" ]; then
         echo "Not valid JSON File" && exit 1
     fi
diff --git a/tools/test/bin/atomix-gen-config b/tools/test/bin/atomix-gen-config
index c3c89ac..d32ca98 100755
--- a/tools/test/bin/atomix-gen-config
+++ b/tools/test/bin/atomix-gen-config
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
 usage: atomix-gen-config [-h] [-s PARTITION_SIZE] [-n NUM_PARTITIONS]
                          [node_ip] [filename] [node_ip [node_ip ...]]
@@ -112,7 +112,7 @@
   args = parser.parse_args()
   filename = args.filename
   partition_size = args.partition_size
-  local_member_id = get_local_node(args.node)
+  local_member_id = get_local_node(args.node, args.nodes)
   local_member_address = get_local_address(args.node, args.nodes)
   nodes = get_nodes(args.nodes)
   num_partitions = args.num_partitions
@@ -158,4 +158,4 @@
     with open(filename, 'w') as f:
       f.write(output)
   else:
-    print output
+    print(output)
diff --git a/tools/test/bin/onos-gen-config b/tools/test/bin/onos-gen-config
index b73df39..e214d2f 100755
--- a/tools/test/bin/onos-gen-config
+++ b/tools/test/bin/onos-gen-config
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 """
 usage: onos-gen-config [-h] [-s PARTITION_SIZE] [-n NUM_PARTITIONS]
                             [filename] [node_ip [node_ip ...]]
@@ -87,7 +87,7 @@
 
     args = parser.parse_args()
     node = args.node
-    print node
+    print(node)
     filename = args.filename
     nodes = get_nodes(args.nodes)
 
@@ -106,4 +106,4 @@
         with open(filename, 'w') as f:
             f.write(output)
     else:
-        print output
+        print(output)