SDN开发之基于floodlight控制器做QoS策略
---温州大学 12网络工程
在floodlight中写主要策略。通过python脚本与ubuntu console界面交互
利用floodlight提供的restAPI开发webUI界面
JAVA部分代码:
package net.floodlightcontroller.qos;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.openflow.protocol.OFFlowMod;
import org.openflow.protocol.OFMatch;
import org.openflow.protocol.OFMessage;
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.OFActionEnqueue;
import org.openflow.protocol.action.OFActionNetworkTypeOfService;
import org.openflow.protocol.action.OFActionType;
import org.openflow.util.HexString;
import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IOFMessageListener;
import net.floodlightcontroller.core.IOFSwitch;
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.restserver.IRestApiService;
import net.floodlightcontroller.staticflowentry.IStaticFlowEntryPusherService;
import net.floodlightcontroller.storage.IResultSet;
import net.floodlightcontroller.storage.IStorageSourceService;
import net.floodlightcontroller.storage.StorageException;
import net.floodlightcontroller.qos.QoSPolicy;
import net.floodlightcontroller.qos.QoSTypeOfService;
import net.floodlightcontroller.core.IFloodlightProviderService;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QoS implements IQoSService, IFloodlightModule,
IOFMessageListener {
protected IFloodlightProviderService floodlightProvider;
protected IStaticFlowEntryPusherService flowPusher;
protected List<QoSPolicy> policies; //Synchronized
protected List<QoSTypeOfService> services; //Synchronized
protected IRestApiService restApi;
protected FloodlightContext cntx;
protected IStorageSourceService storageSource;
protected Properties props = new Properties();
protected String[] tools;
protected static Logger logger;
protected boolean enabled;
protected String dpidPattern = "^[\\d|\\D][\\d|\\D]:[\\d|\\D][\\d|\\D]:" +
"[\\d|\\D][\\d|\\D]:[\\d|\\D][\\d|\\D]:" +
"[\\d|\\D][\\d|\\D]:[\\d|\\D][\\d|\\D]:" +
"[\\d|\\D][\\d|\\D]:[\\d|\\D][\\d|\\D]$";
public static final String TABLE_NAME = "controller_qos";
public static final String COLUMN_POLID = "policyid";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_MATCH_PROTOCOL = "protocol";
public static final String COLUMN_MATCH_ETHTYPE = "eth-type";
public static final String COLUMN_MATCH_INGRESSPRT = "ingressport";
public static final String COLUMN_MATCH_IPDST = "ipdst";
public static final String COLUMN_MATCH_IPSRC = "ipsrc";
public static final String COLUMN_MATCH_VLANID = "vlanid";
public static final String COLUMN_MATCH_ETHSRC = "ethsrc";
public static final String COLUMN_MATCH_ETHDST = "ethdst";
public static final String COLUMN_MATCH_TCPUDP_SRCPRT = "tcpudpsrcport";
public static final String COLUMN_MATCH_TCPUDP_DSTPRT = "tcpudpdstport";
public static final String COLUMN_NW_TOS = "nw_tos";
public static final String COLUMN_SW = "switches";
public static final String COLUMN_QUEUE = "queue";
public static final String COLUMN_ENQPORT = "equeueport";
public static final String COLUMN_PRIORITY = "priority";
public static final String COLUMN_SERVICE = "service";
public static String ColumnNames[] = { COLUMN_POLID,
COLUMN_NAME,COLUMN_MATCH_PROTOCOL, COLUMN_MATCH_ETHTYPE,COLUMN_MATCH_INGRESSPRT,
COLUMN_MATCH_IPDST,COLUMN_MATCH_IPSRC,COLUMN_MATCH_VLANID,
COLUMN_MATCH_ETHSRC,COLUMN_MATCH_ETHDST,COLUMN_MATCH_TCPUDP_SRCPRT,
COLUMN_MATCH_TCPUDP_DSTPRT,COLUMN_NW_TOS,COLUMN_SW,
COLUMN_QUEUE,COLUMN_ENQPORT,COLUMN_PRIORITY,COLUMN_SERVICE,};
public static final String TOS_TABLE_NAME = "controller_qos_tos";
public static final String COLUMN_SID = "serviceid";
public static final String COLUMN_SNAME = "servicename";
public static final String COLUMN_TOSBITS = "tosbits";
public static String TOSColumnNames[] = {COLUMN_SID,
COLUMN_SNAME,
COLUMN_TOSBITS};
@Override
public String getName() {
return "qos";
}
@Override
public boolean isCallbackOrderingPrereq(OFType type, String name) {
return false;
}
@Override
public boolean isCallbackOrderingPostreq(OFType type, String name) {
return false;
}
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Collection<Class<? extends IFloodlightService>> l =
new ArrayList<Class<? extends IFloodlightService>>();
l.add(IQoSService.class);
return l;
}
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Map<Class<? extends IFloodlightService>,
IFloodlightService> m =
new HashMap<Class<? extends IFloodlightService>,
IFloodlightService>();
// We are the class that implements the service
m.put(IQoSService.class, this);
return m;
}
@Override
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
//This module should depend on FloodlightProviderService,
// IStorageSourceProviderService, IRestApiService, &
// IStaticFlowEntryPusherService
Collection<Class<? extends IFloodlightService>> l =
new ArrayList<Class<? extends IFloodlightService>>();
l.add(IFloodlightProviderService.class);
l.add(IStorageSourceService.class);
l.add(IRestApiService.class);
l.add(IStaticFlowEntryPusherService.class);
return l;
}
protected ArrayList<QoSPolicy> readPoliciesFromStorage() {
ArrayList<QoSPolicy> l = new ArrayList<QoSPolicy>();
try{
Map<String, Object> row;
IResultSet policySet = storageSource
.executeQuery(TABLE_NAME, ColumnNames, null, null );
for( Iterator<IResultSet> iter = policySet.iterator(); iter.hasNext();){
row = iter.next().getRow();