blob: f8814a026fd97910c634987d747502ab3908ff2d [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
alshabib7410fea2014-09-16 13:48:39 -070017
alshabib99b8fdc2014-09-25 14:30:22 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
alshabib8ca53902014-10-07 13:11:17 -070020import java.util.Objects;
21
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070022import org.onlab.packet.IpAddress;
alshabib7410fea2014-09-16 13:48:39 -070023
24/**
25 * Abstraction of a single traffic treatment step.
alshabib7410fea2014-09-16 13:48:39 -070026 */
27public abstract class L3ModificationInstruction implements Instruction {
28
29 /**
30 * Represents the type of traffic treatment.
31 */
alshabib35edb1a2014-09-16 17:44:44 -070032 public enum L3SubType {
alshabib7410fea2014-09-16 13:48:39 -070033 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080034 * IPv4 src modification.
alshabib7410fea2014-09-16 13:48:39 -070035 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080036 IPV4_SRC,
alshabib7410fea2014-09-16 13:48:39 -070037
38 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080039 * IPv4 dst modification.
alshabib7410fea2014-09-16 13:48:39 -070040 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080041 IPV4_DST,
sangho3f97a17d2015-01-29 22:56:29 -080042
43 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080044 * 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.
sangho3f97a17d2015-01-29 22:56:29 -080060 */
61 DEC_TTL,
62
63 /**
64 * Copy TTL out.
65 */
66 TTL_OUT,
67
68 /**
69 * Copy TTL in.
70 */
71 TTL_IN
alshabib7410fea2014-09-16 13:48:39 -070072
73 //TODO: remaining types
74 }
75
76 /**
77 * Returns the subtype of the modification instruction.
78 * @return type of instruction
79 */
alshabib35edb1a2014-09-16 17:44:44 -070080 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -070081
82 @Override
83 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070084 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -070085 }
86
87 /**
88 * Represents a L3 src/dst modification instruction.
89 */
90 public static final class ModIPInstruction extends L3ModificationInstruction {
91
alshabib35edb1a2014-09-16 17:44:44 -070092 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070093 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -070094
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070095 public ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -070096
alshabib7410fea2014-09-16 13:48:39 -070097 this.subtype = subType;
98 this.ip = addr;
99 }
100
101 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700102 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700103 return this.subtype;
104 }
105
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700106 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -0700107 return this.ip;
108 }
109
alshabib99b8fdc2014-09-25 14:30:22 -0700110 @Override
111 public String toString() {
112 return toStringHelper(subtype().toString())
113 .add("ip", ip).toString();
114 }
115
alshabib8ca53902014-10-07 13:11:17 -0700116 @Override
117 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800118 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -0700119 }
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;
alshabib2020b892014-10-20 15:47:08 -0700128 return Objects.equals(ip, that.ip) &&
129 Objects.equals(this.type(), that.type()) &&
130 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700131
132 }
133 return false;
134 }
sangho3f97a17d2015-01-29 22:56:29 -0800135 }
alshabib8ca53902014-10-07 13:11:17 -0700136
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800137 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800138 * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction
139 * (20 bits unsigned integer).
140 */
141 public static final class ModIPv6FlowLabelInstruction
142 extends L3ModificationInstruction {
143 private static final int MASK = 0xfffff;
144 private final int flowLabel; // IPv6 flow label: 20 bits
145
146 /**
147 * Constructor.
148 *
149 * flowLabel the IPv6 flow label to set in the treatment (20 bits)
150 */
151 public ModIPv6FlowLabelInstruction(int flowLabel) {
152 this.flowLabel = flowLabel & MASK;
153 }
154
155 @Override
156 public L3SubType subtype() {
157 return L3SubType.IPV6_FLABEL;
158 }
159
160 /**
161 * Gets the IPv6 flow label to set in the treatment.
162 *
163 * @return the IPv6 flow label to set in the treatment (20 bits)
164 */
165 public int flowLabel() {
166 return this.flowLabel;
167 }
168
169 @Override
170 public String toString() {
171 return toStringHelper(subtype().toString())
172 .add("flowLabel", Long.toHexString(flowLabel)).toString();
173 }
174
175 @Override
176 public int hashCode() {
177 return Objects.hash(type(), subtype(), flowLabel);
178 }
179
180 @Override
181 public boolean equals(Object obj) {
182 if (this == obj) {
183 return true;
184 }
185 if (obj instanceof ModIPv6FlowLabelInstruction) {
186 ModIPv6FlowLabelInstruction that =
187 (ModIPv6FlowLabelInstruction) obj;
188 return Objects.equals(flowLabel, that.flowLabel) &&
189 Objects.equals(this.type(), that.type()) &&
190 Objects.equals(this.subtype(), that.subtype());
191 }
192 return false;
193 }
194 }
195
196 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800197 * Represents a L3 TTL modification instruction.
198 */
sangho3f97a17d2015-01-29 22:56:29 -0800199 public static final class ModTtlInstruction extends L3ModificationInstruction {
200
201 private final L3SubType subtype;
202
203 public ModTtlInstruction(L3SubType subtype) {
204 this.subtype = subtype;
205 }
206
207 @Override
208 public L3SubType subtype() {
209 return this.subtype;
210 }
211
212 @Override
213 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800214 return toStringHelper(subtype().toString())
215 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800216 }
217
218 @Override
219 public int hashCode() {
220 return Objects.hash(type(), subtype());
221 }
222
223 @Override
224 public boolean equals(Object obj) {
225 if (this == obj) {
226 return true;
227 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800228 if (obj instanceof ModTtlInstruction) {
229 ModTtlInstruction that = (ModTtlInstruction) obj;
230 return Objects.equals(this.subtype(), that.subtype()) &&
231 Objects.equals(this.type(), that.type());
sangho3f97a17d2015-01-29 22:56:29 -0800232
233 }
234 return false;
235 }
alshabib7410fea2014-09-16 13:48:39 -0700236 }
alshabib7410fea2014-09-16 13:48:39 -0700237}