Fixed P4 tools build script for Ubuntu 14

+ bumped version of p4runtime and BMv2
+ minor update to bmv2.py

Change-Id: I7784b075b2e6015bb3eef1286011968303b66129
diff --git a/protocols/p4runtime/proto/BUCK b/protocols/p4runtime/proto/BUCK
index f7ef33e..33c46f4 100644
--- a/protocols/p4runtime/proto/BUCK
+++ b/protocols/p4runtime/proto/BUCK
@@ -5,7 +5,7 @@
 P4RT_PROTOC_VER = '3.0.2'
 P4RT_GRPC_VER = '1.3.0'
 
-PI_COMMIT = 'f110570ca0c8ddc5feaf5028b08a0f477ceb4eea'
+PI_COMMIT = '8051d54c9c3415ca800db232a226189fd17ad60a'
 PI_BASEURL = 'https://github.com/p4lang/PI.git'
 
 # Wondering which .proto files to build? Check p4runtime's Makefile:
diff --git a/tools/dev/bin/onos-setup-p4-dev b/tools/dev/bin/onos-setup-p4-dev
index f3573a1..65179ef 100755
--- a/tools/dev/bin/onos-setup-p4-dev
+++ b/tools/dev/bin/onos-setup-p4-dev
@@ -15,8 +15,8 @@
 set -e
 
 BUILD_DIR=~/p4tools
-BMV2_COMMIT="41c1f2ed487ff086a1d582069e8bc68cab8ffdb0"
-PI_COMMIT="f110570ca0c8ddc5feaf5028b08a0f477ceb4eea"
+BMV2_COMMIT="654e9c3794273b481284d8bafecfc74c7ffd643d"
+PI_COMMIT="8051d54c9c3415ca800db232a226189fd17ad60a"
 P4C_COMMIT="adce375fd961a9e467c7e77ad1bef647ef28e5e8"
 P4C_BM_COMMIT="8f4abeaa6f8374aaf95ea2aacfc2b750069391b5"
 PROTOBUF_COMMIT="tags/v3.0.2"
@@ -26,13 +26,18 @@
 
 function do_requirements {
     sudo apt update
+    #FIXME the following two lines seem like they are not required, at least for 16.04
     sudo apt install -y python-software-properties software-properties-common
     sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
     sudo apt update
-    sudo apt install -y \
+    #FIXME 14.04 needs special gcc, 16.04 can use apt-get default
+    sudo apt install -y --no-install-recommends \
         autoconf \
         automake \
         bison \
+        build-essential \
+        cmake \
+        cpp \
         curl \
         dpkg-dev \
         flex \
@@ -40,20 +45,38 @@
         gcc-4.9 \
         git \
         libboost-dev \
+        libboost-filesystem-dev \
         libboost-iostreams-dev \
+        libboost-program-options-dev \
+        libboost-system-dev \
+        libboost-test-dev \
+        libboost-thread-dev \
+        libc6-dev \
+        libevent-dev \
+        libffi-dev \
         libfl-dev \
         libgc-dev \
+        libgc1c2 \
+        libgflags-dev \
         libgmp-dev \
+        libgmp10 \
+        libgmpxx4ldbl \
+        libjudy-dev \
+        libpcap-dev \
         libreadline6 \
         libreadline6-dev \
+        libssl-dev \
         libtool \
-        libc6-dev \
         make \
+        mktemp \
         pkg-config \
         python \
+        python-dev \
         python-ipaddr \
+        python-pip \
         python-scapy \
         tcpdump \
+        wget \
         unzip
 
     # Needed for p4c.
@@ -62,7 +85,6 @@
 }
 
 function do_protobuf {
-    # Protobuf
     cd ${BUILD_DIR}
     if [ ! -d protobuf ]; then
       git clone https://github.com/google/protobuf.git
@@ -70,15 +92,19 @@
     cd protobuf
     git fetch
     git checkout ${PROTOBUF_COMMIT}
+
+    export CFLAGS="-Os"
+    export CXXFLAGS="-Os"
+    export LDFLAGS="-Wl,-s"
     ./autogen.sh
     ./configure --prefix=/usr
     make -j${NUM_CORES}
     sudo make install
     sudo ldconfig
+    unset CFLAGS CXXFLAGS LDFLAGS
 }
 
 function do_grpc {
-    # gRPC
     cd ${BUILD_DIR}
     if [ ! -d grpc ]; then
       git clone https://github.com/grpc/grpc.git
@@ -87,30 +113,15 @@
     git fetch
     git checkout ${GRPC_COMMIT}
     git submodule update --init
-    make -j${NUM_CORES}
-    sudo make install
-    sudo ldconfig
-}
 
