#!/usr/bin/make -f
# -*- makefile -*-
#export DH_VERBOSE=1

UPSTREAM_GIT = https://github.com/openvswitch/ovn
include /usr/share/openstack-pkg-tools/pkgos.make

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

%:
	dh $@ --with autoreconf,python3,sphinxdoc

# use --as-needed only if supported by dh-autoreconf (to simplify backporting)
DH_AS_NEEDED=$(shell dpkg --compare-versions $$(dpkg --status dh-autoreconf | grep Version | cut -d' ' -f2) ge 6 && echo --as-needed)
override_dh_autoreconf:
	dh_autoreconf $(DH_AS_NEEDED)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
PARALLEL = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
PARALLEL =
endif

override_dh_auto_configure:
	# Configure OVS before OVN
	test -d $(CURDIR)/ovs || mkdir ovs
	cd ovs && tar -xzf /usr/src/openvswitch/openvswitch.tar.gz --strip-components=1
	cd ovs && ./configure --enable-ssl --localstatedir=/var --sysconfdir=/etc --prefix=/usr
	# Use dh_auto_configure to build OVN
	dh_auto_configure -- \
		--prefix=/usr \
		--localstatedir=/var \
		--sysconfdir=/etc \
		--with-dbdir=/var/lib/ovn \
		--with-ovs-source=$(CURDIR)/ovs \
		--enable-ssl

# amd64 / default:
#  127: ovn -- send gratuitous arp on localnet -- ovn-northd -- dp-groups=yes ok
#  623: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=yes ok
#  624: ovn.at:26583       ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no
#  810: ovn -- northd ssl file change -- ovn-northd -- dp-groups=yes ok
TEST_LIST = 1-126 128-622 625-809 811-

# arm64:
#  168: ovn -- ovn-controller incremental processing    FAILED (ovn-performance.at:483)
#  557: ovn.at:23876       ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=yes
#  558: ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=no FAILED (0)
#  579: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no FAILED (ovn.at:24566)
#  624: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no
#  963. ovn -- ipsec -- basic configuration (ovn-ipsec.at:3)
ifneq (,$(filter arm64, $(DEB_HOST_ARCH)))
TEST_LIST = 1-167 169-390 393-556 559-577 580-962 965-
endif # arm64

# armel:
#  155: ovn -- policy-based routing IPv6: 1 HVs, 3 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=yes FAILED (ovn.at:7180)
#  156: ovn -- policy-based routing IPv6: 1 HVs, 3 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=no FAILED (ovn.at:7180)
#  256: ovn -- 1 LR with distributed router gateway port -- ovn-northd -- dp-groups=no ok
#  331: ovn -- ACL conjunction -- ovn-northd -- dp-groups=yes FAILED (ovs-macros.at:255)
#  332: ovn -- ACL conjunction -- ovn-northd -- dp-groups=no FAILED (ovs-macros.at:255)
#  376: ovn -- IP packet buffering -- ovn-northd -- dp-groups=no ok
#  391: ovn -- test transport zones -- ovn-northd -- dp-groups=yes FAILED (ovn.at:16791)
#  392: ovn -- test transport zones -- ovn-northd -- dp-groups=no ok
#  539: ovn -- lflow cache for conjunctions -- ovn-northd -- dp-groups=yes ok
#  557: ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=yes ok
#  558: ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=no ok
#  578: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes FAILED (ovn.at:24566)
#  579: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no FAILED (ovn.at:24566)
#  811: ovn -- northd ssl file change -- ovn-northd -- dp-groups=no ok
#  945: ovn-ic -- gateway sync -- ovn-northd -- dp-groups=yes FAILED (ovn-ic.at:67)
#  946: ovn-ic -- gateway sync -- ovn-northd -- dp-groups=no FAILED (ovn-ic.at:67)
#  963: ovn -- ipsec -- basic configuration             FAILED (ovn-ipsec.at:48)
ifneq (,$(filter armel, $(DEB_HOST_ARCH)))
TEST_LIST = 1-154 157-254 257-330 333-375 377-378 380-390 393-538 540-556 559-577 580-810 812-944 947-962 964-
endif # armel

# armhf:
#  155: ovn -- policy-based routing IPv6: 1 HVs, 3 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=yes FAILED (ovn.at:7180)
#  156: ovn -- policy-based routing IPv6: 1 HVs, 3 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=no FAILED (ovn.at:7180)
#  168: ovn -- ovn-controller incremental processing    FAILED (ovn-performance.at:483)
#  331: ovn -- ACL conjunction -- ovn-northd -- dp-groups=yes FAILED (ovs-macros.at:255)
#  332: ovn -- ACL conjunction -- ovn-northd -- dp-groups=no FAILED (ovs-macros.at:255)
#  379: ovn -- neighbor update on same HV -- ovn-northd -- dp-groups=yes ok
#  557: ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=yes ok
#  558: ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=no FAILED (0)
#  578: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes FAILED (ovn.at:24566)
#  579: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no FAILED (ovn.at:24566)
#  624: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no (ovn.at:26583)
#  945: ovn-ic.at:67       ovn-ic -- gateway sync -- ovn-northd -- dp-groups=yes
#  946: ovn-ic -- gateway sync -- ovn-northd -- dp-groups=no FAILED (ovn-ic.at:67)
#  963: ovn-ipsec.at:3     ovn -- ipsec -- basic configuration
ifneq (,$(filter armhf, $(DEB_HOST_ARCH)))
TEST_LIST = 1-154 157-167 169-330 333-378 380-390 393-556 559-577 580-623 625-944 947-962 964-
endif # armhf

