/*
	This file defines the default set of map-statments for use in mapping known
	information from the database to terms in policy-statements.  Map-statements
	should be used only when neccessay as there is a small performance penalty
	for database requests which can be greatly magnified by a large data set.
	Additionally, care should be taken when engineering new map-statements; the
	possibility of a severe performance hit exists if a poor SQL statement is
	generated by the map-statement.  Gnerally speaking, only devolpers and those
	integrating SellaNMS into their OSS infrastructure or other product need
	concern themselves with writing map-statements.

	Default set of map-statements:

*/

policy-options {
	map-statement SOURCE_TO_ELEMENT_DATA_MAP {
		term GET_ELEMENT_ID {
			from {
				defined source;
				defined-except element-id;
				query "SELECT element.id AS element_id FROM element, interface, ip, ip_data WHERE element.id = interface.element AND interface.id = ip.interface AND ip_data.ip = ip.id AND ip_data.value = '@source@' LIMIT 1";
			}
			then {
				set {
					element-id	@element_id@;
				}
			}
		}
		/* Alternative method when the loopback interface is missing from the MIB (eg: Netscreen) */
		term ALTERNATIVE_GET_ELEMENT_ID {
			from {
				defined source;
				defined-exclude element-id;
				query "SELECT element.id AS element_id FROM element WHERE element.netid = '@source@' LIMIT 1";
			}
			then {
				set {
					element-id		@element_id@;
				}
			}
		}
		term GET_ELEMENT_DATA {
			from {
				defined element-id;
				query "SELECT variable, value FROM element_data WHERE element_data.element = '@element-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
	}

	map-statement IP_TO_DATA_MAP {
		term GET_IDS {
			from {
				defined ip;
				defined-exclude [ element-id interface-id ip-id ];
				query "SELECT element.id AS element_id, interface.id AS interface_id, ip.id AS ip_id FROM element, interface, ip, ip_data WHERE element.id = interface.element AND interface.id = ip.interface AND ip_data.ip = ip.id AND ip_data.value = '@ip@' LIMIT 1";
			}
			then {
				set {
					element-id		@element_id@;
					interface-id	@interface_id@;
					ip-id				@ip_id@;
				}
			}
		}
		term GET_ELEMENT_DATA {
			from {
				defined element-id;
				query "SELECT variable, value FROM element_data WHERE element_data.element = '@element-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
		term GET_INTERFACE_DATA {
			from {
				defined interface-id;
				query "SELECT variable, value FROM interface_data WHERE interface_data.interface = '@interface-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
		term GET_IP_DATA {
			from {
				defined ip-id;
				query "SELECT variable, value FROM ip_data WHERE ip_data.ip = '@ip-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
	}

	map-statement SOURCE_IFNAME_TO_DATA_MAP {
		term GET_ELEMENT_ID {
			from {
				defined source;
				defined-except element-id;
				query "SELECT element.id AS element_id FROM element, interface, ip, ip_data WHERE element.id = interface.element AND interface.id = ip.interface AND ip_data.ip = ip.id AND ip_data.value = '@source@' LIMIT 1";
			}
			then {
				set {
					element-id	@element_id@;
				}
			}
		}
		term GET_ELEMENT_DATA {
			from {
				defined element-id;
				query "SELECT variable, value FROM element_data WHERE element_data.element = '@element-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
		term GET_INTERFACE_ID {
			from {
				defined [ snmp:ifName element-id ];
				defined-except interface-id;
				query "SELECT interface.id AS interface_id FROM element, interface, interface_data WHERE element.id = interface.element AND interface.id = interface_data.interface AND interface_data.value = '@snmp:ifName@' AND element.id = '@element-id@' LIMIT 1";
			}
			then {
				set {
					interface-id @interface_id@;
				}
			}
		}
		term GET_INTERFACE_DATA {
			from {
				defined interface-id;
				query "SELECT variable, value FROM interface_data WHERE interface_data.interface = '@interface-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
		term GET_IP_ID {
			from {
				defined interface-id;
				defined-except ip-id;
				query "SELECT id AS ip_id FROM ip WHERE ip.interface = '@interface-id@' LIMIT 1";
			}
			then {
				set {
					ip-id @ip_id@;
				}
			}
		}
		term GET_IP_DATA {
			from {
				defined ip-id;
				query "SELECT variable, value FROM ip_data WHERE ip_data.ip = '@ip-id@'";
			}
			then {
				set {
					@variable@ @value@;
				}
			}
		}
		term SET_IP {
			from {
				defined snmp:ipAdEntAddr;
				defined-except ip;
			}
			then {
				set {
					ip	"@snmp:ipAdEntAddr@";
				}
			}
		}
		term SET_DEFAULT_IP {
			from defined-except ip;
			then {
				set {
					ip "-";
				}
			}
		}
	}

	map-statement SOURCE_IFNAME_TO_CONNECTED_MAP {
		term GET_ELEMENT_ID {
			from {
				defined source;
				defined-except element-id;
				query "SELECT element.id AS element_id FROM element, interface, ip, ip_data WHERE element.id = interface.element AND interface.id = ip.interface AND ip_data.ip = ip.id AND ip_data.value = '@source@' LIMIT 1";
			}
			then {
				set {
					element-id	@element_id@;
				}
			}
		}
		term GET_INTERFACE_ID {
			from {
				defined [ snmp:ifName element-id ];
				defined-except interface-id;
				query "SELECT interface.id AS interface_id FROM element, interface, interface_data WHERE element.id = interface.element AND interface.id = interface_data.interface AND interface_data.value = '@snmp:ifName@' AND element.id = '@element-id@' LIMIT 1";
			}
			then {
				set {
					interface-id @interface_id@;
				}
			}
		}
		term GET_IP_ID {
			from {
				defined interface-id;
				defined-except point;
				query "SELECT ip.id AS ip_id FROM ip, ip_data WHERE ip.id = ip_data.ip AND ip.interface = '@interface-id@' LIMIT 1";
			}
			then {
				set {
					/* We don't set 'ip-id' to keep the snmptrap from being assigned to the 'ip'. */
					point @ip_id@;
				}
			}
		}
		term DEFAULT_CONNECTED {
			from defined-except connection-id;
			then set connected false;
		}
		term SET_CONNECTED {
			from {
				defined point;
				defined-except connection-id;
				query "SELECT connection FROM connection_point WHERE point = '@point@' LIMIT 1";
			}
			then {
				set {
					connected		true;
					connection-id	@connection@;
				}
			}
		}
	}

	map-statement IP_TO_CONNECTED_MAP {
		term GET_IP_ID {
			from {
				defined ip;
				defined-except ip-id;
				query	"SELECT ip AS ip_id FROM ip_data WHERE variable = '.1.3.6.1.2.1.4.20.1.1' AND value = '@ip@' LIMIT 1";
			}
			then {
				set {
					ip-id	@ip_id@;
				}
			}
		}
		term DEFAULT_CONNECTED {
			from defined-except connection-id;
			then set connected false;
		}
		term SET_CONNECTED {
			from {
				defined ip-id;
				defined-except connection-id;
				query "SELECT connection FROM connection_point WHERE point = '@ip-id@' LIMIT 1";
			}
			then {
				set {
					connected		true;
					connection-id	@connection@;
				}
			}
		}
	}

	map-statement BGP_PEER_REMOTE_ADDR_TO_ASN_DATA_MAP {
		term GET_ELEMENT_ID {
			from {
				defined source;
				defined-except element-id;
				query "SELECT element.id AS element_id FROM element, interface, ip, ip_data WHERE element.id = interface.element AND interface.id = ip.interface AND ip_data.ip = ip.id AND ip_data.value = '@source@' LIMIT 1";
			}
			then {
				set {
					element-id	@element_id@;
				}
			}
		}
		term GET_ELEMENT_DATA {
			from {
				defined element-id;
				query "SELECT variable, value FROM element_data WHERE element_data.element = '@element-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
		term GET_LOCAL_ASN {
			from {
				defined [ element-id ];
				defined-except local-asn;
				query "SELECT value AS local_asn FROM element_data WHERE element = '@element-id@' AND variable = 'snmp:bgpLocalAs.0'";
			}
			then {
				set {
					local-asn @local_asn@;
				}
			}
		}
		term GET_PEER_ASN {
			from {
				defined [ element-id snmp:bgpPeerRemoteAs.@snmp:bgpPeerRemoteAddr@ ];
				defined-except peer-asn;
				query "SELECT value AS peer_asn FROM element_data WHERE element = '@element-id@' AND variable = 'snmp:bgpPeerRemoteAs.@snmp:bgpPeerRemoteAddr@'";
			}
			then {
				set {
					peer-asn @peer_asn@;
				}
			}
		}
		term GET_PEER_STATE {
			from {
				defined [ element-id ];
				defined [ element-id snmp:bgpPeerState.@snmp:bgpPeerRemoteAddr@ ];
				defined-except peer-state;
				query "SELECT value AS peer_state FROM element_data WHERE element = '@element-id@' AND variable = 'snmp:bgpPeerState.@snmp:bgpPeerRemoteAddr@'";
			}
			then {
				set {
					peer-state @peer_state@;
				}
			}
		}
		term GET_PEER_ADMIN_STATUS {
			from {
				defined [ element-id snmp:bgpPeerAdminStatus.@snmp:bgpPeerRemoteAddr@ ];
				defined-except peer-admin-status;
				query "SELECT value AS peer_admin_status FROM element_data WHERE element = '@element-id@' AND variable = 'snmp:bgpPeerAdminStatus.@snmp:bgpPeerRemoteAddr@'";
			}
			then {
				set {
					peer-admin-status @peer_admin_status@;
				}
			}
		}
		term GET_PEER_VERSION {
			from {
				defined [ element-id snmp:bgpPeerNegotiatedVersion.@snmp:bgpPeerRemoteAddr@ ];
				defined-except peer-version;
				query "SELECT value AS peer_version FROM element_data WHERE element = '@element-id@' AND variable = 'snmp:bgpPeerNegotiatedVersion.@snmp:bgpPeerRemoteAddr@'";
			}
			then {
				set {
					peer-version @peer_version@;
				}
			}
		}
		term GET_PEER_ASN_INFO {
			from {
				defined [ peer-asn ];
				defined-except [ peer-name peer-email peer-phone ];
				query "SELECT name, email, phone FROM asn_data WHERE asn = '@peer-asn@'";
			}
			then {
				set {
					peer-name @name@;
					peer-email @email@;
					peer-phone @phone@;
				}
			}
		}
		term DEFAULT_PEER_ASN {
			from defined-except peer-asn;
			then set peer-asn "unknown";
		}
		term DEFAULT_PEER_NAME {
			from defined-except peer-name;
			then set peer-name "unknown";
		}
		term DEFAULT_PEER_EMAIL {
			from defined-except peer-email;
			then set peer-email "unknown";
		}
		term DEFAULT_PEER_PHONE {
			from defined-except peer-phone;
			then set peer-phone "unknown";
		}
	}

	map-statement ELEMENT_ID_TO_ELEMENT_DATA_MAP {
		term GET_ELEMENT_DATA {
			from {
				defined element-id;
				query "SELECT variable, value FROM element_data WHERE element_data.element = '@element-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
	}

	map-statement INTERFACE_ID_TO_INTERFACE_DATA_MAP {
		term GET_INTERFACE_DATA {
			from {
				defined interface-id;
				query "SELECT variable, value FROM interface_data WHERE interface_data.interface = '@interface-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
	}

	map-statement IP_ID_TO_IP_DATA_MAP {
		term GET_IP_DATA {
			from {
				defined ip-id;
				query "SELECT variable, value FROM ip_data WHERE ip_data.ip = '@ip-id@'";
			}
			then {
				set {
					@variable@	@value@;
				}
			}
		}
	}
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
** vim: syntax=sella_nms
*/
