#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# AUTHORS: Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
#
##############################################################################

SCRIPTDIR="$(dirname "$(readlink -f ${BASH_SOURCE})")"
source "${SCRIPTDIR}/../../Macros"

NO_PROXY_NEEDED

# Functions for drain
python_get_firstrep_property="

import sys
import dpm2 as dpm

replicas = dpm.dpns_getreplicax ('FILE_NAME', None, None)
if len(replicas) > 0 :
   print replicas[0].PROPERTY
   sys.exit (0)
else :
   sys.exit (1)
"

function get_firstrep_property 
{
   func=${python_get_firstrep_property//PROPERTY/$2}
   func=${func//FILE_NAME/\/$1}
   python -c "$func"
}

python_make_replica_volatile="

import sys
import dpm2 as dpm

replicas = dpm.dpns_getreplicax ('FILE_NAME', None, None)
if len(replicas) > 0 :
   dpm.dpns_setrtype (replicas[0].sfn, 'V')
   dpm.dpns_setrltime (replicas[0].sfn, LIFETIME)
   sys.exit (0)
else :
   sys.exit (1)
"

function make_replica_volatile
{
   lifetime=$[$(date +%s) + $2]
   func=${python_make_replica_volatile//LIFETIME/$lifetime}
   func=${func//FILE_NAME/\/$1}
   python -c "$func"
}

function dpm2_remote_exec
{
   ssh root@"$1" "export DPM_HOST=$DPM_HOST; export DPNS_HOST=$DPNS_HOST;\
                  dpm2_location=\$(rpm -q -l DPM-interfaces2 | grep \"/dpm2\.py\")
                  if [ -z \$dpm_location ]; then 
                  dpm2_location=\$(rpm -q -l python-dpm | grep \"/dpm2\.py\")
                  fi; export PYTHONPATH=\${dpm2_location%/*}:\$PYTHONPATH; $2"
   return $?
}

python_enable_allfs="

import sys
import dpm2 as dpm

pools = dpm.dpm_getpools ()
for pool in pools :
   filesystems = dpm.dpm_getpoolfs (pool.poolname)
   for filesystem in filesystems :
      if filesystem.status != 0 :
         dpm.dpm_modifyfs (filesystem.server, filesystem.fs, 0)

"

# Main code

error=0

POOLNAME=pool`date +%s`

TOKEN=t`date +%s`
DISK_NODE=""

STD_OUT=/tmp/stdout-`date +%s%N`
STD_ERR=/tmp/stderr-`date +%s%N`

testName="Test 1:Drain a DPM disk node"
PY_DPM_LOCATION=

error=0
for i in $(rfcat $DPM_HOST:/etc/shift.conf | grep "^DPM TRUST " \
           | sed -e 's/DPM TRUST //'); do 

   if [ "x$DPM_HOST" != "x$i" ]; then
      DISK_NODE=$i
      break
   fi
done

if [ $? -ne 0 ]; then
  exit 1
fi

if [ -n "$DISK_NODE" ]; then

   echo "Using $DISK_NODE as DPM DISK NODE"

   DPM_HOST_EXEC "$DPM_HOST" "dpm-addpool --poolname drain_pool_1"
   DPM_HOST_EXEC "$DPM_HOST" "mkdir /drain_fs_1; chown dpmmgr.dpmmgr /drain_fs_1"
   DPM_HOST_EXEC "$DPM_HOST" "dpm-addfs --poolname drain_pool_1 --server $DPM_HOST --fs /drain_fs_1"
   DPM_HOST_EXEC "$DISK_NODE" "mkdir /drain_fs_1; chown dpmmgr.dpmmgr /drain_fs_1"
   DPM_HOST_EXEC "$DPM_HOST" "dpm-addfs --poolname drain_pool_1 --server $DISK_NODE --fs /drain_fs_1"
   sguid=$(dpm-reservespace --poolname drain_pool_1 --gspace 32M --token_desc drain_space_1)

   pFoundDisk=0
   for i in `seq 11`; do 
      echo -n "Copying srm://${DPM_HOST}${DPNS_HOME}/drain_permanent_file_$i"
      lcg-cp --dst drain_space_1 /bin/bash srm://${DPM_HOST}${DPNS_HOME}/drain_permanent_file_$i
      server=$(get_firstrep_property "${DPNS_HOME}/drain_permanent_file_$i" "host")
      if [ "x$server" == "x$DISK_NODE" ]; then
         PISDISK[$i]=1
         echo " [disk]"
         pFoundDisk=1
      else
         PISDISK[$i]=0
         echo " [head]"
      fi
   done

   tFoundDisk=0
   for i in `seq 11`; do
      echo -n "Copying srm://${DPM_HOST}${DPNS_HOME}/drain_volatile_file_$i"
      lcg-cp --dst drain_space_1 /bin/bash srm://${DPM_HOST}${DPNS_HOME}/drain_volatile_file_$i
      server=$(get_firstrep_property "${DPNS_HOME}/drain_volatile_file_$i" "host")
      if [ "x$server" == "x$DISK_NODE" ]; then
         TISDISK[$i]=1
         echo " [disk]"
         tFoundDisk=1
      else
         TISDISK[$i]=0
         echo " [head]"
      fi
      make_replica_volatile "${DPNS_HOME}/drain_volatile_file_$i" 3600
   done

   eFoundDisk=0
   for i in `seq 11`; do
      echo -n "Copying srm://${DPM_HOST}${DPNS_HOME}/drain_expired_file_$i"
      lcg-cp --dst drain_space_1 /bin/bash srm://${DPM_HOST}${DPNS_HOME}/drain_expired_file_$i
      server=$(get_firstrep_property "${DPNS_HOME}/drain_expired_file_$i" "host")
      if [ "x$server" == "x$DISK_NODE" ]; then
         EISDISK[$i]=1
         echo " [disk]"
         eFoundDisk=1
      else
         EISDISK[$i]=0
         echo " [head]"
      fi
      make_replica_volatile "${DPNS_HOME}/drain_expired_file_$i" -3600
   done

   if [ $pFoundDisk -eq 0 ]; then
      echo "$testName:None of the permanent files are located on the disk node - unexpected:FAILURE"
      error=1
   fi

   if [ $tFoundDisk -eq 0 ]; then
      echo "$testName:None of the volatile files are located on the disk node - unexpected:FAILURE"
      error=1
   fi

   if [ $eFoundDisk -eq 0 ]; then
      echo "$testName:None of the expired files are located on the disk node - unexpected:FAILURE"
      error=1
   fi

   if [ $error -ne 1 ]; then

      DPM_HOST_EXEC "$DPM_HOST" "dpm-drain --server $DISK_NODE"
      rval=$?
      if [ $rval -ne 0 ]; then
         echo "$testName:dpm-drain returned $rval:FAILURE"
      else
         diskLeft=0
         spaceLost=0
         for i in `seq 11`; do
            server=$(get_firstrep_property "${DPNS_HOME}/drain_permanent_file_$i" "host")
            if [ "x$server" == "x$DISK_NODE" ]; then
               diskLeft=1
               break
            fi
            server=$(get_firstrep_property "${DPNS_HOME}/drain_volatile_file_$i" "host")
            if [ "x$server" == "x$DISK_NODE" ]; then
               diskLeft=1
               break
            fi
            setname=$(get_firstrep_property "${DPNS_HOME}/drain_permanent_file_$i" "setname")
            if [ "x$setname" != "x$sguid" ]; then
               spaceLost=1
               break
            fi
         done

         if [ $diskLeft -eq 1 ]; then
            echo "$testName:Some files not relocated to $DPM_HOST:FAILURE"
         elif [ $spaceLost -eq 1 ]; then
            echo "$testName:Some files not in the expected space any more:FAILURE"
         else
            echo "$testName:All files relocated to $DPM_HOST:SUCCESS"
         fi
      fi
   fi

   dpm2_remote_exec "$DPM_HOST" "python -c \"$python_enable_allfs\""

   for i in `seq 11`; do
      lcg-del -l srm://${DPM_HOST}${DPNS_HOME}/drain_permanent_file_$i
      lcg-del -l srm://${DPM_HOST}${DPNS_HOME}/drain_volatile_file_$i
      lcg-del -l srm://${DPM_HOST}${DPNS_HOME}/drain_expired_file_$i
   done

   dpm-releasespace --token_desc drain_space_1

   DPM_HOST_EXEC "$DPM_HOST" "dpm-rmfs --server $DPM_HOST --fs /drain_fs_1"
   DPM_HOST_EXEC "$DPM_HOST" "dpm-rmfs --server $DISK_NODE --fs /drain_fs_1"
   DPM_HOST_EXEC "$DPM_HOST" "rm -rf /drain_fs_1"
   DPM_HOST_EXEC "$DISK_NODE" "rm -rf /drain_fs_1"
   DPM_HOST_EXEC "$DPM_HOST" "dpm-rmpool --poolname drain_pool_1"
else
  echo "There is no disk node defined, so no test will be executed!"
fi

rm -f $STD_OUT
rm -f $STD_ERR

if [ $error -ne 0 ]; then
  TEST_FAILED
fi

TEST_PASSED

