Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.instructions; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 17 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Abstraction of a single traffic treatment step. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 26 | */ |
| 27 | public abstract class L3ModificationInstruction implements Instruction { |
| 28 | |
| 29 | /** |
| 30 | * Represents the type of traffic treatment. |
| 31 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 32 | public enum L3SubType { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 33 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 34 | * IPv4 src modification. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 35 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 36 | IPV4_SRC, |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 39 | * IPv4 dst modification. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 40 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 41 | IPV4_DST, |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 44 | * IPv6 src modification. |
| 45 | */ |
| 46 | IPV6_SRC, |
| 47 | |
| 48 | /** |
| 49 | * IPv6 dst modification. |
| 50 | */ |
| 51 | IPV6_DST, |
| 52 | |
| 53 | /** |
| 54 | * IPv6 flow label modification. |
| 55 | */ |
| 56 | IPV6_FLABEL, |
| 57 | |
| 58 | /** |
| 59 | * Decrement TTL. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 60 | */ |
| 61 | DEC_TTL, |
| 62 | |
| 63 | /** |
| 64 | * Copy TTL out. |
| 65 | */ |
| 66 | TTL_OUT, |
| 67 | |
| 68 | /** |
| 69 | * Copy TTL in. |
| 70 | */ |
| 71 | TTL_IN |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 72 | |
| 73 | //TODO: remaining types |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the subtype of the modification instruction. |
| 78 | * @return type of instruction |
| 79 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 80 | public abstract L3SubType subtype(); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 81 | |
| 82 | @Override |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 83 | public final Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 84 | return Type.L3MODIFICATION; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Represents a L3 src/dst modification instruction. |
| 89 | */ |
| 90 | public static final class ModIPInstruction extends L3ModificationInstruction { |
| 91 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 92 | private final L3SubType subtype; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 93 | private final IpAddress ip; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 94 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 95 | ModIPInstruction(L3SubType subType, IpAddress addr) { |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 96 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 97 | this.subtype = subType; |
| 98 | this.ip = addr; |
| 99 | } |
| 100 | |
| 101 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 102 | public L3SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 103 | return this.subtype; |
| 104 | } |
| 105 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 106 | public IpAddress ip() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 107 | return this.ip; |
| 108 | } |
| 109 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public String toString() { |
| 112 | return toStringHelper(subtype().toString()) |
| 113 | .add("ip", ip).toString(); |
| 114 | } |
| 115 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 116 | @Override |
| 117 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 118 | return Objects.hash(type(), subtype(), ip); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean equals(Object obj) { |
| 123 | if (this == obj) { |
| 124 | return true; |
| 125 | } |
| 126 | if (obj instanceof ModIPInstruction) { |
| 127 | ModIPInstruction that = (ModIPInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 128 | return Objects.equals(ip, that.ip) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 129 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 130 | } |
| 131 | return false; |
| 132 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 133 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 135 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 136 | * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction |
| 137 | * (20 bits unsigned integer). |
| 138 | */ |
| 139 | public static final class ModIPv6FlowLabelInstruction |
| 140 | extends L3ModificationInstruction { |
| 141 | private static final int MASK = 0xfffff; |
| 142 | private final int flowLabel; // IPv6 flow label: 20 bits |
| 143 | |
| 144 | /** |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 145 | * Creates a new flow mod instruction. |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 146 | * |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 147 | * @param flowLabel the IPv6 flow label to set in the treatment (20 bits) |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 148 | */ |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 149 | ModIPv6FlowLabelInstruction(int flowLabel) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 150 | this.flowLabel = flowLabel & MASK; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public L3SubType subtype() { |
| 155 | return L3SubType.IPV6_FLABEL; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Gets the IPv6 flow label to set in the treatment. |
| 160 | * |
| 161 | * @return the IPv6 flow label to set in the treatment (20 bits) |
| 162 | */ |
| 163 | public int flowLabel() { |
| 164 | return this.flowLabel; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public String toString() { |
| 169 | return toStringHelper(subtype().toString()) |
| 170 | .add("flowLabel", Long.toHexString(flowLabel)).toString(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public int hashCode() { |
| 175 | return Objects.hash(type(), subtype(), flowLabel); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public boolean equals(Object obj) { |
| 180 | if (this == obj) { |
| 181 | return true; |
| 182 | } |
| 183 | if (obj instanceof ModIPv6FlowLabelInstruction) { |
| 184 | ModIPv6FlowLabelInstruction that = |
| 185 | (ModIPv6FlowLabelInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 186 | return Objects.equals(flowLabel, that.flowLabel); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 193 | * Represents a L3 TTL modification instruction. |
| 194 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 195 | public static final class ModTtlInstruction extends L3ModificationInstruction { |
| 196 | |
| 197 | private final L3SubType subtype; |
| 198 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 199 | ModTtlInstruction(L3SubType subtype) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 200 | this.subtype = subtype; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public L3SubType subtype() { |
| 205 | return this.subtype; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public String toString() { |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 210 | return toStringHelper(subtype().toString()) |
| 211 | .toString(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public int hashCode() { |
| 216 | return Objects.hash(type(), subtype()); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public boolean equals(Object obj) { |
| 221 | if (this == obj) { |
| 222 | return true; |
| 223 | } |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 224 | if (obj instanceof ModTtlInstruction) { |
| 225 | ModTtlInstruction that = (ModTtlInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 226 | return Objects.equals(this.subtype(), that.subtype()); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 227 | } |
| 228 | return false; |
| 229 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 230 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 231 | } |