# hpc-testing
# Copyright (C) 2018 SUSE LLC
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

setup_rdma_ndd()
{
	local host=$1
	case $(get_suse_version $host) in
		12.3|42.3)
			tp $host 'systemctl status rdma-ndd ||
				   	  (systemctl start rdma-ndd && systemctl status rdma-ndd)'
			;;
		*)
			true
			;;
	esac
}

start_opensm()
{
	local host=$1
	shift
	# Extra args = openSM options
	tp $host "opensm --daemon $*"
}

get_port()
{
	local host=$1
	local host_id=$2

	res=$(tpq $host '(boards=$(/usr/sbin/ibstat -l);
ip_count=0;
for board in $boards; do
	port_count=$(/usr/sbin/ibstat $board -p | wc -l);
	for port in $(seq 1 $port_count); do
		res=$(/usr/sbin/ibstat $board $port);
		status=$(echo "$res" | grep "State:" | awk "{print \$NF}");
		if [ "$status" == "Active" ]; then
			guid=$(echo "$res" | grep "Port GUID:" | awk "{print \$NF}");
			lid=$(echo "$res" | grep "Base lid:" | awk "{print \$NF}");
			sysguid=$(ibstat $board | grep "System image GUID:" | awk "{print \$NF}");
			echo "ib$ip_count $guid $lid $board $port $sysguid";
			exit 0;
		fi;
		ip_count=$(expr $ip_count + 1);
	done;
done
)')

	if [ "$res" == "" ]; then
		fatal_error "Failed to find an active port on $host"
	fi

	ibf=$(echo $res | awk '{ print $1}')
	guid=$(echo $res | awk '{ print $2}')
	lid=$(echo $res | awk '{ print $3}')
	hca=$(echo $res | awk '{ print $4}')
	port=$(echo $res | awk '{ print $5}')
	sysguid=$(echo $res | awk '{ print $6}')
	eval export IPPORT$host_id=$ibf
	eval export GUID$host_id=$guid
	eval export LID$host_id=$lid
	eval export HCA$host_id=$hca
	eval export IBPORT$host_id=$port
	eval export SYSGUID$host_id=$sysguid

	juLogSetProperty $host.ip_if $ibf
	juLogSetProperty $host.guid $guid
	juLogSetProperty $host.lid $lid
	juLogSetProperty $host.hca $hca
	juLogSetProperty $host.port $port
	juLogSetProperty $host.sysguid $sysguid

	echo "[SUCCESS] Host $host uses interface $ibf."\
		 " Board=$board Port=$port GUID=$guid LID=$lid SYSGUID=$sysguid"
}

test_ibdiagnet()
{
	local host=$1
	tp $HOST1 'rm -f topo.out'
	tp $HOST1 'ibdiagnet -wt topo.out'
	tp $HOST1 'ibdiagnet -pc'
	sleep 17
	tp $HOST1 'ibdiagnet -c 1000'

#   We should check the against the topo file
#   But I cannot figure out how to know which of the system name is ours
#	tp $HOST1 ibdiagnet -t topo2.out
}

# Check bsc#972725
test_nodedesc()
{
	local host=$1
	local guid=$2

	node_desc=$(tpq $host "smpquery -G NodeDesc $guid" | \
					sed -e 's/^Node Description:\.*//' | awk '{ print $1}')
	rhostname=$(tpq $host "hostname -s")
	if [ "$node_desc" != "$rhostname" ]; then
		fatal_error "Missing or bad hostname in node description" >&2
	fi
}
