<?php
include("nmicommand.inc");		/* Include this when you need to process nmi command */

/*
 * Retieve the parameters from database and create the NMI "Set" command
 */
function create_sql_string_from_meta_table($db,$bssid,$objectid,$tablename,&$sqlstring, &$parameters, &$attribute_type, $debug)
{
	$name = explode("_",$tablename);
	$sqlstring = "SELECT Attribute_Name,Attribute_Type " .
		"FROM OMCR_Meta_Table WHERE Attribute_Type<>0 AND Object_Name='$name[1]'";
	if ($debug) echo "$sqlstring <BR>"; else echo ".";
	$res = mysql_query($sqlstring, $db) or
		die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	while ( $row = mysqli_fetch_row($res) )
	{
		list($parameters[],$attribute_type[]) = $row;
	}
	mysql_free_result($res);

	$sqlstring = "SELECT " . join(",",$parameters) . " FROM $tablename WHERE bssid=$bssid AND ObjectId='$objectid'";
}

/*
 * Create NMI Set Command for paramter download
 */
function create_nmi_command($db,$sqlstring,$parameters,$attribute_type, &$nmicommand,$debug)
{
	if ($debug) echo "$sqlstring <BR>"; else echo ".";
	$res = mysql_query($sqlstring, $db) or
		die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	$value = mysqli_fetch_row($res);
	mysql_free_result($res);
	$nmicommand = "set";
	for ($i=0;$i<sizeof($parameters);$i++)
	{
		if ($attribute_type[$i] == 1)
			$nmicommand .= " $parameters[$i]=$value[$i]";
		else if ($attribute_type[$i] == 2)
			$nmicommand .= " $parameters[$i]=\"$value[$i]\"";
		else if ($attribute_type[$i] == 3)
		{
			$param = explode("_",$parameters[$i]);
			$nmicommand .= " $param[0]=[$param[1]=$value[$i]]";
		}
	}
}

/*
 * Return 0 if success, otherwise -1
 */
