<commit_before>/**
*    Copyright 2011, Big Switch Networks, Inc. 
*    Originally created by <NAME>, Stanford University
* 
*    Licensed under the Apache License, Version 2.0 (the "License"); you may
*    not use this file except in compliance with the License. You may obtain
*    a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
*    License for the specific language governing permissions and limitations
*    under the License.
**/

package net.floodlightcontroller.proact;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.IFloodlightProviderService.Role;
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.storage.IResultSet;
import net.floodlightcontroller.storage.IStorageSourceService;
import net.floodlightcontroller.storage.OperatorPredicate;
import net.floodlightcontroller.storage.OperatorPredicate.Operator;
import net.floodlightcontroller.storage.RowOrdering;

import org.openflow.protocol.OFFlowMod;
import org.openflow.protocol.OFMatch;
import org.openflow.protocol.OFMessage;
import org.openflow.protocol.OFPacketIn;
import org.openflow.protocol.OFPacketOut;
import org.openflow.protocol.OFPort;
import org.openflow.protocol.OFType;
import org.openflow.protocol.action.OFAction;
import org.openflow.protocol.action.OFActionOutput;
import org.openflow.util.U16;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <NAME> (<EMAIL>) - 04/04/10
 */
public class Proact implements IFloodlightModule, IOFMessageListener, IOFSwitchListener {
    protected static Logger log = LoggerFactory.getLogger(Proact.class);

    protected IFloodlightProviderService floodlightProvider;

	private IStorageSourceService storage;

    /**
     * @param floodlightProvider the floodlightProvider to set
     */
    public void setFloodlightProvider(IFloodlightProviderService floodlightProvider) {
        this.floodlightProvider = floodlightProvider;
    }

    @Override
    public String getName() {
        return Proact.class.getPackage().getName();
    }

    public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
        OFPacketIn pi = (OFPacketIn) msg;
        log.info("Ignoring packetin {} sw: {}, in: {}", new Object[] { msg, sw, pi.getInPort()});

        return Command.CONTINUE;
    }

    @Override
    public boolean isCallbackOrderingPrereq(OFType type, String name) {
        return false;
    }

    @Override
    public boolean isCallbackOrderingPostreq(OFType type, String name) {
        return false;
    }

    // IFloodlightModule
    
    @Override
    public Collection<Class<? extends IFloodlightService>> getModuleServices() {
        // We don't provide any services, return null
        return null;
    }

    @Override
    public Map<Class<? extends IFloodlightService>, IFloodlightService>
            getServiceImpls() {
        // We don't provide any services, return null
        return null;
    }

    @Override
    public Collection<Class<? extends IFloodlightService>>
            getModuleDependencies() {
        Collection<Class<? extends IFloodlightService>> l = 
                new ArrayList<Class<? extends IFloodlightService>>();
        l.add(IFloodlightProviderService.class);
        l.add(IStorageSourceService.class);

        return l;
    }

    @Override
    public void init(FloodlightModuleContext context)
            throws FloodlightModuleException {
        log.info("Proact init start");

        floodlightProvider =
                context.getServiceImpl(IFloodlightProviderService.class);
        storage = context.getServiceImpl(IStorageSourceService.class);
        log.info("Got storage: "+storage);
        HashSet<String> set = new HashSet<String>();
        set.add("id");
        storage.createTable("rules", set);        
        
        storage.insertRow("rules", map(
        		"id", 1,
        		"dpid", 1,
        		"match", match(1),
        		"action", output(2)
        		));
        
        storage.insertRow("rules", map(
        		"id", 2,
        		"dpid", 1,
        		"match", match(2),
        		"action", output(1)
        		));
        
        storage.insertRow("rules", map(
        		"id", 3,
        		"dpid", 2,
        		"match", match(1),
        		"action", output(2)
        		));    
        
        storage.insertRow("rules", map(
        		"id", 4,
        		"dpid", 2,
        		"match", match(2),
        		"action", output(1)
        		));
        floodlightProvider.addOFSwitchListener(this);
        log.info("Proact OK");
    }

    OFMatch match(int inport){
    	OFMatch m = new OFMatch();
    	m.setInputPort((short) inport);
    	return m;
    }
    
    OFActionOutput output(int port) {
    	OFActionOutput o = new OFActionOutput((short) port, (short) 0);
    	return o;
    }
    
    Map<String, Object> map(Object...objects) {
    	Map<String, Object> res = new LinkedHashMap<String, Object>();
    	if(objects.length % 2 > 0) {
    		throw new IllegalArgumentException("must have even arg count");
    	}
    	for(int i=0; i < objects.length; i+=2) {
    		res.put((String) objects[i], objects[i+1]);
    	}
    	return res;
    }
    <commit_msg>Proact: fix to flow_mods<commit_after>/**
*    Copyright 2011, Big Switch Networks, Inc. 
*    Originally created by <NAME>, Stanford University
* 
*    Licensed under the Apache License, Version 2.0 (the "License"); you may
*    not use this file except in compliance with the License. You may obtain
*    a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
*    License for the specific language governing permissions and limitations
*    under the License.
**/