# i386:
#  151: ovn -- policy-based routing: 1 HVs, 2 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=yes ok
#  557: ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=yes ok
#  562: ovn -- ARP replies for SNAT external ips -- ovn-northd -- dp-groups=no ok
#  569: ovn -- conflict ACLs with address set           FAILED (ovs-macros.at:255)
#  578: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes FAILED (ovn.at:24566)
#  579: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no FAILED (ovn.at:24566)
#  623: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=yes ok
#  624: ovn.at:26583       ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no
#  946: ovn-ic -- gateway sync -- ovn-northd -- dp-groups=no FAILED (ovn-ic.at:67)
#  963: ovn-ipsec.at:3     ovn -- ipsec -- basic configuration
ifneq (,$(filter i386, $(DEB_HOST_ARCH)))
TEST_LIST = 1-150 152-556 558-561 563-568 570-577 580-622 625-809 811-945 967-962 964-
endif # i386

#mips64el
#  578: ovn.at:24566       ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes
#  579: ovn.at:24566       ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no
#  624: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no
#  945: ovn-ic.at:67       ovn-ic -- gateway sync -- ovn-northd -- dp-groups=yes
#  963: ovn-ipsec.at:3     ovn -- ipsec -- basic configuration
ifneq (,$(filter mips64el, $(DEB_HOST_ARCH)))
TEST_LIST = 1-154 156-167 169-330 332-390 393-557 559-577 580-623 625-944 946-962 964-
endif # mips64el

#mipsel
#   92: ovn -- vtep: 3 HVs, 1 VIFs/HV, 1 GW, 1 LS -- ovn-northd -- dp-groups=no ok
#  151: ovn -- policy-based routing: 1 HVs, 2 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=yes ok
#  155: ovn -- policy-based routing IPv6: 1 HVs, 3 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=yes FAILED (ovn.at:7180)
#  156: ovn -- policy-based routing IPv6: 1 HVs, 3 LSs, 1 lport/LS, 1 LR -- ovn-northd -- dp-groups=no FAILED (ovn.at:7180)
#  191: ovn -- send gratuitous arp for nat ips in localnet -- ovn-northd -- dp-groups=yes ok
#  259: ovn -- send gratuitous arp for NAT rules on distributed router -- ovn-northd -- dp-groups=yes ok
#  264: ovn -- vlan traffic for external network with distributed router gateway port -- ovn-northd -- dp-groups=no ok
#  287: ovn -- send gratuitous ARP for NAT rules on HA distributed router -- ovn-northd -- dp-groups=yes ok
#  331: ovn -- ACL conjunction -- ovn-northd -- dp-groups=yes FAILED (ovs-macros.at:255)
#  332: ovn -- ACL conjunction -- ovn-northd -- dp-groups=no FAILED (ovs-macros.at:255)
#  371: ovn -- router - check packet length - icmp defrag -- ovn-northd -- dp-groups=yes ok
#  372: ovn -- router - check packet length - icmp defrag -- ovn-northd -- dp-groups=no ok
#  376: ovn -- IP packet buffering -- ovn-northd -- dp-groups=no ok
#  391: ovn -- test transport zones -- ovn-northd -- dp-groups=yes FAILED (ovn.at:16791)
#  392: ovn -- test transport zones -- ovn-northd -- dp-groups=no FAILED (ovn.at:16791)
#  451: ovn -- Load balancer health checks -- ovn-northd -- dp-groups=yes ok
#  452: ovn -- Load balancer health checks -- ovn-northd -- dp-groups=no ok
#  467: ovn -- interconnection -- ovn-northd -- dp-groups=yes ok
#  468: ovn -- interconnection -- ovn-northd -- dp-groups=no ok
#  ovn -- nb_cfg timestamp -- ovn-northd -- dp-groups=yes
#  569: ovn -- conflict ACLs with address set           FAILED (ovs-macros.at:255)
#  578: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes FAILED (ovn.at:24566)
#  579: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no FAILED (ovn.at:24566)
#  623: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=yes ok
#  624: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no FAILED (ovn.at:26583)
#  810: ovn-northd.at:3610 ovn -- northd ssl file change -- ovn-northd -- dp-groups=yes
#  811: ovn -- northd ssl file change -- ovn-northd -- dp-groups=no
#  945: ovn-ic -- gateway sync -- ovn-northd -- dp-groups=yes FAILED (ovn-ic.at:67)
#  963: ovn -- ipsec -- basic configuration             FAILED (ovn-ipsec.at:48)
ifneq (,$(filter mipsel, $(DEB_HOST_ARCH)))
TEST_LIST = 1-91 93-150 152-154 157-190 192-258 260-263 265-330 333-370 373-375 377-390 393-450 453-466 469-556 558-568 570-577 580-622 625-809 812-944 946-962 964-
endif # mipsel

