[VOL-4673] Update libyang version to support schema-mount, build dependencies from source

Change-Id: I1a4be6a89f35a104bef01e2c8b276150fb68bca8
diff --git a/Makefile b/Makefile
index 41523df..be17a23 100644
--- a/Makefile
+++ b/Makefile
@@ -49,13 +49,25 @@
 # tool containers
 VOLTHA_TOOLS_VERSION ?= 2.5.3
 
+# Dependencies versions
+LIBYANG_VERSION		?= v2.0.164
+SYSREPO_VERSION		?= v2.1.42
+LIBNETCONF2_VERSION	?= v2.1.7
+NETOPEER2_VERSION	?= v2.1.16
+
+# Default user and password for the netconf user in docker-build
+NETCONF_USER ?= voltha
+NETCONF_PASSWORD ?= onf
+
 # This container is built to include the necessary sysrepo libraries
 # to succesfully build and test the code in this repository
 BUILDER_IMAGE_AND_TAG ?= voltha/bbf-adapter-builder:local 
 build-tools: build/tools/Dockerfile.builder
 	docker build \
 	  -t ${BUILDER_IMAGE_AND_TAG} \
-	  -f build/tools/Dockerfile.builder .
+	  -f build/tools/Dockerfile.builder . \
+	  --build-arg LIBYANG_VERSION=${LIBYANG_VERSION} \
+	  --build-arg SYSREPO_VERSION=${SYSREPO_VERSION}
 
 GO_LOCAL_BUILDER            = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg ${BUILDER_IMAGE_AND_TAG} go
 GO                          = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
@@ -64,10 +76,6 @@
 GOLANGCI_LINT_LOCAL_BUILDER = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg ${BUILDER_IMAGE_AND_TAG} golangci-lint
 HADOLINT                    = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
 
-# Default user and password for the netconf user in docker-build
-NETCONF_USER ?= voltha
-NETCONF_PASSWORD ?= onf
-
 .PHONY: docker-build local-protos local-lib-go help test sca
 .DEFAULT_GOAL := help
 
@@ -105,7 +113,9 @@
 	  -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG} \
 	  -f build/package/Dockerfile.bbf-adapter . \
 	  --build-arg NETCONF_USER=${NETCONF_USER} \
-	  --build-arg NETCONF_PASSWORD=${NETCONF_PASSWORD}
+	  --build-arg NETCONF_PASSWORD=${NETCONF_PASSWORD} \
+	  --build-arg LIBNETCONF2_VERSION=${LIBNETCONF2_VERSION} \
+	  --build-arg NETOPEER2_VERSION=${NETOPEER2_VERSION}
 
 docker-push: ## Push the docker images to an external repository
 	docker push ${ADAPTER_IMAGENAME}
diff --git a/build/package/Dockerfile.bbf-adapter b/build/package/Dockerfile.bbf-adapter
index eccf3fa..72a4661 100644
--- a/build/package/Dockerfile.bbf-adapter
+++ b/build/package/Dockerfile.bbf-adapter
@@ -35,7 +35,7 @@
 ARG org_opencord_vcs_dirty=unknown
 
 # Build
-SHELL ["/bin/bash", "-o", "pipefail", "-c"]
+SHELL ["/bin/ash", "-o", "pipefail", "-c"]
 RUN \
       CGO_ENABLED=$CGO_PARAMETER go build $EXTRA_GO_BUILD_TAGS -mod=vendor -o /app/bbf-adapter \
       -ldflags \
@@ -52,40 +52,64 @@
 
 # -------------
 # Image creation stage
-FROM ubuntu:hirsute-20220113 AS prod
+FROM alpine:3.13 AS prod
 