package net.floodlightcontroller.proact;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.IFloodlightProviderService.Role;
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.storage.IResultSet;
import net.floodlightcontroller.storage.IStorageSourceService;
import net.floodlightcontroller.storage.OperatorPredicate;
import net.floodlightcontroller.storage.OperatorPredicate.Operator;
import net.floodlightcontroller.storage.RowOrdering;

import org.openflow.protocol.OFFlowMod;
import org.openflow.protocol.OFMatch;
import org.openflow.protocol.OFMessage;
import org.openflow.protocol.OFPacketIn;
import org.openflow.protocol.OFPacketOut;
import org.openflow.protocol.OFPort;
import org.openflow.protocol.OFType;
import org.openflow.protocol.action.OFAction;
import org.openflow.protocol.action.OFActionOutput;
import org.openflow.util.U16;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <NAME> (<EMAIL>) - 04/04/10
 */
public class Proact implements IFloodlightModule, IOFMessageListener, IOFSwitchListener {
    protected static Logger log = LoggerFactory.getLogger(Proact.class);

    protected IFloodlightProviderService floodlightProvider;

	private IStorageSourceService storage;

    /**
     * @param floodlightProvider the floodlightProvider to set
     */
    public void setFloodlightProvider(IFloodlightProviderService floodlightProvider) {
        this.floodlightProvider = floodlightProvider;
    }

    @Override
    public String getName() {
        return Proact.class.getPackage().getName();
    }

    public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
        OFPacketIn pi = (OFPacketIn) msg;
        log.info("Ignoring packetin {} sw: {}, in: {}", new Object[] { msg, sw, pi.getInPort()});

        return Command.CONTINUE;
    }

    @Override
    public boolean isCallbackOrderingPrereq(OFType type, String name) {
        return false;
    }

    @Override
    public boolean isCallbackOrderingPostreq(OFType type, String name) {
        return false;
    }

    // IFloodlightModule
    
    @Override
    public Collection<Class<? extends IFloodlightService>> getModuleServices() {
        // We don't provide any services, return null
        return null;
    }

    @Override
    public Map<Class<? extends IFloodlightService>, IFloodlightService>
            getServiceImpls() {
        // We don't provide any services, return null
        return null;
    }

    @Override
    public Collection<Class<? extends IFloodlightService>>
            getModuleDependencies() {
        Collection<Class<? extends IFloodlightService>> l = 
                new ArrayList<Class<? extends IFloodlightService>>();
        l.add(IFloodlightProviderService.class);
        l.add(IStorageSourceService.class);

        return l;
    }

    @Override
    public void init(FloodlightModuleContext context)
            throws FloodlightModuleException {
        log.info("Proact init start");

        floodlightProvider =
                context.getServiceImpl(IFloodlightProviderService.class);
        storage = context.getServiceImpl(IStorageSourceService.class);
        log.info("Got storage: "+storage);
        HashSet<String> set = new HashSet<String>();
        set.add("id");
        storage.createTable("rules", set);        
        
        storage.insertRow("rules", map(
        		"id", 1,
        		"dpid", 1,
        		"match", match(1),
        		"action", output(2)
        		));
        
        storage.insertRow("rules", map(
        		"id", 2,
        		"dpid", 1,
        		"match", match(2),
        		"action", output(1)
        		));
        
        storage.insertRow("rules", map(
        		"id", 3,
        		"dpid", 2,
        		"match", match(1),
        		"action", output(2)
        		));    
        
        storage.insertRow("rules", map(
        		"id", 4,
        		"dpid", 2,
        		"match", match(2),
        		"action", output(1)
        		));
        floodlightProvider.addOFSwitchListener(this);
        log.info("Proact OK");
    }

    OFMatch match(int inport){
    	OFMatch m = new OFMatch();
    	m.setInputPort((short) inport);
    	m.setWildcards(OFMatch.OFPFW_ALL & ~OFMatch.OFPFW_IN_PORT);
    	return m;
    }
    
    OFActionOutput output(int port) {
    	OFActionOutput o = new OFActionOutput((short) port, (short) 0);
    	return o;
    }
    
    Map<String, Object> map(Object...objects) {
    	Map<String, Object> res = new LinkedHashMap<String, Object>();
    	if(objects.length % 2 > 0) {
    		throw new IllegalArgumentException("must have even arg count");
    	}
    	for(int i=0; i < objects.length; i+=2) {
    		res.put((String) objects[i], objects[i+1]);
    	}
    	return res;
    }
    
{'fraction_non_alphanumeric': 0.10258261148540262, 'fraction_numerical': 0.006416426050689766, 'mean_word_length': 3.1192599933927982, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '5997688', 'n_tokens_mistral': 3792, 'n_tokens_neox': 3676, 'n_words': 839}