First Commit of Voltha-Go-Controller from Radisys

Change-Id: I8e2e908e7ab09a4fe3d86849da18b6d69dcf4ab0
diff --git a/Makefile b/Makefile
index ac7f994..c79c276 100644
--- a/Makefile
+++ b/Makefile
@@ -1,29 +1,124 @@
-# SPDX-FileCopyrightText: ${today.year}-present Intel Corporation
-#
-# SPDX-License-Identifier: Apache-2.0
+DOCKER_NAME := voltha-go-controller
+DOCKER_TAG := latest
+GOLINT := golint
+GOCMD = 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
+GOBUILD=$(GOCMD) build
+GOCLEAN=$(GOCMD) clean
+GOTEST=$(GOCMD) test
+GOTOOL=$(GOCMD) tool
+
+# Static code analyzers
+GOIMPORTS := goimports
+GO_FILES := `find . -path vendor -prune -o -name '*.go'`
+STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS)
+#STATIC_TOOLS := $(GOFMT)
 
 # set default shell
 SHELL = bash -e -o pipefail
 
 # Variables
-VERSION                    ?= $(shell cat ./VERSION)
+VERSION                  ?= $(shell head -n 1 ./VERSION)
+# DOCKER_TAG               ?= ${VERSION}
+IMAGENAME                ?= ${DOCKER_NAME}:${DOCKER_TAG}
 
+COVERAGE_DIR = ./tests/results
+COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out
+TEST_TARGETS := test-default test-verbose test-short
+test-short: ARGS=-short
+test-verbose: ARGS=-v
 # tool containers
 VOLTHA_TOOLS_VERSION ?= 2.4.0
 
-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
-GO_JUNIT_REPORT   = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
-GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/bbsim-sadis-server -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
-GOLANGCI_LINT     = 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}-golangci-lint 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
+GOLANGCI_LINT     = 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}-golangci-lint golangci-lint
+GO_JUNIT_REPORT   = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
+GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openolt-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
+
+## Local Development Helpers
+local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
+ifdef LOCAL_PROTOS
+	rm -rf vendor/github.com/opencord/voltha-protos/v5/go
+	mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
+	cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
+	rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
+endif
+
+local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
+ifdef LOCAL_LIB_GO
+	mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
+	cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
+endif
+
+# This should to be the first and default target in this Makefile
+help: ## Print help for each Makefile target
+	@echo "Usage: make [<target>]"
+	@echo "where available targets are:"
+	@echo
+	@grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
+		| sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
+
+
+exe:
+	@echo Building voltha-go-controller....
+	@cd voltha-go-controller && go build
+
+vgcctl:
+	@echo Building vgcctl....
+	@cd voltha-go-controller/cli && go build -o vgcctl
+
+docker: exe vgcctl
+	@echo Building Docker $(DOCKER_NAME)....
+	sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
+
+## Docker targets
+build:	local-protos local-lib-go docker  ## Build voltha-go-controller image
+
+build-docker-profile: sca exe-profile vgcctl
+	@echo Building Docker $(DOCKER_NAME)....
+	sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller .
+
+sca: ## Runs static code analysis with the golangci-lint tool
+	@echo "Running static code analysis..."
+	sudo docker run --rm --user $$(id -u):$$(id -g) -v $(CURDIR):/go/src ciena/go-lint
+
+clean: ## Removes any local filesystem artifacts generated by a build
+	rm -f voltha-go-controller/voltha-go-controller
+	rm -f voltha-go-controller/cli/vgcctl
+
+mock-gen:
+	mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
 
 ## lint and unit tests
+lint-dockerfile: ## Perform static analysis on Dockerfile
+	@echo "Running Dockerfile lint check ..."
+	@${HADOLINT} $$(find . -name "Dockerfile.*")
+	@echo "Dockerfile lint check OK"
 
-lint:
-	@echo "Add lint command for Dockerfile and go modules, see https://github.com/opencord/bbsim-sadis-server/blob/master/Makefile#L83-L103"
+lint-mod: ## Verify the Go dependencies
+	@echo "Running dependency check..."
+	@${GOCMD} mod verify
+	@echo "Dependency check OK. Running vendor check..."
+	@git status > /dev/null
+	@git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1)
+	@[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1)
+	${GOCMD} mod tidy
+	${GOCMD} mod vendor
+	@git status > /dev/null
+	@git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
+	@[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
+	@echo "Vendor check OK."
 
-sca:
-	@echo "Add static code analysis command for Go code, see https://github.com/opencord/bbsim-sadis-server/blob/master/Makefile#L105-L111"
+lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
 
-test:
-	@echo "Add unit test command for Go code, see https://github.com/opencord/bbsim-sadis-server/blob/master/Makefile#L113-L119"
\ No newline at end of file
+mod-update: ## Update go mod files
+	${GOCMD} mod tidy
+	${GOCMD} mod vendor
+
+test: ## Run unit tests
+	@mkdir -p ./tests/results
+	@${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
+	RETURN=$$? ;\
+	${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
+	${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
+	exit $$RETURN
+