blob: 139cc18e3cdb012efeaf785f41f6b5073bbc0806 [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 /**
34 * Ether src modification.
35 */
alshabib99b8fdc2014-09-25 14:30:22 -070036 IP_SRC,
alshabib7410fea2014-09-16 13:48:39 -070037
38 /**
39 * Ether dst modification.
40 */
sangho3f97a17d2015-01-29 22:56:29 -080041 IP_DST,
42
43 /**
44 * Decrease TTL.
45 */
46 DEC_TTL,
47
48 /**
49 * Copy TTL out.
50 */
51 TTL_OUT,
52
53 /**
54 * Copy TTL in.
55 */
56 TTL_IN
alshabib7410fea2014-09-16 13:48:39 -070057
58 //TODO: remaining types
59 }
60
61 /**
62 * Returns the subtype of the modification instruction.
63 * @return type of instruction
64 */
alshabib35edb1a2014-09-16 17:44:44 -070065 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -070066
67 @Override
68 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070069 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -070070 }
71
72 /**
73 * Represents a L3 src/dst modification instruction.
74 */
75 public static final class ModIPInstruction extends L3ModificationInstruction {
76
alshabib35edb1a2014-09-16 17:44:44 -070077 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070078 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -070079
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070080 public ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -070081
alshabib7410fea2014-09-16 13:48:39 -070082 this.subtype = subType;
83 this.ip = addr;
84 }
85
86 @Override
alshabib35edb1a2014-09-16 17:44:44 -070087 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -070088 return this.subtype;
89 }
90
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070091 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -070092 return this.ip;
93 }
94
alshabib99b8fdc2014-09-25 14:30:22 -070095 @Override
96 public String toString() {
97 return toStringHelper(subtype().toString())
98 .add("ip", ip).toString();
99 }
100
alshabib8ca53902014-10-07 13:11:17 -0700101 @Override
102 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800103 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -0700104 }
105
106 @Override
107 public boolean equals(Object obj) {
108 if (this == obj) {
109 return true;
110 }
111 if (obj instanceof ModIPInstruction) {
112 ModIPInstruction that = (ModIPInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700113 return Objects.equals(ip, that.ip) &&
114 Objects.equals(this.type(), that.type()) &&
115 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700116
117 }
118 return false;
119 }
sangho3f97a17d2015-01-29 22:56:29 -0800120 }
alshabib8ca53902014-10-07 13:11:17 -0700121
sangho3f97a17d2015-01-29 22:56:29 -0800122 public static final class ModTtlInstruction extends L3ModificationInstruction {
123
124 private final L3SubType subtype;
125
126 public ModTtlInstruction(L3SubType subtype) {
127 this.subtype = subtype;
128 }
129
130 @Override
131 public L3SubType subtype() {
132 return this.subtype;
133 }
134
135 @Override
136 public String toString() {
137 return subtype().toString();
138 }
139
140 @Override
141 public int hashCode() {
142 return Objects.hash(type(), subtype());
143 }
144
145 @Override
146 public boolean equals(Object obj) {
147 if (this == obj) {
148 return true;
149 }
150 if (obj instanceof ModIPInstruction) {
151 ModIPInstruction that = (ModIPInstruction) obj;
152 return Objects.equals(this.type(), that.type()) &&
153 Objects.equals(this.subtype(), that.subtype());
154
155 }
156 return false;
157 }
alshabib7410fea2014-09-16 13:48:39 -0700158 }
alshabib7410fea2014-09-16 13:48:39 -0700159}