OFType枚举类列出了openflow消息类型,并将之映射到类型协议值。
package org.openflow.protocol;
import java.lang.reflect.Constructor;
/*
*OFType枚举类,初始化HELLO等共20个消息对象实例,即openflow消息类型,
*第一个参数为openflow消息类型协议值
*第二个参数为openflow消息类的class对象
*第三个参数在新建Instantiable实例同时改写Instantiable接口的instantiate方法,该方法返回openflow消息类实例
*/
public enum OFType {
HELLO (0, OFHello.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFHello();
}}),
ERROR (1, OFError.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFError();
}}),
ECHO_REQUEST (2, OFEchoRequest.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFEchoRequest();
}}),
ECHO_REPLY (3, OFEchoReply.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFEchoReply();
}}),
VENDOR (4, OFVendor.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFVendor();
}}),
FEATURES_REQUEST (5, OFFeaturesRequest.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFFeaturesRequest();
}}),
FEATURES_REPLY (6, OFFeaturesReply.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFFeaturesReply();
}}),
GET_CONFIG_REQUEST (7, OFGetConfigRequest.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFGetConfigRequest();
}}),
GET_CONFIG_REPLY (8, OFGetConfigReply.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFGetConfigReply();
}}),
SET_CONFIG (9, OFSetConfig.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFSetConfig();
}}),
PACKET_IN (10, OFPacketIn.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFPacketIn();
}}),
FLOW_REMOVED (11, OFFlowRemoved.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFFlowRemoved();
}}),
PORT_STATUS (12, OFPortStatus.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFPortStatus();
}}),
PACKET_OUT (13, OFPacketOut.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFPacketOut();
}}),
FLOW_MOD (14, OFFlowMod.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFFlowMod();
}}),
PORT_MOD (15, OFPortMod.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFPortMod();
}}),
STATS_REQUEST (16, OFStatisticsRequest.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFStatisticsRequest();
}}),
STATS_REPLY (17, OFStatisticsReply.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFStatisticsReply();
}}),
BARRIER_REQUEST (18, OFBarrierRequest.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFBarrierRequest();
}}),
BARRIER_REPLY (19, OFBarrierReply.class, new Instantiable() {
@Override
public OFMessage instantiate() {
return new OFBarrierReply();
}});
/*
* OFType对象数组,用于存放OFType对象,数组下表为openflow消息类型协议值,完成映射功能
*/
static OFType[] mapping;
protected Class extends OFMessage> clazz; //声明class类对象
protected Constructor extends OFMessage> constructor; //声明Constructor对象
protected Instantiable instantiable; //声明Instantiable接口对象,用于返回openflow消息类实例
protected byte type; //存放消息类型协议值
/*
*枚举类 OFType的构造函数
*/
OFType(int type, Class extends OFMessage> clazz, Instantiable instantiator) {
this.type = (byte) type; //保存消息类型协议值
this.clazz = clazz; //参考class类实例
this.instantiable = instantiator; //参考Instantiable实例
try {
this.constructor = clazz.getConstructor(new Class[]{}); //获取各消息类的构造函数
} catch (Exception e) {
throw new RuntimeException(
"Failure getting constructor for class: " + clazz, e);
}
OFType.addMapping(this.type, this); //添加枚举对象跟消息协议值的映射
}
/**
* 将枚举类对象映射到消息类型协议值
*/
static public void addMapping(byte i, OFType t) {
if (mapping == null)
mapping = new OFType[32];
OFType.mapping[i] = t;
}
/**
*解除映射
*/
static public void removeMapping(byte i) {
OFType.mapping[i] = null;
}
/**
*通过协议值获取OFType枚举类对象,即openflow消息类型
*/
static public OFType valueOf(Byte i) {
return OFType.mapping[i];
}
/**
* 获取消息类型协议值
*/
public byte getTypeValue() {
return this.type;
}
/**
* @return return the OFMessage subclass corresponding to this OFType
*/
public Class extends OFMessage> toClass() {
return clazz;
}
/**
* 返回对象该枚举类实例的消息类的无参构造函数
*/
public Constructor extends OFMessage> getConstructor() {
return constructor;
}
/**
* Returns a new instance of the OFMessage represented by this OFType
* 返回象该枚举类实例的消息类实例
*/
public OFMessage newInstance() {
return instantiable.instantiate();
}
/**
* 返回Instantiable实例
*/
public Instantiable getInstantiable() {
return instantiable;
}
/**
* 设置Instantiable新参考
*/
public void setInstantiable(Instantiable instantiable) {
this.instantiable = instantiable;
}
}