VOL-455: asfvolt16-driver build script and fixed grpc-c patches

    Added asfvolt-driver-build.sh helper script for building asfvolt16-driver
    Added rolled-up grpc-c patches used by helper script
    Added asfvolt-driver-package.sh helper script to add voltha_bal_driver
        and grpc/proto libs to ASFvOLT16 release package

Change-Id: I259c6d2174208d15ccf7d74d3d30977fedc79385
diff --git a/device_simulator/README.md b/device_simulator/README.md
index 7d2a1fd..f2bb862 100644
--- a/device_simulator/README.md
+++ b/device_simulator/README.md
@@ -1,7 +1,7 @@
 GRPC_C DEVICE SIMULATOR
 
- This is a executable(voltha_bal_driver), which will recevie asfvolt16 adapter grpc-c messages and call respective device stub.
- It will send recevied responses/asynchronous indications from Device stub to Adapter.
+ This is an executable (voltha_bal_driver), which will receive asfvolt16 adapter grpc-c messages and call respective device stub.
+ It will send received responses/asynchronous indications from Device stub to Adapter.
 
 GETTING STARTED
 
@@ -54,22 +54,28 @@
      - sudo make install
    
 To obtain proto files - Have a repo sync of opencord code base:
+
      - cp opencord/incubator/voltha/voltha/adapters/asfvolt16_olt/protos/* examples/
 
 To autogenerate code from proto files:
+
      - make autogen
        
 Build voltha_bal_driver:
+
      - Note: Remove "-O2" from Makefile
      - make clean_all;make 
 
 The ultimate executable voltha_bal_driver can be found under ~/grpc-c/build/examples/.libs/
 
 USAGE:
+
     ./voltha_bal_driver "serverIP:port1" -C "serverIP:port2" -A "serverIP:port3"
     ./bal_core_dist -C "serverIP:port2" -A "serverIP:port3"
 
-NOTE: bal_core_dist is a broadcom executable
+NOTE:
+    'bal_core_dist' is a broadcom executable.
+    A bash helper script 'asfvolt-driver-build.sh' is available to automate most of the build steps
 
 CONTRIBUTING
      <TBD>
diff --git a/device_simulator/asfvolt-driver-build.sh b/device_simulator/asfvolt-driver-build.sh
new file mode 100755
index 0000000..86bb360
--- /dev/null
+++ b/device_simulator/asfvolt-driver-build.sh
@@ -0,0 +1,200 @@
+#
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed 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.
+#
+#
+# Run from OpenNetworkLinux top directory
+#
+#cd /aux/OpenNetworkLinux
+set -e
+MAKE_JOBS=4
+ASFVOLT_REPO_NAME=asfvolt16-driver
+
+# override shell variables to match custom local build environment
+: ${ONL_TOPDIR:=`pwd`}
+: ${BALSRC_RELEASE=bal_src_release}
+: ${VOLTHA_TOPDIR:=${HOME}/voltha/incubator/voltha}
+: ${BALSRC_TOPDIR:=${ONL_TOPDIR}/${BALSRC_RELEASE}/bal_release}
+: ${ASFSRC_TOPDIR:=${ONL_TOPDIR}/${ASFVOLT_REPO_NAME}/src}
+: ${DEVSIM_TOPDIR:=${ONL_TOPDIR}/${ASFVOLT_REPO_NAME}/device_simulator}
+: ${PATCHF_TOPDIR:=${DEVSIM_TOPDIR}}
+
+echo ONL_TOPDIR=${ONL_TOPDIR}
+echo BALSRC_RELEASE=${BALSRC_RELEASE}
+echo VOLTHA_TOPDIR=${VOLTHA_TOPDIR}
+echo BALSRC_TOPDIR=${BALSRC_TOPDIR}
+echo ASFSRC_TOPDIR=${ASFSRC_TOPDIR}
+echo DEVSIM_TOPDIR=${DEVSIM_TOPDIR}
+echo PATCHF_TOPDIR=${PATCHF_TOPDIR}
+
+# archived ZIP files from "https://github.com/opennetworkinglab/asfvolt16-driver/tree/master/third_party"
+GRPC_ARCH=ed7d06af3eef1c27f10328c73b3ae3ab10d72b10
+GRPC_C_ARCH=be82ab1605717f33e2e0d3038996ea46d9efe25e
+PROTOBUF_ARCH=703cd8e11c8d34283d4c8bf869c61866e8211c9d
+PROTOBUF_C_ARCH=6a4f9a9a67c06769aaa9f65e8f89a56483271f5a
+
+# Note: removes existing directories: asfvolt16-driver, grpc, protobuf
+rm -rf ${ASFVOLT_REPO_NAME}
+rm -rf grpc protobuf grpc-* protobuf-*
+
+#Clone asfvolt16-driver
+#    - The guide assumes that the asfvolt16-driver gerrit repo is cloned under the ONL toplevel directory:
+#    - git clone ssh://user@gerrit.opencord.org:29418/asfvolt16-driver ${HOME}/OpenNetworkLinux/asfvolt16-driver
+
+git clone https://gerrit.opencord.org/${ASFVOLT_REPO_NAME} ${ONL_TOPDIR}/${ASFVOLT_REPO_NAME}
+
+#steps to install grpc
+#   - Download as zip "grpc", "grpc-c", "protobuf" and "protobuf-c" from "https://github.com/opennetworkinglab/asfvolt16-driver/tree/master/third_party"
+wget https://github.com/grpc/grpc/archive/${GRPC_ARCH}.zip
+wget https://github.com/Juniper/grpc-c/archive/${GRPC_C_ARCH}.zip
+wget https://github.com/google/protobuf/archive/${PROTOBUF_ARCH}.zip
+wget https://github.com/shadansari/protobuf-c/archive/${PROTOBUF_C_ARCH}.zip
+
+#   - extract grpc at ${HOME}/OpenNetworkLinux/grpc
+unzip -q ${GRPC_ARCH}.zip; rm ${GRPC_ARCH}.zip
+mv grpc-${GRPC_ARCH} grpc
+
+#   - extract grpc-c at ${HOME}/OpenNetworkLinux/grpc-c
+unzip -q ${GRPC_C_ARCH}.zip; rm ${GRPC_C_ARCH}.zip
+mv grpc-c-${GRPC_C_ARCH} grpc-c
+
+#   - extract protobuf at ${HOME}/OpenNetworkLinux/grpc/thirdparty/protobuf
+unzip -q ${PROTOBUF_ARCH}.zip; rm ${PROTOBUF_ARCH}.zip
+mkdir -p grpc/thirdparty
+mv protobuf-${PROTOBUF_ARCH} grpc/thirdparty/protobuf
+
+
+#   - extract protobuf-c folder at ${HOME}/OpenNetworkLinux/grpc-c/third_party/protobuf-c
+unzip -q ${PROTOBUF_C_ARCH}.zip; rm ${PROTOBUF_C_ARCH}.zip
+(cd protobuf-c-${PROTOBUF_C_ARCH};tar cf - .)|(cd grpc-c/third_party/protobuf-c;tar xf -)
+rm -rf protobuf-c-${PROTOBUF_C_ARCH}
+
+#steps to install grpc-c
+# cd ${HOME}/OpenNetworkLinux/grpc/thirdparty/protobuf
+#  - ./autogen.sh
+#  - ./configure
+#  - make
+#  - sudo make install
+cd ${ONL_TOPDIR}/grpc/thirdparty/protobuf
+./autogen.sh
+./configure
+make --jobs=${MAKE_JOBS}
+sudo make install
+
+# cd ${HOME}/OpenNetworkLinux/grpc
+# - export LD_LIBRARY_PATH=/usr/local/lib
+# - make
+# - sudo make install
+
+cd ${ONL_TOPDIR}/grpc
+export LD_LIBRARY_PATH=/usr/local/lib
+make --jobs=${MAKE_JOBS}
+sudo make install
+
+#cd ${HOME}/OpenNetworkLinux/grpc-c/third_party/protobuf-c
+#- ./autogen.sh
+#- ./configure
+#- export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/protobuf
+#- make
+#- sudo make install
+
+cd ${ONL_TOPDIR}/grpc-c/third_party/protobuf-c
+./autogen.sh
+./configure
+export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/protobuf
+make --jobs=${MAKE_JOBS}
+sudo make install
+
+#Apply grpc-c patch
+#   - cd ${HOME}/OpenNetworkLinux/grpc-c/
+cd ${ONL_TOPDIR}/grpc-c
+#     Patch 1:
+#     Apply patch in following link for grpc-c/lib - "https://github.com/Juniper/grpc-c/commit/353b40cd920cd749ed6cf71f8df17f1d5cf2c89d"
+#     Note:
+#        (This patch is having very few changes in two files(grpc-c/lib/client.c, grpc-c/lib/service.c. 
+#         Download these two files from above link and replace at grpc-c/lib or merge these changes manually.)       
+patch -p1 -i ${PATCHF_TOPDIR}/grpc-c_compile-error.patch
+
+#     Patch 2:
+#     Apply patch in service.c.patch and client.c.patch
+patch -p1 -i ${PATCHF_TOPDIR}/grpc-c_asfvolt16.patch
+
+#
+#     - cd ${HOME}/OpenNetworkLinux
+#     - cp asfvolt16-driver/device_simulator/Makefile.am grpc-c/examples/
+#     - cp asfvolt16-driver/device_simulator/voltha_bal_driver.c grpc-c/examples/
+#     - cp asfvolt16-driver/device_simulator/bal_stub.c grpc-c/examples/
+#     - cp asfvolt16-driver/device_simulator/bal_stub.h grpc-c/examples/
+#         - Note: Update voltha adaptor IP in bal_stub.c (Is this required? Not clear which variable needs to be updated).
+cd ${ONL_TOPDIR}
+cp ${DEVSIM_TOPDIR}/Makefile.am grpc-c/examples/
+cp ${DEVSIM_TOPDIR}/voltha_bal_driver.c grpc-c/examples/
+cp ${DEVSIM_TOPDIR}/bal_stub.c grpc-c/examples/
+cp ${DEVSIM_TOPDIR}/bal_stub.h grpc-c/examples/
+
+#
+#     - cd ${HOME}/OpenNetworkLinux/grpc-c
+#     - autoreconf --install
+#     - mkdir build && cd build
+#     - ../configure
+#     - make
+#     - sudo make install
+cd ${ONL_TOPDIR}/grpc-c
+autoreconf --install
+mkdir build && cd build
+../configure
+make --jobs=${MAKE_JOBS}
+sudo make install
+
+#To obtain proto files - Have a repo sync of opencord voltha code base:
+#     - git clone https://github.com/opencord/voltha.git ${HOME}/voltha
+#     - cd ${HOME}/OpenNetworkLinux/grpc-c/
+#     - cp ${HOME}/voltha/voltha/adapters/asfvolt16_olt/protos/* examples/
+#
+cd ${ONL_TOPDIR}/grpc-c
+cp ${VOLTHA_TOPDIR}/voltha/adapters/asfvolt16_olt/protos/* examples
+
+#To autogenerate code from proto files:
+#     - cd ${HOME}/OpenNetworkLinux/grpc-c/build/examples
+#     - make autogen
+cd ${ONL_TOPDIR}/grpc-c/build/examples
+make autogen
+
+#
+#Build voltha_bal_driver:
+#     - cd ${HOME}/OpenNetworkLinux/grpc-c/build/examples
+#     - Note: Remove "-O2" from Makefile
+#     - Note: Set EDGECORE and BRCM_PATH in Makefile
+cd ${ONL_TOPDIR}/grpc-c/build/examples
+sed -i -e 's/-O2/-O0/g' \
+       -e "s:^EDGECORE = /home/asfvolt/shared.*:#&\nEDGECORE = ${ASFSRC_TOPDIR}:" \
+       -e "s:^BRCM_PATH = /home/asfvolt/shared.*:#&\nBRCM_PATH = ${BALSRC_TOPDIR}:" \
+        Makefile
+
+pushd ${ASFSRC_TOPDIR}
+sed -i -e "s:^BRDCM_SRC=/home/asfvolt/shared.*:#&\nBRDCM_SRC = ${BALSRC_TOPDIR}:" \
+       -e "s:^GRPC_C_PATH= /home/asfvolt/shared.*:#&\nGRPC_C_PATH = ${ONL_TOPDIR}/grpc-c:" \
+        Makefile
+popd
+#```
+#       EDGECORE = ${HOME}/OpenNetworkLinux/asfvolt16-driver/src/
+#       BRCM_PATH = ${HOME}/OpenNetworkLinux/bal_src_release/bal_release
+#```
+#     - make clean_all;make
+make clean_all
+make
+#
+#The ultimate executable voltha_bal_driver can be found under ~/grpc-c/build/examples/.libs/
+
+echo "${ASFVOLT_REPO_NAME} build complete"
diff --git a/device_simulator/asfvolt-driver-package.sh b/device_simulator/asfvolt-driver-package.sh
new file mode 100755
index 0000000..928a8ef
--- /dev/null
+++ b/device_simulator/asfvolt-driver-package.sh
@@ -0,0 +1,80 @@
+#
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed 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.
+#
+#
+
+# Run from OpenNetworkLinux top directory
+#
+#cd /aux/OpenNetworkLinux
+set -e
+ASFVOLT_REPO_NAME=asfvolt16-driver
+
+# override shell variables to match custom local build environment
+: ${ONL_TOPDIR:=`pwd`}
+: ${BALSRC_RELEASE:=bal_src_release}
+: ${BALREL_TOPDIR:=${ONL_TOPDIR}/${BALSRC_RELEASE}}
+: ${BALSRC_TOPDIR:=${ONL_TOPDIR}/${BALSRC_RELEASE}/bal_release}
+: ${ASFDRVR_PKGDIR:=${ONL_TOPDIR}/asfdrvr-package-dir}
+: ${ASFDVR_TARBALL_NAME:=asfvolt16-voltha-bal}
+
+echo ONL_TOPDIR=${ONL_TOPDIR}
+echo BALSRC_RELEASE=${BALSRC_RELEASE}
+echo BALREL_TOPDIR=${BALREL_TOPDIR}
+echo BALSRC_TOPDIR=${BALSRC_TOPDIR}
+echo ASFDRVR_PKGDIR=${ASFDRVR_PKGDIR}
+echo ASFDVR_TARBALL_NAME=${ASFDVR_TARBALL_NAME}
+
+ASFDVR_TARBALL_FNAME=${ASFDVR_TARBALL_NAME}.tgz
+
+# Note: removes existing directories: asfvolt16-driver, grpc, protobuf
+rm -rf ${ASFDRVR_PKGDIR}
+
+
+mkdir -p ${ASFDRVR_PKGDIR}
+cd ${ASFDRVR_PKGDIR}
+
+#extrace ASFvOLT16 BAL/Maple built release tarball
+tar zxf ${BALREL_TOPDIR}/bcm68620_release/asfvolt16/release/release_asfvolt16_V*.tar.gz
+
+# voltha_bal_driver and grpc/protobufs live in /broadcom dir on ASFvOLT16 target
+cd broadcom
+
+# add grpc-c libs
+cp -pR ${ONL_TOPDIR}/grpc-c/build/lib/.libs/libgrpc-c.so.0.0.0 .
+ln -s /broadcom/libgrpc-c.so.0.0.0 libgrpc-c.so.0
+ln -s /broadcom/libgrpc-c.so.0.0.0 libgrpc-c.so
+
+# add grpc libs
+cp -pR ${ONL_TOPDIR}/grpc/libs/opt/libgrp*.so* .
+cp -pR ${ONL_TOPDIR}/grpc/libs/opt/libgpr*.so* .
+
+# add protobuf-c libs
+cp -pR ${ONL_TOPDIR}/grpc-c/third_party/protobuf-c/protobuf-c/.libs/libprotobuf-c.so* .
+
+# add protobuf libs
+cp -pR ${ONL_TOPDIR}/grpc/thirdparty/protobuf/src/.libs/libprotobuf.so* .
+cp -pR ${ONL_TOPDIR}/grpc/thirdparty/protobuf/src/.libs/libprotobuf-lite.so* .
+cp -pR ${ONL_TOPDIR}/grpc/thirdparty/protobuf/src/.libs/libprotoc.so* .
+rm *.0T
+
+# add voltha_bal_driver
+cp -pR ${ONL_TOPDIR}/grpc-c/build/examples/.libs/voltha_bal_driver .
+
+#extract ASFvOLT16 BAL/Maple built release tarball
+cd ..
+tar cvzf ${ASFDVR_TARBALL_FNAME} broadcom opt
+
+ls -l `pwd`/${ASFDVR_TARBALL_FNAME}
+echo "${ASFVOLT_REPO_NAME} package build complete"
diff --git a/device_simulator/grpc-c_asfvolt16.patch b/device_simulator/grpc-c_asfvolt16.patch
new file mode 100644
index 0000000..d1e07c7
--- /dev/null
+++ b/device_simulator/grpc-c_asfvolt16.patch
@@ -0,0 +1,49 @@
+diff -Naur grpc-c_comperr/lib/client.c grpc-c/lib/client.c
+--- grpc-c_comperr/lib/client.c	2017-10-01 15:04:38.028008999 -0700
++++ grpc-c/lib/client.c	2017-10-01 15:41:38.928008999 -0700
+@@ -134,8 +134,8 @@
+     }
+ 
+     bzero(buf, BUFSIZ);
+-    snprintf_safe(buf, sizeof(buf), "%s%s", PATH_GRPC_C_DAEMON_SOCK, 
+-		  server_name);
++    /*snprintf_safe(buf, sizeof(buf), "%s%s", PATH_GRPC_C_DAEMON_SOCK, server_name);*/
++    snprintf_safe(buf, sizeof(buf), "%s", server_name);
+     if (buf[0] == '\0') {
+ 	return NULL;
+     }
+@@ -967,7 +967,6 @@
+ 
+     if (*output == NULL) {
+ 	grpc_c_context_free(context);
+-	gpr_log(GPR_ERROR, "No output to return");
+ 	rc = GRPC_C_FAIL;
+ 	goto cleanup;
+     }
+@@ -1039,11 +1038,13 @@
+     gpr_timespec tout;
+     grpc_c_context_t *context;
+ 
++#if 0
+     if (context == NULL) {
+ 	gpr_log(GPR_ERROR, "Invalid context pointer provided");
+ 	rc = GRPC_C_FAIL;
+ 	goto cleanup;
+     }
++#endif
+ 
+     context = gc_client_prepare_ops(client, mdarray, 1, input, NULL, 
+ 				    client_streaming, server_streaming, NULL, 
+diff -Naur grpc-c_comperr/lib/service.c grpc-c/lib/service.c
+--- grpc-c_comperr/lib/service.c	2017-10-01 14:54:11.676008999 -0700
++++ grpc-c/lib/service.c	2017-10-01 15:41:09.768008999 -0700
+@@ -1224,7 +1224,8 @@
+ 	return NULL;
+     }
+ 
+-    snprintf_safe(buf, sizeof(buf), "%s%s", PATH_GRPC_C_DAEMON_SOCK, name);
++    /*snprintf_safe(buf, sizeof(buf), "%s%s", PATH_GRPC_C_DAEMON_SOCK, name);*/
++    snprintf_safe(buf, sizeof(buf), "%s", name);
+ 
+     if (buf[0] == '\0') {
+ 	return NULL;
diff --git a/device_simulator/grpc-c_compile-error.patch b/device_simulator/grpc-c_compile-error.patch
new file mode 100644
index 0000000..0b2e802
--- /dev/null
+++ b/device_simulator/grpc-c_compile-error.patch
@@ -0,0 +1,57 @@
+diff -Naur grpc-c_orig/lib/client.c grpc-c/lib/client.c
+--- grpc-c_orig/lib/client.c	2017-06-26 01:14:17.000000000 -0700
++++ grpc-c/lib/client.c	2017-10-01 15:04:38.028008999 -0700
+@@ -239,7 +239,7 @@
+     if (grpc_c_ops_alloc(context, 1)) return 1;
+ 
+     context->gcc_ops[op_count].op = GRPC_OP_RECV_MESSAGE;
+-    context->gcc_ops[op_count].data.recv_message = &context->gcc_payload;
++    context->gcc_ops[op_count].data.recv_message.recv_message = &context->gcc_payload;
+     context->gcc_op_count++;
+ 
+     grpc_call_error e = grpc_call_start_batch(context->gcc_call, 
+@@ -752,7 +752,7 @@
+     input_packer(input, &context->gcc_ops_payload[op_count]);
+ 
+     context->gcc_ops[op_count].op = GRPC_OP_SEND_MESSAGE;
+-    context->gcc_ops[op_count].data.send_message 
++    context->gcc_ops[op_count].data.send_message.send_message 
+ 	= context->gcc_ops_payload[op_count];
+     op_count++;
+ 
+@@ -760,12 +760,12 @@
+     op_count++;
+ 
+     context->gcc_ops[op_count].op = GRPC_OP_RECV_INITIAL_METADATA;
+-    context->gcc_ops[op_count].data.recv_initial_metadata 
++    context->gcc_ops[op_count].data.recv_initial_metadata.recv_initial_metadata 
+ 	= context->gcc_initial_metadata;
+     op_count++;
+ 
+     context->gcc_ops[op_count].op = GRPC_OP_RECV_MESSAGE;
+-    context->gcc_ops[op_count].data.recv_message = &context->gcc_payload;
++    context->gcc_ops[op_count].data.recv_message.recv_message = &context->gcc_payload;
+     op_count++;
+ 
+     /*
+diff -Naur grpc-c_orig/lib/service.c grpc-c/lib/service.c
+--- grpc-c_orig/lib/service.c	2017-06-26 01:14:17.000000000 -0700
++++ grpc-c/lib/service.c	2017-10-01 14:54:11.676008999 -0700
+@@ -169,7 +169,7 @@
+ 	 */
+ 	if (context->gcc_read_event->gce_refcount == 0) {
+ 	    context->gcc_ops[op_count].op = GRPC_OP_RECV_MESSAGE;
+-	    context->gcc_ops[op_count].data.recv_message 
++	    context->gcc_ops[op_count].data.recv_message.recv_message
+ 		= &context->gcc_payload;
+ 
+ 	    context->gcc_read_event->gce_type = GRPC_C_EVENT_READ;
+@@ -343,7 +343,7 @@
+ 	.gcmf_output_packer(output, &context->gcc_ops_payload[op_count]);
+ 
+     context->gcc_ops[context->gcc_op_count].op = GRPC_OP_SEND_MESSAGE;
+-    context->gcc_ops[context->gcc_op_count].data.send_message 
++    context->gcc_ops[context->gcc_op_count].data.send_message.send_message
+ 	= context->gcc_ops_payload[op_count];
+ 
+     context->gcc_op_count++;