#! /bin/sh
#
# (C) Copyright 2009 The OpenSAF Foundation
#
# 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. This file and program are licensed
# under the GNU Lesser General Public License Version 2.1, February 1999.
# The complete license can be accessed from the following location:
# http://opensource.org/licenses/lgpl-license.php
# See the Copying file included with the OpenSAF distribution for full
# licensing terms.
#
# Author(s): Ericsson
#

usage ()
{
	echo ""
	echo "usage: `basename $0` <class name> [ancestor DN"]
	echo ""
	echo "	class name: <app|comp|csi|csiass|node|sg|si|siass|su|
			|appbasetype|apptype|csbasetype|csiattr|cstype|cluster|
			|compbasetype|compcstype|compglobalattr|comptype|ctcstype|
			|hc|hctype|ng|nodeswbundle|sgbasetype|sgtype|sidep|sirankedsu|
			|subasetype|sutype|sutcomptype|sibasetype|sitype|sitypecstype>"
	echo "	ancestor DN: display objects of <class name> having DN as ancestor." 
}


CLASSNAME=""
ANCESTOR=$2
ANCESTOR_VALID=`immfind $2 2> /dev/null`
case "$1" in
	app)
		CLASSNAME="SaAmfApplication"
		;;
	comp)
		CLASSNAME="SaAmfComp"
		;;
	csi)
		CLASSNAME="SaAmfCSI"
		;;
	csiass)
		CLASSNAME="SaAmfCSIAssignment"
		;;
	node)
		CLASSNAME="SaAmfNode"
		;;
	sg)
		CLASSNAME="SaAmfSG"
		;;
	si)
		CLASSNAME="SaAmfSI"
		;;
	siass)
		CLASSNAME="SaAmfSIAssignment"
		;;
	su)
		CLASSNAME="SaAmfSU"
		;;
	appbasetype)
		CLASSNAME="SaAmfAppBaseType"
		;;
	apptype)
		CLASSNAME="SaAmfAppType"
		;;
	csbasetype)
		CLASSNAME="SaAmfCSBaseType"
		;;
	csiattr)
		CLASSNAME="SaAmfCSIAttribute"
		;;
	cstype)
		CLASSNAME="SaAmfCSType"
		;;
	cluster)
		CLASSNAME="SaAmfCluster"
		;;
	compbasetype)
		CLASSNAME="SaAmfCompBaseType"
		;;
	compcstype)
		CLASSNAME="SaAmfCompCsType"
		;;
	compglobalattr)
		CLASSNAME="SaAmfCompGlobalAttributes"
		;;
	comptype)
		CLASSNAME="SaAmfCompType"
		;;
	ctcstype)
		CLASSNAME="SaAmfCtCsType"
		;;
	hc)
		CLASSNAME="SaAmfHealthcheck"
		;;
	hctype)
		CLASSNAME="SaAmfHealthcheckType"
		;;
	ng)
		CLASSNAME="SaAmfNodeGroup"
		;;
	nodeswbundle)
		CLASSNAME="SaAmfNodeSwBundle"
		;;
	sgbasetype)
		CLASSNAME="SaAmfSGBaseType"
		;;
	sgtype)
		CLASSNAME="SaAmfSGType"
		;;
	sidep)
		CLASSNAME="SaAmfSIDependency"
		;;
	sirankedsu)
		CLASSNAME="SaAmfSIRankedSU"
		;;
	subasetype)
		CLASSNAME="SaAmfSUBaseType"
		;;
	sutype)
		CLASSNAME="SaAmfSUType"
		;;
	sutcomptype)
		CLASSNAME="SaAmfSutCompType"
		;;
	sibasetype)
		CLASSNAME="SaAmfSvcBaseType"
		;;
	sitype)
		CLASSNAME="SaAmfSvcType"
		;;
	sitypecstype)
		CLASSNAME="SaAmfSvcTypeCSTypes"
		;;
esac


if [ $# -eq 0 ]; then
	usage
	exit 1
elif  [ "$CLASSNAME" = "" ]; then 
	echo "Class name incorrect."
	exit 1
elif [ $# -eq 2 ]; then
	#TODO: Even if ancestor DN is valid, check if AMF implements it.
	if [ "$ANCESTOR_VALID" = "" ]; then
		echo "Invalid DN: "$2" does not exist in the model."
		exit 1
	fi
fi

if [ $# -eq 1 ]; then
	immfind -c $CLASSNAME
elif [ $# -eq 2 ]; then
	immfind -c $CLASSNAME | grep $ANCESTOR
fi

exit $?