#ppc64el
#  300: ovn -- options:requested-chassis for logical port -- ovn-northd -- dp-groups=no ok
#  578: ovn.at:24566       ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes
#  579: ovn.at:24566       ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no
#  623: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=yes FAILED (ovn.at:26583)
#  624: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no ok
#  810: ovn -- northd ssl file change -- ovn-northd -- dp-groups=yes FAILED (--wait=sb)
#  811: ovn -- northd ssl file change -- ovn-northd -- dp-groups=no
#  945: ovn-ic -- gateway sync -- ovn-northd -- dp-groups=yes FAILED (ovn-ic.at:67)
#  963: ovn-ipsec.at:3     ovn -- ipsec -- basic configuration
ifneq (,$(filter ppc64el, $(DEB_HOST_ARCH)))
TEST_LIST = 1-154 156-167 169-299 301-330 332-390 393-557 559-577 580-622 625-809 812-944 946-962 964-
endif # ppc64el

#s390x
#  300: ovn -- options:requested-chassis for logical port -- ovn-northd -- dp-groups=no ok
#  319: ovn -- ACLs on Port Groups -- ovn-northd -- dp-groups=yes FAILED (ovs-macros.at:255)
#  578: ovn.at:24566       ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes
#  579: ovn.at:24566       ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=no
#  623: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=yes FAILED (ovn.at:26583)
#  624: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=no ok
#  963: ovn-ipsec.at:3     ovn -- ipsec -- basic configuration
ifneq (,$(filter s390x, $(DEB_HOST_ARCH)))
TEST_LIST = 1-154 156-167 169-299 301-318 320-330 332-390 393-557 559-577 580-622 625-962 965-
endif # s390x

# riscv64:
#  127: ovn -- send gratuitous arp on localnet -- ovn-northd -- dp-groups=yes ok
#  128: ovn -- send gratuitous arp on localnet -- ovn-northd -- dp-groups=no ok
#  255: ovn -- 1 LR with distributed router gateway port -- ovn-northd -- dp-groups=yes FAILED (ovn.at:10659)
#  256: ovn -- 1 LR with distributed router gateway port -- ovn-northd -- dp-groups=no FAILED (ovn.at:10659)
#  371: ovn -- router - check packet length - icmp defrag -- ovn-northd -- dp-groups=yes FAILED (ovs-macros.at:255)
#  372: ovn -- router - check packet length - icmp defrag -- ovn-northd -- dp-groups=no FAILED (ovs-macros.at:255)
#  578: ovn -- multi-vtep SB Chassis encap updates -- ovn-northd -- dp-groups=yes FAILED (ovn.at:24566)
#  623: ovn -- ACL with Port Group conjunction flow efficiency -- ovn-northd -- dp-groups=yes FAILED (ovn.at:26583)
#  963: ovn-ipsec.at:3     ovn -- ipsec -- basic configuration
ifneq (,$(filter riscv64, $(DEB_HOST_ARCH)))
TEST_LIST = 1-126 129-254 257-370 373-577 579-622 624-962 965-
endif # riscv64

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	if $(MAKE) check TESTSUITEFLAGS='$(PARALLEL) $(TEST_LIST)' || \
                $(MAKE) check TESTSUITEFLAGS='--recheck'; then :; \
	else \
		cat tests/testsuite.log; \
		exit 1; \
	fi
endif # nocheck

override_dh_auto_build:
	cd ovs && $(MAKE) $(PARALLEL)
	$(MAKE) $(PARALLEL)

override_dh_auto_clean:
	dh_auto_clean
	rm -rf ovs

override_dh_installinit:
	# Package does not ship any init.d files
	dh_installinit --no-scripts

override_dh_installsystemd:
	dh_installsystemd --restart-after-upgrade -povn-central --name=ovn-northd
	dh_installsystemd --restart-after-upgrade -povn-central --name=ovn-ovsdb-server-sb
	dh_installsystemd --restart-after-upgrade -povn-central --name=ovn-ovsdb-server-nb
	dh_installsystemd --restart-after-upgrade -povn-ic-db   --name=ovn-ovsdb-server-ic-sb
	dh_installsystemd --restart-after-upgrade -povn-ic-db   --name=ovn-ovsdb-server-ic-nb
	dh_installsystemd --restart-after-upgrade -povn-host    --name=ovn-controller
	dh_installsystemd --restart-after-upgrade

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
