<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
	xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0"
	xsi:schemaLocation="
		http://www.osgi.org/xmlns/blueprint/v1.0.0 
		http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd

		http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0
		http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd

		http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0
		http://aries.apache.org/schemas/blueprint-ext/blueprint-ext-1.1.xsd
">

	<!-- Event IPC manager service -->
	<reference id="eventForwarder" interface="org.opennms.netmgt.events.api.EventForwarder" />

	<!-- Persistent DiscoveryConfiguration -->
	<reference id="discoveryFactory" interface="org.opennms.netmgt.config.api.DiscoveryConfigurationFactory"/>

	<bean id="queueNameFactory" class="org.opennms.core.camel.JmsQueueNameFactory">
		<argument value="Discovery"/>
		<argument value="Discoverer"/>
	</bean>

	<!-- Configuration properties -->
	<!-- 
		update-strategy="reload" causes unit test failures because the Blueprint context
		can be destroyed and recreated by CM properties updates while the Camel context
		is initializing:

		https://issues.apache.org/jira/browse/ARIES-961
	-->
	<cm:property-placeholder id="discoveryProperties" persistent-id="org.opennms.netmgt.discovery" update-strategy="none">
		<cm:default-properties>
			<!-- Expose some of the Discovery config params as property placeholders -->
			<cm:property name="restartSleepTime">
				<bean factory-ref="discoveryFactory" factory-method="getRestartSleepTime"/>
			</cm:property>
			<cm:property name="initialSleepTime">
				<bean factory-ref="discoveryFactory" factory-method="getInitialSleepTime"/>
			</cm:property>
		</cm:default-properties>
	</cm:property-placeholder>

	<!--
		TODO: Hook this filter into DAO/event channel so that it can provide an up-to-date
		list of managed IP addresses like the legacy Discovery process did.
	-->
	<bean id="unmanagedInterfaceFilter" class="org.opennms.netmgt.discovery.UnmanagedInterfaceFilter"/>

	<!-- This processor generates a list of DiscoveryJobs from a DiscoveryConfiguration -->
	<bean id="rangeChunker" class="org.opennms.netmgt.discovery.actors.RangeChunker">
		<property name="ipAddressFilter" ref="unmanagedInterfaceFilter"/>
	</bean>

	<!-- This processor sends newSuspect events for IP addresses reported in a DiscoveryResult -->
	<bean id="eventWriter" class="org.opennms.netmgt.discovery.actors.EventWriter">
		<argument ref="eventForwarder" />
	</bean>
	
	<!-- ActiveMQ component from OpenNMS deamon context -->
	<reference id="queuingservice" interface="org.apache.camel.Component" filter="(alias=onms.broker)"/>

	<camelContext id="discoveryCamelContext" xmlns="http://camel.apache.org/schema/blueprint">
		<propertyPlaceholder id="properties" location="blueprint:discoveryProperties" />

		<!-- Bootstrap timer -->
		<route id="startDiscoveryTimer">
			<from uri="timer:discoveryTimer?period={{restartSleepTime}}&amp;delay={{initialSleepTime}}&amp;fixedRate=true&amp;daemon=true" />
			<!-- Set the DiscoveryConfiguration as the message body -->
			<transform><simple>${bean:discoveryFactory.getConfiguration}</simple></transform>
			<!-- Submit it as a Discovery task -->
			<to uri="seda:submitDiscoveryTask" />
		</route>

		<route id="submitDiscoveryTask">
			<from uri="seda:submitDiscoveryTask" />
			<!-- 
				Make sure that this exchange is using an InOut exchange so that the output
				of each to: endpoint is passed to the next to: endpoint
			-->
			<bean ref="rangeChunker" />
			<split>
				<!-- Split the DiscoveryJobs -->
				<simple>${body}</simple>
				<!-- Calculate an appropriate JMS timeout based on the task -->
				<setHeader headerName="CamelJmsRequestTimeout">
					<simple>${body.calculateTaskTimeout}</simple>
				</setHeader>
				<!--
					Use recipientList so that we can have a dynamic 'to:' URI.
					Make sure that the exchange is using an InOut pattern so 
					that the output is passed to the next endpoint.
				-->
				<setExchangePattern pattern="InOut"/>
				<recipientList>
					<simple>queuingservice:${bean:queueNameFactory.getNameWithLocation(body.location)}</simple>
				</recipientList>
				<!-- Send the DiscoveryResults to the eventWriter -->
				<bean ref="eventWriter" />
			</split>
		</route>
	</camelContext>

</blueprint>
