VOL-1554 Updated build to use LOCAL_PROTO for using local protos, and using a single base dockerfile

Change-Id: Ifa4ec6094c260a604918ccee75420bf328e647dd
diff --git a/.gitignore b/.gitignore
index 5fd0857..229b4bd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -63,6 +63,5 @@
 python/cli/.voltha_cli_history
 
 # Development versions of locally built libraries
-python/pyvoltha
-python/voltha-protos
+python/local_imports
 
diff --git a/python/Makefile b/python/Makefile
index 4799a78..1cb11de 100644
--- a/python/Makefile
+++ b/python/Makefile
@@ -43,6 +43,10 @@
 	 --rm --force-rm \
 	$(DOCKER_BUILD_EXTRA_ARGS)
 
+DOCKER_BUILD_EXTRA_ARGS = \
+	--build-arg LOCAL_PYVOLTHA=$(LOCAL_PYVOLTHA) \
+	--build-arg LOCAL_PROTOS=$(LOCAL_PROTOS)
+
 DOCKER_IMAGE_LIST = \
 	voltha-openolt-adapter-base \
 	voltha-openolt-adapter
@@ -75,15 +79,17 @@
 containers: base openolt_adapter
 
 base:
+	mkdir -p local_imports/
 ifdef LOCAL_PYVOLTHA
