blob: 4db46d12fd5a93d1a86eb9f8361d8f7bdb842aab [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
15VERSION ?= $(shell cat ./VERSION)
Matteo Scandolo84f7d482019-08-08 19:00:47 -070016DIFF ?= $(git diff --shortstat 2> /dev/null | tail -n1)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020017GIT_STATUS ?= $(shell [ -z "$DIFF" ] && echo "Dirty" || echo "Clean")
Matteo Scandolo4747d292019-08-05 11:50:18 -070018
19## Docker related
20DOCKER_TAG ?= ${VERSION}
Matteo Scandolo5daa2ab2019-10-08 08:27:18 -070021DOCKER_REPOSITORY ?= ""
Matteo Scandolo4747d292019-08-05 11:50:18 -070022DOCKER_REGISTRY ?= ""
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070023DOCKER_RUN_ARGS ?= ""
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010024DOCKER_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 -080025TYPE ?= minimal
Zdravko Bozakov2da76342019-10-21 09:47:35 +020026
Kent Hagerman60d62302020-03-10 17:02:36 -040027# tool containers
28VOLTHA_TOOLS_VERSION ?= 2.1.0
Zdravko Bozakov2da76342019-10-21 09:47:35 +020029
Kent Hagerman60d62302020-03-10 17:02:36 -040030GO = 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
31GO_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 '
32BBSIM_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
33GO_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
34GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
35BBSIM_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
36HADOLINT = 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
37PROTOC = 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
38
39builder:
40 @docker build -t bbsim-builder:latest -f build/ci/builder.Dockerfile build/ci/
41
42linter:
43 # since this repo depends on C libs (libpcap), they are first added into a local container
44 @[[ "$(docker images -q bbsim-linter:${VOLTHA_TOOLS_VERSION}-golangci-lint 2> /dev/null)" != "" ]] || \
45 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/
46
Matteo Scandolo4747d292019-08-05 11:50:18 -070047
48# Public targets
Matteo Scandolo4747d292019-08-05 11:50:18 -070049all: help
50
Kent Hagerman60d62302020-03-10 17:02:36 -040051protos: 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 -070052
Matteo Scandolo6866b8c2019-10-28 16:15:24 -070053.PHONY: build
Kent Hagerman60d62302020-03-10 17:02:36 -040054build: protos build-bbsim build-bbsimctl build-bbr
55
56## lint and unit tests
57
58lint-dockerfile:
59 @echo "Running Dockerfile lint check..."
60 @${HADOLINT} $$(find ./build -name "Dockerfile*")
61 @echo "Dockerfile lint check OK"
62
63lint-mod:
64 @echo "Running dependency check..."
65 @${GO} mod verify
66 @echo "Dependency check OK. Running vendor check..."
67 @git status > /dev/null
68 @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)
69 @[[ `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)
70 ${GO} mod tidy
71 ${GO} mod vendor
72 @git status > /dev/null
73 @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)
74 @[[ `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)
75 @echo "Vendor check OK."
76
77lint: lint-mod lint-dockerfile
78
79sca: linter
80 @rm -rf ./sca-report
81 @mkdir -p ./sca-report
82 @echo "Running static code analysis..."
83 @${BBSIM_LINTER} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
84 @echo ""
85 @echo "Static code analysis OK"
Matteo Scandolo4747d292019-08-05 11:50:18 -070086
Matteo Scandolo569e7172019-12-20 11:51:51 -080087test: test-unit test-bbr
88
Kent Hagerman60d62302020-03-10 17:02:36 -040089test-unit: clean local-omci-sim builder # @HELP Execute unit tests
90 @mkdir -p ./tests/results
91 @${BBSIM_BUILDER} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
92 RETURN=$$? ;\
93 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
94 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
95 exit $$RETURN
Matteo Scandolo4747d292019-08-05 11:50:18 -070096
Kent Hagerman60d62302020-03-10 17:02:36 -040097test-bbr: release-bbr docker-build # @HELP Validate that BBSim and BBR are working together
Matteo Scandolo583f17d2020-02-13 10:35:17 -080098 DOCKER_RUN_ARGS="-auth -dhcp -pon 2 -onu 2" make docker-run
Matteo Scandolo569e7172019-12-20 11:51:51 -080099 sleep 5
Kent Hagerman60d62302020-03-10 17:02:36 -0400100 ./$(RELEASE_DIR)/$(RELEASE_BBR_NAME)-linux-amd64 -pon 2 -onu 2
Matteo Scandolo569e7172019-12-20 11:51:51 -0800101 docker rm -f bbsim
102
Kent Hagerman60d62302020-03-10 17:02:36 -0400103mod-update: # @HELP Download the dependencies to the vendor folder
104 ${GO} mod tidy
105 ${GO} mod vendor
Matteo Scandolo38305492019-10-11 11:36:00 -0700106
Matteo Scandoloca08fc72019-12-04 14:17:39 -0800107docker-build: local-omci-sim# @HELP Build the BBSim docker container (contains BBSimCtl too)
Kent Hagerman60d62302020-03-10 17:02:36 -0400108 docker build \
109 -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} \
110 -f build/package/Dockerfile .
Matteo Scandolo4747d292019-08-05 11:50:18 -0700111
Matteo Scandolo01d41ce2019-10-28 15:42:47 -0700112docker-push: # @HELP Push the docker container to a registry
Matteo Scandolo4747d292019-08-05 11:50:18 -0700113 docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG}
114
Andrea Campanella64dffd52019-12-17 17:29:57 -0800115docker-kind-load:
116 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
117 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/,$$//')
118
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700119docker-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 +0200120 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 -0700121
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700122docker-run-dev: # @HELP Runs the container locally (intended for development purposes, not in detached mode)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200123 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 -0700124
Zack Williams4b0ef4d2019-12-18 14:25:20 -0700125.PHONY: docs docs-lint
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200126docs: swagger # @HELP Generate docs and opens them in the browser
Zack Williams4b0ef4d2019-12-18 14:25:20 -0700127 make -C docs html
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200128 @echo -e "\nBBSim documentation generated in file://${PWD}/docs/build/html/index.html"
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700129
Zack Williams4b0ef4d2019-12-18 14:25:20 -0700130docs-lint:
131 make -C docs lint
132
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700133# Release related items
134# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
135# Inspired by: https://github.com/kubernetes/minikube/releases
136RELEASE_DIR ?= release
137RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64
138
Kent Hagerman60d62302020-03-10 17:02:36 -0400139RELEASE_BBR_NAME ?= bbr
140RELEASE_BBSIM_NAME ?= bbsim
141RELEASE_BBSIMCTL_NAME ?= bbsimctl
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700142
Kent Hagerman60d62302020-03-10 17:02:36 -0400143release-bbr: builder
144 @echo "$(RELEASE_BBR_NAME)-linux-amd64"
145 @${BBSIM_BUILDER} build -mod vendor \
146 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
147 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
148 -X main.gitStatus=${GIT_STATUS} \
149 -X main.version=${VERSION}" \
150 -o "$(RELEASE_DIR)/$(RELEASE_BBR_NAME)-linux-amd64" ./cmd/bbr
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700151
Kent Hagerman60d62302020-03-10 17:02:36 -0400152release-bbsim: builder
153 @echo "$(RELEASE_BBSIM_NAME)-linux-amd64"
154 @${BBSIM_BUILDER} build -mod vendor \
155 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
156 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
157 -X main.gitStatus=${GIT_STATUS} \
158 -X main.version=${VERSION}" \
159 -o "$(RELEASE_DIR)/$(RELEASE_BBSIM_NAME)-linux-amd64" ./cmd/bbsim
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700160
Kent Hagerman60d62302020-03-10 17:02:36 -0400161release-bbsimctl:
162 @${GO_SH} set -eo pipefail; \
163 for os_arch in ${RELEASE_OS_ARCH}; do \
164 echo "$(RELEASE_BBSIMCTL_NAME)-$$os_arch"; \
165 GOOS="$${os_arch%-*}" GOARCH="$${os_arch#*-}" go build -mod vendor \
166 -ldflags "-w -X github.com/opencord/bbsim/internal/bbsimctl/config.BuildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
167 -X github.com/opencord/bbsim/internal/bbsimctl/config.CommitHash=$(shell git log --pretty=format:%H -n 1) \
168 -X github.com/opencord/bbsim/internal/bbsimctl/config.GitStatus=${GIT_STATUS} \
169 -X github.com/opencord/bbsim/internal/bbsimctl/config.Version=${VERSION}" \
170 -o "$(RELEASE_DIR)/$(RELEASE_BBSIMCTL_NAME)-$$os_arch" ./cmd/bbsimctl; \
171 done'
172
173.PHONY: release release-bbr release-bbsim release-bbsimctl
174release: release-bbr release-bbsim release-bbsimctl # @HELP Creates release ready bynaries for BBSimctl and BBR artifacts
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200175swagger: 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 -0700176
Matteo Scandolo4747d292019-08-05 11:50:18 -0700177help: # @HELP Print the command options
178 @echo
179 @echo "\033[0;31m BroadBand Simulator (BBSim) \033[0m"
180 @echo
181 @echo Emulates the control plane of an openolt compatible device
182 @echo Useful for development and scale testing
183 @echo
184 @grep -E '^.*: .* *# *@HELP' $(MAKEFILE_LIST) \
185 | sort \
186 | awk ' \
187 BEGIN {FS = ": .* *# *@HELP"}; \
188 {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}; \
189 '
190
Matteo Scandoloca08fc72019-12-04 14:17:39 -0800191## Local Development Helpers
192local-omci-sim:
193ifdef LOCAL_OMCI_SIM
194 mkdir -p vendor/github.com/opencord/omci-sim/
195 cp -r ${LOCAL_OMCI_SIM}/* vendor/github.com/opencord/omci-sim/
196endif
Matteo Scandolo4747d292019-08-05 11:50:18 -0700197
198# Internals
Matteo Scandolo8dea3992019-10-22 10:54:25 -0700199
Matteo Scandolo3de9de02019-11-14 13:40:03 -0800200clean:
Kent Hagerman60d62302020-03-10 17:02:36 -0400201 @rm -f bbsim
202 @rm -f bbsimctl
203 @rm -f bbr
204 @rm -rf tools/bin
205 @rm -rf release/*
Matteo Scandolo3de9de02019-11-14 13:40:03 -0800206
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800207build-bbr: local-omci-sim
Kent Hagerman60d62302020-03-10 17:02:36 -0400208 @go build -mod vendor \
209 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
210 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
211 -X main.gitStatus=${GIT_STATUS} \
212 -X main.version=${VERSION}" \
213 ./cmd/bbr
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700214
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700215build-bbsim:
Kent Hagerman60d62302020-03-10 17:02:36 -0400216 @go build -mod vendor \
217 -ldflags "-w -X main.buildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
218 -X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
219 -X main.gitStatus=${GIT_STATUS} \
220 -X main.version=${VERSION}" \
221 ./cmd/bbsim
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700222
223build-bbsimctl:
Kent Hagerman60d62302020-03-10 17:02:36 -0400224 @go build -mod vendor \
225 -ldflags "-w -X github.com/opencord/bbsim/internal/bbsimctl/config.BuildTime=$(shell date +%Y/%m/%d-%H:%M:%S) \
226 -X github.com/opencord/bbsim/internal/bbsimctl/config.CommitHash=$(shell git log --pretty=format:%H -n 1) \
227 -X github.com/opencord/bbsim/internal/bbsimctl/config.GitStatus=${GIT_STATUS} \
228 -X github.com/opencord/bbsim/internal/bbsimctl/config.Version=${VERSION}" \
229 ./cmd/bbsimctl
Matteo Scandolo4747d292019-08-05 11:50:18 -0700230
Kent Hagerman60d62302020-03-10 17:02:36 -0400231setup_tools:
232 @echo "Downloading dependencies..."
233 @${GO} mod download github.com/grpc-ecosystem/grpc-gateway github.com/opencord/voltha-protos/v2
234 @echo "Dependencies downloaded OK"
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700235
Kent Hagerman60d62302020-03-10 17:02:36 -0400236VOLTHA_PROTOS ?= $(shell ${GO} list -f '{{ .Dir }}' -m github.com/opencord/voltha-protos/v2)
237GOOGLEAPI ?= $(shell ${GO} list -f '{{ .Dir }}' -m github.com/grpc-ecosystem/grpc-gateway)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700238
Kent Hagerman60d62302020-03-10 17:02:36 -0400239.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
240api/openolt/openolt.pb.go: api/openolt/openolt.proto setup_tools
241 @echo $@
242 @${PROTOC} -I. \
243 -I${GOOGLEAPI}/third_party/googleapis \
244 --go_out=plugins=grpc:./ \
245 $<
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200246
Kent Hagerman60d62302020-03-10 17:02:36 -0400247api/bbsim/bbsim.pb.go api/bbsim/bbsim.pb.gw.go: api/bbsim/bbsim.proto api/bbsim/bbsim.yaml setup_tools
248 @echo $@
249 @${PROTOC} -I. \
250 -I${GOOGLEAPI}/third_party/googleapis \
251 -I${VOLTHA_PROTOS}/protos/ \
252 --go_out=plugins=grpc:./ \
253 --grpc-gateway_out=logtostderr=true,grpc_api_configuration=api/bbsim/bbsim.yaml,allow_delete_body=true:./ \
254 $<
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200255
Kent Hagerman60d62302020-03-10 17:02:36 -0400256api/legacy/bbsim.pb.go api/legacy/bbsim.pb.gw.go: api/legacy/bbsim.proto setup_tools
257 @echo $@
258 @${PROTOC} -I. \
259 -I${GOOGLEAPI}/third_party/googleapis/ \
260 -I${GOOGLEAPI}/ \
261 -I${VOLTHA_PROTOS}/protos/ \
262 --go_out=plugins=grpc:./ \
263 --grpc-gateway_out=logtostderr=true,allow_delete_body=true:./ \
264 $<
265
266docs/swagger/bbsim/bbsim.swagger.json: api/bbsim/bbsim.yaml setup_tools
267 @echo $@
268 @${PROTOC} -I ./api \
269 -I${GOOGLEAPI}/ \
270 -I${VOLTHA_PROTOS}/protos/ \
271 --swagger_out=logtostderr=true,allow_delete_body=true,grpc_api_configuration=$<:docs/swagger/ \
272 api/bbsim/bbsim.proto
273
274docs/swagger/leagacy/bbsim.swagger.json: api/legacy/bbsim.proto setup_tools
275 @echo $@
276 @${PROTOC} -I ./api \
277 -I${GOOGLEAPI}/ \
278 -I${VOLTHA_PROTOS}/protos/ \
279 --swagger_out=logtostderr=true,allow_delete_body=true:docs/swagger/ \
280 $<