def __init__(self, connection, config):
		SimpleL2LearningSwitch.__init__(self, connection, False)
        	self._connection = connection;
		self._serverip1 = config['server_ip1']
		self._serverip2 = config['server_ip2']   #This will be the LoadBalancer IP
		self._serverip1_mac = config['server_ip1_mac']
		self._serverip2_mac = config['server_ip2_mac']
		self._ts1_total_bw = float(config['ts1_total_bw']) * 1024 * 1024
		self._ts2_total_bw = float(config['ts2_total_bw']) * 1024 * 1024
		self.FLAG = 0
		self.Weight = 0.2
		self.lock1 = threading.Lock() 
		self.lock2 = threading.Lock()
		self.total_ts1_bytes = 0
		self.total_ts1_queue = 0
		self.total_ts2_bytes = 0
		self.total_ts2_queue = 0
		self.thread_flag = 1
		self.threshold = 0.50 
		self.ts1_path_counter = 0  #These two values are purely for experimental purposes
		self.ts2_path_counter = 0
		self.ts1_bw_threshold_more_available_bw = 0 
		self.ts2_bw_threshold_more_available_bw = 0 
		self.ts1_bw_threshold_equal_bw_lower_q = 0 
		self.ts2_bw_threshold_equal_bw_lower_q = 0 
		self.ts1_q_threshold_lower_q = 0 
		self.ts2_q_threshold_lower_q = 0 
		self.ts1_q_threshold_equal_more_available_bw = 0 
		self.ts2_q_threshold_equal_more_available_bw = 0 
		self.ts1_roundrobin = 0 
		self.ts2_roundrobin = 0 
        def __init__(self, connection, config):
	        SimpleL2LearningSwitch.__init__(self, connection, False)
        	self._connection = connection;

                #self.report = "~/testreportlog";
		
		self._serverip1 = config['server_ip1']
		self._serverip2 = config['server_ip2']   
		self._serverip1_mac = config['server_ip1_mac']
		self._serverip2_mac = config['server_ip2_mac']
		
		self._clientip1 = config['client_ip1']
		self._clientip2 = config['client_ip2'] 
		self._clientip3 = config['client_ip3']
		self._clientip4 = config['client_ip4']
		self._clientip1_mac = config['client_ip1_mac']
		self._clientip2_mac = config['client_ip2_mac']
		self._clientip3_mac = config['client_ip3_mac']
		self._clientip4_mac = config['client_ip4_mac']

		self.FLAG = 0
		self.lock1 = threading.Lock() 
		self.thread_flag = 1
		
		#Client Threat Status - 0: Low, 1: Medium, 2: High
		self.client1_status = 0
		self.client2_status = 0
		self.client3_status = 0
		
		#Server Threat Status - 0: Low, 1: Medium, 2: High
		self.server1_status = 0

                #Info Counters
                self.server1_low_threat             = 0
                self.server1_medium_threat          = 0
                self.server1_high_threat            = 0

                self.client1_low_threat             = 0
                self.client1_medium_threat          = 0
                self.client1_medium_threat          = 0

                self.client2_low_threat             = 0
                self.client2_medium_threat          = 0
                self.client2_medium_threat          = 0

                self.client3_low_threat             = 0
                self.client3_medium_threat          = 0
                self.client3_medium_threat          = 0

                self.client1_traffic_blocked        = 0
                self.client2_traffic_blocked        = 0
                self.client3_traffic_blocked        = 0
                self.user_traffic                   = 0
                self.user_traffic_redirected        = 0
                self.user_return_traffic_redirected = 0
                self.other_traffic                  = 0
    def _handle_PacketIn(self, event):
        log.debug("Got a packet : " + str(event.parsed))
        self.packet = event.parsed
        self.event = event
        self.macLearningHandle()

        if packetIsTCP(self.packet, log):
            self._handle_PacketInTCP(event)
            return
        SimpleL2LearningSwitch._handle_PacketIn(self, event)
	def _handle_PacketIn(self, event):
		self.packet = event.parsed
	        self.event = event
	        self.macLearningHandle()
		if packetIsTCP(self.packet, log) :
		        self._handle_PacketInTCP(event)
		        return
		elif packetIsUDP(self.packet, log):
			self._handle_PacketInUDP(event)
			return	
	        SimpleL2LearningSwitch._handle_PacketIn(self, event) #Default traffic, need to decide where to send
	def __init__(self, connection, config):
		SimpleL2LearningSwitch.__init__(self, connection, False)
        	self._connection = connection;
		self._serverip1 = config['server_ip1']
		self._serverip1_mac = config['server_ip1_mac']    #MAC Address of the Neighbour connected to Backup Link
		self._serverip2 = config['server_ip2']
		self._serverip2_mac = config['server_ip2_mac']
		self._p1_total_bw = float(config['p1_total_bw']) * 1024 * 1024
		self._p2_total_bw = float(config['p2_total_bw']) * 1024 * 1024
		self._p1_delay = float(config['p1_delay'])
		self._p2_delay = float(config['p2_delay'])
		self._delay_threshold = float(config['delay_threshold'])
		self.FLAG = 0
	def _handle_PacketIn(self, event):
	        #log.debug("Got a packet : " + str(event.parsed))
	        self.packet = event.parsed
	        self.event = event
	        self.macLearningHandle()
		if self.thread_flag == 1:
			thr1 = threading.Thread(target = self.TS1_info)
			thr2 = threading.Thread(target = self.TS2_info)
			thr1.start()
			thr2.start()
			self.thread_flag = 0
	        if packetIsTCP(self.packet, log) :
		          self._handle_PacketInTCP(event)
		          return
	        SimpleL2LearningSwitch._handle_PacketIn(self, event)
	def _handle_PacketInUDP(self,event):
		inport = event.port
        	actions = []	
		if ((self._p1_total_bw == self._p2_total_bw) and (self._p1_delay == self._p2_delay)):
			if packetDstIp(self.packet, self._serverip2, log ):
				if self.FLAG == 0:
					newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip1_mac, log)
					actions.append(newaction)
					newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip1, log)
					actions.append(newaction)
					newaction = createOFAction(of.OFPAT_SET_TP_DST, 5004, log)
					actions.append(newaction)
					self.FLAG = 1
					out_port = 3
					newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
					actions.append(newaction)
					match = getFullMatch(self.packet, inport)
	        			msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  	FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        			event.connection.send(msg.pack())
				else:
					SimpleL2LearningSwitch._handle_PacketIn(self, event)
					self.FLAG = 0
			elif packetSrcIp(self.packet, self._serverip1, log ):
				newaction = createOFAction(of.OFPAT_SET_NW_SRC, self._serverip2, log)
				actions.append(newaction)
				newaction = createOFAction(of.OFPAT_SET_TP_SRC, 5001, log)
				actions.append(newaction)
				out_port = self.get_out_port()
				newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
				actions.append(newaction)
				match = getFullMatch(self.packet, inport)
				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                	FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        		event.connection.send(msg.pack())
			else:
				SimpleL2LearningSwitch._handle_PacketIn(self, event)

		elif ((self._p1_total_bw == self._p2_total_bw) and (self._p1_delay < self._p2_delay)):
			if packetDstIp(self.packet, self._serverip2, log ):
		  		if packetDstUDPPort(self.packet,5001, log):  #RTP Traffic
					SimpleL2LearningSwitch._handle_PacketIn(self, event)	
			elif packetSrcIp(self.packet, self._serverip1, log ):
				newaction = createOFAction(of.OFPAT_SET_NW_SRC, self._serverip2, log)
				actions.append(newaction)
				out_port = self.get_out_port()
				newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
				actions.append(newaction)
				match = getFullMatch(self.packet, inport)
				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        		event.connection.send(msg.pack())
			else:
				SimpleL2LearningSwitch._handle_PacketIn(self, event)
		elif ((self._p1_total_bw == self._p2_total_bw) and (self._p1_delay > self._p2_delay)):
			if packetDstIp(self.packet, self._serverip2, log ):
				if packetDstUDPPort(self.packet,5001, log):  #RTP Traffic
					self.backup_udp_route(event)
				else:
					self.backup_route(event)
			elif packetSrcIp(self.packet, self._serverip1, log ):
				newaction = createOFAction(of.OFPAT_SET_NW_SRC, self._serverip2, log)
				actions.append(newaction)
				newaction = createOFAction(of.OFPAT_SET_TP_SRC, 5001, log)
				actions.append(newaction)
				out_port = self.get_out_port()
				newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
				actions.append(newaction)
				match = getFullMatch(self.packet, inport)
				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        		event.connection.send(msg.pack())
			else:
				SimpleL2LearningSwitch._handle_PacketIn(self, event)
		elif ((self._p1_total_bw > self._p2_total_bw) and (self._p2_delay <= self._delay_threshold)):
			if packetDstIp(self.packet, self._serverip2, log ):
				if packetDstUDPPort(self.packet,5001, log):  #RTP Traffic
					self.backup_udp_route(event)
			elif packetSrcIp(self.packet, self._serverip1, log ):
				newaction = createOFAction(of.OFPAT_SET_NW_SRC, self._serverip2, log)
				actions.append(newaction)
				newaction = createOFAction(of.OFPAT_SET_TP_SRC, 5001, log)
				actions.append(newaction)
				out_port = self.get_out_port()
				newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
				actions.append(newaction)
				match = getFullMatch(self.packet, inport)
				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        		event.connection.send(msg.pack())
			else:
				SimpleL2LearningSwitch._handle_PacketIn(self, event)
		elif ((self._p1_total_bw > self._p2_total_bw) and (self._p2_delay > self._delay_threshold)):
			if packetDstIp(self.packet, self._serverip2, log ):
				if packetDstUDPPort(self.packet,5001, log):  #RTP Traffic
					SimpleL2LearningSwitch._handle_PacketIn(self, event)
				else:
					self.backup_route(event)
			else:
				SimpleL2LearningSwitch._handle_PacketIn(self, event)
 def __init__(self, connection, config):
     SimpleL2LearningSwitch.__init__(self, connection, False)
     self._connection = connection
     self._serverip = config['server_ip']
     self._origport = int(config['orig_port'])
     self._forwport = int(config['forw_port'])
	def _handle_PacketInTCP(self, event) :
		inport = event.port
        	actions = []
        	if packetDstIp(self.packet, self._serverip2, log ):
		  if packetDstTCPPort(self.packet,5001, log):
			self.lock1.acquire()
			try:
				temp_ts1_bw = self.total_ts1_bytes * 8 #to convert bytes into bits
				temp_ts1_queue = self.total_ts1_queue
			finally:
				self.lock1.release()
			self.lock2.acquire()
			try:
				temp_ts2_bw = self.total_ts2_bytes * 8
				temp_ts2_queue = self.total_ts2_queue
			finally:
				self.lock2.release()
			log.info("TS1 Bandwidth Utilization  %.4f" % temp_ts1_bw + "  TS1 Queue %.4f " % temp_ts1_queue)
			log.info("TS2 Bandwidth Utilization  %.4f" % temp_ts2_bw + "  TS2 Queue %.4f " % temp_ts2_queue)
			if (self.threshold <= (temp_ts1_bw / self._ts1_total_bw)) or (self.threshold <= (temp_ts2_bw / self._ts2_total_bw)):
				if (self._ts2_total_bw - temp_ts2_bw) > (self._ts1_total_bw - temp_ts1_bw):
					#TS2 path is less loaded and hence flow is routed that path
					log.info("Path Selected: TS2, Reason: More Bandwidth Available on TS2")
					self.ts2_path_counter = self.ts2_path_counter + 1
					self.ts2_bw_threshold_more_available_bw = self.ts2_bw_threshold_more_available_bw + 1
					newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip1_mac, log)
					actions.append(newaction)
					newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip1, log)
					log.debug("MAC %s IP %s"%(self._serverip1_mac,self._serverip1))
					actions.append(newaction)
					out_port = 3
					newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
					actions.append(newaction)
					match = getFullMatch(self.packet, inport)
	        			msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  	  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        			event.connection.send(msg.pack())
				elif (self._ts2_total_bw - temp_ts2_bw) == (self._ts1_total_bw - temp_ts1_bw) :
					if temp_ts2_queue < temp_ts1_queue:
						#if both links are equally utilized, choose path whose queue is smaller
						log.info("Path Selected: TS2, Reason: Equal Bandwidth Available on TS1 and TS2, Smaller Queue Size on TS2")
						self.ts2_path_counter = self.ts2_path_counter + 1
						self.ts2_bw_threshold_equal_bw_lower_q = self.ts2_bw_threshold_equal_bw_lower_q + 1
						newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip1_mac, log)
						actions.append(newaction)
						newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip1, log)
						log.debug("MAC %s IP %s"%(self._serverip1_mac,self._serverip1))
						actions.append(newaction)
						out_port = 3
						newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
						actions.append(newaction)
						match = getFullMatch(self.packet, inport)
	        				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  	  	  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        				event.connection.send(msg.pack())
					else:
						log.info("Path Selected: TS1, Reason: Equal Bandwidth Available on TS1 and TS2, Smaller Queue Size on TS1")
						self.ts1_path_counter = self.ts1_path_counter + 1
						self.ts1_bw_threshold_equal_bw_lower_q = self.ts1_bw_threshold_equal_bw_lower_q + 1 
						SimpleL2LearningSwitch._handle_PacketIn(self, event) #Go Through TS1 path
				else:
					log.info("Path Selected: TS1, Reason: More Bandwidth Available on TS1")
					self.ts1_path_counter = self.ts1_path_counter + 1
					self.ts1_bw_threshold_more_available_bw = self.ts1_bw_threshold_more_available_bw + 1
					SimpleL2LearningSwitch._handle_PacketIn(self, event)   # Go through TS1	path
			elif (self.threshold < (temp_ts1_queue / 10.0)) or (self.threshold < (temp_ts2_queue / 10.0)):
				#Queue Depth is of 10 Packets. Checks if any Queue Depth is over 50%
				if temp_ts2_queue < temp_ts1_queue:
						log.info("Path Selected: TS2, Reason: Bandwidth below Threshold on TS1 and TS2,  Smaller Queue Size on TS2")
						self.ts2_path_counter = self.ts2_path_counter + 1
						self.ts2_q_threshold_lower_q = self.ts2_q_threshold_lower_q + 1 
						newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip1_mac, log)
						actions.append(newaction)
						newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip1, log)
						log.debug("MAC %s IP %s"%(self._serverip1_mac,self._serverip1))
						actions.append(newaction)
						out_port = 3
						newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
						actions.append(newaction)
						match = getFullMatch(self.packet, inport)
	        				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  	  	  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        				event.connection.send(msg.pack())
				elif temp_ts2_queue == temp_ts1_queue:
					if (self._ts2_total_bw - temp_ts2_bw) > (self._ts1_total_bw - temp_ts1_bw):
						log.info("Path Selected: TS2, Reason: Bandwidth below Threshold on TS1 and TS2, Queue Sizes equal on TS1 and TS2, More Bandwidth available on TS2")
						self.ts2_path_counter = self.ts2_path_counter + 1
						self.ts2_q_threshold_equal_more_available_bw = self.ts2_q_threshold_equal_more_available_bw
						newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip1_mac, log)
						actions.append(newaction)
						newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip1, log)
						log.debug("MAC %s IP %s"%(self._serverip1_mac,self._serverip1))
						actions.append(newaction)
						out_port = 3
						newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
						actions.append(newaction)
						match = getFullMatch(self.packet, inport)
	        				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  	  	  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        				event.connection.send(msg.pack())
					else:
						log.info("Path Selected: TS1, Reason: Bandwidth below Threshold on TS1 and TS2, Queue Sizes equal on TS1 and TS2, More Bandwidth available on TS1")
						self.ts1_path_counter = self.ts1_path_counter + 1
						self.ts1_q_threshold_equal_more_available_bw = self.ts1_q_threshold_equal_more_available_bw
						SimpleL2LearningSwitch._handle_PacketIn(self, event)
				else:
					log.info("Path Selected: TS1, Reason: Bandwidth below Threshold on TS1 and TS2, Smaller Queue Size on TS2")
					self.ts1_path_counter = self.ts1_path_counter + 1
					self.ts1_q_threshold_lower_q = self.ts1_q_threshold_lower_q + 1
					SimpleL2LearningSwitch._handle_PacketIn(self, event)
			elif self.FLAG == 0:
				log.debug("Packet is TCP destined to %s:" % (self._serverip2))
				log.info("Path Selected: TS2, Reason: Round Robin")
				self.ts2_path_counter = self.ts2_path_counter + 1
				self.ts2_roundrobin = self.ts2_roundrobin + 1
				newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip1_mac, log)
				actions.append(newaction)
				newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip1, log)
				log.debug("MAC %s IP %s"%(self._serverip1_mac,self._serverip1))
				actions.append(newaction)
				self.FLAG = 1
				out_port = 3
				newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
				actions.append(newaction)
				match = getFullMatch(self.packet, inport)
	        		msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        		event.connection.send(msg.pack())
			else:
				log.info("Path Selected: TS1, Reason: Round Robin")
				self.ts1_path_counter = self.ts1_path_counter + 1
				self.ts1_roundrobin = self.ts1_roundrobin + 1 
				SimpleL2LearningSwitch._handle_PacketIn(self, event)
				self.FLAG = 0
				
		  log.info(" ts1_total_loadbalanced : %d " % self.ts1_path_counter)
		  log.info(" TS2 total_loadbalanced : %d " % self.ts2_path_counter)
		  log.info(" ts1_bw_threshold_more_available_bw : %d" % self.ts1_bw_threshold_more_available_bw)
		  log.info(" ts2_bw_threshold_more_available_bw : %d" % self.ts2_bw_threshold_more_available_bw) 
		  log.info(" ts1_bw_threshold_equal_bw_lower_q : %d" % self.ts1_bw_threshold_equal_bw_lower_q) 
	          log.info(" ts2_bw_threshold_equal_bw_lower_q : %d" % self.ts2_bw_threshold_equal_bw_lower_q) 
		  log.info(" ts1_q_threshold_lower_q : %d" % self.ts1_q_threshold_lower_q) 
		  log.info(" ts2_q_threshold_lower_q : %d" % self.ts2_q_threshold_lower_q) 
		  log.info(" ts1_q_threshold_equal_more_available_bw : %d" % self.ts1_q_threshold_equal_more_available_bw) 
		  log.info(" ts2_q_threshold_equal_more_available_bw : %d" % self.ts2_q_threshold_equal_more_available_bw) 
		  log.info(" ts1_roundrobin : %d" % self.ts1_roundrobin)
		  log.info(" ts2_roundrobin : %d" % self.ts2_roundrobin) 
			
		elif packetSrcIp(self.packet, self._serverip1, log ):
			log.debug("Packet is sourced by TCP at %s:" % (self._serverip1))
			newaction = createOFAction(of.OFPAT_SET_NW_SRC, self._serverip2, log)
			actions.append(newaction)
			out_port = self.get_out_port()
			newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
			actions.append(newaction)
			match = getFullMatch(self.packet, inport)
	        	msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, 
                                  FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        	event.connection.send(msg.pack())

		else:
			
			SimpleL2LearningSwitch._handle_PacketIn(self, event)
	def _handle_PacketIn(self, event):
                inport = event.port
	        self.packet = event.parsed
	        self.event = event
	        self.macLearningHandle()

                actions = []
		
	        if self.thread_flag == 1:
		    thr1 = threading.Thread(target = self.parseAttackGraph)
		    thr1.start()
		    self.thread_flag = 0
	        
		if packetDstIp(self.packet, self._serverip1, log):
		    if (self.server1_status == 0): # Server 1 is in Low Attack Threat Mode
                        # Allow all traffic to pass through to Server 1 : Attack Traffic + Legitimate Traffic
                        self.server1_low_threat = self.server1_low_threat + 1
                        SimpleL2LearningSwitch._handle_PacketIn(self, event)
                        if (self.server1_low_threat % 5000 == 0) :
                            log.info("Server1_Low_Threat_Level: %d Client_Threats: %d, %d, %d" %(self.server1_low_threat, self.client1_status, self.client2_status, self.client3_status))
				
		    elif (self.server1_status == 1): #Server 1 is in Medium Attack Threat Mode
                        #log.info("Packet SRC MAC: %s" %(self.packet.src))
                        self.server1_medium_threat = self.server1_medium_threat + 1
                        if (self.server1_medium_threat % 5000 == 0) :
                            log.info("Server1_Medium_Threat_Level: %d Client_Threats : %d, %d, %d" %(self.server1_medium_threat, self.client1_status, self.client2_status, self.client3_status))
			if(str(self.packet.src) == str(self._clientip1_mac)):
			    if (self.client1_status == 0):
                                self.client1_low_threat = self.client1_low_threat + 1
                                if (self.client1_low_threat % 500 == 0) :
                                    log.info("Server1_Medium_Threat_Level + Client1_Low_Threat_Level: %d " % self.client1_low_threat)
			        SimpleL2LearningSwitch._handle_PacketIn(self, event)
			    elif (self.client1_status == 1 or self.client1_status == 2): 
                                if (self.client1_status == 1):
                                    self.client1_medium_threat = self.client1_medium_threat + 1
                                    if (self.client1_medium_threat % 500 == 0) :
                                        log.info("Server1_Medium_Threat_Level + Client1_Medium_Threat_Level: %d " % self.client1_medium_threat)
                                elif (self.client1_status == 2):
                                    self.client1_high_threat = self.client1_high_threat + 1
                                    if (self.client1_high_threat % 500 == 0) :
                                        log.info("Server1 Medium_Threat_Level + Client1_High_Threat_Level: %d " % self.client1_high_threat)
				#Block Traffic
                                self.client1_traffic_blocked = self.client1_traffic_blocked + 1
                                if (self.client1_traffic_blocked % 500 == 0) :
                                    log.info("Server1_Medium_Threat_Level + Client1_Attack_Blocked: %d " % self.client1_traffic_blocked)
				msg = of.ofp_flow_mod()
				msg.priority = 20
				msg.actions.append(of.ofp_action_output(port=of.OFPP_NONE))
				# create generic match
				#match = of.ofp_match()
			        # policy in one direction
				#match.dl_src = self._clientip1_mac
				#match.nw_dst = self._serverip1
				match = getFullMatch(self.packet, inport)
				msg.match = match
	    			event.connection.send(msg)
			
			elif(str(self.packet.src) == str(self._clientip2_mac)):
			    if (self.client2_status == 0):
                                self.client2_low_threat = self.client2_low_threat + 1
                                if (self.client2_low_threat % 500 == 0) :
                                    log.info("Server1_Medium_Threat_Level + Client2_Low_Threat_Level: %d " % self.client2_low_threat)
			        SimpleL2LearningSwitch._handle_PacketIn(self, event)
			    elif (self.client2_status == 1 or self.client2_status == 2): 
                                if (self.client2_status == 1):
                                    self.client2_medium_threat = self.client2_medium_threat + 1
                                    if (self.client2_medium_threat % 500 == 0) :
                                        log.info("Server1_Medium_Threat_Level + Client2_Medium_Threat_Level: %d " % self.client2_medium_threat)
                                elif (self.client2_status == 2):
                                    self.client2_high_threat = self.client2_high_threat + 1
                                    if (self.client2_high_threat % 500 == 0) :
                                        log.info("Server1_Medium_Threat_Level + Client2_High_Threat_Level: %d " % self.client2_high_threat)
				#Block Traffic
                                self.client2_traffic_blocked = self.client2_traffic_blocked + 1
                                if (self.client2_traffic_blocked % 500 == 0) :
                                    log.info("Server1_Medium_Threat_Level + Client2_Attack_Blocked: %d " % self.client2_traffic_blocked)
			        msg = of.ofp_flow_mod()
		                msg.priority = 20
			        msg.actions.append(of.ofp_action_output(port=of.OFPP_NONE))
			        # create generic match
			        #match = of.ofp_match()
			        # policy in one direction
			        #match.dl_src = self._clientip2_mac
			        #match.nw_dst = self._serverip1
				match = getFullMatch(self.packet, inport)
			        msg.match = match
			        event.connection.send(msg)
					
			elif(str(self.packet.src) == str(self._clientip3_mac)):
			    if (self.client3_status == 0):
                                self.client3_low_threat = self.client3_low_threat + 1
                                if (self.client3_low_threat % 500 == 0) :
                                    log.info("Server1_Medium_Threat_Level + Client3_Low_Threat_Level: %d " % self.client3_low_threat)
			        SimpleL2LearningSwitch._handle_PacketIn(self, event)
			    elif (self.client3_status == 1 or self.client3_status == 2): 
                                if (self.client3_status == 1):
                                    self.client3_medium_threat = self.client3_medium_threat + 1
                                    if (self.client3_medium_threat % 500 == 0) :
                                        log.info("Server1_Medium_Threat_Level + Client3_Medium_Threat_Level: %d " % self.client3_medium_threat)
                                elif (self.client3_status == 2):
                                    self.client3_high_threat = self.client3_high_threat + 1
                                    if (self.client3_high_threat % 500 == 0) :
                                        log.info("Server1_Medium_Threat_Level + Client3_High_Threat_Level: %d " % self.client3_high_threat)
				#Block Traffic
                                self.client3_traffic_blocked = self.client3_traffic_blocked + 1
                                if (self.client3_traffic_blocked % 500 == 0) :
                                    log.info("Server1_Medium_Threat_Level + Client3_Attack_Blocked: %d " % self.client3_traffic_blocked)
				msg = of.ofp_flow_mod()
			    	msg.priority = 20
			    	msg.actions.append(of.ofp_action_output(port=of.OFPP_NONE))
			    	# create generic match
			    	#match = of.ofp_match()
			    	# policy in one direction
			    	#match.dl_src = self._clientip3_mac
			    	#match.nw_dst = self._serverip1
				match = getFullMatch(self.packet, inport)
			    	msg.match = match
			    	event.connection.send(msg)
					
		        elif(str(self.packet.src) == str(self._clientip4_mac)):
                            self.user_traffic = self.user_traffic + 1
                            if (self.user_traffic % 500 == 0) :
                                log.info("Server1_Medium_Threat_Level + User_Traffic_From_Client4: %d " % self.user_traffic)
		            SimpleL2LearningSwitch._handle_PacketIn(self, event) #Consider Load Balancing Here
						
		    elif (self.server1_status == 2): #Server 1 is in High Attack Threat Mode 
                        self.server1_high_threat = self.server1_high_threat + 1
                        if (self.server1_high_threat % 5000 == 0) :
                            log.info("Server1_High_Threat_Level: %d Client_Threats : %d, %d, %d" %(self.server1_high_threat, self.client1_status, self.client2_status, self.client3_status))
		        if(str(self.packet.src) == str(self._clientip1_mac) or str(self.packet.src) == str(self._clientip2_mac) or str(self.packet.src) == str(self._clientip3_mac)):
			    #Block Traffic
			    msg = of.ofp_flow_mod()
	    		    msg.priority = 20
	    		    msg.actions.append(of.ofp_action_output(port=of.OFPP_NONE))	
			    # create generic match
                            #match = of.ofp_match()
			    # policy in one direction
			    if(self.packet.src == self._clientip1_mac):
                                self.client1_traffic_blocked = self.client1_traffic_blocked + 1
                                if (self.client1_traffic_blocked % 500 == 0) :
                                    log.info("Server1_High_Threat_Level + Client1_Attack_Blocked: %d " % self.client1_traffic_blocked)
			        #match.dl_src = self._clientip1_mac
			    if(self.packet.src == self._clientip2_mac):
                                self.client2_traffic_blocked = self.client2_traffic_blocked + 1
                                if (self.client2_traffic_blocked % 500 == 0) :
                                    log.info("Server1_High_Threat_Level + Client 2_Attack_Blocked: %d " % self.client2_traffic_blocked)
			        #match.dl_src = self._clientip2_mac
			    if(self.packet.src == self._clientip3_mac):
                                self.client3_traffic_blocked = self.client3_traffic_blocked + 1
                                if (self.client3_traffic_blocked % 500 == 0) :
                                    log.info("Server1_High_Threat_Level + Client3_Attack_Blocked: %d " % self.client3_traffic_blocked)
				#match.dl_src = self._clientip3_mac
			    #match.nw_dst = self._serverip1
			    match = getFullMatch(self.packet, inport)
			    msg.match = match
			    event.connection.send(msg)
						
			elif(str(self.packet.src) == str(self._clientip4_mac)):
		            if packetIsTCP(self.packet, log):
			        #Redirect Traffic to Server 2; Modify Dst IP and Dst MAC 
                                self.user_traffic_redirected = self.user_traffic_redirected + 1
                                log.info("Server1_High_Threat + Traffic_from_client4_Redirected_to_Server2: %d " % self.user_traffic_redirected)
				newaction = createOFAction(of.OFPAT_SET_DL_DST, self._serverip2_mac, log)
				actions.append(newaction)
				newaction = createOFAction(of.OFPAT_SET_NW_DST, self._serverip2, log)
				actions.append(newaction)
				newaction = createOFAction(of.OFPAT_SET_TP_DST, 6001, log)
				actions.append(newaction)
				out_port = 7 ## Outport 7 on OpenVSwitch is connected to Server 2
				newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
				actions.append(newaction)
				#match = of.ofp_match()
				#match.dl_src = str(self._clientip4_mac)
				#match.nw_src = str(self._clientip4)
				match = getFullMatch(self.packet, inport)
				msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
				event.connection.send(msg.pack())
			
		elif (packetDstIp (self.packet, self._clientip4, log) and (str(self.packet.src) == str(self._serverip2_mac))):
		    if packetIsTCP(self.packet, log):
		        # Modify Source IP and Source Mac
                        self.user_return_traffic_redirected = self.user_return_traffic_redirected + 1
                        log.info("Server1_High_Threat + Return_Traffic_to_client4_modified: %d " % self.user_return_traffic_redirected)
			newaction = createOFAction(of.OFPAT_SET_DL_SRC, self._serverip1_mac, log)
			actions.append(newaction)
			newaction = createOFAction(of.OFPAT_SET_NW_SRC, self._serverip1, log)
			actions.append(newaction)
			newaction = createOFAction(of.OFPAT_SET_TP_SRC, 5001, log)
			actions.append(newaction)
			out_port = 4 ## Outport 4 on OpenVSwitch is connected to Client 4 
			newaction = createOFAction(of.OFPAT_OUTPUT, out_port, log)
			actions.append(newaction)
			#match = of.ofp_match()
			#match.dl_src = str(self._serverip2_mac)
			#match.nw_src = str(self._serverip2)
			match = getFullMatch(self.packet, inport)
	        	msg = createFlowMod(match, actions, FLOW_HARD_TIMEOUT, FLOW_IDLE_TIMEOUT, event.ofp.buffer_id)
	        	event.connection.send(msg.pack())
		else: 
                    self.other_traffic = self.other_traffic + 1
                    if (self.other_traffic % 1000 == 0) :
                        log.info("Legitmate_User_Traffic + Return_Traffic: %d " % self.other_traffic)
		    SimpleL2LearningSwitch._handle_PacketIn(self, event)