MYIP:=$(shell hostname -i)
RUNNING_CONTAINER:=$(shell sudo docker ps|grep "xos"|awk '{print $$NF}')
LAST_CONTAINER=$(shell sudo docker ps -l -q)

cord: common_cloudlab ceilometer_dashboard virtualbng_json vtn_network_cfg_json
	rm ../../xos_configuration/*
	echo "# Autogenerated -- do not edit" > Dockerfile
	cat ../common/Dockerfile.common Dockerfile.cord >> Dockerfile
	cp ../common/xos_common_config ../../xos_configuration/
	cp ./xos_cord_config ../../xos_configuration/
	cd ../../..; sudo docker build -t xos -f xos/configurations/cord/Dockerfile .
	sudo docker run -d --add-host="ctl:$(MYIP)" -p 9999:8000 xos
	bash ../common/wait_for_xos.sh

common_cloudlab:
	make -C ../common -f Makefile.cloudlab

ceilometer_dashboard:
	#NOTE: The below dashboard install scripts assume 
	#clouldlab openstack environment created using "OpenStack" profile
	#bash install_xos_ceilometer_dashboard.sh
	bash install_ceilometer_patch.sh

virtualbng_json:
	bash make-virtualbng-json.sh

vtn_network_cfg_json:
	bash make-vtn-networkconfig-json.sh

stop:
	sudo docker stop $(RUNNING_CONTAINER)

showlogs:
	sudo docker logs $(LAST_CONTAINER)

enter:
	sudo docker exec -t -i $(RUNNING_CONTAINER) bash

dataplane: etc_hosts
	cd dataplane; ./gen-inventory.sh > hosts
	cd dataplane; ansible-playbook -i hosts dataplane.yaml

dataplane_bm: dataplane
	cd dataplane; bash -c "./generate-bm.sh > hosts-bm"
	cd dataplane; sudo bash -c "ansible-playbook -i hosts-bm dataplane-bm.yaml"

etc_hosts:
	sudo bash -c "sed -i '/^10.11.10/ d' /etc/hosts"
	cd dataplane; sudo bash -c "./gen-etc-hosts.sh >> /etc/hosts"

setup_client:
	# add subscriber to vOLT?  Is there a REST API?
	echo "Don't forget: add-subscriber-access of:0000000000000001 1 432"
	cd dataplane; ansible -i hosts client -m shell -s -a "route del default gw 10.11.10.5; dhclient br-sub"
	# reboot the vBNG ONOS
	cd dataplane; ansible -i hosts onos_vbng -m shell -s -a "docker restart ONOS"

cleanup: stop
	cd dataplane; ./cleanup.sh
	bash -c "source ../common/admin-openrc.sh; nova list --all-tenants; neutron net-list"
	echo "Don't forget to clean up containers"
