Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.instructions; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 17 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Abstraction of a single traffic treatment step. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 26 | */ |
| 27 | public abstract class L3ModificationInstruction implements Instruction { |
| 28 | |
| 29 | /** |
| 30 | * Represents the type of traffic treatment. |
| 31 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 32 | public enum L3SubType { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Ether src modification. |
| 35 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 36 | IP_SRC, |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Ether dst modification. |
| 40 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 41 | 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 |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 57 | |
| 58 | //TODO: remaining types |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the subtype of the modification instruction. |
| 63 | * @return type of instruction |
| 64 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 65 | public abstract L3SubType subtype(); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | public Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 69 | return Type.L3MODIFICATION; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Represents a L3 src/dst modification instruction. |
| 74 | */ |
| 75 | public static final class ModIPInstruction extends L3ModificationInstruction { |
| 76 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 77 | private final L3SubType subtype; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 78 | private final IpAddress ip; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 79 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 80 | public ModIPInstruction(L3SubType subType, IpAddress addr) { |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 81 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 82 | this.subtype = subType; |
| 83 | this.ip = addr; |
| 84 | } |
| 85 | |
| 86 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 87 | public L3SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 88 | return this.subtype; |
| 89 | } |
| 90 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 91 | public IpAddress ip() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 92 | return this.ip; |
| 93 | } |
| 94 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public String toString() { |
| 97 | return toStringHelper(subtype().toString()) |
| 98 | .add("ip", ip).toString(); |
| 99 | } |
| 100 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 103 | return Objects.hash(type(), subtype(), ip); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 104 | } |
| 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; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 113 | return Objects.equals(ip, that.ip) && |
| 114 | Objects.equals(this.type(), that.type()) && |
| 115 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 116 | |
| 117 | } |
| 118 | return false; |
| 119 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 120 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 121 | |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Represents a L3 TTL modification instruction. |
| 124 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 125 | public static final class ModTtlInstruction extends L3ModificationInstruction { |
| 126 | |
| 127 | private final L3SubType subtype; |
| 128 | |
| 129 | public ModTtlInstruction(L3SubType subtype) { |
| 130 | this.subtype = subtype; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public L3SubType subtype() { |
| 135 | return this.subtype; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 140 | return toStringHelper(subtype().toString()) |
| 141 | .toString(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public int hashCode() { |
| 146 | return Objects.hash(type(), subtype()); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean equals(Object obj) { |
| 151 | if (this == obj) { |
| 152 | return true; |
| 153 | } |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 154 | if (obj instanceof ModTtlInstruction) { |
| 155 | ModTtlInstruction that = (ModTtlInstruction) obj; |
| 156 | return Objects.equals(this.subtype(), that.subtype()) && |
| 157 | Objects.equals(this.type(), that.type()); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 158 | |
| 159 | } |
| 160 | return false; |
| 161 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 162 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 163 | } |