Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 19 | package org.onlab.onos.net.flow.instructions; |
| 20 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 22 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Abstraction of a single traffic treatment step. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 29 | */ |
| 30 | public abstract class L3ModificationInstruction implements Instruction { |
| 31 | |
| 32 | /** |
| 33 | * Represents the type of traffic treatment. |
| 34 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 35 | public enum L3SubType { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Ether src modification. |
| 38 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 39 | IP_SRC, |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Ether dst modification. |
| 43 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 44 | IP_DST |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 45 | |
| 46 | //TODO: remaining types |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the subtype of the modification instruction. |
| 51 | * @return type of instruction |
| 52 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 53 | public abstract L3SubType subtype(); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | public Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 57 | return Type.L3MODIFICATION; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Represents a L3 src/dst modification instruction. |
| 62 | */ |
| 63 | public static final class ModIPInstruction extends L3ModificationInstruction { |
| 64 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 65 | private final L3SubType subtype; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 66 | private final IpPrefix ip; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 67 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 68 | public ModIPInstruction(L3SubType subType, IpPrefix addr) { |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 69 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 70 | this.subtype = subType; |
| 71 | this.ip = addr; |
| 72 | } |
| 73 | |
| 74 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 75 | public L3SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 76 | return this.subtype; |
| 77 | } |
| 78 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 79 | public IpPrefix ip() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 80 | return this.ip; |
| 81 | } |
| 82 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public String toString() { |
| 85 | return toStringHelper(subtype().toString()) |
| 86 | .add("ip", ip).toString(); |
| 87 | } |
| 88 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 91 | return Objects.hash(ip, type(), subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 92 | } |
| 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; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 101 | return Objects.equals(ip, that.ip) && |
| 102 | Objects.equals(this.type(), that.type()) && |
| 103 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 104 | |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 109 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 110 | } |