blob: 51f78d932ef2a83f075fd08a22ed7f27a205d3e9 [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 Battistonb244bb52022-03-24 15:47:16 +010052# Dependencies versions
Elia Battiston589addb2022-04-04 16:40:01 +020053LIBYANG_VERSION ?= f9bbd46fa3a6b09291ec0c1e93ebf569f3e30bd1
54SYSREPO_VERSION ?= 64e3c66442d682c31e979db289c4c64a3ec1f6c1
55LIBNETCONF2_VERSION ?= ef7d3e3ca1504e8ca9c4f4b5dd3847ba17bb809d
56NETOPEER2_VERSION ?= 39800066f9fbbde9b55e6cfde77927eeb5627c83
Elia Battistonb244bb52022-03-24 15:47:16 +010057
58# Default user and password for the netconf user in docker-build
59NETCONF_USER ?= voltha
60NETCONF_PASSWORD ?= onf
61
Elia Battistonac8d23f2022-03-14 17:54:56 +010062# This container is built to include the necessary sysrepo libraries
63# to succesfully build and test the code in this repository
64BUILDER_IMAGE_AND_TAG ?= voltha/bbf-adapter-builder:local
65build-tools: build/tools/Dockerfile.builder
66 docker build \
67 -t ${BUILDER_IMAGE_AND_TAG} \
Elia Battistonb244bb52022-03-24 15:47:16 +010068 -f build/tools/Dockerfile.builder . \
69 --build-arg LIBYANG_VERSION=${LIBYANG_VERSION} \
70 --build-arg SYSREPO_VERSION=${SYSREPO_VERSION}
Elia Battistonc8d0d462022-02-22 16:30:51 +010071
Elia Battistonac8d23f2022-03-14 17:54:56 +010072GO_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
73GO = 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
74GO_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
75GOCOVER_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
76GOLANGCI_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
77HADOLINT = 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
78
Elia Battistonac8d23f2022-03-14 17:54:56 +010079.PHONY: docker-build local-protos local-lib-go help test sca
Elia Battistonc8d0d462022-02-22 16:30:51 +010080.DEFAULT_GOAL := help
81
82help: ## Print help for each Makefile target
83 @echo
84 @echo Northbound BBF Adapter
85 @echo
86 @echo Translates the BBF yang model to VOLTHA Northbound APIs
87 @echo
88 @echo "Usage: make [<target>]"
89 @echo "where available targets are:"
90 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
91 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
92
93## Local Development Helpers
94local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
95ifdef LOCAL_PROTOS
96 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
97 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
98 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
99 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
100endif
101
102local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
103ifdef LOCAL_LIB_GO
104 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
105 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
106endif
107
108## Docker targets
109build: docker-build ## Alias for 'docker build'
110
Elia Battistonac8d23f2022-03-14 17:54:56 +0100111docker-build: local-lib-go build-tools ## Build the BBF Adapter docker container
Elia Battistonc8d0d462022-02-22 16:30:51 +0100112 docker build \
113 -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG} \
Elia Battistonac8d23f2022-03-14 17:54:56 +0100114 -f build/package/Dockerfile.bbf-adapter . \
115 --build-arg NETCONF_USER=${NETCONF_USER} \
Elia Battistonb244bb52022-03-24 15:47:16 +0100116 --build-arg NETCONF_PASSWORD=${NETCONF_PASSWORD} \
117 --build-arg LIBNETCONF2_VERSION=${LIBNETCONF2_VERSION} \
118 --build-arg NETOPEER2_VERSION=${NETOPEER2_VERSION}
Elia Battistonc8d0d462022-02-22 16:30:51 +0100119
120docker-push: ## Push the docker images to an external repository
121 docker push ${ADAPTER_IMAGENAME}
122ifdef BUILD_PROFILED
123 docker push ${ADAPTER_IMAGENAME}-profile
124endif
125ifdef BUILD_RACE
126 docker push ${ADAPTER_IMAGENAME}-rd
127endif
128
129docker-kind-load: ## Load docker images into a KinD cluster
130 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
131 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
132
Elia Battistonac8d23f2022-03-14 17:54:56 +0100133test: build-tools ## Run unit tests
Elia Battistonc8d0d462022-02-22 16:30:51 +0100134 @mkdir -p ./tests/results
Elia Battistonac8d23f2022-03-14 17:54:56 +0100135 @${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 +0100136 RETURN=$$? ;\
137 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
138 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
139 exit $$RETURN
140
141lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
142
143lint-dockerfile: ## Perform static analysis on Dockerfile
144 @echo "Running Dockerfile lint check..."
145 @${HADOLINT} $$(find ./build -name "Dockerfile*")
146 @echo "Dockerfile lint check OK"
147
148lint-mod: ## Verify the Go dependencies
149 @echo "Running dependency check..."
150 @${GO} mod verify
151 @echo "Dependency check OK. Running vendor check..."
152 @git status > /dev/null
153 @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)
154 @[[ `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)
155 ${GO} mod tidy
156 ${GO} mod vendor
157 @git status > /dev/null
158 @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)
159 @[[ `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)
160 @echo "Vendor check OK."
161
Elia Battistonac8d23f2022-03-14 17:54:56 +0100162sca: build-tools ## Runs static code analysis with the golangci-lint tool
Elia Battistonc8d0d462022-02-22 16:30:51 +0100163 @rm -rf ./sca-report
164 @mkdir -p ./sca-report
165 @echo "Running static code analysis..."
Elia Battistonac8d23f2022-03-14 17:54:56 +0100166 @${GOLANGCI_LINT_LOCAL_BUILDER} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Elia Battistonc8d0d462022-02-22 16:30:51 +0100167 @echo ""
168 @echo "Static code analysis OK"
169
170clean: distclean ## Removes any local filesystem artifacts generated by a build
171
172distclean: ## Removes any local filesystem artifacts generated by a build or test run
173 rm -rf ./sca-report
174
175mod-update: ## Update go mod files
176 ${GO} mod tidy
177 ${GO} mod vendor