-SHELL ["/bin/bash", "-o", "pipefail", "-c"]
-RUN \
-      apt-get update && \
-      apt-get install -y \
-            wget=1.21-1ubuntu3 ca-certificates=20210119ubuntu0.21.04.1 \
-            libpcre2-8-0=10.36-2ubuntu5 \
-            libcrypt1=1:4.4.17-1ubuntu3 \
-            libssh-4=0.9.5-1ubuntu0.1 \
-            --no-install-recommends && \
-      rm -rf /var/lib/apt/lists/*
+SHELL ["/bin/ash", "-o", "pipefail", "-c"]
 
-# Download and install library packages
-RUN \
-      wget https://github.com/CESNET/libyang/releases/download/v2.0.112/libyang2_2.0.112.1-1_amd64.deb -O libyang2.deb && \
-      wget https://github.com/sysrepo/sysrepo/releases/download/v2.0.53/libsysrepo6_2.0.53.1-1_amd64.deb -O libsysrepo6.deb && \
-      wget https://github.com/sysrepo/sysrepo/releases/download/v2.0.53/sysrepo-tools_2.0.53.1-1_amd64.deb -O sysrepo-tools.deb && \
-      wget https://github.com/CESNET/libnetconf2/releases/download/v2.0.24/libnetconf2-2_2.0.24.1-1_amd64.deb -O libnetconf2-2.deb && \
-      wget https://github.com/CESNET/netopeer2/releases/download/v2.0.35/netopeer2_2.0.35.2-1_amd64.deb -O netopeer2_2.deb
+RUN apk add --no-cache build-base=0.5-r2 pcre2-dev=10.36-r0 git=2.30.2-r0 cmake=3.18.4-r1 libssh-dev=0.9.5-r0 openssl-dev=1.1.1n-r0 openssl=1.1.1n-r0 bash=5.1.16-r0
 
-RUN dpkg -i libyang2.deb libsysrepo6.deb sysrepo-tools.deb libnetconf2-2.deb netopeer2_2.deb
+RUN rm -r /usr/local/lib && ln -s /usr/local/lib64 /usr/local/lib
 
-RUN rm libyang2.deb libsysrepo6.deb sysrepo-tools.deb libnetconf2-2.deb netopeer2_2.deb
+WORKDIR /
+
+COPY --from=dev /libyang /libyang
+COPY --from=dev /sysrepo /sysrepo
+
+#Build libyang
+WORKDIR /libyang/build
+RUN make install && \
+    rm -rf libyang
+
+#Build sysrepo
+WORKDIR /sysrepo/build
+RUN make install && \
+    rm -rf sysrepo
+
+ARG LIBNETCONF2_VERSION
+ARG NETOPEER2_VERSION
+
+#Build libnetconf2
+WORKDIR /
+RUN git clone https://github.com/CESNET/libnetconf2.git
+WORKDIR /libnetconf2
+RUN git checkout $LIBNETCONF2_VERSION && mkdir build
+WORKDIR /libnetconf2/build
+RUN cmake -D CMAKE_BUILD_TYPE:String="Release" .. && \
+    make && \
+    make install && \
+    rm -rf libnetconf2
+
+#Build netopeer2
+WORKDIR /
+RUN git clone https://github.com/CESNET/netopeer2.git
+WORKDIR /netopeer2
+RUN git checkout $NETOPEER2_VERSION && mkdir build
+WORKDIR /netopeer2/build
+RUN cmake -D CMAKE_BUILD_TYPE:String="Release" .. && \
+    make && \
+    make install && \
+    rm -rf netopeer2
 
 # Copy yang files and install them to sysrepo
 COPY ./build/yang-files /yang
-RUN for f in /yang/*.yang; do sysrepoctl -a -i "$f" -s /yang -p 664 -v3; done
+RUN for f in /yang/*.yang; do sysrepoctl -i "$f" -s /yang -p 664 -v3; done
 
 # Add user for connecting to netopeer2-server through ssh
 ARG NETCONF_USER=voltha
 ARG NETCONF_PASSWORD=onf
 
-RUN useradd -rm -g netconf -u 1001 $NETCONF_USER
+RUN addgroup -S netconf
+RUN adduser $NETCONF_USER --uid 1001 -G netconf --disabled-password
 RUN echo $NETCONF_USER:$NETCONF_PASSWORD | chpasswd
 
 # Set the working directory
diff --git a/build/tools/Dockerfile.builder b/build/tools/Dockerfile.builder
index 7e7f710..1fe22ad 100644
--- a/build/tools/Dockerfile.builder
+++ b/build/tools/Dockerfile.builder
@@ -15,27 +15,38 @@
 # Dockerfile with golang and the sysrepo dependencies for voltha-northbound-bff-adapter
 # This image is used for testing, static code analysis and building
 
-FROM --platform=linux/amd64 golang:1.16.3-buster AS dev
+FROM --platform=linux/amd64 golang:1.16.3-alpine3.13 AS dev
 
 RUN mkdir -m 777 /.cache /go/pkg
 
+RUN apk add --no-cache build-base=0.5-r2 pcre2-dev=10.36-r0 git=2.30.2-r0 cmake=3.18.4-r1
+
 #Install golangci-lint
 RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2
 
-RUN \
-    apt-get update && apt-get install -y libpcre2-dev=10.32-5 --no-install-recommends && \
-    rm -rf /var/lib/apt/lists/*
 
-# Download and install library packages
-RUN \
-    wget https://github.com/CESNET/libyang/releases/download/v2.0.112/libyang2_2.0.112.1-1_amd64.deb -O libyang2.deb && \
-    wget https://github.com/CESNET/libyang/releases/download/v2.0.112/libyang2-dev_2.0.112.1-1_amd64.deb -O libyang2-dev.deb && \
-    wget https://github.com/sysrepo/sysrepo/releases/download/v2.0.53/libsysrepo6_2.0.53.1-1_amd64.deb -O libsysrepo6.deb && \
-    wget https://github.com/sysrepo/sysrepo/releases/download/v2.0.53/libsysrepo-dev_2.0.53.1-1_amd64.deb -O libsysrepo-dev.deb
+ARG LIBYANG_VERSION
+ARG SYSREPO_VERSION
 
-RUN dpkg -i libyang2.deb libyang2-dev.deb libsysrepo6.deb libsysrepo-dev.deb
+#Build libyang
+WORKDIR /
+RUN git clone https://github.com/CESNET/libyang.git
+WORKDIR /libyang
+RUN git checkout $LIBYANG_VERSION && mkdir build
+WORKDIR /libyang/build
+RUN cmake -D CMAKE_BUILD_TYPE:String="Release" .. && \
+    make && \
+    make install
 
-RUN rm libyang2.deb libyang2-dev.deb libsysrepo6.deb libsysrepo-dev.deb
+#Build sysrepo
+WORKDIR /
+RUN git clone https://github.com/sysrepo/sysrepo.git
+WORKDIR /sysrepo
+RUN git checkout $SYSREPO_VERSION && mkdir build
+WORKDIR /sysrepo/build
+RUN cmake -D CMAKE_BUILD_TYPE:String="Release" .. && \
+    make && \
+    make install
 
 WORKDIR /app
 
diff --git a/internal/sysrepo/sysrepo.go b/internal/sysrepo/sysrepo.go
index 57de757..4ab96ff 100644
--- a/internal/sysrepo/sysrepo.go
+++ b/internal/sysrepo/sysrepo.go
@@ -16,7 +16,6 @@
 
 package sysrepo
 
-//#cgo CFLAGS: -I/usr/include
 //#cgo LDFLAGS: -lsysrepo -lyang -Wl,--allow-multiple-definition
 //#include "plugin.c"
 import "C"
@@ -53,7 +52,7 @@
 	}
 
 	//libyang context
-	ly_ctx := C.sr_get_context(conn)
+	ly_ctx := C.sr_acquire_context(conn)
 	if ly_ctx == nil {
 		return fmt.Errorf("null-libyang-context")
 	}
@@ -68,7 +67,12 @@
 		if lyErr != C.LY_SUCCESS {
 			freeCString(path)
 			freeCString(value)
-			return fmt.Errorf("libyang-new-path-failed: %d", lyErr)
+
+			lyErrString := C.ly_errmsg(ly_ctx)
+			err := fmt.Errorf("libyang-new-path-failed: %d %s", lyErr, C.GoString(lyErrString))
+			freeCString(lyErrString)
+
+			return err
 		}
 
 		freeCString(path)
@@ -164,13 +168,13 @@
 	path := C.CString(core.DevicesPath + "/*")
 	defer freeCString(path)
 
-	errCode := C.sr_oper_get_items_subscribe(
+	errCode := C.sr_oper_get_subscribe(
 		plugin.session,
 		module,
 		path,
 		C.function(C.get_devices_cb_wrapper),
 		C.NULL,
-		C.SR_SUBSCR_CTX_REUSE,
+		C.SR_SUBSCR_DEFAULT,
 		&plugin.subscription,
 	)
 	if errCode != C.SR_ERR_OK {