Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 19 | package org.onlab.onos.net.flow.instructions; |
| 20 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 22 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Abstraction of a single traffic treatment step. |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 30 | */ |
| 31 | public abstract class L2ModificationInstruction implements Instruction { |
| 32 | |
| 33 | /** |
| 34 | * Represents the type of traffic treatment. |
| 35 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 36 | public enum L2SubType { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Ether src modification. |
| 39 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 40 | ETH_SRC, |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Ether dst modification. |
| 44 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 45 | ETH_DST, |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 48 | * VLAN id modification. |
| 49 | */ |
| 50 | VLAN_ID, |
| 51 | |
| 52 | /** |
| 53 | * VLAN priority modification. |
| 54 | */ |
| 55 | VLAN_PCP |
| 56 | } |
| 57 | |
| 58 | // TODO: Create factory class 'Instructions' that will have various factory |
| 59 | // to create specific instructions. |
| 60 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 61 | public abstract L2SubType subtype(); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 62 | |
| 63 | @Override |
| 64 | public Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 65 | return Type.L2MODIFICATION; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Represents a L2 src/dst modification instruction. |
| 70 | */ |
| 71 | public static final class ModEtherInstruction extends L2ModificationInstruction { |
| 72 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 73 | private final L2SubType subtype; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 74 | private final MacAddress mac; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 75 | |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 76 | public ModEtherInstruction(L2SubType subType, MacAddress addr) { |
| 77 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 78 | this.subtype = subType; |
| 79 | this.mac = addr; |
| 80 | } |
| 81 | |
| 82 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 83 | public L2SubType subtype() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 84 | return this.subtype; |
| 85 | } |
| 86 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 87 | public MacAddress mac() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 88 | return this.mac; |
| 89 | } |
| 90 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public String toString() { |
| 93 | return toStringHelper(subtype().toString()) |
| 94 | .add("mac", mac).toString(); |
| 95 | } |
| 96 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 99 | return Objects.hash(mac, type(), subtype); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean equals(Object obj) { |
| 104 | if (this == obj) { |
| 105 | return true; |
| 106 | } |
| 107 | if (obj instanceof ModEtherInstruction) { |
| 108 | ModEtherInstruction that = (ModEtherInstruction) obj; |
| 109 | return Objects.equals(mac, that.mac) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 110 | Objects.equals(this.type(), that.type()) && |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 111 | Objects.equals(subtype, that.subtype); |
| 112 | |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 117 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 121 | * Represents a VLAN id modification instruction. |
| 122 | */ |
| 123 | public static final class ModVlanIdInstruction extends L2ModificationInstruction { |
| 124 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 125 | public final VlanId vlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 126 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 127 | public ModVlanIdInstruction(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 128 | this.vlanId = vlanId; |
| 129 | } |
| 130 | |
| 131 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 132 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 133 | return L2SubType.VLAN_ID; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 136 | public VlanId vlanId() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 137 | return this.vlanId; |
| 138 | } |
| 139 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public String toString() { |
| 142 | return toStringHelper(subtype().toString()) |
| 143 | .add("id", vlanId).toString(); |
| 144 | } |
| 145 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 148 | return Objects.hash(vlanId, type(), subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean equals(Object obj) { |
| 153 | if (this == obj) { |
| 154 | return true; |
| 155 | } |
| 156 | if (obj instanceof ModVlanIdInstruction) { |
| 157 | ModVlanIdInstruction that = (ModVlanIdInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 158 | return Objects.equals(vlanId, that.vlanId) && |
| 159 | Objects.equals(this.type(), that.type()) && |
| 160 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 161 | |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 167 | } |
| 168 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Represents a VLAN PCP modification instruction. |
| 171 | */ |
| 172 | public static final class ModVlanPcpInstruction extends L2ModificationInstruction { |
| 173 | |
| 174 | public final Byte vlanPcp; |
| 175 | |
| 176 | public ModVlanPcpInstruction(Byte vlanPcp) { |
| 177 | this.vlanPcp = vlanPcp; |
| 178 | } |
| 179 | |
| 180 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 181 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 182 | return L2SubType.VLAN_PCP; |
| 183 | } |
| 184 | |
| 185 | public Byte vlanPcp() { |
| 186 | return this.vlanPcp; |
| 187 | } |
| 188 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public String toString() { |
| 191 | return toStringHelper(subtype().toString()) |
| 192 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 193 | } |
| 194 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 195 | @Override |
| 196 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 197 | return Objects.hash(vlanPcp, type(), subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean equals(Object obj) { |
| 202 | if (this == obj) { |
| 203 | return true; |
| 204 | } |
| 205 | if (obj instanceof ModVlanPcpInstruction) { |
| 206 | ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 207 | return Objects.equals(vlanPcp, that.vlanPcp) && |
| 208 | Objects.equals(this.type(), that.type()) && |
| 209 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 210 | |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 215 | } |
| 216 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 217 | |
| 218 | } |