alshabib

pretty builders

Change-Id: If03b60f97be1eba3803c6fcb328196a4a195c7e8
......@@ -21,8 +21,6 @@ import org.onlab.onos.net.flow.FlowRule;
import org.onlab.onos.net.flow.FlowRuleService;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
import org.onlab.onos.net.flow.criteria.Criteria;
import org.onlab.onos.net.flow.instructions.Instructions;
import org.onlab.onos.net.host.HostService;
import org.onlab.onos.net.packet.InboundPacket;
import org.onlab.onos.net.packet.PacketContext;
......@@ -153,7 +151,7 @@ public class ReactiveForwarding {
// Sends a packet out the specified port.
private void packetOut(PacketContext context, PortNumber portNumber) {
context.treatmentBuilder().add(Instructions.createOutput(portNumber));
context.treatmentBuilder().setOutput(portNumber);
context.send();
}
......@@ -165,13 +163,13 @@ public class ReactiveForwarding {
// Install the flow rule to handle this type of message from now on.
Ethernet inPkt = context.inPacket().parsed();
TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder();
builder.add(Criteria.matchEthType(inPkt.getEtherType()))
.add(Criteria.matchEthSrc(inPkt.getSourceMAC()))
.add(Criteria.matchEthDst(inPkt.getDestinationMAC()))
.add(Criteria.matchInPort(context.inPacket().receivedFrom().port()));
builder.matchEthType(inPkt.getEtherType())
.matchEthSrc(inPkt.getSourceMAC())
.matchEthDst(inPkt.getDestinationMAC())
.matchInport(context.inPacket().receivedFrom().port());
TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder();
treat.add(Instructions.createOutput(portNumber));
treat.setOutput(portNumber);
FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
builder.build(), treat.build(), 0, appId);
......
......@@ -6,7 +6,12 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.flow.criteria.Criteria;
import org.onlab.onos.net.flow.criteria.Criterion;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.slf4j.Logger;
public final class DefaultTrafficSelector implements TrafficSelector {
......@@ -29,11 +34,47 @@ public final class DefaultTrafficSelector implements TrafficSelector {
private final List<Criterion> selector = new LinkedList<>();
@Override
public TrafficSelector.Builder add(Criterion criterion) {
public Builder add(Criterion criterion) {
selector.add(criterion);
return this;
}
public Builder matchInport(PortNumber port) {
return add(Criteria.matchInPort(port));
}
public Builder matchEthSrc(MacAddress addr) {
return add(Criteria.matchEthSrc(addr));
}
public Builder matchEthDst(MacAddress addr) {
return add(Criteria.matchEthDst(addr));
}
public Builder matchEthType(short ethType) {
return add(Criteria.matchEthType(ethType));
}
public Builder matchVlanId(VlanId vlanId) {
return add(Criteria.matchVlanId(vlanId));
}
public Builder matchVlanPcp(Byte vlanPcp) {
return add(Criteria.matchVlanPcp(vlanPcp));
}
public Builder matchIPProtocol(Byte proto) {
return add(Criteria.matchIPProtocol(proto));
}
public Builder matchIPSrc(IpPrefix ip) {
return add(Criteria.matchIPSrc(ip));
}
public Builder matchIPDst(IpPrefix ip) {
return add(Criteria.matchIPDst(ip));
}
@Override
public TrafficSelector build() {
return new DefaultTrafficSelector(selector);
......
......@@ -6,7 +6,12 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.flow.instructions.Instruction;
import org.onlab.onos.net.flow.instructions.Instructions;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.slf4j.Logger;
public final class DefaultTrafficTreatment implements TrafficTreatment {
......@@ -42,9 +47,10 @@ public final class DefaultTrafficTreatment implements TrafficTreatment {
// TODO: should be a list of instructions based on modification objects
List<Instruction> modifications = new LinkedList<>();
@Override
public Builder add(Instruction instruction) {
if (drop) {
return this;
}
switch (instruction.type()) {
case DROP:
drop = true;
......@@ -67,6 +73,46 @@ public final class DefaultTrafficTreatment implements TrafficTreatment {
}
@Override
public void drop() {
add(Instructions.createDrop());
}
@Override
public Builder setOutput(PortNumber number) {
return add(Instructions.createOutput(number));
}
@Override
public Builder setEthSrc(MacAddress addr) {
return add(Instructions.modL2Src(addr));
}
@Override
public Builder setEthDst(MacAddress addr) {
return add(Instructions.modL2Dst(addr));
}
@Override
public Builder setVlanId(VlanId id) {
return add(Instructions.modVlanId(id));
}
@Override
public Builder setVlanPcp(Byte pcp) {
return add(Instructions.modVlanPcp(pcp));
}
@Override
public Builder setIpSrc(IpPrefix addr) {
return add(Instructions.modL3Src(addr));
}
@Override
public Builder setIpDst(IpPrefix addr) {
return add(Instructions.modL3Dst(addr));
}
@Override
public TrafficTreatment build() {
//If we are dropping should we just return an emptry list?
......
......@@ -2,7 +2,11 @@ package org.onlab.onos.net.flow;
import java.util.List;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.flow.criteria.Criterion;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
/**
* Abstraction of a slice of network traffic.
......@@ -31,6 +35,69 @@ public interface TrafficSelector {
Builder add(Criterion criterion);
/**
* Matches an inport.
* @param port the inport
* @return a selection builder
*/
public Builder matchInport(PortNumber port);
/**
* Matches a l2 src address.
* @param addr a l2 address
* @return a selection builder
*/
public Builder matchEthSrc(MacAddress addr);
/**
* Matches a l2 dst address.
* @param addr a l2 address
* @return a selection builder
*/
public Builder matchEthDst(MacAddress addr);
/**
* Matches the ethernet type.
* @param ethType an ethernet type
* @return a selection builder
*/
public Builder matchEthType(short ethType);
/**
* Matches the vlan id.
* @param vlanId a vlan id
* @return a selection builder
*/
public Builder matchVlanId(VlanId vlanId);
/**
* Matches a vlan priority.
* @param vlanPcp a vlan priority
* @return a selection builder
*/
public Builder matchVlanPcp(Byte vlanPcp);
/**
* Matches the l3 protocol.
* @param proto a l3 protocol
* @return a selection builder
*/
public Builder matchIPProtocol(Byte proto);
/**
* Matches a l3 address.
* @param ip a l3 address
* @return a selection builder
*/
public Builder matchIPSrc(IpPrefix ip);
/**
* Matches a l3 address.
* @param ip a l3 address
* @return a selection builder
*/
public Builder matchIPDst(IpPrefix ip);
/**
* Builds an immutable traffic selector.
*
* @return traffic selector
......
......@@ -2,7 +2,11 @@ package org.onlab.onos.net.flow;
import java.util.List;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.flow.instructions.Instruction;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
/**
* Abstraction of network traffic treatment.
......@@ -22,14 +26,67 @@ public interface TrafficTreatment {
public interface Builder {
/**
* Adds a traffic treatment instruction. If a same type instruction has
* already been added, it will be replaced by this one.
*
* @param instruction new instruction
* Adds an instruction to the builder.
* @param instruction an instruction
* @return a treatment builder
*/
Builder add(Instruction instruction);
/**
* Adds a drop instruction and does not return a builder.
*/
public void drop();
/**
* Set the output port.
* @param number the out port
* @return a treatment builder
*/
public Builder setOutput(PortNumber number);
/**
* Sets the src l2 address.
* @param addr a macaddress
* @return a treatment builder
*/
public Builder setEthSrc(MacAddress addr);
/**
* Sets the dst l2 address.
* @param addr a macaddress
* @return a treatment builder
*/
public Builder setEthDst(MacAddress addr);
/**
* Sets the vlan id.
* @param id a vlanid
* @return a treatment builder
*/
public Builder setVlanId(VlanId id);
/**
* Sets the vlan priority.
* @param pcp a vlan priority
* @return a treatment builder
*/
public Builder setVlanPcp(Byte pcp);
/**
* Sets the src l3 address.
* @param addr an ip
* @return a treatment builder
*/
public Builder setIpSrc(IpPrefix addr);
/**
* Sets the dst l3 address.
* @param addr an ip
* @return a treatment builder
*/
public Builder setIpDst(IpPrefix addr);
/**
* Builds an immutable traffic treatment descriptor.
*
* @return traffic treatment
......
......@@ -65,12 +65,12 @@ public class HostMonitor implements TimerTask {
private final long probeRate;
private Timeout timeout;
private final Timeout timeout;
public HostMonitor(HostService hostService, TopologyService topologyService,
DeviceService deviceService,
HostProvider hostProvider, PacketService packetService,
HostStore hostStore) {
DeviceService deviceService,
HostProvider hostProvider, PacketService packetService,
HostStore hostStore) {
this.hostService = hostService;
this.topologyService = topologyService;
this.deviceService = deviceService;
......@@ -147,10 +147,9 @@ public class HostMonitor implements TimerTask {
List<Instruction> instructions = new ArrayList<>();
instructions.add(Instructions.createOutput(port.number()));
TrafficTreatment treatment =
new DefaultTrafficTreatment.Builder()
.add(Instructions.createOutput(port.number()))
.build();
TrafficTreatment treatment = new DefaultTrafficTreatment.Builder()
.setOutput(port.number())
.build();
OutboundPacket outboundPacket =
new DefaultOutboundPacket(deviceId, treatment,
......@@ -163,9 +162,9 @@ public class HostMonitor implements TimerTask {
ARP arp = new ARP();
arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
.setProtocolType(ARP.PROTO_TYPE_IP)
.setProtocolAddressLength((byte) IpPrefix.INET_LEN);
.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
.setProtocolType(ARP.PROTO_TYPE_IP)
.setProtocolAddressLength((byte) IpPrefix.INET_LEN);
byte[] sourceMacAddress;
if (portAddresses.mac() == null) {
......@@ -175,15 +174,15 @@ public class HostMonitor implements TimerTask {
}
arp.setSenderHardwareAddress(sourceMacAddress)
//TODO .setSenderProtocolAddress(portAddresses.ips().toOctets())
.setTargetHardwareAddress(ZERO_MAC_ADDRESS)
.setTargetProtocolAddress(targetIp.toOctets());
//TODO .setSenderProtocolAddress(portAddresses.ips().toOctets())
.setTargetHardwareAddress(ZERO_MAC_ADDRESS)
.setTargetProtocolAddress(targetIp.toOctets());
Ethernet ethernet = new Ethernet();
ethernet.setEtherType(Ethernet.TYPE_ARP)
.setDestinationMACAddress(BROADCAST_MAC)
.setSourceMACAddress(sourceMacAddress)
.setPayload(arp);
.setDestinationMACAddress(BROADCAST_MAC)
.setSourceMACAddress(sourceMacAddress)
.setPayload(arp);
return ethernet;
}
......
......@@ -13,8 +13,6 @@ import org.onlab.onos.net.flow.FlowRule;
import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
import org.onlab.onos.net.flow.criteria.Criteria;
import org.onlab.onos.net.flow.instructions.Instructions;
import org.onlab.onos.openflow.controller.Dpid;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
......@@ -88,53 +86,53 @@ public class FlowRuleBuilder {
TrafficTreatment.Builder builder = new DefaultTrafficTreatment.Builder();
// If this is a drop rule
if (actions.size() == 0) {
builder.add(Instructions.createDrop());
builder.drop();
return builder.build();
}
for (OFAction act : actions) {
switch (act.getType()) {
case OUTPUT:
OFActionOutput out = (OFActionOutput) act;
builder.add(Instructions.createOutput(
PortNumber.portNumber(out.getPort().getPortNumber())));
builder.setOutput(
PortNumber.portNumber(out.getPort().getPortNumber()));
break;
case SET_VLAN_PCP:
case SET_VLAN_VID:
OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
builder.add(Instructions.modVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan())));
builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
break;
case SET_VLAN_VID:
case SET_VLAN_PCP:
OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
builder.add(Instructions.modVlanId(VlanId.vlanId(pcp.getVlanPcp().getValue())));
builder.setVlanId(VlanId.vlanId(pcp.getVlanPcp().getValue()));
break;
case SET_DL_DST:
OFActionSetDlDst dldst = (OFActionSetDlDst) act;
builder.add(Instructions.modL2Dst(
MacAddress.valueOf(dldst.getDlAddr().getLong())));
builder.setEthDst(
MacAddress.valueOf(dldst.getDlAddr().getLong()));
break;
case SET_DL_SRC:
OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
builder.add(Instructions.modL2Src(
MacAddress.valueOf(dlsrc.getDlAddr().getLong())));
builder.setEthSrc(
MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
break;
case SET_NW_DST:
OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
IPv4Address di = nwdst.getNwAddr();
if (di.isCidrMask()) {
builder.add(Instructions.modL3Dst(IpPrefix.valueOf(di.getInt(),
di.asCidrMaskLength())));
builder.setIpDst(IpPrefix.valueOf(di.getInt(),
di.asCidrMaskLength()));
} else {
builder.add(Instructions.modL3Dst(IpPrefix.valueOf(di.getInt())));
builder.setIpDst(IpPrefix.valueOf(di.getInt()));
}
break;
case SET_NW_SRC:
OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
IPv4Address si = nwsrc.getNwAddr();
if (si.isCidrMask()) {
builder.add(Instructions.modL3Dst(IpPrefix.valueOf(si.getInt(),
si.asCidrMaskLength())));
builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
si.asCidrMaskLength()));
} else {
builder.add(Instructions.modL3Dst(IpPrefix.valueOf(si.getInt())));
builder.setIpSrc(IpPrefix.valueOf(si.getInt()));
}
break;
case SET_TP_DST:
......@@ -174,20 +172,20 @@ public class FlowRuleBuilder {
for (MatchField<?> field : match.getMatchFields()) {
switch (field.id) {
case IN_PORT:
builder.add(Criteria.matchInPort(PortNumber
.portNumber(match.get(MatchField.IN_PORT).getPortNumber())));
builder.matchInport(PortNumber
.portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
break;
case ETH_SRC:
MacAddress sMac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
builder.add(Criteria.matchEthSrc(sMac));
builder.matchEthSrc(sMac);
break;
case ETH_DST:
MacAddress dMac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
builder.add(Criteria.matchEthDst(dMac));
builder.matchEthDst(dMac);
break;
case ETH_TYPE:
int ethType = match.get(MatchField.ETH_TYPE).getValue();
builder.add(Criteria.matchEthType((short) ethType));
builder.matchEthType((short) ethType);
break;
case IPV4_DST:
IPv4Address di = match.get(MatchField.IPV4_DST);
......@@ -197,7 +195,7 @@ public class FlowRuleBuilder {
} else {
dip = IpPrefix.valueOf(di.getInt());
}
builder.add(Criteria.matchIPDst(dip));
builder.matchIPDst(dip);
break;
case IPV4_SRC:
IPv4Address si = match.get(MatchField.IPV4_SRC);
......@@ -207,19 +205,19 @@ public class FlowRuleBuilder {
} else {
sip = IpPrefix.valueOf(si.getInt());
}
builder.add(Criteria.matchIPSrc(sip));
builder.matchIPSrc(sip);
break;
case IP_PROTO:
short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
builder.add(Criteria.matchIPProtocol((byte) proto));
builder.matchIPProtocol((byte) proto);
break;
case VLAN_PCP:
byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
builder.add(Criteria.matchVlanPcp(vlanPcp));
builder.matchVlanPcp(vlanPcp);
break;
case VLAN_VID:
VlanId vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
builder.add(Criteria.matchVlanId(vlanId));
builder.matchVlanId(vlanId);
break;
case ARP_OP:
case ARP_SHA:
......