-	mkdir -p pyvoltha/dist
-	cp ../../pyvoltha/dist/*.tar.gz pyvoltha/dist/
-	mkdir -p voltha-protos/dist
-	cp ../../voltha-protos/dist/*.tar.gz voltha-protos/dist/
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-openolt-adapter-base:${TAG} -f docker/Dockerfile.base_local .
-else
-	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-openolt-adapter-base:${TAG} -f docker/Dockerfile.base .
+	mkdir -p local_imports/pyvoltha/dist
+	cp ../../pyvoltha/dist/*.tar.gz local_imports/pyvoltha/dist/
 endif
+ifdef LOCAL_PROTOS
+	mkdir -p local_imports/voltha-protos/dist
+	cp ../../voltha-protos/dist/*.tar.gz local_imports/voltha-protos/dist/
+endif
+	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-openolt-adapter-base:${TAG} -f docker/Dockerfile.base .
+
 
 openolt_adapter: base
 	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-openolt-adapter:${TAG} -f docker/Dockerfile.openolt_adapter .
@@ -104,8 +110,7 @@
 	docker pull ${REGISTRY}${REPOSITORY}voltha-$(subst .pull,,$@):${TAG}
 
 clean:
-	rm -rf pyvoltha
-	rm -rf voltha-protos
+	rm -rf local_imports
 	find . -name '*.pyc' | xargs rm -f
 
 distclean: clean
@@ -133,13 +138,16 @@
 	    fi
 
 ifdef LOCAL_PYVOLTHA
-	mkdir -p pyvoltha/dist
-	cp ../../pyvoltha/dist/*.tar.gz pyvoltha/dist/
-	mkdir -p voltha-protos/dist
-	cp ../../voltha-protos/dist/*.tar.gz voltha-protos/dist/
+	mkdir -p local_imports/pyvoltha/dist
+	cp ../../pyvoltha/dist/*.tar.gz local_imports/pyvoltha/dist/
 	. ${VENVDIR}/bin/activate && \
-	    pip install pyvoltha/dist/*.tar.gz && \
-	    pip install voltha-protos/dist/*.tar.gz
+	    pip install local_imports/pyvoltha/dist/*.tar.gz
+endif
+ifdef LOCAL_PROTOS
+	mkdir -p local_imports/voltha-protos/dist
+	cp ../../voltha-protos/dist/*.tar.gz local_imports/voltha-protos/dist/
+	. ${VENVDIR}/bin/activate && \
+	    pip install local_imports/voltha-protos/dist/*.tar.gz
 endif
 
 # end file
diff --git a/python/docker/Dockerfile.base b/python/docker/Dockerfile.base
index 898f7fa..94a3797 100644
--- a/python/docker/Dockerfile.base
+++ b/python/docker/Dockerfile.base
@@ -16,6 +16,9 @@
 
 MAINTAINER Voltha Community <info@opennetworking.org>
 
+ARG LOCAL_PYVOLTHA
+ARG LOCAL_PROTOS
+
 # Update to have latest images
 RUN apt-get update && \
     apt-get install -y python openssl iproute2 libpcap-dev wget build-essential git binutils python-dev libffi-dev libssl-dev 
@@ -28,3 +31,16 @@
 COPY requirements.txt /tmp/requirements.txt
 RUN pip install -r /tmp/requirements.txt
 
+#COPY POTENTIAL LOCAL IMPORTS
+COPY local_imports/ /local_imports/
+
+RUN if [ -n "$LOCAL_PYVOLTHA" ] ; then \
+    pip install /local_imports/pyvoltha/dist/*.tar.gz ; \
+fi
+
+RUN if [ -n "$LOCAL_PROTOS" ] ; then \
+    pip install /local_imports/voltha-protos/dist/*.tar.gz ; \
+fi
+
+
+
diff --git a/python/docker/Dockerfile.base_local b/python/docker/Dockerfile.base_local
deleted file mode 100644
index d2aae88..0000000
--- a/python/docker/Dockerfile.base_local
+++ /dev/null
@@ -1,38 +0,0 @@
-# Copyright 2016 the original author or authors.
-#
-# 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.
-
-FROM ubuntu:xenial
-
-MAINTAINER Voltha Community <info@opennetworking.org>
-
-# Update to have latest images
-RUN apt-get update && \
-    apt-get install -y python openssl iproute2 libpcap-dev wget build-essential git binutils python-dev libffi-dev libssl-dev 
-
-# Install current version of pip rather than outdated pip from apt
-RUN wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
-RUN python /tmp/get-pip.py
-
-# Install adapter requirements. 
-COPY requirements.txt /tmp/requirements.txt
-RUN pip install -r /tmp/requirements.txt
-
-# Install local pyvoltha
-COPY pyvoltha/dist /pyvoltha/dist
-RUN pip install /pyvoltha/dist/*.tar.gz
-
-# Install local voltha-protos
-COPY voltha-protos/dist /voltha-protos/dist
-RUN pip install /voltha-protos/dist/*.tar.gz
-
diff --git a/python/docker/Dockerfile.openolt_adapter b/python/docker/Dockerfile.openolt_adapter
index 81f2690..db52a0e 100644
--- a/python/docker/Dockerfile.openolt_adapter
+++ b/python/docker/Dockerfile.openolt_adapter
@@ -15,8 +15,6 @@
 ARG REGISTRY=
 ARG REPOSITORY=
 
-#FROM ${REGISTRY}${REPOSITORY}voltha-protos:${TAG} as protos
-# FROM ${REGISTRY}${REPOSITORY}voltha-openolt-protos:${TAG} as openolt_protos
 FROM ${REGISTRY}${REPOSITORY}voltha-openolt-adapter-base:${TAG}
 
 MAINTAINER Voltha Community <info@opennetworking.org>
diff --git a/python/docker/Dockerfile.openolt_protos b/python/docker/Dockerfile.openolt_protos
deleted file mode 100644
index 5585942..0000000
--- a/python/docker/Dockerfile.openolt_protos
+++ /dev/null
@@ -1,34 +0,0 @@
-# Copyright 2018 the original author or authors.
-#
-# 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.
-
-ARG REGISTRY=
-ARG REPOSITORY=
-ARG TAG=latest
-
-FROM ${REGISTRY}${REPOSITORY}voltha-protos:${TAG} as protos
-FROM ${REGISTRY}${REPOSITORY}voltha-protoc:${TAG} as builder
-
-MAINTAINER Voltha Community <info@opennetworking.org>
-
-COPY --from=protos /protos/voltha/ /protos/voltha/
-COPY protos/*.proto /protos/voltha/
-COPY protos/third_party/google/ /protos/voltha/google/
-COPY docker/config/Makefile.protos /protos/voltha/Makefile.protos
-
-WORKDIR /protos/voltha
-RUN make -f Makefile.protos build
-
-# Copy the files to a scrach based container to minimize its size
-FROM ${REGISTRY}scratch
-COPY --from=builder /protos/ /protos/
diff --git a/python/docker/config/Makefile.protos b/python/docker/config/Makefile.protos
deleted file mode 100644
index 12ff9e3..0000000
--- a/python/docker/config/Makefile.protos
+++ /dev/null
@@ -1,59 +0,0 @@
-#
-# Copyright 2017 the original author or authors.
-#
-# 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.
-#
-
-# Makefile to build all protobuf and gRPC related artifacts
-
-default: build
-
-PROTO_FILES := $(wildcard *.proto)
-PROTO_ALL_FILES := $(PROTO_FILES) $(PROTO_GOOGLE_API)
-PROTO_PB2_FILES := $(foreach f,$(PROTO_FILES),$(subst .proto,_pb2.py,$(f)))
-PROTO_PB2_GOOGLE_API := $(foreach f,$(PROTO_GOOGLE_API),$(subst .proto,_pb2.py,$(f)))
-PROTO_All_PB2_C_FILES := $(foreach f,$(PROTO_ALL_FILES),$(subst .proto,_pb2.pyc,$(f)))
-PROTO_ALL_PB2_GPRC_FILES := $(foreach f,$(PROTO_ALL_FILES),$(subst .proto,_pb2_grpc.py,$(f)))
-PROTO_ALL_DESC_FILES := $(foreach f,$(PROTO_ALL_FILES),$(subst .proto,.desc,$(f)))
-
-# Google API needs to be built from within the third party directory
-#
-google_api:
-	python -m grpc.tools.protoc \
-	    -I. \
-            --python_out=. \
-            --grpc_python_out=. \
-            --descriptor_set_out=google/api/annotations.desc \
-            --include_imports \
-            --include_source_info \
-        google/api/annotations.proto google/api/http.proto
-
-build: $(PROTO_PB2_FILES)
-
-%_pb2.py: %.proto
-	python -m grpc.tools.protoc \
-                -I. \
-                -I/protos \
-                --python_out=. \
-                --grpc_python_out=. \
-                --descriptor_set_out=$(basename $<).desc \
-                --include_imports \
-                --include_source_info \
-                $<
-
-clean:
-	rm -f $(PROTO_PB2_FILES) \
-		$(PROTO_ALL_DESC_FILES) \
-		$(PROTO_ALL_PB2_GPRC_FILES) \
-		$(PROTO_All_PB2_C_FILES) \
-		$(PROTO_PB2_GOOGLE_API)