blob: 8aff5df6a13bf648d75cdd577719157af75577c1 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib7410fea2014-09-16 13:48:39 -070019package org.onlab.onos.net.flow.instructions;
20
alshabib99b8fdc2014-09-25 14:30:22 -070021import static com.google.common.base.MoreObjects.toStringHelper;
22
alshabib8ca53902014-10-07 13:11:17 -070023import java.util.Objects;
24
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070025import org.onlab.packet.IpPrefix;
alshabib7410fea2014-09-16 13:48:39 -070026
27/**
28 * Abstraction of a single traffic treatment step.
alshabib7410fea2014-09-16 13:48:39 -070029 */
30public abstract class L3ModificationInstruction implements Instruction {
31
32 /**
33 * Represents the type of traffic treatment.
34 */
alshabib35edb1a2014-09-16 17:44:44 -070035 public enum L3SubType {
alshabib7410fea2014-09-16 13:48:39 -070036 /**
37 * Ether src modification.
38 */
alshabib99b8fdc2014-09-25 14:30:22 -070039 IP_SRC,
alshabib7410fea2014-09-16 13:48:39 -070040
41 /**
42 * Ether dst modification.
43 */
alshabib99b8fdc2014-09-25 14:30:22 -070044 IP_DST
alshabib7410fea2014-09-16 13:48:39 -070045
46 //TODO: remaining types
47 }
48
49 /**
50 * Returns the subtype of the modification instruction.
51 * @return type of instruction
52 */
alshabib35edb1a2014-09-16 17:44:44 -070053 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -070054
55 @Override
56 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070057 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -070058 }
59
60 /**
61 * Represents a L3 src/dst modification instruction.
62 */
63 public static final class ModIPInstruction extends L3ModificationInstruction {
64
alshabib35edb1a2014-09-16 17:44:44 -070065 private final L3SubType subtype;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070066 private final IpPrefix ip;
alshabib7410fea2014-09-16 13:48:39 -070067
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070068 public ModIPInstruction(L3SubType subType, IpPrefix addr) {
alshabib64231f62014-09-16 17:58:36 -070069
alshabib7410fea2014-09-16 13:48:39 -070070 this.subtype = subType;
71 this.ip = addr;
72 }
73
74 @Override
alshabib35edb1a2014-09-16 17:44:44 -070075 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -070076 return this.subtype;
77 }
78
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070079 public IpPrefix ip() {
alshabib7410fea2014-09-16 13:48:39 -070080 return this.ip;
81 }
82
alshabib99b8fdc2014-09-25 14:30:22 -070083 @Override
84 public String toString() {
85 return toStringHelper(subtype().toString())
86 .add("ip", ip).toString();
87 }
88
alshabib8ca53902014-10-07 13:11:17 -070089 @Override
90 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -070091 return Objects.hash(ip, type(), subtype());
alshabib8ca53902014-10-07 13:11:17 -070092 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (this == obj) {
97 return true;
98 }
99 if (obj instanceof ModIPInstruction) {
100 ModIPInstruction that = (ModIPInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700101 return Objects.equals(ip, that.ip) &&
102 Objects.equals(this.type(), that.type()) &&
103 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700104
105 }
106 return false;
107 }
108
alshabib7410fea2014-09-16 13:48:39 -0700109 }
alshabib7410fea2014-09-16 13:48:39 -0700110}