blob: 41523df652690314921c6e14ea1f819d4b290cba [file] [log] [blame]
Elia Battistonc8d0d462022-02-22 16:30:51 +01001# Copyright 2022-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# set default shell
16SHELL = bash -e -o pipefail
17
18# Variables
19VERSION ?= $(shell cat ./VERSION)
20
21DOCKER_LABEL_VCS_DIRTY = false
22ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
23 DOCKER_LABEL_VCS_DIRTY = true
24endif
25## Docker related
26DOCKER_EXTRA_ARGS ?=
27DOCKER_REGISTRY ?=
28DOCKER_REPOSITORY ?=
29DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
30ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG}
31DOCKER_TARGET ?= prod
32TYPE ?= minimal
33
34## Docker labels. Only set ref and commit date if committed
35DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
36DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
37DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
38DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
39
40DOCKER_BUILD_ARGS ?= \
41 ${DOCKER_EXTRA_ARGS} \
42 --build-arg org_label_schema_version="${VERSION}" \
43 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
44 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
45 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
46 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
47 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
48
49# tool containers
Elia Battistonbe9edc12022-03-09 11:35:58 +010050VOLTHA_TOOLS_VERSION ?= 2.5.3
Elia Battistonc8d0d462022-02-22 16:30:51 +010051
Elia Battistonac8d23f2022-03-14 17:54:56 +010052# This container is built to include the necessary sysrepo libraries
53# to succesfully build and test the code in this repository
54BUILDER_IMAGE_AND_TAG ?= voltha/bbf-adapter-builder:local
55build-tools: build/tools/Dockerfile.builder
56 docker build \
57 -t ${BUILDER_IMAGE_AND_TAG} \
58 -f build/tools/Dockerfile.builder .
Elia Battistonc8d0d462022-02-22 16:30:51 +010059
Elia Battistonac8d23f2022-03-14 17:54:56 +010060GO_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
61GO = 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
62GO_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
63GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-northbound-bbf-adapter -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
64GOLANGCI_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
65HADOLINT = 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
66
67# Default user and password for the netconf user in docker-build
68NETCONF_USER ?= voltha
69NETCONF_PASSWORD ?= onf
70
71.PHONY: docker-build local-protos local-lib-go help test sca
Elia Battistonc8d0d462022-02-22 16:30:51 +010072.DEFAULT_GOAL := help
73
74help: ## Print help for each Makefile target
75 @echo
76 @echo Northbound BBF Adapter
77 @echo
78 @echo Translates the BBF yang model to VOLTHA Northbound APIs
79 @echo
80 @echo "Usage: make [<target>]"
81 @echo "where available targets are:"
82 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
83 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
84
85## Local Development Helpers
86local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
87ifdef LOCAL_PROTOS
88 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
89 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
90 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
91 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
92endif
93
94local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
95ifdef LOCAL_LIB_GO
96 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
97 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
98endif
99
100## Docker targets
101build: docker-build ## Alias for 'docker build'
102
Elia Battistonac8d23f2022-03-14 17:54:56 +0100103docker-build: local-lib-go build-tools ## Build the BBF Adapter docker container
Elia Battistonc8d0d462022-02-22 16:30:51 +0100104 docker build \
105 -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG} \
Elia Battistonac8d23f2022-03-14 17:54:56 +0100106 -f build/package/Dockerfile.bbf-adapter . \
107 --build-arg NETCONF_USER=${NETCONF_USER} \
108 --build-arg NETCONF_PASSWORD=${NETCONF_PASSWORD}
Elia Battistonc8d0d462022-02-22 16:30:51 +0100109
110docker-push: ## Push the docker images to an external repository
111 docker push ${ADAPTER_IMAGENAME}
112ifdef BUILD_PROFILED
113 docker push ${ADAPTER_IMAGENAME}-profile
114endif
115ifdef BUILD_RACE
116 docker push ${ADAPTER_IMAGENAME}-rd
117endif
118
119docker-kind-load: ## Load docker images into a KinD cluster
120 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
121 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
122
Elia Battistonac8d23f2022-03-14 17:54:56 +0100123test: build-tools ## Run unit tests
Elia Battistonc8d0d462022-02-22 16:30:51 +0100124 @mkdir -p ./tests/results
Elia Battistonac8d23f2022-03-14 17:54:56 +0100125 @${GO_LOCAL_BUILDER} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Elia Battistonc8d0d462022-02-22 16:30:51 +0100126 RETURN=$$? ;\
127 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
128 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
129 exit $$RETURN
130
131lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
132
133lint-dockerfile: ## Perform static analysis on Dockerfile
134 @echo "Running Dockerfile lint check..."
135 @${HADOLINT} $$(find ./build -name "Dockerfile*")
136 @echo "Dockerfile lint check OK"
137
138lint-mod: ## Verify the Go dependencies
139 @echo "Running dependency check..."
140 @${GO} mod verify
141 @echo "Dependency check OK. Running vendor check..."
142 @git status > /dev/null
143 @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)
144 @[[ `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)
145 ${GO} mod tidy
146 ${GO} mod vendor
147 @git status > /dev/null
148 @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)
149 @[[ `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)
150 @echo "Vendor check OK."
151
Elia Battistonac8d23f2022-03-14 17:54:56 +0100152sca: build-tools ## Runs static code analysis with the golangci-lint tool
Elia Battistonc8d0d462022-02-22 16:30:51 +0100153 @rm -rf ./sca-report
154 @mkdir -p ./sca-report
155 @echo "Running static code analysis..."
Elia Battistonac8d23f2022-03-14 17:54:56 +0100156 @${GOLANGCI_LINT_LOCAL_BUILDER} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Elia Battistonc8d0d462022-02-22 16:30:51 +0100157 @echo ""
158 @echo "Static code analysis OK"
159
160clean: distclean ## Removes any local filesystem artifacts generated by a build
161
162distclean: ## Removes any local filesystem artifacts generated by a build or test run
163 rm -rf ./sca-report
164
165mod-update: ## Update go mod files
166 ${GO} mod tidy
167 ${GO} mod vendor