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 | */ |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.flow; |
| 17 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | import java.util.Objects; |
| 21 | |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.PortNumber; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.flow.instructions.Instructions; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 28 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 29 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 2809bf3 | 2014-10-20 22:44:12 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableList; |
| 31 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Default traffic treatment implementation. |
| 34 | */ |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 35 | public final class DefaultTrafficTreatment implements TrafficTreatment { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 36 | |
| 37 | private final List<Instruction> instructions; |
| 38 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Creates a new traffic treatment from the specified list of instructions. |
| 41 | * |
| 42 | * @param instructions treatment instructions |
| 43 | */ |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 44 | private DefaultTrafficTreatment(List<Instruction> instructions) { |
Yuta HIGUCHI | 2809bf3 | 2014-10-20 22:44:12 -0700 | [diff] [blame] | 45 | this.instructions = ImmutableList.copyOf(instructions); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public List<Instruction> instructions() { |
| 50 | return instructions; |
| 51 | } |
| 52 | |
| 53 | /** |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 54 | * Returns a new traffic treatment builder. |
| 55 | * |
| 56 | * @return traffic treatment builder |
| 57 | */ |
| 58 | public static TrafficTreatment.Builder builder() { |
| 59 | return new Builder(); |
| 60 | } |
| 61 | |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Returns a new traffic treatment builder primed to produce entities |
| 64 | * patterned after the supplied treatment. |
| 65 | * |
| 66 | * @return traffic treatment builder |
| 67 | */ |
| 68 | public static TrafficTreatment.Builder builder(TrafficTreatment treatment) { |
| 69 | return new Builder(treatment); |
| 70 | } |
| 71 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 72 | //FIXME: Order of instructions may affect hashcode |
| 73 | @Override |
| 74 | public int hashCode() { |
| 75 | return Objects.hash(instructions); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean equals(Object obj) { |
| 80 | if (this == obj) { |
| 81 | return true; |
| 82 | } |
| 83 | if (obj instanceof DefaultTrafficTreatment) { |
| 84 | DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 85 | return Objects.equals(instructions, that.instructions); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 86 | |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public String toString() { |
| 93 | return MoreObjects.toStringHelper(getClass()) |
| 94 | .add("instructions", instructions) |
| 95 | .toString(); |
| 96 | } |
| 97 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 98 | /** |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 99 | * Builds a list of treatments following the following order. |
| 100 | * Modifications -> Group -> Output (including drop) |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 101 | */ |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 102 | public static final class Builder implements TrafficTreatment.Builder { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 103 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 104 | boolean drop = false; |
| 105 | |
| 106 | List<Instruction> outputs = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 107 | |
| 108 | // TODO: should be a list of instructions based on group objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 109 | List<Instruction> groups = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 110 | |
| 111 | // TODO: should be a list of instructions based on modification objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 112 | List<Instruction> modifications = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 113 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 114 | // Creates a new builder |
| 115 | private Builder() { |
| 116 | } |
| 117 | |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 118 | // Creates a new builder based off an existing treatment |
| 119 | private Builder(TrafficTreatment treatment) { |
| 120 | for (Instruction instruction : treatment.instructions()) { |
| 121 | add(instruction); |
| 122 | } |
| 123 | } |
| 124 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 125 | @Override |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 126 | public Builder add(Instruction instruction) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 127 | if (drop) { |
| 128 | return this; |
| 129 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 130 | switch (instruction.type()) { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 131 | case DROP: |
| 132 | drop = true; |
| 133 | break; |
| 134 | case OUTPUT: |
| 135 | outputs.add(instruction); |
| 136 | break; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 137 | case L0MODIFICATION: |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 138 | case L2MODIFICATION: |
| 139 | case L3MODIFICATION: |
| 140 | // TODO: enforce modification order if any |
| 141 | modifications.add(instruction); |
| 142 | break; |
| 143 | case GROUP: |
| 144 | groups.add(instruction); |
| 145 | break; |
| 146 | default: |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 147 | throw new IllegalArgumentException("Unknown instruction type: " + |
| 148 | instruction.type()); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 149 | } |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 154 | public void drop() { |
| 155 | add(Instructions.createDrop()); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Builder setOutput(PortNumber number) { |
| 160 | return add(Instructions.createOutput(number)); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Builder setEthSrc(MacAddress addr) { |
| 165 | return add(Instructions.modL2Src(addr)); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Builder setEthDst(MacAddress addr) { |
| 170 | return add(Instructions.modL2Dst(addr)); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Builder setVlanId(VlanId id) { |
| 175 | return add(Instructions.modVlanId(id)); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public Builder setVlanPcp(Byte pcp) { |
| 180 | return add(Instructions.modVlanPcp(pcp)); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public Builder setIpSrc(IpPrefix addr) { |
| 185 | return add(Instructions.modL3Src(addr)); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public Builder setIpDst(IpPrefix addr) { |
| 190 | return add(Instructions.modL3Dst(addr)); |
| 191 | } |
| 192 | |
| 193 | @Override |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 194 | public Builder setLambda(short lambda) { |
| 195 | return add(Instructions.modL0Lambda(lambda)); |
| 196 | } |
| 197 | |
| 198 | @Override |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 199 | public TrafficTreatment build() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 200 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 201 | //If we are dropping should we just return an empty list? |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 202 | List<Instruction> instructions = new LinkedList<Instruction>(); |
| 203 | instructions.addAll(modifications); |
| 204 | instructions.addAll(groups); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 205 | if (!drop) { |
| 206 | instructions.addAll(outputs); |
| 207 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 208 | |
| 209 | return new DefaultTrafficTreatment(instructions); |
| 210 | } |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } |