<commit_before>package net.floodlightcontroller.odinmaster;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.openflow.protocol.OFMessage;
import org.openflow.protocol.OFType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.IOFMessageListener;
import net.floodlightcontroller.core.IOFSwitch;
import net.floodlightcontroller.core.IOFSwitchListener;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.odinmaster.NotificationCallback;
import net.floodlightcontroller.odinmaster.OdinEventSubscription;
import net.floodlightcontroller.odinmaster.NotificationCallbackContext;
import net.floodlightcontroller.odinmaster.SubscriptionCallbackTuple;
import net.floodlightcontroller.odinmaster.IOdinAgent;
import net.floodlightcontroller.odinmaster.OdinClient;
import net.floodlightcontroller.packet.DHCP;
import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPacket;
import net.floodlightcontroller.restserver.IRestApiService;
import net.floodlightcontroller.util.MACAddress;


/**
 * OdinMaster implementation. Exposes interfaces to OdinApplications,
 * and keeps track of agents and clients in the system.
 * 
 * @author <NAME> <<EMAIL>>
 *
 */
public class OdinMaster implements IFloodlightModule, IOFSwitchListener, IOdinApplicationInterface, IOFMessageListener, IFloodlightService {
	protected static Logger log = LoggerFactory.getLogger(OdinMaster.class);
	protected IRestApiService restApi;

	private IFloodlightProviderService floodlightProvider;
	private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
	
	private final AgentManager agentManager;
	private final ClientManager clientManager;	
	private final ILvapManager lvapManager;
	
	private long subscriptionId = 0;
	private String subscriptionList = "";
	private final ConcurrentMap<Long, SubscriptionCallbackTuple> subscriptions = new ConcurrentHashMap<Long, SubscriptionCallbackTuple>();
	private int idleLvapTimeout = 30; // Seconds

	public OdinMaster(){
		clientManager = new ClientManager();
		agentManager = new AgentManager(clientManager);
		lvapManager = new LvapManagerImpl();
	}
	
	public OdinMaster(AgentManager agentManager, ClientManager clientManager, ILvapManager lvapManager){
		this.agentManager = agentManager;
		this.clientManager = clientManager;
		this.lvapManager = lvapManager;
	}
	
	
	/** Odin Agent->Master protocol handlers **/
	
	/**
	 * Handle a ping from an agent
	 * 
	 * @param InetAddress of the agent
	 */
	public synchronized void receivePing (final InetAddress odinAgentAddr) {
		if (agentManager.receivePing(odinAgentAddr)) {
			// if the above leads to a new agent being
			// tracked, push the current subscription list
			// to it.
			pushSubscriptionListToAgent(agentManager.getOdinAgents().get(odinAgentAddr));			

			// Reclaim idle lvaps
			for (OdinClient client: agentManager.getOdinAgents().get(odinAgentAddr).getLvapsLocal()) {
				executor.schedule(new IdleLvapReclaimTask(client), idleLvapTimeout, TimeUnit.SECONDS);
			}
		}

		// Perform some book-keeping<commit_msg>Attach flows to LVAPs that are added upon post-failure agent-sync<commit_after>package net.floodlightcontroller.odinmaster;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.openflow.protocol.OFMessage;
import org.openflow.protocol.OFType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.IOFMessageListener;
import net.floodlightcontroller.core.IOFSwitch;
import net.floodlightcontroller.core.IOFSwitchListener;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.odinmaster.NotificationCallback;
import net.floodlightcontroller.odinmaster.OdinEventSubscription;
import net.floodlightcontroller.odinmaster.NotificationCallbackContext;
import net.floodlightcontroller.odinmaster.SubscriptionCallbackTuple;
import net.floodlightcontroller.odinmaster.IOdinAgent;
import net.floodlightcontroller.odinmaster.OdinClient;
import net.floodlightcontroller.packet.DHCP;
import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPacket;
import net.floodlightcontroller.restserver.IRestApiService;
import net.floodlightcontroller.util.MACAddress;


/**
 * OdinMaster implementation. Exposes interfaces to OdinApplications,
 * and keeps track of agents and clients in the system.
 * 
 * @author <NAME> <<EMAIL>>
 *
 */
public class OdinMaster implements IFloodlightModule, IOFSwitchListener, IOdinApplicationInterface, IOFMessageListener, IFloodlightService {
	protected static Logger log = LoggerFactory.getLogger(OdinMaster.class);
	protected IRestApiService restApi;

	private IFloodlightProviderService floodlightProvider;
	private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
	
	private final AgentManager agentManager;
	private final ClientManager clientManager;	
	private final ILvapManager lvapManager;
	
	private long subscriptionId = 0;
	private String subscriptionList = "";
	private final ConcurrentMap<Long, SubscriptionCallbackTuple> subscriptions = new ConcurrentHashMap<Long, SubscriptionCallbackTuple>();
	private int idleLvapTimeout = 30; // Seconds

	public OdinMaster(){
		clientManager = new ClientManager();
		agentManager = new AgentManager(clientManager);
		lvapManager = new LvapManagerImpl();
	}
	
	public OdinMaster(AgentManager agentManager, ClientManager clientManager, ILvapManager lvapManager){
		this.agentManager = agentManager;
		this.clientManager = clientManager;
		this.lvapManager = lvapManager;
	}
	
	
	/** Odin Agent->Master protocol handlers **/
	
	/**
	 * Handle a ping from an agent
	 * 
	 * @param InetAddress of the agent
	 */
	public synchronized void receivePing (final InetAddress odinAgentAddr) {
		if (agentManager.receivePing(odinAgentAddr)) {
			// if the above leads to a new agent being
			// tracked, push the current subscription list
			// to it.
			pushSubscriptionListToAgent(agentManager.getOdinAgents().get(odinAgentAddr));			

			// Reclaim idle lvaps and also attach flows to lvaps
			for (OdinClient client: agentManager.getOdinAgents().get(odinAgentAddr).getLvapsLocal()) {
				executor.schedule(new IdleLvapReclaimTask(client), idleLvapTimeout, TimeUnit.SECONDS);
				
				// Assign flow tables
				if (!client.getIpAddress().getHostAddress().equals("0.0.0.0")) {
					
					// Obtain reference to client entity from clientManager, because agent.getLvapsLocal()
					// returns a separate copy of the client objects.
					OdinClient trackedClient = clientManager.getClients().get(client.getMacAddress());
					trackedClient.setOFMessageList(lvapManager.getDefaultOFModList(client.getIpAddress()));
					
					// Push flow messages associated with the client
        			try {
        				trackedClient.getOdinAgent().getSwitch().write(trackedClient.getOFMessageList(), null);
        			} catch (IOException e) {
        				log.error("Failed to update switch's flow tables " + trackedClient.getOdinAgent().getSwitch());
        			}
				}
			}
		}

		// Perform some book-keeping
{'fraction_non_alphanumeric': 0.08361391694725027, 'fraction_numerical': 0.00202020202020202, 'mean_word_length': 5.881081081081081, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3522625', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2538, 'n_words': 544}