/**
 * 
 */
package com.boundary.sdk.event.service.port;

import static com.boundary.sdk.event.service.ServiceCheckPropertyNames.SERVICE_TEST_INSTANCE;

import java.io.IOException;
import java.util.Properties;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boundary.camel.component.common.ServiceStatus;
import com.boundary.camel.component.ping.PingInfo;
import com.boundary.camel.component.port.PortInfo;
import com.boundary.sdk.event.RawEvent;
import com.boundary.sdk.event.Severity;
import com.boundary.sdk.event.Status;
import com.boundary.sdk.event.service.ServiceTest;


/**
 * Responsible for translating a {@link PortInfo} to {@link com.boundary.sdk.event.RawEvent}.
 * 
 * @author davidg
 * 
 */
public class PortInfoToEventProcessor implements Processor {

	private static Logger LOG = LoggerFactory.getLogger(PortInfoToEventProcessor.class);

	public PortInfoToEventProcessor() {
	}
	
	@Override
	public void process(Exchange exchange) throws Exception {
		portInfoToRawEvent(exchange);
	}
	
	public void portInfoToRawEvent(Exchange exchange) throws Exception {
		
		LOG.debug("PROCESS");
		Message message = exchange.getIn();

		// Extract SyslogMessage from the message body.
		PortInfo info = message.getBody(PortInfo.class);
		
		// Extract {@link ServiceTest} from message headers
		ServiceTest<PortInfo> serviceTest = message.getHeader(SERVICE_TEST_INSTANCE, ServiceTest.class);

		// Create new event to translate the syslog message to
		RawEvent event = new RawEvent();

		// Delegate to member method call to perform the translation
		portInfoToEvent(serviceTest,info,event);
		
		LOG.debug("RawEvent: " + event);

		// Set the message body to the RawEvent
		message.setBody(event);
	}

	/**
	 * Converts a {@link SyslogMessage} to {@link RawEvent}
	 * 
	 * @param sm
	 * @param e
	 */
	private void portInfoToEvent(ServiceTest<PortInfo> serviceTest,PortInfo info, RawEvent event) {
		
		// Add the hostname
		String hostname = info.getHost();
		String serviceName = serviceTest.getServiceName();
		
		event.getSource().setRef(hostname).setType("host");
		event.addProperty("hostname",hostname);
		event.addProperty("port",info.getPort());
		event.addProperty("port-status",info.getPortStatus());
		event.addProperty("time-out", info.getTimeout());
		event.addProperty("service-test", serviceTest.getName());
		event.addProperty("service-test-type",serviceTest.getServiceTestType());
		event.addProperty("service", serviceName);
		
		event.addTag(serviceName);
		event.addTag(hostname);
		int port = info.getPort();
		
		// Set the Severity and Message based on the results
		// of the service test
		if (info.getStatus() == ServiceStatus.FAIL) {
			event.setTitle(serviceName + " - " + port + " on " + hostname + " is OFFLINE");
			event.setMessage("Failed to connect the port, reason: " + info.getMessage());
			event.setSeverity(Severity.WARN);
			event.setStatus(Status.OPEN);
		}
		else {;
			event.setTitle(serviceName + " - " + port + " on " + hostname + " is ONLINE");
			event.setMessage("Successfully connected to " + port);
			event.setSeverity(Severity.INFO);
			event.setStatus(Status.CLOSED);
		}


		// Map to the Service Status
		Severity severity = info.getStatus() == ServiceStatus.FAIL ? Severity.CRITICAL : Severity.INFO;
	

		if (event.getSeverity() != Severity.INFO) {
			event.setStatus(Status.OPEN);
		}
		else {
			event.setStatus(Status.OK);
		}
		
		event.addFingerprintField("service");
		event.addFingerprintField("service-test");
		event.addFingerprintField("hostname");
		
		// Set the time at which the Syslog record was created
		event.setCreatedAt(info.getTimestamp());

		// Set Title
		event.setTitle("Checking host: " + info.getHost() + " on port: " + info.getPort());
		
		// Set Sender
		event.getSender().setRef("Service Health Check");
		event.getSender().setType("Boundary Event SDK");
	}
}

@@ -2025,32 +2025,8 @@
 ) %7B%0A
-%09%09%0A%09%09// Add the hostname
 %0A%09%09S
@@ -2805,32 +2805,29 @@
 %22 + 
-port + %22 on %22 + hostname
+hostname + %22:%22 + port
  + %22
@@ -3004,17 +3004,16 @@
 %09%09else %7B
-;
 %0A%09%09%09even
@@ -3049,32 +3049,29 @@
 %22 + 
-port + %22 on %22 + hostname
+hostname + %22:%22 + port
  + %22
@@ -3109,22 +3109,9 @@
 ge(%22
-Successfully c
+C
 onne
@@ -3118,24 +3118,29 @@
 cted to 
+port 
 %22 + port
 );%0A%09%09%09ev
@@ -3135,234 +3135,41 @@
 port
-);%0A%09%09%09event.setSeverity(Severity.INFO);%0A%09%09%09event.setStatus(Status.CLOSED);%0A%09%09%7D%0A%0A%0A%09%09// Map to the Service Status%0A%09%09Severity severity = info.getStatus() == ServiceStatus.FAIL ? Severity.CRITICAL : Severity.INFO;%0A%09%0A%0A%09%09if (
+ + %22 on %22 + hostname);%0A%09%09%09
 event.
-g
+s
 etSe
@@ -3179,13 +3179,8 @@
 ity(
-) != 
 Seve
@@ -3193,56 +3193,9 @@
 NFO)
- %7B%0A%09%09%09event.setStatus(Status.OPEN);%0A%09%09%7D%0A%09%09else %7B
+;
 %0A%09%09%09
@@ -3221,10 +3221,14 @@
 tus.
-OK
+CLOSED
 );%0A%09
@@ -3465,110 +3465,8 @@
 ());
-%0A%0A%09%09// Set Title%0A%09%09event.setTitle(%22Checking host: %22 + info.getHost() + %22 on port: %22 + info.getPort());
 %0A%09%09%0A

Fix numerous problems with event created for port checking