blob: 4496f19073c568dabb3a509110afdcdf0e03a0c0 [file] [log] [blame]
Carmelo Cascone1e8843f2018-07-19 19:01:12 +02001/*
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 Cascone356ab8b2019-09-25 01:02:53 -070017package org.onosproject.pipelines.fabric.impl.behaviour;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020018
19import org.onosproject.net.PortNumber;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080020import org.onosproject.net.flow.TrafficSelector;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020021import org.onosproject.net.flow.TrafficTreatment;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080022import org.onosproject.net.flow.criteria.Criterion;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020023import org.onosproject.net.flow.instructions.Instruction;
24import org.onosproject.net.flow.instructions.Instructions;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080025import org.onosproject.net.flow.instructions.L2ModificationInstruction;
26import org.onosproject.net.flowobjective.DefaultNextTreatment;
27import org.onosproject.net.flowobjective.NextTreatment;
Wailok Shumfb7e7872021-06-18 17:30:08 +080028import org.onosproject.net.pi.model.PiPipelineInterpreter;
29import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020030
Carmelo Casconeb5324e72018-11-25 02:26:32 -080031import java.util.Collection;
Daniele Morof51d0c12019-07-30 10:43:10 -070032import java.util.List;
33import java.util.stream.Collectors;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080034
35import static com.google.common.base.Preconditions.checkNotNull;
36import static java.lang.String.format;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020037
38/**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080039 * Utility class with methods common to fabric pipeconf operations.
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020040 */
41public final class FabricUtils {
42
43 private FabricUtils() {
44 // Hides constructor.
45 }
46
Carmelo Casconeb5324e72018-11-25 02:26:32 -080047 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 Cascone1e8843f2018-07-19 19:01:12 +020051 }
52
Carmelo Casconeb5324e72018-11-25 02:26:32 -080053 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 Shumfb7e7872021-06-18 17:30:08 +080067 public static Instruction instruction(TrafficTreatment treatment, Instruction.Type type) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080068 return treatment.allInstructions()
69 .stream()
70 .filter(inst -> inst.type() == type)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080071 .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 Shumfb7e7872021-06-18 17:30:08 +080083 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 Morof51d0c12019-07-30 10:43:10 -070094 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 Casconeb5324e72018-11-25 02:26:32 -0800103 public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) {
Wailok Shumfb7e7872021-06-18 17:30:08 +0800104 return (Instructions.OutputInstruction) instruction(treatment, Instruction.Type.OUTPUT);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800105 }
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 Cascone1e8843f2018-07-19 19:01:12 +0200118 }
Wailok Shumfb7e7872021-06-18 17:30:08 +0800119
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 Cascone1e8843f2018-07-19 19:01:12 +0200126}