#!/bin/sh

. ../common/ajax_common

ifNote="${PGETC}/remark/ifnote.conf"

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"
	LANG002="Success"
	LANG003="Set Interface Configure"
	LANG004="Set Interface Queue"
fi


show_interface()
{
	# name mode zone state driver mac type inbps outbps inpps outpps bgname speed peer ifgrp misspkt theothers

	${FLOWEYE} if list showsum=1 | awk \
	-v swgrp=${CGI_swgrp} \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if(swgrp != "" && swgrp != "swgrp"$18)
			next;
			
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"name\":\"%s\"", $(col++);
		printf ",\"mode\":\"%s\"", $(col++);
		printf ",\"zone\":\"%s\"", $(col++);
		printf ",\"state\":\"%s\"", $(col++);
		printf ",\"driver\":\"%s\"", $(col++);
		printf ",\"mac\":\"%s\"", $(col++);
		printf ",\"type\":\"%s\"", $(col++);
		printf ",\"inbps\":\"%s\"", $(col++);
		printf ",\"outbps\":\"%s\"", $(col++);
		printf ",\"inpps\":\"%s\"", $(col++);
		printf ",\"outpps\":\"%s\"", $(col++);
		printf ",\"bgname\":\"%s\"", $(col++);
		printf ",\"speed\":\"%s\"", $(col++);
		printf ",\"peer\":\"%s\"", $(col++);
		printf ",\"ifgrp\":\"%s\"", $(col++);
		printf ",\"misspkt\":\"%s\"", $(col++);
		printf ",\"vrstat\":\"%s\"", $(col++);
		printf ",\"swgrp\":\"%s\"", $(col++);
		printf "}";
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


show_console()
{
	who | grep ttyAMA1 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s", dot;
		printf "{";
		printf "\"name\":\"%s\"", $1;
		printf "}";
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


get_tap_ifbps()
{
	${FLOWEYE} swchip list port=1  | awk \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		if(NR == 1) next;

		printf "%s\"%s\":{", dot, $1;
		printf "\"inbps\":\"%s\"", $16;
		printf ",\"inpps\":\"%s\"", $17;
		printf ",\"outbps\":\"%s\"", $18;
		printf ",\"outpps\":\"%s\"", $19;
		printf "}";

		if(dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


load_if_list()
{
	printf "{"
	printf "\"list\":`show_interface`"

	if [ "${TAP}" = "1" ]; then
		switch_mode=`${FLOWEYE} swchip stat | grep switch_model | cut -d"=" -f2`
		if [ "${switch_mode}" = "NGTAP-PX8" ]; then
			printf ",\"iftap\":`get_tap_ifbps`"
		fi
	fi

	if [ "${CGI_reload}" = "1" ]; then
		printf "}"
		return
	fi

	printf ",\"iflacp\":`load_if_lacp`"
	printf ",\"note\":"
	
	if [ ! -f ${ifNote} ]; then
		printf "{}"
	else
		awk -F "=" \
		'BEGIN{
			dot = "";
			printf "{";
		}{
			printf "%s", dot;
			printf "\"%s\":\"%s\"", $1, $2;
			if ( dot == "") dot = ",";
		}END{
			printf "}";
		}' ${ifNote}
	fi
	printf ",\"console\":`show_console`"

	printf "}"
}


load_if_core()
{
	# flow cpu wait burst pps pktrejs theother
	${FLOWEYE} system list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if(NR == 1) next;
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"flow\":%s,", $(col++);
		printf "\"cpu\":%s,", $(col++);
		printf "\"wait\":%s,", $(col++);
		printf "\"burst\":%s,", $(col++);
		printf "\"pps\":%s,", $(col++);
		printf "\"pktrejs\":%s", $(col++);
		printf "}";
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


load_if_queue()
{
	# name queue autoflow setflow pps theother
	${FLOWEYE} ifmap list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"name\":\"%s\",", $(col++);
		printf "\"queue\":\"%s\",", $(col++);
		printf "\"autoflow1\":%s,", $(col++);
		printf "\"autoflow2\":%s,", $(col++);
		printf "\"pps\":%s,", $(col++);
		printf "\"setflow\":\"%s\"", $(col++);
		printf "}";
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


load_ifsch_env()
{
	printf "{"
	printf "\"core\":`load_if_core`,"
	printf "\"queue\":`load_if_queue`"
	printf "}"
}


get_bg_name()
{
	${FLOWEYE} policy listlink | sort -nk2 | awk \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		if($2 > 4) exit;

		printf "%s", dot;
		printf "\"bg_%s\":\"%s\"", $2, $1;
		if ( dot == "" ) dot = ",";
	}END{
		printf "}";
	}'
}


get_if_status()
{
	note=`grep "${CGI_if}=" ${ifNote} | cut -d"=" -f2`

	printf "{"
	printf "\"name\":\"${CGI_if}\""
	printf ",\"note\":\"${note}\""

	${FLOWEYE} if get ${CGI_if} | awk -F "=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'
	
	printf "}"
}


get_if_lacp_status()
{
	printf "{"
	printf "\"_none_\":\"_none\""
	
	${FLOWEYE} lacp get if=${CGI_if} | awk -F "=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'
	
	${FLOWEYE} lacp get lag=${CGI_id} | awk -F "=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'

	printf "}"
}


dump_status()
{
	port=`${FLOWEYE} if stat ${CGI_if} | grep port | cut -d"=" -f2`
	printf "{"
	printf "\"mtu1\":`mtu1_status`,"
	printf "\"mtu2\":`mtu2_status`,"
	printf "\"mtu2\":`mtu2_status`,"
	printf "\"statistics\":`statistics_status`,"
	printf "\"packet_counter\":`packet_counter`,"
	printf "\"contract_counter\":`contract_counter`"
	printf "}"
}


mtu1_status()
{
	tapeye ctcsdk get show stats mtu1-pkt-length port $port | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


mtu2_status()
{
	tapeye ctcsdk get show stats mtu2-pkt-length port $port | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


statistics_status()
{
	tapeye ctcsdk get show stats mac-all port $port | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


packet_counter()
{
	tapeye ctcsdk get show stats mac-rx port $port | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


contract_counter()
{
	tapeye ctcsdk get show stats mac-tx port $port | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


dump_if_status()
{
	${FLOWEYE} if stat ${CGI_if} | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		gsub("\\t", "    ", $0);
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


if_num_list()
{
	${FLOWEYE} if list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s", dot;
		printf "\"%s\"", $1;
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_if_lacp()
{
	dot=""
	lag=1
	lag_max=`${FLOWEYE} lacp stat | grep lag_poolsz | cut -d"=" -f2`

	printf "["

	while [ ${lag} -le ${lag_max} ]
	do
		printf "${dot}{"

		${FLOWEYE} lacp get lag=${lag} | awk -F"=" \
		'BEGIN{
			dot = "";	
		}{
			printf "%s\"lg_%s\":\"%s\"", dot, $1, $2;
			if(dot == "") dot = ",";
		}'

		printf "}"

		lag=$((${lag} + 1))
		[ "${dot}" = "" ] && dot=","
	done

	printf "]"
}


load_edit_env()
{
	max_lagroup=`${FLOWEYE} system stat | grep MAX_LAGROUP | cut -d"=" -f2`
	[ "${max_lagroup}" = "" ] && max_lagroup=6

	swgrp_num=`${FLOWEYE} if list showswgrp=1 | awk 'END{print NR}'`

	printf "{"
	printf "\"max_lagroup\":${max_lagroup}"
	printf ",\"info\":`get_if_status`"
	printf ",\"bgname\":`get_bg_name`"
	printf ",\"iflist\":`if_num_list`"
	printf ",\"iflacp\":`load_if_lacp`"
	printf ",\"swgrp_num\":${swgrp_num}"
	printf "}"
}


set_if_access()
{
	# ע
	mkdir -p "${PGETC}/remark"
	grep -v "${CGI_ifname}=" ${ifNote} > ${ifNote}.bak
	if [ "${CGI_note}" != "" ]; then
		echo "${CGI_ifname}=${CGI_note}" >> ${ifNote}.bak
	fi
	mv ${ifNote}.bak ${ifNote}

	# 
	if [ "${CGI_mode}" != "0" ]; then
		errmsg=`${FLOWEYE} policy setlink link=${CGI_mode} name="${CGI_bgname}"`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "policy setlink link=${CGI_mode} name=${CGI_bgname}"
	fi
	#  
	[ "${CGI_lagroup}" = "" ] && CGI_lagroup="0"
	[ "${CGI_mixmode}" = "on" ] && mixmode=1 || mixmode=0
	cmdargs="name=${CGI_ifname} mode=${CGI_mode} zone=${CGI_zone} lagroup=${CGI_lagroup} mixmode=${mixmode} swgrp=${CGI_swgrp}"
	[ "${CGI_mode}" != "0" ] && cmdargs="${cmdargs} peer=${CGI_peer}"
	
	errmsg=`${FLOWEYE} if set ${cmdargs}`
	[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
	sync_floweye "if set ${cmdargs}"
	WEB_LOGGER "${LANG003:=޸}" "${cmdargs}"

	# 
	if [ "${CGI_media}" != "" ]; then
		errmsg=`${FLOWEYE} em setspeed ${CGI_ifname} ${CGI_media}`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "em setspeed ${CGI_ifname} ${CGI_media}"
	fi

	# LACP
	if [ "${CGI_lagroup}" != "0" ]; then
		[ "${CGI_lg_passive}" = "on" ] && CGI_lg_passive=1 || CGI_lg_passive=0
		cmdargs="lag=${CGI_lagroup} enable=${CGI_lg_enable} timeout=${CGI_lg_timeout} passive=${CGI_lg_passive}"
		${FLOWEYE} lacp set ${cmdargs}
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "lacp set ${cmdargs}"
		WEB_LOGGER "LACP" "name=${CGI_ifname} ${cmdargs}"
	fi

	retjson 0 "${LANG002:=óɹ}"
}


load_if_rate()
{
	printf "["
	
	${FLOWEYE} dbifpxy get name=${CGI_if} type=in
	
	printf ","

	${FLOWEYE} dbifpxy get name=${CGI_if} type=out
	
	printf "]"
}


set_if_queue()
{
	for line in `echo ${CGI_data} | tr ";" " "`
	do
		core1=`echo ${line} | cut -d"_" -f1`
		core2=`echo ${line} | cut -d"_" -f2`
		queue=`echo ${line} | cut -d"_" -f3`
		ifname=`echo ${line} | cut -d"_" -f4`

		[ "${core2}" = "2" ] && core2=2
		cmdargs="name=${ifname} queue=${queue} flow=${core1} flow2=${core2}"
		errmsg=`${FLOWEYE} ifmap set ${cmdargs}`

		if [ $? -ne 0 ]; then
			[ "${errmsg}" = "NEXIST" ] && continue
			retjson 1 "${ifname}:${errmsg}"
		fi

		sync_flow "ifmap set ${cmdargs}"
	done

	WEB_LOGGER "${LANG004:=}" "${cmdargs}"
	retjson 0 "${LANG002:=óɹ}"
}


load_swgrp_mac_list()
{
	if [ "${CGI_name}" = "" ]; then
		printf "[]"
		return
	fi

	${FLOWEYE} swgrp list mac | awk \
	-v name="${CGI_name}" \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if(name != "swgrp"$1)
			next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"grpid\":\"%s\"", $(col++);
		printf ",\"vlan\":\"%s\"", $(col++);
		printf ",\"mac\":\"%s\"", $(col++);
		printf ",\"ip\":\"%s\"", $(col++);
		printf ",\"last\":\"%s\"", $(col++);
		printf ",\"portid\":\"%s\"", $(col++);
		printf ",\"port\":\"%s\"", $(col++);
		printf "}";
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


case "${CGI_action}" in

	"load_if_rate")
		retjson 0 "OK" "`load_if_rate`"
		;;
		
	"show_interface")
		retjson 0 "OK" "`show_interface`"
		;;

	"load_if_list")
		retjson 0 "OK" "`load_if_list`"
		;;
		
	"load_if_core")
		retjson 0 "OK" "`load_if_core`"
		;;
	
	"load_if_queue")
		retjson 0 "OK" "`load_if_queue`"
		;;

	"load_ifsch_env")
		retjson 0 "OK" "`load_ifsch_env`"
		;;

	"get_if_status")
		retjson 0 "OK" "`get_if_status`"
		;;

	"get_if_lacp_status")
		retjson 0 "OK" "`get_if_lacp_status`"
		;;

	"dump_if_status")
		retjson 0 "OK" "`dump_if_status`"
		;;

	"load_edit_env")
		retjson 0 "OK" "`load_edit_env`"
		;;

	"set_if_access")
		action_check
		set_if_access
		;;
		
	"set_if_queue")
		action_check
		set_if_queue
		;;
	"dump_status")
		action_check
		retjson 0 "OK" "`dump_status`"
		;;

	"load_swgrp_mac_list")
		retjson 0 "OK" "`load_swgrp_mac_list`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
