blob: 41819504390c6ab06648405f0024abb092e641da [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080083 public final 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
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080095 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) &&
alshabib2020b892014-10-20 15:47:08 -0700129 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700130 }
131 return false;
132 }
sangho3f97a17d2015-01-29 22:56:29 -0800133 }
alshabib8ca53902014-10-07 13:11:17 -0700134
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800135 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800136 * 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 Vachuska8ab196c2015-02-17 23:53:48 -0800145 * Creates a new flow mod instruction.
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800146 *
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800147 * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800148 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800149 ModIPv6FlowLabelInstruction(int flowLabel) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800150 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 HIGUCHI6a479642015-02-08 01:28:50 -0800186 return Objects.equals(flowLabel, that.flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800187 }
188 return false;
189 }
190 }
191
192 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800193 * Represents a L3 TTL modification instruction.
194 */
sangho3f97a17d2015-01-29 22:56:29 -0800195 public static final class ModTtlInstruction extends L3ModificationInstruction {
196
197 private final L3SubType subtype;
198
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800199 ModTtlInstruction(L3SubType subtype) {
sangho3f97a17d2015-01-29 22:56:29 -0800200 this.subtype = subtype;
201 }
202
203 @Override
204 public L3SubType subtype() {
205 return this.subtype;
206 }
207
208 @Override
209 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800210 return toStringHelper(subtype().toString())
211 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800212 }
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 HIGUCHIcca5e722015-02-05 14:00:41 -0800224 if (obj instanceof ModTtlInstruction) {
225 ModTtlInstruction that = (ModTtlInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800226 return Objects.equals(this.subtype(), that.subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800227 }
228 return false;
229 }
alshabib7410fea2014-09-16 13:48:39 -0700230 }
alshabib7410fea2014-09-16 13:48:39 -0700231}