-function do_bmv2 {
-    # BMv2 part 1 (without PI)
-    cd ${BUILD_DIR}
-    if [ ! -d bmv2 ]; then
-        git clone https://github.com/p4lang/behavioral-model.git bmv2
-    fi
-    cd bmv2
-    git fetch
-    git checkout ${BMV2_COMMIT}
-    ./install_deps.sh
-    ./autogen.sh
-    ./configure --enable-debugger
+    export LDFLAGS="-Wl,-s"
     make -j${NUM_CORES}
     sudo make install
     sudo ldconfig
+    unset LDFLAGS
 }
 
 function do_p4runtime {
-    # P4Runtime (PI)
     cd ${BUILD_DIR}
     if [ ! -d p4runtime ]; then
         git clone https://github.com/p4lang/PI.git p4runtime
@@ -119,17 +130,26 @@
     git fetch
     git checkout ${PI_COMMIT}
     git submodule update --init --recursive
+
     ./autogen.sh
-    ./configure --with-bmv2 --with-proto
+    ./configure --with-proto --without-cli --without-internal-rpc
     make -j${NUM_CORES}
     sudo make install
     sudo ldconfig
 }
 
-function do_bmv2_with_pi {
-    # BMv2 part 2 (with PI)
-    cd ${BUILD_DIR}/bmv2
-    ./configure --enable-debugger --with-pi
+function do_bmv2 {
+    cd ${BUILD_DIR}
+    if [ ! -d bmv2 ]; then
+        git clone https://github.com/p4lang/behavioral-model.git bmv2
+    fi
+    cd bmv2
+    git fetch
+    git checkout ${BMV2_COMMIT}
+
+    ./autogen.sh
+    # FIXME: to build with debugger, we need to install nanomsg first (see bmv2's install_deps.sh)
+    ./configure --without-thrift --without-nanomsg --with-pi
     make -j${NUM_CORES}
     sudo make install
     sudo ldconfig
@@ -144,7 +164,6 @@
 }
 
 function do_p4c {
-    # p4c
     cd ${BUILD_DIR}
     if [ ! -d p4c ]; then
         git clone https://github.com/p4lang/p4c.git
@@ -153,6 +172,7 @@
     git fetch
     git checkout ${P4C_COMMIT}
     git submodule update --init --recursive
+
     ./bootstrap.sh
     cd build
     make -j${NUM_CORES}
@@ -160,8 +180,9 @@
     sudo ldconfig
 }
 
+# FIXME: Can we deprecate this?
 function do_p4c_bm {
-    # p4c-bmv2 (old BMv2 compiler)
+    # old BMv2 compiler
     cd ${BUILD_DIR}
     if [ ! -d p4c-bmv2 ]; then
         git clone https://github.com/p4lang/p4c-bm.git p4c-bmv2
@@ -196,6 +217,7 @@
         echo "# Building ${proj_dir} (${commit_id})"
         echo "#"
         if ! ${DID_REQUIREMENTS} = true; then
+        #FIXME need version check for 14.04 vs 16.04
             do_requirements
             DID_REQUIREMENTS=true
         fi
@@ -213,9 +235,8 @@
 # In dependency order.
 check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf
 check_and_do ${GRPC_COMMIT} grpc do_grpc
-check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
 check_and_do ${PI_COMMIT} p4runtime do_p4runtime
-check_and_do ${BMV2_COMMIT} bmv2 do_bmv2_with_pi
+check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
 check_and_do ${P4C_COMMIT} p4c do_p4c
 check_and_do ${P4C_BM_COMMIT} p4c-bmv2 do_p4c_bm
 
diff --git a/tools/dev/mininet/bmv2.py b/tools/dev/mininet/bmv2.py
index a0f92d2..6b31d05 100644
--- a/tools/dev/mininet/bmv2.py
+++ b/tools/dev/mininet/bmv2.py
@@ -22,16 +22,16 @@
     deviceId = 0
     instanceCount = 0
 
-    def __init__(self, name, debugger=False, loglevel="warn", elogger=False, persistent=False,
-                 thriftPort=None, grpcPort=None, netcfg=True, **kwargs):
+    def __init__(self, name, json=None, debugger=False, loglevel="warn", elogger=False,
+                 persistent=False, grpcPort=None, netcfg=True, **kwargs):
         Switch.__init__(self, name, **kwargs)
-        self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort
         self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort
         if self.dpid:
             self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16)
         else:
             self.deviceId = ONOSBmv2Switch.deviceId
             ONOSBmv2Switch.deviceId += 1
+        self.json = json
         self.debugger = debugger
         self.loglevel = loglevel
         self.logfile = '/tmp/bmv2-%d.log' % self.deviceId
@@ -43,7 +43,6 @@
             self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId
             self.cmd("touch %s" % self.exectoken)
         # Store thrift port for future uses.
-        self.cmd("echo %d > /tmp/bmv2-%d-thrift-port" % (self.thriftPort, self.deviceId))
         self.cmd("echo %d > /tmp/bmv2-%d-grpc-port" % (self.grpcPort, self.deviceId))
 
     @classmethod
@@ -114,15 +113,17 @@
         for port, intf in self.intfs.items():
             if not intf.IP():
                 args.append('-i %d@%s' % (port, intf.name))
-        if self.thriftPort:
-            args.append('--thrift-port %d' % self.thriftPort)
         if self.elogger:
             nanomsg = 'ipc:///tmp/bmv2-%d-log.ipc' % self.deviceId
             args.append('--nanolog %s' % nanomsg)
         if self.debugger:
             args.append('--debugger')
+        args.append('--log-console')
         args.append('-L%s' % self.loglevel)
-        args.append('--no-p4')
+        if not self.json:
+            args.append('--no-p4')
+        else:
+            args.append(self.json)
 
         # gRPC target-specific options.
         args.append('--')
@@ -131,14 +132,12 @@
 
         bmv2cmd = " ".join(args)
         info("\nStarting BMv2 target: %s\n" % bmv2cmd)
+
         if self.persistent:
             # Bash loop to re-exec the switch if it crashes.
-            cmdStr = "(while [ -e {} ]; " \
-                     "do {} ; " \
-                     "sleep 1; " \
-                     "done;) > {} 2>&1 &".format(self.exectoken, bmv2cmd, self.logfile)
-        else:
-            cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile)
+            bmv2cmd = "(while [ -e {} ]; do {} ; sleep 1; done;)".format(self.exectoken, bmv2cmd)
+
+        cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile)
 
         # Starts the switch.
         out = self.cmd(cmdStr)
@@ -157,7 +156,7 @@
     def stop(self, deleteIntfs=True):
         """Terminate switch."""
         self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId)
-        self.cmd("rm -f /tmp/bmv2-%d.log" % self.deviceId)
+        self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId)
         self.cmd('kill %' + BMV2_TARGET)
         Switch.stop(self, deleteIntfs)