blob: 21d73e05a5d96eb2f60b50e7580f18c1a8b7999b [file] [log] [blame]
Matteo Scandolo4747d292019-08-05 11:50:18 -07001# Copyright 2019-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
Matteo Scandolo0b603aa2020-08-21 14:40:48 -070015SHELL = bash -e -o pipefail
Matteo Scandolo4747d292019-08-05 11:50:18 -070016VERSION ?= $(shell cat ./VERSION)
Matteo Scandolo84f7d482019-08-08 19:00:47 -070017DIFF ?= $(git diff --shortstat 2> /dev/null | tail -n1)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020018GIT_STATUS ?= $(shell [ -z "$DIFF" ] && echo "Dirty" || echo "Clean")
Matteo Scandolo4747d292019-08-05 11:50:18 -070019
20## Docker related
21DOCKER_TAG ?= ${VERSION}
Matteo Scandolo5daa2ab2019-10-08 08:27:18 -070022DOCKER_REPOSITORY ?= ""
Matteo Scandolo4747d292019-08-05 11:50:18 -070023DOCKER_REGISTRY ?= ""
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070024DOCKER_RUN_ARGS ?= ""
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010025DOCKER_PORTS ?= -p 50070:50070 -p 50060:50060 -p 50071:50071 -p 50072:50072 -p 50073:50073 -p 50074:50074
Andrea Campanella64dffd52019-12-17 17:29:57 -080026TYPE ?= minimal
Zdravko Bozakov2da76342019-10-21 09:47:35 +020027
Kent Hagerman60d62302020-03-10 17:02:36 -040028# tool containers
Zack Williamsd2907e62020-04-03 10:23:02 -070029VOLTHA_TOOLS_VERSION ?= 2.2.0
Zdravko Bozakov2da76342019-10-21 09:47:35 +020030
Kent Hagerman60d62302020-03-10 17:02:36 -040031GO = 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
32GO_SH = 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 sh -c '
33BBSIM_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 bbsim-builder go
34GO_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
35GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
36BBSIM_LINTER = 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 bbsim-linter:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
37HADOLINT = 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
38PROTOC = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc protoc
39
40builder:
41 @docker build -t bbsim-builder:latest -f build/ci/builder.Dockerfile build/ci/
42
43linter:
44 # since this repo depends on C libs (libpcap), they are first added into a local container
45 @[[ "$(docker images -q bbsim-linter:${VOLTHA_TOOLS_VERSION}-golangci-lint 2> /dev/null)" != "" ]] || \
46 docker build --build-arg=VOLTHA_TOOLS_VERSION=${VOLTHA_TOOLS_VERSION} -t bbsim-linter:${VOLTHA_TOOLS_VERSION}-golangci-lint -f build/ci/linter.Dockerfile build/ci/
47
Matteo Scandolo4747d292019-08-05 11:50:18 -070048
49# Public targets
Matteo Scandolo4747d292019-08-05 11:50:18 -070050all: help
51
Kent Hagerman60d62302020-03-10 17:02:36 -040052protos: api/bbsim/bbsim.pb.go api/bbsim/bbsim.pb.gw.go api/legacy/bbsim.pb.go api/legacy/bbsim.pb.gw.go # @HELP Build proto files
Matteo Scandolo01d41ce2019-10-28 15:42:47 -070053
Matteo Scandolo6866b8c2019-10-28 16:15:24 -070054.PHONY: build
Kent Hagerman60d62302020-03-10 17:02:36 -040055build: protos build-bbsim build-bbsimctl build-bbr
56
57## lint and unit tests
58
59lint-dockerfile:
60 @echo "Running Dockerfile lint check..."
61 @${HADOLINT} $$(find ./build -name "Dockerfile*")
62 @echo "Dockerfile lint check OK"
63
64lint-mod:
65 @echo "Running dependency check..."
66 @${GO} mod verify
67 @echo "Dependency check OK. Running vendor check..."
68 @git status > /dev/null
69 @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)
70 @[[ `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)
71 ${GO} mod tidy
72 ${GO} mod vendor
73 @git status > /dev/null
74 @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)
75 @[[ `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)
76 @echo "Vendor check OK."
77
78lint: lint-mod lint-dockerfile
79
80sca: linter
81 @rm -rf ./sca-report
82 @mkdir -p ./sca-report
83 @echo "Running static code analysis..."
84 @${BBSIM_LINTER} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
85 @echo ""
86 @echo "Static code analysis OK"
Matteo Scandolo4747d292019-08-05 11:50:18 -070087
Matteo Scandolo569e7172019-12-20 11:51:51 -080088test: test-unit test-bbr
89
Kent Hagerman60d62302020-03-10 17:02:36 -040090test-unit: clean local-omci-sim builder # @HELP Execute unit tests
91 @mkdir -p ./tests/results
92 @${BBSIM_BUILDER} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
93 RETURN=$$? ;\
94 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
95 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
96 exit $$RETURN
Matteo Scandolo4747d292019-08-05 11:50:18 -070097
Kent Hagerman60d62302020-03-10 17:02:36 -040098test-bbr: release-bbr docker-build # @HELP Validate that BBSim and BBR are working together
Matteo Scandolo4a036262020-08-17 15:56:13 -070099 DOCKER_RUN_ARGS="-pon 2 -onu 2" make docker-run
Matteo Scandolo569e7172019-12-20 11:51:51 -0800100 sleep 5
Kent Hagerman60d62302020-03-10 17:02:36 -0400101 ./$(RELEASE_DIR)/$(RELEASE_BBR_NAME)-linux-amd64 -pon 2 -onu 2
Matteo Scandolo569e7172019-12-20 11:51:51 -0800102 docker rm -f bbsim
103
Kent Hagerman60d62302020-03-10 17:02:36 -0400104mod-update: # @HELP Download the dependencies to the vendor folder
105 ${GO} mod tidy
106 ${GO} mod vendor
Matteo Scandolo38305492019-10-11 11:36:00 -0700107
Matteo Scandoloca08fc72019-12-04 14:17:39 -0800108docker-build: local-omci-sim# @HELP Build the BBSim docker container (contains BBSimCtl too)
Kent Hagerman60d62302020-03-10 17:02:36 -0400109 docker build \
110 -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} \
111 -f build/package/Dockerfile .
Matteo Scandolo4747d292019-08-05 11:50:18 -0700112
Matteo Scandolo01d41ce2019-10-28 15:42:47 -0700113docker-push: # @HELP Push the docker container to a registry
Matteo Scandolo4747d292019-08-05 11:50:18 -0700114 docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG}
115
Andrea Campanella64dffd52019-12-17 17:29:57 -0800116docker-kind-load:
117 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
118 kind load docker-image ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
119
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700120docker-run: # @HELP Runs the container locally (available options: DOCKER_RUN_ARGS="-pon 2 -onu 2" make docker-run)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200121 docker run -d ${DOCKER_PORTS} --privileged --rm --name bbsim ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} /app/bbsim ${DOCKER_RUN_ARGS}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700122
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700123docker-run-dev: # @HELP Runs the container locally (intended for development purposes, not in detached mode)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200124 docker run ${DOCKER_PORTS} --privileged --rm --name bbsim ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} /app/bbsim ${DOCKER_RUN_ARGS}
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700125
Zack Williams4b0ef4d2019-12-18 14:25:20 -0700126.PHONY: docs docs-lint
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200127docs: swagger # @HELP Generate docs and opens them in the browser
Zack Williams4b0ef4d2019-12-18 14:25:20 -0700128 make -C docs html
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200129 @echo -e "\nBBSim documentation generated in file://${PWD}/docs/build/html/index.html"
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700130
Zack Williams4b0ef4d2019-12-18 14:25:20 -0700131docs-lint:
132 make -C docs lint
133
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700134# Release related items
135# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
136# Inspired by: https://github.com/kubernetes/minikube/releases
137RELEASE_DIR ?= release
138RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64
139
Kent Hagerman60d62302020-03-10 17:02:36 -0400140RELEASE_BBR_NAME ?= bbr
141RELEASE_BBSIM_NAME ?= bbsim
142RELEASE_BBSIMCTL_NAME ?= bbsimctl
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700143
Kent Hagerman60d62302020-03-10 17:02:36 -0400144release-bbr: builder
145 @echo "$(RELEASE_BBR_NAME)-linux-amd64"
146 @${BBSIM_BUILDER} build -mod vendor \
147 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
148 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
149 -X main.gitStatus=${GIT_STATUS} \
150 -X main.version=${VERSION}" \
151 -o "$(RELEASE_DIR)/$(RELEASE_BBR_NAME)-linux-amd64" ./cmd/bbr
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700152
Kent Hagerman60d62302020-03-10 17:02:36 -0400153release-bbsim: builder
154 @echo "$(RELEASE_BBSIM_NAME)-linux-amd64"
155 @${BBSIM_BUILDER} build -mod vendor \
156 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
157 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
158 -X main.gitStatus=${GIT_STATUS} \
159 -X main.version=${VERSION}" \
160 -o "$(RELEASE_DIR)/$(RELEASE_BBSIM_NAME)-linux-amd64" ./cmd/bbsim
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700161
Kent Hagerman60d62302020-03-10 17:02:36 -0400162release-bbsimctl:
163 @${GO_SH} set -eo pipefail; \
164 for os_arch in ${RELEASE_OS_ARCH}; do \
165 echo "$(RELEASE_BBSIMCTL_NAME)-$$os_arch"; \
166 GOOS="$${os_arch%-*}" GOARCH="$${os_arch#*-}" go build -mod vendor \
167 -ldflags "-w -X github.com/opencord/bbsim/internal/bbsimctl/config.BuildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
168 -X github.com/opencord/bbsim/internal/bbsimctl/config.CommitHash=$(shell git log --pretty=format:%H -n 1) \
169 -X github.com/opencord/bbsim/internal/bbsimctl/config.GitStatus=${GIT_STATUS} \
170 -X github.com/opencord/bbsim/internal/bbsimctl/config.Version=${VERSION}" \
171 -o "$(RELEASE_DIR)/$(RELEASE_BBSIMCTL_NAME)-$$os_arch" ./cmd/bbsimctl; \
172 done'
173
174.PHONY: release release-bbr release-bbsim release-bbsimctl
175release: release-bbr release-bbsim release-bbsimctl # @HELP Creates release ready bynaries for BBSimctl and BBR artifacts
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200176swagger: docs/swagger/bbsim/bbsim.swagger.json docs/swagger/leagacy/bbsim.swagger.json # @HELP Generate swagger documentation for BBSim API
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700177
Matteo Scandolo4747d292019-08-05 11:50:18 -0700178help: # @HELP Print the command options
179 @echo
180 @echo "\033[0;31m BroadBand Simulator (BBSim) \033[0m"
181 @echo
182 @echo Emulates the control plane of an openolt compatible device
183 @echo Useful for development and scale testing
184 @echo
185 @grep -E '^.*: .* *# *@HELP' $(MAKEFILE_LIST) \
186 | sort \
187 | awk ' \
188 BEGIN {FS = ": .* *# *@HELP"}; \
189 {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}; \
190 '
191
Matteo Scandoloca08fc72019-12-04 14:17:39 -0800192## Local Development Helpers
193local-omci-sim:
194ifdef LOCAL_OMCI_SIM
195 mkdir -p vendor/github.com/opencord/omci-sim/
196 cp -r ${LOCAL_OMCI_SIM}/* vendor/github.com/opencord/omci-sim/
197endif
Matteo Scandolo4747d292019-08-05 11:50:18 -0700198
199# Internals
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700200
Matteo Scandolo3de9de02019-11-14 13:40:03 -0800201clean:
Kent Hagerman60d62302020-03-10 17:02:36 -0400202 @rm -f bbsim
203 @rm -f bbsimctl
204 @rm -f bbr
205 @rm -rf tools/bin
206 @rm -rf release/*
Matteo Scandolo3de9de02019-11-14 13:40:03 -0800207
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800208build-bbr: local-omci-sim
Kent Hagerman60d62302020-03-10 17:02:36 -0400209 @go build -mod vendor \
210 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
211 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
212 -X main.gitStatus=${GIT_STATUS} \
213 -X main.version=${VERSION}" \
214 ./cmd/bbr
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700215
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700216build-bbsim:
Kent Hagerman60d62302020-03-10 17:02:36 -0400217 @go build -mod vendor \
218 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
219 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
220 -X main.gitStatus=${GIT_STATUS} \
221 -X main.version=${VERSION}" \
222 ./cmd/bbsim
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700223
224build-bbsimctl:
Kent Hagerman60d62302020-03-10 17:02:36 -0400225 @go build -mod vendor \
226 -ldflags "-w -X github.com/opencord/bbsim/internal/bbsimctl/config.BuildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
227 -X github.com/opencord/bbsim/internal/bbsimctl/config.CommitHash=$(shell git log --pretty=format:%H -n 1) \
228 -X github.com/opencord/bbsim/internal/bbsimctl/config.GitStatus=${GIT_STATUS} \
229 -X github.com/opencord/bbsim/internal/bbsimctl/config.Version=${VERSION}" \
230 ./cmd/bbsimctl
Matteo Scandolo4747d292019-08-05 11:50:18 -0700231
Kent Hagerman60d62302020-03-10 17:02:36 -0400232setup_tools:
233 @echo "Downloading dependencies..."
234 @${GO} mod download github.com/grpc-ecosystem/grpc-gateway github.com/opencord/voltha-protos/v2
235 @echo "Dependencies downloaded OK"
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700236
Kent Hagerman60d62302020-03-10 17:02:36 -0400237VOLTHA_PROTOS ?= $(shell ${GO} list -f '{{ .Dir }}' -m github.com/opencord/voltha-protos/v2)
238GOOGLEAPI ?= $(shell ${GO} list -f '{{ .Dir }}' -m github.com/grpc-ecosystem/grpc-gateway)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700239
Kent Hagerman60d62302020-03-10 17:02:36 -0400240.PHONY: api/openolt/openolt.pb.go api/bbsim/bbsim.pb.go api/bbsim/bbsim.pb.gw.go api/legacy/bbsim.pb.go api/legacy/bbsim.pb.gw.go docs/swagger/bbsim/bbsim.swagger.json docs/swagger/leagacy/bbsim.swagger.json
241api/openolt/openolt.pb.go: api/openolt/openolt.proto setup_tools
242 @echo $@
243 @${PROTOC} -I. \
244 -I${GOOGLEAPI}/third_party/googleapis \
245 --go_out=plugins=grpc:./ \
246 $<
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200247
Kent Hagerman60d62302020-03-10 17:02:36 -0400248api/bbsim/bbsim.pb.go api/bbsim/bbsim.pb.gw.go: api/bbsim/bbsim.proto api/bbsim/bbsim.yaml setup_tools
249 @echo $@
250 @${PROTOC} -I. \
251 -I${GOOGLEAPI}/third_party/googleapis \
252 -I${VOLTHA_PROTOS}/protos/ \
253 --go_out=plugins=grpc:./ \
254 --grpc-gateway_out=logtostderr=true,grpc_api_configuration=api/bbsim/bbsim.yaml,allow_delete_body=true:./ \
255 $<
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200256
Kent Hagerman60d62302020-03-10 17:02:36 -0400257api/legacy/bbsim.pb.go api/legacy/bbsim.pb.gw.go: api/legacy/bbsim.proto setup_tools
258 @echo $@
259 @${PROTOC} -I. \
260 -I${GOOGLEAPI}/third_party/googleapis/ \
261 -I${GOOGLEAPI}/ \
262 -I${VOLTHA_PROTOS}/protos/ \
263 --go_out=plugins=grpc:./ \
264 --grpc-gateway_out=logtostderr=true,allow_delete_body=true:./ \
265 $<
266
267docs/swagger/bbsim/bbsim.swagger.json: api/bbsim/bbsim.yaml setup_tools
268 @echo $@
269 @${PROTOC} -I ./api \
270 -I${GOOGLEAPI}/ \
271 -I${VOLTHA_PROTOS}/protos/ \
Zack Williamsd2907e62020-04-03 10:23:02 -0700272 --swagger_out=logtostderr=true,allow_delete_body=true,disable_default_errors=true,grpc_api_configuration=$<:docs/swagger/ \
Kent Hagerman60d62302020-03-10 17:02:36 -0400273 api/bbsim/bbsim.proto
274
275docs/swagger/leagacy/bbsim.swagger.json: api/legacy/bbsim.proto setup_tools
276 @echo $@
277 @${PROTOC} -I ./api \
278 -I${GOOGLEAPI}/ \
279 -I${VOLTHA_PROTOS}/protos/ \
Zack Williamsd2907e62020-04-03 10:23:02 -0700280 --swagger_out=logtostderr=true,allow_delete_body=true,disable_default_errors=true:docs/swagger/ \
Kent Hagerman60d62302020-03-10 17:02:36 -0400281 $<