function upload_parameters($db,$bssid,$debug)
{
	$resultcode = $responsecode = $response = null;
	$retval = 0;
	if ($debug) echo "==) oid BssFunc" . "<BR>"; else echo ".";
	$retval = send_nmicommand($db,$bssid,"oid BssFunc",$resultcode, $responsecode,$response,$debug);
	if (($retval != 0) || ($resultcode[0] != 0))
		return (-1);
	flush();
	if ($debug) echo "==) set DownLoadInProgress=1" . "<BR>"; else echo ".";
	$retval = send_nmicommand($db,$bssid,"set DownLoadInProgress=1",$resultcode, $responsecode,$response,$debug);
	if (($retval != 0) || ($resultcode[0] != 0))
		return (-1);
	flush();

	$objectlist = Array(
		"Card.0.10",
		"Bsc.0",
		"AIf.0",
		"ALinkSet.0.0",
		"ALink.0.0.0"
	);
	$tablenamelist = Array(
		"OMCR_ClockMod",
		"OMCR_Bsc",
		"OMCR_AIf",
		"OMCR_ALinkSet",
		"OMCR_ALink"
	);

	$sqlstring = "SELECT SUBSTRING_INDEX(ObjectId,'.',-1) FROM OMCR_BSSTREE WHERE bssid=$bssid" .
                     " AND Presence=1 AND ObjectId='ALink.0.0.%'" .
                     " AND ObjectId<>'ALink.0.0.0'";
	$res = mysql_query($sqlstring, $db) or
		die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
	while ((list($row) = mysqli_fetch_row($res)) != null)
	{
		if ($debug) echo "==) oid ALinkSet.0.0" . "<BR>"; else echo ".";
		$retval = send_nmicommand($db,$bssid,"oid ALinkSet.0.0",$resultcode, $responsecode,$response,$debug);
		if (($retval != 0) || ($resultcode[0] != 0))
			return (-1);
		flush();
		if ($debug) echo "==) create ALink $row" . "<BR>"; else echo ".";
		$retval = send_nmicommand($db,$bssid,"create ALink $row",$resultcode, $responsecode,$response,$debug);
		if (($retval != 0) || ($resultcode[0] != 0))
			return (-1);
		flush();
		$objectlist = array_merge($objectlist, Array ( "ALink.0.0.$row"));
		$tablenamelist = array_merge($tablenamelist, Array("OMCR_ALink"));
	}

	$sqlstring = "SELECT SUBSTRING_INDEX(ObjectId,'.',-1) FROM OMCR_BSSTREE WHERE bssid=$bssid" .
                     " AND Presence=1 AND ObjectId REGEXP 'Card.0.[1-8]$'";
	$res = mysql_query($sqlstring, $db) or
		die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
	$e1trmod_id_list = null;
	while ( $row = mysqli_fetch_row($res) )
	{
		list($e1trmod_id_list[]) = $row;
	}
	//echo "==) oid Shelf.0" . "<BR>";
	//$retval = send_nmicommand($db,$bssid,"oid Shelf.0",$resultcode, $responsecode,$response,$debug);
	//if (($retval != 0) || ($resultcode[0] != 0))
	//	return (-1);
	//flush();

	for ($i=0; $i<sizeof($e1trmod_id_list); $i++)
	{
		$objectlist = array_merge($objectlist,
			Array ( "Port.0.$e1trmod_id_list[$i].0",
				"Port.0.$e1trmod_id_list[$i].1")
		);
		$tablenamelist = array_merge($tablenamelist, Array("OMCR_E1port", "OMCR_E1port"));

		//echo "==) create E1trmod $e1trmod_id_list[$i]" . "<BR>";
		//$retval = send_nmicommand($db,$bssid,"create E1trmod $e1trmod_id_list[$i]",$resultcode, $responsecode,$response,$debug);
		//if (($retval != 0) || ($resultcode[0] != 0))
		//	return (-1);
		//flush();
	}

	for ($i=0; $i<sizeof($objectlist); $i++)
	{
		$parameters=$attribute_type=$nmicommand=null;

		$objectid = $objectlist[$i];
		$tablename = $tablenamelist[$i];
		if ($debug) echo "==) oid $objectid" . "<BR>"; else echo ".";
		$retval = send_nmicommand($db,$bssid,"oid $objectid",$resultcode, $responsecode,$response,$debug);
		if (($retval != 0) || ($resultcode[0] != 0))
			return (-1);
		flush();
		create_sql_string_from_meta_table(
			$db,$bssid,$objectid,$tablename,$sqlstring,$parameters,$attribute_type,$debug);
		create_nmi_command($db,$sqlstring,$parameters,$attribute_type,$nmicommand,$debug);
		if ($debug) echo "==) $nmicommand" . "<BR>"; else echo ".";
		$retval = send_nmicommand($db,$bssid,"$nmicommand",$resultcode, $responsecode,$response,$debug);
		if (($retval != 0) || ($resultcode[0] != 0))
			return (-1);
		flush();
	}

	/*********************************************
	 * For each BtsMgr, download parameters
	 *********************************************/
	$sqlstring = "SELECT SUBSTRING_INDEX(ObjectId,'.',-1) FROM OMCR_BSSTREE WHERE bssid=$bssid AND Presence=1 AND ObjectId Like 'BtsMgr.%'";
	if ($debug) echo "$sqlstring<BR>\n"; else echo ".";
	$res = mysql_query($sqlstring, $db) or
		die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

	$btsmgr_list=null;
	while ( $row = mysqli_fetch_row($res) )
	{
		list($btsmgr_list[]) = $row;
	}
	mysql_free_result($res);

	//for ($i=0; $i<sizeof($btsmgr_list); $i++)
	foreach ($btsmgr_list as $btsid)
	{
		//$btsid = $btsmgr_list[$i];

		$objectlist = Array(
			"AbisMgr.$btsid.0", "Bts.$btsid.0",
			"AdjHand.$btsid.0.0",  "AdjHand.$btsid.0.1",  "AdjHand.$btsid.0.2",  "AdjHand.$btsid.0.3",
			"AdjHand.$btsid.0.4",  "AdjHand.$btsid.0.5",  "AdjHand.$btsid.0.6",  "AdjHand.$btsid.0.7",
			"AdjHand.$btsid.0.8",  "AdjHand.$btsid.0.9",  "AdjHand.$btsid.0.10", "AdjHand.$btsid.0.11",
			"AdjHand.$btsid.0.12", "AdjHand.$btsid.0.13", "AdjHand.$btsid.0.14", "AdjHand.$btsid.0.15",
			"AdjHand.$btsid.0.16", "AdjHand.$btsid.0.17", "AdjHand.$btsid.0.18", "AdjHand.$btsid.0.19",
			"AdjHand.$btsid.0.20", "AdjHand.$btsid.0.21", "AdjHand.$btsid.0.22", "AdjHand.$btsid.0.23",
			"AdjHand.$btsid.0.24", "AdjHand.$btsid.0.25", "AdjHand.$btsid.0.26", "AdjHand.$btsid.0.27",
			"AdjHand.$btsid.0.28", "AdjHand.$btsid.0.29", "AdjHand.$btsid.0.30", "AdjHand.$btsid.0.31",
			"BtsMgr_Shelf.$btsid.0"
		);
		$tablenamelist = Array(
			"OMCR_AbisMgr", "OMCR_Bts",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand", "OMCR_AdjHand",
			"OMCR_Wavex"
		);

		if ($debug) echo "<HR>";
		if ($debug) echo "==) oid BssFunc" . "<BR>"; else echo ".";
		$retval = send_nmicommand($db,$bssid,"oid BssFunc",$resultcode, $responsecode,$response,$debug);
		if (($retval != 0) || ($resultcode[0] != 0))
			return (-1);
		flush();
		if ($debug) echo "==) create BtsMgr $btsid" . "<BR>"; else echo ".";
		$retval = send_nmicommand($db,$bssid,"create BtsMgr $btsid",$resultcode, $responsecode,$response,$debug);
		if (($retval != 0) || ($resultcode[0] != 0))
			return (-1);
		flush();
		//echo "==) oid BtsMgr_Shelf.$btsid.0" . "<BR>";
		//$retval = send_nmicommand($db,$bssid,"oid BtsMgr_Shelf.$btsid.0",$resultcode, $responsecode,$response,$debug);
		//if (($retval != 0) || ($resultcode[0] != 0))
		//	return (-1);
		//flush();


		/*************************
		 * Create E1TrMod
		 *************************/
		$sqlstring = "SELECT SUBSTRING_INDEX(ObjectId,'.',-1) FROM OMCR_BSSTREE WHERE bssid=$bssid" .
                             " AND Presence=1 AND ObjectId REGEXP 'BtsMgr_Shelf_Card.$btsid.0.[1-8]$'";
		$res = mysql_query($sqlstring, $db) or
			die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
		$card_id_list = null;
		while ( $row = mysqli_fetch_row($res) )
		{
			list($card_id_list) = $row;
		}
		mysql_free_result($res);

		for ($j=0; $j<sizeof($card_id_list); $j++)
		{
			if ($debug) echo "==) oid BtsMgr_Shelf.$btsid.0" . "<BR>"; else echo "<BR>.";
			$retval = send_nmicommand($db,$bssid,"oid BtsMgr_Shelf.$btsid.0",$resultcode, $responsecode,$response,$debug);
			if (($retval != 0) || ($resultcode[0] != 0))
				return (-1);
			flush();
			if ($card_id_list[$j] == 1)
			{
				$objectlist = array_merge($objectlist,
					Array ( "BtsMgr_Shelf_Card_Port.$btsid.0.$card_id_list[$j].0",
						"BtsMgr_Shelf_Card_Port.$btsid.0.$card_id_list[$j].1")
				);
				$tablenamelist = array_merge($tablenamelist, Array("OMCR_E1port", "OMCR_E1port"));

				if ($debug) echo "==) create E1trmod $card_id_list[$j]" . "<BR>"; else echo ".";
				$retval = send_nmicommand($db,$bssid,"create E1trmod $card_id_list[$j]",$resultcode, $responsecode,$response,$debug);
			}
			else
			{
				$objectlist = array_merge($objectlist,
					Array ( "Trx.$btsid.0.$card_id_list[$j]",
						"RCarrier.$btsid.0.$card_id_list[$j]",
						"TrxCh.$btsid.0.$card_id_list[$j].0",
						"TrxCh.$btsid.0.$card_id_list[$j].1",
						"TrxCh.$btsid.0.$card_id_list[$j].2",
						"TrxCh.$btsid.0.$card_id_list[$j].3",
						"TrxCh.$btsid.0.$card_id_list[$j].4",
						"TrxCh.$btsid.0.$card_id_list[$j].5",
						"TrxCh.$btsid.0.$card_id_list[$j].6",
						"TrxCh.$btsid.0.$card_id_list[$j].7")
				);
				$tablenamelist = array_merge($tablenamelist,
					Array ( "OMCR_Trx", "OMCR_RCarrier",
						"OMCR_TrxCh","OMCR_TrxCh",
						"OMCR_TrxCh","OMCR_TrxCh",
						"OMCR_TrxCh","OMCR_TrxCh",
						"OMCR_TrxCh","OMCR_TrxCh")
				);

				if ($debug) echo "==) create TrxMod $card_id_list[$j]" . "<BR>"; else echo ".";
				$retval = send_nmicommand($db,$bssid,"create TrxMod $card_id_list[$j]",$resultcode, $responsecode,$response,$debug);
			}
			if (($retval != 0) || ($resultcode[0] != 0))
				return (-1);
			flush();
		}

		for ($j=0; $j<sizeof($objectlist); $j++)
		{
			$parameters=$attribute_type=$nmicommand=null;

			$objectid = $objectlist[$j];
			$tablename = $tablenamelist[$j];
			if ($debug) echo "==) oid $objectid" . "<BR>"; else echo ".";
			$retval = send_nmicommand($db,$bssid,"oid $objectid",$resultcode, $responsecode,$response,$debug);
			if (($retval != 0) || ($resultcode[0] != 0))
				return (-1);
			flush();
			create_sql_string_from_meta_table(
				$db,$bssid,$objectid,$tablename,$sqlstring,$parameters,$attribute_type,$debug);
			create_nmi_command($db,$sqlstring,$parameters,$attribute_type,$nmicommand,$debug);
			if ($debug) echo "==) $nmicommand" . "<BR>"; else echo ".";
			$retval = send_nmicommand($db,$bssid,"$nmicommand",$resultcode, $responsecode,$response,$debug);
			if (($retval != 0) || ($resultcode[0] != 0))
				return (-1);
			flush();
		}
	}
	if ($debug) echo "==) oid BssFunc" . "<BR>"; else echo ".";
	$retval = send_nmicommand($db,$bssid,"oid BssFunc",$resultcode, $responsecode,$response,$debug);
	if (($retval != 0) || ($resultcode[0] != 0))
		return -1;
	flush();
	if ($debug) echo "==) set DownLoadInProgress=0" . "<BR>"; else echo ".";
	$retval = send_nmicommand($db,$bssid,"set DownLoadInProgress=0",$resultcode, $responsecode,$response,$debug);
	if (($retval != 0) || ($resultcode[0] != 0))
		return -1;
	flush();

	return 0;
}

?>
