2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.flow.instructions;
18 import static com.google.common.base.MoreObjects.toStringHelper;
20 import java.util.Objects;
22 import org.onlab.packet.IpAddress;
25 * Abstraction of a single traffic treatment step.
27 public abstract class L3ModificationInstruction implements Instruction {
30 * Represents the type of traffic treatment.
32 public enum L3SubType {
34 * IPv4 src modification.
39 * IPv4 dst modification.
44 * IPv6 src modification.
49 * IPv6 dst modification.
54 * IPv6 flow label modification.
73 //TODO: remaining types
77 * Returns the subtype of the modification instruction.
78 * @return type of instruction
80 public abstract L3SubType subtype();
83 public final Type type() {
84 return Type.L3MODIFICATION;
88 * Represents a L3 src/dst modification instruction.
90 public static final class ModIPInstruction extends L3ModificationInstruction {
92 private final L3SubType subtype;
93 private final IpAddress ip;
95 ModIPInstruction(L3SubType subType, IpAddress addr) {
97 this.subtype = subType;
102 public L3SubType subtype() {
106 public IpAddress ip() {
111 public String toString() {
112 return toStringHelper(subtype().toString())
113 .add("ip", ip).toString();
117 public int hashCode() {
118 return Objects.hash(type(), subtype(), ip);
122 public boolean equals(Object obj) {
126 if (obj instanceof ModIPInstruction) {
127 ModIPInstruction that = (ModIPInstruction) obj;
128 return Objects.equals(ip, that.ip) &&
129 Objects.equals(this.subtype(), that.subtype());
136 * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction
137 * (20 bits unsigned integer).
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
145 * Creates a new flow mod instruction.
147 * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
149 ModIPv6FlowLabelInstruction(int flowLabel) {
150 this.flowLabel = flowLabel & MASK;
154 public L3SubType subtype() {
155 return L3SubType.IPV6_FLABEL;
159 * Gets the IPv6 flow label to set in the treatment.
161 * @return the IPv6 flow label to set in the treatment (20 bits)
163 public int flowLabel() {
164 return this.flowLabel;
168 public String toString() {
169 return toStringHelper(subtype().toString())
170 .add("flowLabel", Long.toHexString(flowLabel)).toString();
174 public int hashCode() {
175 return Objects.hash(type(), subtype(), flowLabel);
179 public boolean equals(Object obj) {
183 if (obj instanceof ModIPv6FlowLabelInstruction) {
184 ModIPv6FlowLabelInstruction that =
185 (ModIPv6FlowLabelInstruction) obj;
186 return Objects.equals(flowLabel, that.flowLabel);
193 * Represents a L3 TTL modification instruction.
195 public static final class ModTtlInstruction extends L3ModificationInstruction {
197 private final L3SubType subtype;
199 ModTtlInstruction(L3SubType subtype) {
200 this.subtype = subtype;
204 public L3SubType subtype() {
209 public String toString() {
210 return toStringHelper(subtype().toString())
215 public int hashCode() {
216 return Objects.hash(type(), subtype());
220 public boolean equals(Object obj) {
224 if (obj instanceof ModTtlInstruction) {
225 ModTtlInstruction that = (ModTtlInstruction) obj;
226 return Objects.equals(this.subtype(), that.subtype());