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 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 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; |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 28 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 29 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 30 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 31 | import java.util.Collection; |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | import java.util.stream.Collectors; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static java.lang.String.format; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 37 | |
| 38 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 39 | * Utility class with methods common to fabric pipeconf operations. |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 40 | */ |
| 41 | public final class FabricUtils { |
| 42 | |
| 43 | private FabricUtils() { |
| 44 | // Hides constructor. |
| 45 | } |
| 46 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 47 | public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 48 | return criteria.stream() |
| 49 | .filter(c -> c.type().equals(type)) |
| 50 | .findFirst().orElse(null); |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 53 | public static Criterion criterion(TrafficSelector selector, Criterion.Type type) { |
| 54 | return selector.getCriterion(type); |
| 55 | } |
| 56 | |
| 57 | public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) { |
| 58 | return checkNotNull(criterion(selector, type), |
| 59 | format("%s criterion cannot be null", type)); |
| 60 | } |
| 61 | |
| 62 | public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) { |
| 63 | return checkNotNull(criterion(criteria, type), |
| 64 | format("%s criterion cannot be null", type)); |
| 65 | } |
| 66 | |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 67 | public static Instruction instruction(TrafficTreatment treatment, Instruction.Type type) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 68 | return treatment.allInstructions() |
| 69 | .stream() |
| 70 | .filter(inst -> inst.type() == type) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 71 | .findFirst().orElse(null); |
| 72 | } |
| 73 | |
| 74 | public static L2ModificationInstruction l2Instruction( |
| 75 | TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) { |
| 76 | return treatment.allInstructions().stream() |
| 77 | .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION)) |
| 78 | .map(i -> (L2ModificationInstruction) i) |
| 79 | .filter(i -> i.subtype().equals(subType)) |
| 80 | .findFirst().orElse(null); |
| 81 | } |
| 82 | |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 83 | public static Instruction l2InstructionOrFail( |
| 84 | TrafficTreatment treatment, |
| 85 | L2ModificationInstruction.L2SubType subType, PiTableId tableId) |
| 86 | throws PiPipelineInterpreter.PiInterpreterException { |
| 87 | final Instruction inst = l2Instruction(treatment, subType); |
| 88 | if (inst == null) { |
| 89 | treatmentException(tableId, treatment, format("missing %s instruction", subType)); |
| 90 | } |
| 91 | return inst; |
| 92 | } |
| 93 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 94 | public static List<L2ModificationInstruction> l2Instructions( |
| 95 | TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) { |
| 96 | return treatment.allInstructions().stream() |
| 97 | .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION)) |
| 98 | .map(i -> (L2ModificationInstruction) i) |
| 99 | .filter(i -> i.subtype().equals(subType)) |
| 100 | .collect(Collectors.toList()); |
| 101 | } |
| 102 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 103 | public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) { |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 104 | return (Instructions.OutputInstruction) instruction(treatment, Instruction.Type.OUTPUT); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public static PortNumber outputPort(TrafficTreatment treatment) { |
| 108 | final Instructions.OutputInstruction inst = outputInstruction(treatment); |
| 109 | return inst == null ? null : inst.port(); |
| 110 | } |
| 111 | |
| 112 | public static PortNumber outputPort(NextTreatment treatment) { |
| 113 | if (treatment.type() == NextTreatment.Type.TREATMENT) { |
| 114 | final DefaultNextTreatment t = (DefaultNextTreatment) treatment; |
| 115 | return outputPort(t.treatment()); |
| 116 | } |
| 117 | return null; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 118 | } |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 119 | |
| 120 | public static void treatmentException( |
| 121 | PiTableId tableId, TrafficTreatment treatment, String explanation) |
| 122 | throws PiPipelineInterpreter.PiInterpreterException { |
| 123 | throw new PiPipelineInterpreter.PiInterpreterException(format( |
| 124 | "Invalid treatment for table '%s', %s: %s", tableId, explanation, treatment)); |
| 125 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 126 | } |