blob: e8b72e7a9fc12ce63bf39bad5e602d124618b4a8 [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 */
alshabib7410fea2014-09-16 13:48:39 -070016package org.onlab.onos.net.flow.instructions;
17
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 */
alshabib99b8fdc2014-09-25 14:30:22 -070041 IP_DST
alshabib7410fea2014-09-16 13:48:39 -070042
43 //TODO: remaining types
44 }
45
46 /**
47 * Returns the subtype of the modification instruction.
48 * @return type of instruction
49 */
alshabib35edb1a2014-09-16 17:44:44 -070050 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -070051
52 @Override
53 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070054 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -070055 }
56
57 /**
58 * Represents a L3 src/dst modification instruction.
59 */
60 public static final class ModIPInstruction extends L3ModificationInstruction {
61
alshabib35edb1a2014-09-16 17:44:44 -070062 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070063 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -070064
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070065 public ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -070066
alshabib7410fea2014-09-16 13:48:39 -070067 this.subtype = subType;
68 this.ip = addr;
69 }
70
71 @Override
alshabib35edb1a2014-09-16 17:44:44 -070072 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -070073 return this.subtype;
74 }
75
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070076 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -070077 return this.ip;
78 }
79
alshabib99b8fdc2014-09-25 14:30:22 -070080 @Override
81 public String toString() {
82 return toStringHelper(subtype().toString())
83 .add("ip", ip).toString();
84 }
85
alshabib8ca53902014-10-07 13:11:17 -070086 @Override
87 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -080088 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -070089 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) {
94 return true;
95 }
96 if (obj instanceof ModIPInstruction) {
97 ModIPInstruction that = (ModIPInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -070098 return Objects.equals(ip, that.ip) &&
99 Objects.equals(this.type(), that.type()) &&
100 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700101
102 }
103 return false;
104 }
105
alshabib7410fea2014-09-16 13:48:39 -0700106 }
alshabib7410fea2014-09-16 13:48:39 -0700107}