| package org.onlab.onos.net.flow.instructions; |
| |
| import static com.google.common.base.MoreObjects.toStringHelper; |
| |
| import java.util.Objects; |
| |
| import org.onlab.packet.MacAddress; |
| import org.onlab.packet.VlanId; |
| |
| /** |
| * Abstraction of a single traffic treatment step. |
| */ |
| public abstract class L2ModificationInstruction implements Instruction { |
| |
| /** |
| * Represents the type of traffic treatment. |
| */ |
| public enum L2SubType { |
| /** |
| * Ether src modification. |
| */ |
| ETH_SRC, |
| |
| /** |
| * Ether dst modification. |
| */ |
| ETH_DST, |
| |
| /** |
| * VLAN id modification. |
| */ |
| VLAN_ID, |
| |
| /** |
| * VLAN priority modification. |
| */ |
| VLAN_PCP |
| } |
| |
| // TODO: Create factory class 'Instructions' that will have various factory |
| // to create specific instructions. |
| |
| public abstract L2SubType subtype(); |
| |
| @Override |
| public Type type() { |
| return Type.L2MODIFICATION; |
| } |
| |
| /** |
| * Represents a L2 src/dst modification instruction. |
| */ |
| public static final class ModEtherInstruction extends L2ModificationInstruction { |
| |
| private final L2SubType subtype; |
| private final MacAddress mac; |
| |
| public ModEtherInstruction(L2SubType subType, MacAddress addr) { |
| |
| this.subtype = subType; |
| this.mac = addr; |
| } |
| |
| @Override |
| public L2SubType subtype() { |
| return this.subtype; |
| } |
| |
| public MacAddress mac() { |
| return this.mac; |
| } |
| |
| @Override |
| public String toString() { |
| return toStringHelper(subtype().toString()) |
| .add("mac", mac).toString(); |
| } |
| |
| @Override |
| public int hashCode() { |
| return Objects.hash(mac, subtype); |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) { |
| return true; |
| } |
| if (obj instanceof ModEtherInstruction) { |
| ModEtherInstruction that = (ModEtherInstruction) obj; |
| return Objects.equals(mac, that.mac) && |
| Objects.equals(subtype, that.subtype); |
| |
| } |
| return false; |
| } |
| |
| |
| } |
| |
| /** |
| * Represents a VLAN id modification instruction. |
| */ |
| public static final class ModVlanIdInstruction extends L2ModificationInstruction { |
| |
| public final VlanId vlanId; |
| |
| public ModVlanIdInstruction(VlanId vlanId) { |
| this.vlanId = vlanId; |
| } |
| |
| @Override |
| public L2SubType subtype() { |
| return L2SubType.VLAN_ID; |
| } |
| |
| public VlanId vlanId() { |
| return this.vlanId; |
| } |
| |
| @Override |
| public String toString() { |
| return toStringHelper(subtype().toString()) |
| .add("id", vlanId).toString(); |
| } |
| |
| @Override |
| public int hashCode() { |
| return Objects.hash(vlanId, subtype()); |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) { |
| return true; |
| } |
| if (obj instanceof ModVlanIdInstruction) { |
| ModVlanIdInstruction that = (ModVlanIdInstruction) obj; |
| return Objects.equals(vlanId, that.vlanId); |
| |
| } |
| return false; |
| } |
| |
| |
| } |
| |
| /** |
| * Represents a VLAN PCP modification instruction. |
| */ |
| public static final class ModVlanPcpInstruction extends L2ModificationInstruction { |
| |
| public final Byte vlanPcp; |
| |
| public ModVlanPcpInstruction(Byte vlanPcp) { |
| this.vlanPcp = vlanPcp; |
| } |
| |
| @Override |
| public L2SubType subtype() { |
| return L2SubType.VLAN_PCP; |
| } |
| |
| public Byte vlanPcp() { |
| return this.vlanPcp; |
| } |
| |
| @Override |
| public String toString() { |
| return toStringHelper(subtype().toString()) |
| .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| } |
| |
| @Override |
| public int hashCode() { |
| return Objects.hash(vlanPcp, subtype()); |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) { |
| return true; |
| } |
| if (obj instanceof ModVlanPcpInstruction) { |
| ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj; |
| return Objects.equals(vlanPcp, that.vlanPcp); |
| |
| } |
| return false; |
| } |
| |
| } |
| |
| |
| } |