/*
** SellaNMS
** $Id: policy_statement_then_output.cxx,v 1.12 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static const char rcsid[] = "$Id: policy_statement_then_output.cxx,v 1.12 2006/01/24 07:08:09 sella Exp $";

#include "policy_statement_then_output.h"
#include "common/snmp_util.h"
#include <stdio.h>

PolicyStatementThenOutput::PolicyStatementThenOutput() {
}

ACTION PolicyStatementThenOutput::evaluate(variable_binding_t &v) {
	StorageData	data;
	string		value, sql;
	
	sql = "INSERT INTO output (element,interface,ip,source,module,protocol,channel,state,brief,detail) VALUES (";

	value = this->element;
	this->replaceVariables(v, value);
	if (strtoul(value.c_str(), NULL, 10) == 0) { 
		sql.append("NULL,");
	} else {
		sql.append(value + ",");
	}

	value = this->interface;
	this->replaceVariables(v, value);
	if (strtoul(value.c_str(), NULL, 10) == 0) { 
		sql.append("NULL,");
	} else {
		sql.append(value + ",");
	}

	value = this->ip;
	this->replaceVariables(v, value);
	if (strtoul(value.c_str(), NULL, 10) == 0) { 
		sql.append("NULL,");
	} else {
		sql.append(value + ",");
	}

	value = this->source;
	this->replaceVariables(v, value);
	if (value.compare("@source@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ",");

	value = this->module;
	this->replaceVariables(v, value);
	if (value.compare("@module@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ",");

	value = this->protocol;
	this->replaceVariables(v, value);
	if (value.compare("@protocol@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ",");

	value = this->channel;
	this->replaceVariables(v, value);
	if (value.compare("@channel@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ",");

	value = this->state;
	this->replaceVariables(v, value);
	if (value.compare("@state@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ",");

	value = this->brief;
	this->replaceVariables(v, value);
	if (value.compare("@brief@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ",");

	value = this->detail;
	this->replaceVariables(v, value);
	if (value.compare("@detail@") == 0) {
		value.erase();
	}
	sql.append(data.quoteString(value) + ")");

	if (this->storage->query(data, sql.c_str()) != SUCCESS) {
		return ACTION_FAILURE;
	}

	return ACTION_NEXT_TERM;
}

RESULT PolicyStatementThenOutput::setName(string &module) {
	this->module = module;

	return SUCCESS;
}

RESULT PolicyStatementThenOutput::compile(ConfigNode *config) {
	RESULT result = SUCCESS;

	string	value;
	
	if (config->assignValue("element", value)) {
		resolveMIBObject(value);

		this->element = value;
	} else {
		this->element = "@element-id@";
	}
	if (config->assignValue("interface", value)) {
		resolveMIBObject(value);

		this->interface = value;
	} else {
		this->interface = "@interface-id@";
	}
	if (config->assignValue("ip", value)) {
		resolveMIBObject(value);

		this->ip = value;
	} else {
		this->ip = "@ip-id@";
	}
	if (config->assignValue("source", value)) {
		resolveMIBObject(value);

		this->source = value;
	} else {
		this->source = "@source@";
	}
	if (config->assignValue("protocol", value)) {
		resolveMIBObject(value);

		this->protocol = value;
	} else {
		this->protocol = "@protocol@";
	}
	if (config->assignValue("channel", value)) {
		resolveMIBObject(value);

		this->channel = value;
	} else {
		this->channel = "@channel@";
	}
	if (config->assignValue("state", value)) {
		resolveMIBObject(value);

		this->state = value;
	} else {
		this->state = "@state@";
	}
	if (config->assignValue("brief", value)) {
		resolveMIBObject(value);

		this->brief = value;
	} else {
		this->brief = "@brief@";
	}
	if (config->assignValue("detail", value)) {
		resolveMIBObject(value);

		this->detail = value;
	} else {
		this->detail = "@detail@";
	}
	return result;
}

RESULT PolicyStatementThenOutput::clear() {
	this->element.erase();
	this->interface.erase();
	this->ip.erase();

	this->source.erase();
	this->module.erase();
	this->protocol.erase();
	this->channel.erase();
	this->state.erase();
	this->brief.erase();
	this->detail.erase();

	return SUCCESS;
}

RESULT PolicyStatementThenOutput::print(unsigned long depth) {
	string	tab = "";
	for (unsigned long i = 0; i < depth; i++) {
		tab.append("   ");
	}

	LMSG("%s%s {", tab.c_str(), this->module.c_str());
	LMSG("%s   element   '%s';", tab.c_str(), this->element.c_str());
	LMSG("%s   interface '%s';", tab.c_str(), this->interface.c_str());
	LMSG("%s   ip        '%s';", tab.c_str(), this->ip.c_str());
	LMSG("");
	LMSG("%s   source    '%s';", tab.c_str(), this->source.c_str());
	LMSG("%s   module    '%s';", tab.c_str(), this->module.c_str());
	LMSG("%s   protocol  '%s';", tab.c_str(), this->protocol.c_str());
	LMSG("%s   state     '%s';", tab.c_str(), this->state.c_str());
	LMSG("%s   channel   '%s';", tab.c_str(), this->channel.c_str());
	LMSG("");
	LMSG("%s   brief     '%s';", tab.c_str(), this->brief.c_str());
	LMSG("%s   detail    '%s';", tab.c_str(), this->detail.c_str());
	LMSG("%s}", tab.c_str());
	
	return SUCCESS;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3 sts=3
*/
