Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.pipelines.fabric; |
| 18 | |
| 19 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 20 | import org.onosproject.net.flow.TrafficSelector; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 21 | import org.onosproject.net.flow.TrafficTreatment; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.criteria.Criterion; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 23 | import org.onosproject.net.flow.instructions.Instruction; |
| 24 | import org.onosproject.net.flow.instructions.Instructions; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 25 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 26 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
| 27 | import org.onosproject.net.flowobjective.NextTreatment; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 28 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 29 | import java.util.Collection; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static java.lang.String.format; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 33 | |
| 34 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 35 | * Utility class with methods common to fabric pipeconf operations. |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 36 | */ |
| 37 | public final class FabricUtils { |
| 38 | |
| 39 | private FabricUtils() { |
| 40 | // Hides constructor. |
| 41 | } |
| 42 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 43 | public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 44 | return criteria.stream() |
| 45 | .filter(c -> c.type().equals(type)) |
| 46 | .findFirst().orElse(null); |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 49 | public static Criterion criterion(TrafficSelector selector, Criterion.Type type) { |
| 50 | return selector.getCriterion(type); |
| 51 | } |
| 52 | |
| 53 | public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) { |
| 54 | return checkNotNull(criterion(selector, type), |
| 55 | format("%s criterion cannot be null", type)); |
| 56 | } |
| 57 | |
| 58 | public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) { |
| 59 | return checkNotNull(criterion(criteria, type), |
| 60 | format("%s criterion cannot be null", type)); |
| 61 | } |
| 62 | |
| 63 | public static Instructions.OutputInstruction instruction(TrafficTreatment treatment, Instruction.Type type) { |
| 64 | return treatment.allInstructions() |
| 65 | .stream() |
| 66 | .filter(inst -> inst.type() == type) |
| 67 | .map(inst -> (Instructions.OutputInstruction) inst) |
| 68 | .findFirst().orElse(null); |
| 69 | } |
| 70 | |
| 71 | public static L2ModificationInstruction l2Instruction( |
| 72 | TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) { |
| 73 | return treatment.allInstructions().stream() |
| 74 | .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION)) |
| 75 | .map(i -> (L2ModificationInstruction) i) |
| 76 | .filter(i -> i.subtype().equals(subType)) |
| 77 | .findFirst().orElse(null); |
| 78 | } |
| 79 | |
| 80 | public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) { |
| 81 | return instruction(treatment, Instruction.Type.OUTPUT); |
| 82 | } |
| 83 | |
| 84 | public static PortNumber outputPort(TrafficTreatment treatment) { |
| 85 | final Instructions.OutputInstruction inst = outputInstruction(treatment); |
| 86 | return inst == null ? null : inst.port(); |
| 87 | } |
| 88 | |
| 89 | public static PortNumber outputPort(NextTreatment treatment) { |
| 90 | if (treatment.type() == NextTreatment.Type.TREATMENT) { |
| 91 | final DefaultNextTreatment t = (DefaultNextTreatment) treatment; |
| 92 | return outputPort(t.treatment()); |
| 93 | } |
| 94 | return null; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 95 | } |
| 96 | } |