/****************************************************************************
  Name          : mqa_track_tree_find_and_del

  Description   : This routine deletes a new group track element to the
                  client tree.

  Arguments     :
                  client_info : pointer to the mqa client.
                  group : the group name to delete from track list.

  Return Values : returns status of deletion, true if deleted, false otherwise

  Notes         : The caller takes the cb lock before calling this function

******************************************************************************/
bool mqa_track_tree_find_and_del(MQA_CLIENT_INFO *client_info, SaNameT *group)
{
	MQA_TRACK_INFO *track_info = NULL;
	uint32_t rc = NCSCC_RC_SUCCESS;
	TRACE_ENTER();

	track_info = (MQA_TRACK_INFO *)ncs_patricia_tree_get(&client_info->mqa_track_tree, (uint8_t *)group->value);

	if (!track_info)
		return false;

	if ((rc = ncs_patricia_tree_del(&client_info->mqa_track_tree, &track_info->patnode)) != NCSCC_RC_SUCCESS) {
		TRACE_2("Track Database Deregistration Failed");
		if (track_info->notificationBuffer.notification)
			m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
		m_MMGR_FREE_MQA_TRACK_INFO(track_info);
		return false;
	}

	if (track_info->notificationBuffer.notification)
		m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
	m_MMGR_FREE_MQA_TRACK_INFO(track_info);

	TRACE_LEAVE();
	return true;
}
/****************************************************************************
  Name          : mqa_track_tree_find_and_add

  Description   : This routine adds/searches the new group track element to the
                  client tree.

  Arguments     :
                  client_info : pointer to the mqa client.
                  hdl_id : the handle id.
                  flag   : true/false if true create the new node if node
                           doesn't exist.FALSE -> search for an existing node.

  Return Values : returns the MQA_TRACK_INFO node.

  Notes         : The caller takes the cb lock before calling this function

******************************************************************************/
MQA_TRACK_INFO *mqa_track_tree_find_and_add(MQA_CLIENT_INFO *client_info, SaNameT *group, bool flag)
{
	MQA_TRACK_INFO *track_info = NULL;
	uint32_t rc = NCSCC_RC_SUCCESS;
	TRACE_ENTER();

	track_info = (MQA_TRACK_INFO *)ncs_patricia_tree_get(&client_info->mqa_track_tree, (uint8_t *)group->value);

	if (flag == true) {
		/* create and allocate the memory */
		if (!track_info) {
			track_info = (MQA_TRACK_INFO *)m_MMGR_ALLOC_MQA_TRACK_INFO;
			if (!track_info) {
				TRACE_2("Track database creation failed");
				return NULL;
			}
			memset(track_info, 0, sizeof(MQA_TRACK_INFO));
			track_info->queueGroupName = *group;
			track_info->patnode.key_info = (uint8_t *)track_info->queueGroupName.value;
			if ((rc = ncs_patricia_tree_add(&client_info->mqa_track_tree,
							&track_info->patnode)) != NCSCC_RC_SUCCESS) {
				TRACE_2("Track Database Registration Failed");
				if (track_info->notificationBuffer.notification)
					m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
				m_MMGR_FREE_MQA_TRACK_INFO(track_info);
				return NULL;
			}

		}
	}
	TRACE_LEAVE();
	return track_info;

}
/****************************************************************************
  Name          : mqa_track_tree_find_and_add

  Description   : This routine adds/searches the new group track element to the
                  client tree.

  Arguments     :
                  client_info : pointer to the mqa client.
                  hdl_id : the handle id.
                  flag   : TRUE/FALSE if TRUE create the new node if node
                           doesn't exist.FALSE -> search for an existing node.

  Return Values : returns the MQA_TRACK_INFO node.

  Notes         : The caller takes the cb lock before calling this function

******************************************************************************/
MQA_TRACK_INFO *mqa_track_tree_find_and_add(MQA_CLIENT_INFO *client_info, SaNameT *group, NCS_BOOL flag)
{
	MQA_TRACK_INFO *track_info = NULL;
	uns32 rc = NCSCC_RC_SUCCESS;

	track_info = (MQA_TRACK_INFO *)ncs_patricia_tree_get(&client_info->mqa_track_tree, (uns8 *)group->value);

	if (flag == TRUE) {
		/* create and allocate the memory */
		if (!track_info) {
			track_info = (MQA_TRACK_INFO *)m_MMGR_ALLOC_MQA_TRACK_INFO;
			if (!track_info) {
				m_LOG_MQSV_A(MQA_TRACK_ALLOC_FAILED, NCSFL_LC_MQSV_INIT, NCSFL_SEV_ERROR, 0, __FILE__,
					     __LINE__);
				return NULL;
			}
			memset(track_info, 0, sizeof(MQA_TRACK_INFO));
			track_info->queueGroupName = *group;
			track_info->patnode.key_info = (uns8 *)track_info->queueGroupName.value;
			if ((rc = ncs_patricia_tree_add(&client_info->mqa_track_tree,
							&track_info->patnode)) != NCSCC_RC_SUCCESS) {
				m_LOG_MQSV_A(MQA_TRACK_TREE_ADD_FAILED, NCSFL_LC_MQSV_INIT, NCSFL_SEV_ERROR, rc,
					     __FILE__, __LINE__);
				if (track_info->notificationBuffer.notification)
					m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
				m_MMGR_FREE_MQA_TRACK_INFO(track_info);
				return NULL;
			}

		}
	}
	return track_info;

}
/****************************************************************************
  Name          : mqa_track_tree_find_and_del

  Description   : This routine deletes a new group track element to the
                  client tree.

  Arguments     :
                  client_info : pointer to the mqa client.
                  group : the group name to delete from track list.

  Return Values : returns status of deletion, TRUE if deleted, FALSE otherwise

  Notes         : The caller takes the cb lock before calling this function

******************************************************************************/
NCS_BOOL mqa_track_tree_find_and_del(MQA_CLIENT_INFO *client_info, SaNameT *group)
{
	MQA_TRACK_INFO *track_info = NULL;
	uns32 rc = NCSCC_RC_SUCCESS;

	track_info = (MQA_TRACK_INFO *)ncs_patricia_tree_get(&client_info->mqa_track_tree, (uns8 *)group->value);

	if (!track_info)
		return FALSE;

	if ((rc = ncs_patricia_tree_del(&client_info->mqa_track_tree, &track_info->patnode)) != NCSCC_RC_SUCCESS) {
		m_LOG_MQSV_A(MQA_TRACK_TREE_DEL_FAILED, NCSFL_LC_MQSV_INIT, NCSFL_SEV_ERROR, rc, __FILE__, __LINE__);
		if (track_info->notificationBuffer.notification)
			m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
		m_MMGR_FREE_MQA_TRACK_INFO(track_info);
		return FALSE;
	}

	if (track_info->notificationBuffer.notification)
		m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
	m_MMGR_FREE_MQA_TRACK_INFO(track_info);

	return TRUE;
}
/****************************************************************************
  Name          : mqa_client_tree_delete_node
 
  Description   : This routine adds the deletes the client from the client tree
 
  Arguments     : client_info - pointer to the client node.
                  
 
  Return Values : NCSCC_RC_FAILURE/NCSCC_RC_SUCCESS
 
  Notes         : None
******************************************************************************/
uint32_t mqa_client_tree_delete_node(MQA_CB *mqa_cb, MQA_CLIENT_INFO *client_info)
{
	MQA_TRACK_INFO *track_info;
	SaNameT *temp_ptr = 0;
	SaNameT temp_name;
	uint8_t *value = NULL;
	uint32_t rc = NCSCC_RC_SUCCESS;
	TRACE_ENTER();

	/* scan the entire group track db & delete each record */
	while ((track_info = (MQA_TRACK_INFO *)ncs_patricia_tree_getnext(&client_info->mqa_track_tree, value))) {
		/* delete the track info */
		temp_name = track_info->queueGroupName;
		temp_ptr = &temp_name;
		value = temp_ptr->value;
		/* delete from the tree */
		if ((rc = ncs_patricia_tree_del(&client_info->mqa_track_tree,
						&track_info->patnode)) != NCSCC_RC_SUCCESS)
			TRACE_2("Track Database Deregistration Failed");

		/* free the mem */
		if (track_info->notificationBuffer.notification)
			m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
		m_MMGR_FREE_MQA_TRACK_INFO(track_info);
	}

	/* destroy the tree */
	ncs_patricia_tree_destroy(&client_info->mqa_track_tree);

	/* delete from the tree */
	if ((rc = ncs_patricia_tree_del(&mqa_cb->mqa_client_tree, &client_info->patnode)) != NCSCC_RC_SUCCESS)
		TRACE_2("Client database Deregistration Failed");

	/* free the mem */
	m_MMGR_FREE_MQA_CLIENT_INFO(client_info);
	TRACE_LEAVE();
	return NCSCC_RC_SUCCESS;
}
/****************************************************************************
  Name          : mqa_client_tree_delete_node
 
  Description   : This routine adds the deletes the client from the client tree
 
  Arguments     : client_info - pointer to the client node.
                  
 
  Return Values : NCSCC_RC_FAILURE/NCSCC_RC_SUCCESS
 
  Notes         : None
******************************************************************************/
uns32 mqa_client_tree_delete_node(MQA_CB *mqa_cb, MQA_CLIENT_INFO *client_info)
{
	MQA_TRACK_INFO *track_info;
	SaNameT *temp_ptr = 0;
	SaNameT temp_name;
	uns8 *value = NULL;
	uns32 rc = NCSCC_RC_SUCCESS;
	/* scan the entire group track db & delete each record */
	while ((track_info = (MQA_TRACK_INFO *)ncs_patricia_tree_getnext(&client_info->mqa_track_tree, value))) {
		/* delete the track info */
		temp_name = track_info->queueGroupName;
		temp_ptr = &temp_name;
		value = temp_ptr->value;
		/* delete from the tree */
		if ((rc = ncs_patricia_tree_del(&client_info->mqa_track_tree,
						&track_info->patnode)) != NCSCC_RC_SUCCESS)
			m_LOG_MQSV_A(MQA_TRACK_TREE_DEL_FAILED, NCSFL_LC_MQSV_INIT, NCSFL_SEV_ERROR, rc, __FILE__,
				     __LINE__);

		/* free the mem */
		if (track_info->notificationBuffer.notification)
			m_MMGR_FREE_MQA_TRACK_BUFFER_INFO(track_info->notificationBuffer.notification);
		m_MMGR_FREE_MQA_TRACK_INFO(track_info);
	}

	/* destroy the tree */
	ncs_patricia_tree_destroy(&client_info->mqa_track_tree);

	/* delete from the tree */
	if ((rc = ncs_patricia_tree_del(&mqa_cb->mqa_client_tree, &client_info->patnode)) != NCSCC_RC_SUCCESS)
		m_LOG_MQSV_A(MQA_CLIENT_TREE_DEL_FAILED, NCSFL_LC_MQSV_INIT, NCSFL_SEV_ERROR, rc, __FILE__, __LINE__);

	/* free the mem */
	m_MMGR_FREE_MQA_CLIENT_INFO(client_info);
	return NCSCC_RC_SUCCESS;
}