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