#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_release_sna_dlc.sh 1.16 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1998,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)41  1.16  src/43haes/usr/sbin/cluster/events/utils/cl_release_sna_dlc.sh, hacmp.hawan, 61haes_r714 3/26/07 17:51:39

###############################################################################
#
#  Name:  cl_release_sna_dlc
#
#  This script is used to release Communications Server resources on the 
#  given interface to facilitate a successful swap_adapter event and to
#  allow SNA DLC profiles to be reconfigured.
#
# Arguments:    INTERFACE - Interface name (e.g. en0, tr0)
#
# Returns:      0 - Success
#               1 - Failure
#		2 - Usage error
#
# Called by: release_service_addr, release_takeover_addr
# Calls to: snaversion,mkdevname	
#
# Environment:  VERBOSE_LOGGING
###############################################################################

###############################################################################
#
# Name: mkdevname
#
#	Make a device name from an interface name. That is,  translate
#	en0 to ent0, tr0 to tok0, etc.
#
# Arguments: Interface Name
#
# Returns: 0 success
#	   1 failure
###############################################################################
mkdevname () {
	typeset PS4_FUNC="mkdevname"
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	TYPE=$(expr "$1" : "^\([a-z]*\)[0-9]*")
	NUM=$(expr "$1" : "^[a-z]*\([0-9]*\)")

	if [ -z "$NUM" ]
	then
		cl_log 530 "$PROGNAME: Invalid interface name." $PROGNAME
		return 1
	fi

	case "$TYPE" in
	"en"|"et")		# Ethernet
       		NAME="ent$NUM"
		;;
	"tr")			# token ring
       		NAME="tok$NUM"
		;;
	"fi")			# FDDI
       		NAME="fddi$NUM"
		;;
	"hdlc"|"twd")		# qllc (get the COMIO port name)
		NAME=$(lsx25 | awk "\$2 == \"$1\" { print \$4 }")
		NUM=$(expr "$NAME" : "^x25s\([0-9]*\)")
		NAME="comio${NUM}"
		;;
        *)                      # default
                NAME="$TYPE$NUM"
		;;
	esac
	echo $NAME
	return 0
}
###############################################################################

###############################################################################
#
# Main Starts Here
#
###############################################################################

PROGNAME=$(basename ${0})
export PATH="$($(dirname ${0})/../../utilities/cl_get_path all)"
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.16'
HA_DIR="$(cl_get_path)"

echo "Starting execution of $0 with parameters $*" $0 "$*"

set -u
if [ $# -lt 1 ] ; then
    cl_echo 4159 "Usage: $PROGNAME interface" $PROGNAME
    exit 2
fi

INTERFACES=$*

#Determine SNA version
ISSNA5=$(snaversion)
if (( $ISSNA5 == 2 )) ; then
   cl_echo 4151 "Communications Server not installed or unsupported version."
   exit 1
fi

for INTERFACE in $INTERFACES
do
    #SNA DLCs are changed at the device adapter level
    DEVICE=$(mkdevname $INTERFACE)
    if (($? != 0))
    then
	cl_log 533 "$PROGNAME: Unable to make device name for interface $INTERFACE." $PROGNAME $INTERFACE
	exit 1
    fi

    #query SNA for all SNA DLCs running on the specified device and
    #issue the command to stop them

    #Determine whether SNA is running
    #
    # if SNA v5, check return code  from snaadmin. then check uptime. 
    #
    if (( $ISSNA5 ))
    then
	#if SNA is not active then there is nothing to do
	snaadmin query_node
	if (($? != 0))
	then
	    cl_echo 4160 "Communications Server is not active."
	    exit 0
	fi

	UPTIME=$(snaadmin query_node | grep up_time | cut -f2 -d"=")
	if(( $UPTIME == 0))
	then
	    cl_echo 4160 "Communications Server is not active."
	    exit 0
	fi

	#set the dlc type according to the $DEVICE
	TYPEDEV=$(expr "$DEVICE" : "^\([a-z]*\)[0-9]*") 
	case $TYPEDEV in
	    ent)
		DLCTYPE="ETHERNET";;
	    tok)
		DLCTYPE="TR";;
	    fddi)
		DLCTYPE="FDDI";;
	    comio)
	    	DLCTYPE="X25";;
	esac

	#get a list of all the dlc profiles
	SNADLC=$(snaadmin query_dlc | grep dlc_name | \
		cut -f2 -d"=")

	for snadlc in $SNADLC
	do
	    #see if the dlc uses $DLCTYPE
	    DLC=$(snaadmin query_dlc, dlc_name=$snadlc | \
	    grep "$DLCTYPE")
	    if [[ -n $DLC ]]
	    then
		#retrieve the adapter number and make a device name
		ADP=$(snaadmin -d query_dlc, dlc_name=$snadlc | \
		    grep adapter_number | cut -f2 -d"=" | cut -c2-)
		if [[ -n $ADP ]]
		then
		    SNADEV=$TYPEDEV$ADP
		else
		    SNADEV=""
		fi
	    else
		SNADEV=""
	    fi

	    if [[ $SNADEV = $DEVICE ]]
	    then
		#stop the dlc, also stops ports, link stations
		#using that dlc
		snaadmin stop_dlc, dlc_name = $snadlc
		if (($? != 0))
		then
		    snaadmin stop_dlc, dlc_name = $snadlc, \
			stop_type = IMMEDIATE_STOP
		    if (($? != 0))
		    then
			cl_log 4161 "Unable to stop DLC $snadlc ." $snadlc
			exit 1
		    fi
		fi
	    fi
	done
    else
	# in SNA 4.x you cannot stop a DLC all by itself.
	# SNA must be stopped to be able to release a device
	# see if SNA is running
	#
	sna -d global
	if (($? != 0))
	then
	    cl_echo 4160 "Communications Server is not active."
	    exit 0
	fi

	#Test to see if there are any sessions active
	#If so, build the session recovery file
	#
	$(sna -d s | cut -c26-50 | sed /"^  *"/,/-/d | \
            sort > /tmp/sna.session)
	if [[ ! -s /tmp/sna.session ]]
	then
	    #There were no sessions
	    rm /tmp/sna.session
	fi

	#Test to see if there are any link stations active
	#If so, build the link station recovery file
	for l_s in $(lssnaobj -t link_station -U)
	do
	    sna -d l | grep $l_s
	    if (($? == 0))
	    then
		echo $l_s >> /tmp/sna.link_station
		sna -stop l -p $l_s
	    fi
	done
    
	#Stop SNA (normal stop)
	sna -stop sna -t n
	if (($? != 0))
	then
	    sna -stop sna -t c
	    if (($? != 0))
	    then
		cl_log 4156 "Unable to stop Communications Server."
		exit 1
	    fi
	fi
    fi
done

exit 0

