else {
flowModBodySchema = protocol.getType("of13.flow_mod_body_add");
isDelete = false;
}
GenericRecordBuilder flowModBodyBuilder = new GenericRecordBuilder(flowModBodySchema);
GenericRecord flowModBodyRecord = flowModBodyBuilder.build();
GenericRecord actionSetRecord = null; // TODO Empty actions instead of null!
List <GenericRecord> instructions = new ArrayList<>();
List <GenericRecord> matches = new ArrayList<>();
for (String key : args.keySet()) {
if (args.get(key) == null)
continue;
/*
* PRIORITY
*/
if (key.equals("priority")) {
short priority = U16.t(Integer.valueOf((String) args.get(key)));
byte temp [] = {(byte)(priority >> 8), (byte)(priority) };
GenericData.Fixed priorityFixed = new GenericData.Fixed(uint_16Schema, temp);
flowModBodyRecord.put("priority", priorityFixed);
/*
* ACTIONS
*/
} else if ((key.equals("goto_table")) || (key.equals("write_metadata")) || (key.equals("meter"))) {
continue;
} else if ( (key.equals("apply_actions")) || (key.equals("write_actions")) || (key.equals("clear_actions"))) {
actionSetRecord = parseActionString((String) args.get(key));
/*
* Build Write Actions Instruction for Test
*/
Schema instrHeaderSchema = null;
Schema instrSchema = null;
GenericRecord instrHeaderRecord = null;
GenericRecord instrRecord = null;
if (key.equals("apply_actions")) {
instrHeaderSchema = protocol.getType("of13.instruction_apply_actions_header");
instrSchema = protocol.getType("of13.ofp_instruction_apply_actions");
instrRecord = new GenericData.Record(instrSchema);
} else if (key.equals("write_actions")) {
instrHeaderSchema = protocol.getType("of13.instruction_write_actions_header");
instrSchema = protocol.getType("of13.ofp_instruction_write_actions");
instrRecord = new GenericData.Record(instrSchema);
} else if (key.equals("clear_actions")) {
instrHeaderSchema = protocol.getType("of13.instruction_clear_actions_header");
instrSchema = protocol.getType("of13.ofp_instruction_clear_actions");
instrRecord = new GenericData.Record(instrSchema);
}
GenericRecordBuilder instrHeaderBuilder = new GenericRecordBuilder(instrHeaderSchema);
instrHeaderRecord = instrHeaderBuilder.build();
instrRecord.put("header", instrHeaderRecord);
instrRecord.put("actions", actionSetRecord);
instrHeaderRecord.put("length", getUint16Fixed(calculateLength(instrSchema, instrRecord)));
instrRecord.put("header", instrHeaderRecord);
/*
* Create ofp_instruction_write_actions
*/
Schema ofpInstrSchema = protocol.getType("of13.ofp_instruction");
GenericRecord ofpInstrRecord = new GenericData.Record(ofpInstrSchema);
ofpInstrRecord.put("instruction", instrRecord);
instructions.add(ofpInstrRecord);
/*
* MATCH - INGRESS PORT
*/
} else if (key.equals("in_port")) {
/*Schema oxmTlvIngressPortSchema = protocol.getType("of13.oxm_tlv_ingress_port");
GenericRecord oxmTlvIngressPortRecord = new GenericData.Record(oxmTlvIngressPortSchema);
int inPort = Integer.valueOf((String) args.get(key));
oxmTlvIngressPortRecord.put("tlv", getUint32Fixed(inPort));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvIngressPortRecord);*/
matches.add(getMatch("of13.oxm_tlv_ingress_port", getUint32Fixed(Integer.valueOf((String) args.get(key)))));
/*
* MATCH - IN_PHY PORT
*/
} else if (key.equals("in_phy_port")) {
Schema oxmTlvInPhyPortSchema = protocol.getType("of13.oxm_tlv_in_phy_port");
GenericRecord oxmTlvInPhyPortRecord = new GenericData.Record(oxmTlvInPhyPortSchema);
int inPort = Integer.valueOf((String) args.get(key));
oxmTlvInPhyPortRecord.put("tlv", getUint32Fixed(inPort));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvInPhyPortRecord);
matches.add(oxmTlvRecord);
/*
* MATCH - METADATA
*/
} else if (key.equals("metadata")) {
Schema oxmTlvMetadataSchema = protocol.getType("of13.oxm_tlv_metadata");
GenericRecord oxmTlvMetadataRecord = new GenericData.Record(oxmTlvMetadataSchema);
long mdata = Integer.valueOf((String) args.get(key));
oxmTlvMetadataRecord.put("tlv", getUint64Fixed(mdata));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvMetadataRecord);
matches.add(oxmTlvRecord);
/*
* MATCH - ETH_SRC
*/
} else if (key.equals("dl_src")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_eth_src");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint48Fixed(get_mac_addr((String) args.get(key))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
matches.add(oxmTlvRecord);
/*
* MATCH - ETH_DST
*/
} else if (key.equals("dl_dst")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_eth_dst");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint48Fixed(get_mac_addr((String) args.get(key))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
matches.add(oxmTlvRecord);
/*
* MATCH - ETH_TYPE
*/
} else if (key.equals("dl_type")) {
Schema oxmTlvEthTypeSchema = protocol.getType("of13.oxm_tlv_eth_type");
GenericRecord oxmTlvEthTypeRecord = new GenericData.Record(oxmTlvEthTypeSchema);
short ethType = U16.t(Integer.valueOf(((String) args.get(key)).replaceFirst("0x", ""), 16));
oxmTlvEthTypeRecord.put("tlv", getUint16Fixed(ethType));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvEthTypeRecord);
matches.add(oxmTlvRecord);
if (this.delayedIpv4Src != null)
matches.add(this.delayedIpv4Src);
if (this.delayedIpv4Dst != null)
matches.add(this.delayedIpv4Dst);
if (this.delayedIpv6Src != null)
matches.add(this.delayedIpv6Src);
if (this.delayedIpv6Dst != null)
matches.add(this.delayedIpv6Dst);
if (this.delayedIpProto != null)
matches.add(this.delayedIpProto);
this.isEtherType = true;
/*
* VLAN_VID
*/
} else if (key.equals("vlan_vid")) {
Schema oxmTlvVlanVidSchema = protocol.getType("of13.oxm_tlv_vlan_vid");
GenericRecord oxmTlvVlanVidRecord = new GenericData.Record(oxmTlvVlanVidSchema);
short vid = U16.t(Integer.valueOf((String) args.get(key)));
oxmTlvVlanVidRecord.put("tlv", getUint16Fixed(vid));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvVlanVidRecord);
matches.add(oxmTlvRecord);
if (this.delayedVlanPcp != null)
matches.add(this.delayedVlanPcp);
else
this.isVlanVid = true;
/*
* VLAN_PCP
*/
} else if (key.equals("dl_vlan_pcp")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_vlan_pcp");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
byte tmp = U8.t(Short.valueOf((String) args.get(key)));
oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isVlanVid == true)
matches.add(oxmTlvRecord);
else
this.delayedVlanPcp = oxmTlvRecord;
/*
* MATCH - IPV6_SRC
*/
} else if (key.equals("ipv6_src")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv6_src");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint128Fixed(get_ipv6((String) args.get(key))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isEtherType)
matches.add(oxmTlvRecord);
else
this.delayedIpv6Src = oxmTlvRecord;
/*
* MATCH - IPV6_SRC
*/
} else if (key.equals("ipv6_dst")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv6_dst");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint128Fixed(get_ipv6((String) args.get(key))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isEtherType)
matches.add(oxmTlvRecord);
else
this.delayedIpv6Dst = oxmTlvRecord;
/*
* OXM_OF_IP_DSCP
*/
} else if (key.equals("ip_dscp")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ip_dscp");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
byte tmp = U8.t(Short.valueOf((String) args.get(key)));
oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
matches.add(oxmTlvRecord);
/*
* OXM_OF_IP_ECN
*/
} else if (key.equals("ip_ecn")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ip_ecn");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
byte tmp = U8.t(Short.valueOf((String) args.get(key)));
oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
matches.add(oxmTlvRecord);
/*
* OXM_OF_IP_PROTO
*/
} else if (key.equals("nw_proto")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ip_proto");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
byte tmp = U8.t(Short.valueOf((String) args.get(key)));
oxmTlvFieldRecord.put("tlv", getUint8Fixed(tmp));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isEtherType) {
matches.add(oxmTlvRecord);
this.isIpProto = true;
}
else {
this.delayedIpProto = oxmTlvRecord;
}
if (this.delayedTcpSrc != null)
matches.add(this.delayedTcpSrc);
if (this.delayedTcpDst != null)
matches.add(this.delayedTcpDst);
if (this.delayedUdpSrc != null)
matches.add(this.delayedUdpSrc);
if (this.delayedUdpDst != null)
matches.add(this.delayedUdpDst);
/* if (this.delayedIpv6Src != null)
matches.add(this.delayedIpv6Src);
if (this.delayedIpv6Dst != null)
matches.add(this.delayedIpv6Dst);
if (this.delayedIpProto != null)
matches.add(this.delayedIpProto);*/
/*
* MATCH - IPV4_SRC
*/
} else if (key.equals("nw_src")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv4_src");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint32Fixed(get_ipv4((String) args.get(key))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isEtherType)
matches.add(oxmTlvRecord);
else
this.delayedIpv4Src = oxmTlvRecord;
/*
* MATCH - IPV4_DST
*/
} else if (key.equals("nw_dst")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_ipv4_dst");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint32Fixed(get_ipv4((String) args.get(key))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isEtherType)
matches.add(oxmTlvRecord);
else
this.delayedIpv4Dst = oxmTlvRecord;
/*
* MATCH - TCP_SRC
*/
} else if (key.equals("tp_src")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_tcp_src");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isIpProto)
matches.add(oxmTlvRecord);
else
this.delayedTcpSrc = oxmTlvRecord;
/*
* MATCH - TCP_DST
*/
} else if (key.equals("tp_dst")) {
Schema oxmTlvFieldSchema = protocol.getType("of13.oxm_tlv_tcp_dst");
GenericRecord oxmTlvFieldRecord = new GenericData.Record(oxmTlvFieldSchema);
oxmTlvFieldRecord.put("tlv", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericRecord oxmTlvRecord = new GenericData.Record(oxmTlvSchema);
oxmTlvRecord.put("match", oxmTlvFieldRecord);
if (this.isIpProto)
matches.add(oxmTlvRecord);
else
this.delayedTcpDst = oxmTlvRecord;
/*
* MATCH - UDP_SRC
*/
} else if (key.equals("udp_src")) {
if (this.isIpProto)
matches.add(getMatch("of13.oxm_tlv_udp_src", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key))))));
else
this.delayedUdpSrc = getMatch("of13.oxm_tlv_udp_src", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));
/*
* MATCH - UDP_DST
*/
} else if (key.equals("udp_dst")) {
if (this.isIpProto)
matches.add(getMatch("of13.oxm_tlv_udp_dst", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key))))));
else
this.delayedUdpDst = getMatch("of13.oxm_tlv_udp_dst", getUint16Fixed(U16.t(Integer.valueOf((String) args.get(key)))));
}
}
Schema oxmTlvSchema = protocol.getType("of13.oxm_tlv");
GenericArray<GenericRecord> oxmTlvsArray = new GenericData.Array<>(Schema.createArray(oxmTlvSchema), matches);
Schema oxmTlvFieldsSchema = protocol.getType("of13.oxm_tlv_fields");
GenericRecord oxmTlvFieldsRecord = new GenericData.Record(oxmTlvFieldsSchema);
oxmTlvFieldsRecord.put("oxm_tlvs", oxmTlvsArray);
/*
* Build match header
*/
Schema matchHeaderSchema = protocol.getType("of13.match_header");
GenericRecordBuilder matchHeaderBuilder = new GenericRecordBuilder(matchHeaderSchema);
GenericRecord matchHeaderRecord = matchHeaderBuilder.build();
/*
* Calculating oxm_tlvs length
*/
ByteArrayOutputStream oxmOut = new ByteArrayOutputStream();
DatumWriter<GenericRecord> oxmWriter = new GenericDatumWriter<GenericRecord>(oxmTlvFieldsSchema);
Encoder oxmEncoder = EncoderFactory.get().binaryNonEncoder(oxmOut, null);
int closingPadLength = 4;
try {
oxmWriter.write(oxmTlvFieldsRecord, oxmEncoder);
oxmEncoder.flush();
int matchLength = oxmOut.size() + 4;
closingPadLength = (int) ((matchLength + 7)/8*8 - matchLength);
Schema uint16Schema = protocol.getType("of13.uint_16");
byte len[] = {(byte)(matchLength >> 8), (byte)(255 & matchLength)};
GenericData.Fixed lenght = new GenericData.Fixed(uint16Schema, len);
matchHeaderRecord.put("length", lenght);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
* Build closing pad
*/
ByteBuffer clP = ByteBuffer.allocate(closingPadLength);
// GenericData.Fixed closingPadRecord = new GenericData.Fixed(Schema.createFixed("", "", "of", closingPadLength), clP.toByteArray());
/*
* Assemble ofp_match structure
*/
Schema ofpMatchSchema = protocol.getType("of13.ofp_match");
GenericRecord ofpMatchRecord = new GenericData.Record(ofpMatchSchema);
ofpMatchRecord.put("header", matchHeaderRecord);
ofpMatchRecord.put("fields", oxmTlvFieldsRecord);
ofpMatchRecord.put("closing_pad", clP);
/* I N S T R U C T I O N S */
/*
* Build Instruction records
* Build GoTo Instruction for Test
*/
Schema ofpInstrSchema = protocol.getType("of13.ofp_instruction");
/* GenericRecord ofpInstrRecord = new GenericData.Record(ofpInstrSchema);
Schema ofpInstrGoToSchema = protocol.getType("of13.ofp_instruction_goto_table");
GenericRecordBuilder instrBuilder = new GenericRecordBuilder(ofpInstrGoToSchema);
GenericRecord ofpInstrGoToRecord = instrBuilder.build();
ofpInstrRecord.put("instruction", ofpInstrGoToRecord);
instructions.add(ofpInstrRecord);
*/
GenericArray<GenericRecord> instrArray = new GenericData.Array<>(Schema.createArray(ofpInstrSchema), instructions);
/*
* Create Instruction Set
*/
Schema instrSetSchema = protocol.getType("of13.instruction_set");
GenericRecord instrSetRecord = new GenericData.Record(instrSetSchema);
instrSetRecord.put("set", instrArray);
/* F L O W M O D M E S S A G E */
/*
* Create FlowMod Header
*/
GenericRecordBuilder flowModHeaderBuilder = new GenericRecordBuilder(flowModHeaderSchema);
GenericRecord flowModHeaderRecord = flowModHeaderBuilder.build();
/*
* Assemble Flow_mod message
*/
ofpFlowModRecord.put("header", flowModHeaderRecord);