blob: 5d89f99bf29354d2616b38763b130fb4ad13deeb [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;
pierventre4d29d3c2021-08-27 17:20:00 +020023import org.onosproject.net.flow.criteria.MetadataCriterion;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020024import org.onosproject.net.flow.instructions.Instruction;
25import org.onosproject.net.flow.instructions.Instructions;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080026import org.onosproject.net.flow.instructions.L2ModificationInstruction;
27import org.onosproject.net.flowobjective.DefaultNextTreatment;
pierventre4d29d3c2021-08-27 17:20:00 +020028import org.onosproject.net.flowobjective.FilteringObjective;
29import org.onosproject.net.flowobjective.ForwardingObjective;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080030import org.onosproject.net.flowobjective.NextTreatment;
pierventre4d29d3c2021-08-27 17:20:00 +020031import org.onosproject.net.flowobjective.Objective;
Wailok Shumfb7e7872021-06-18 17:30:08 +080032import org.onosproject.net.pi.model.PiPipelineInterpreter;
33import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020034
Carmelo Casconeb5324e72018-11-25 02:26:32 -080035import java.util.Collection;
Daniele Morof51d0c12019-07-30 10:43:10 -070036import java.util.List;
37import java.util.stream.Collectors;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080038
39import static com.google.common.base.Preconditions.checkNotNull;
40import static java.lang.String.format;
pierventre4d29d3c2021-08-27 17:20:00 +020041import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.EDGE_PORT;
42import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.INFRA_PORT;
43import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.METADATA_MASK;
44import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.METADATA_TO_PORT_TYPE;
45import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.PAIR_PORT;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020046
47/**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080048 * Utility class with methods common to fabric pipeconf operations.
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020049 */
50public final class FabricUtils {
51
52 private FabricUtils() {
53 // Hides constructor.
54 }
55
Carmelo Casconeb5324e72018-11-25 02:26:32 -080056 public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) {
57 return criteria.stream()
58 .filter(c -> c.type().equals(type))
59 .findFirst().orElse(null);
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020060 }
61
Carmelo Casconeb5324e72018-11-25 02:26:32 -080062 public static Criterion criterion(TrafficSelector selector, Criterion.Type type) {
63 return selector.getCriterion(type);
64 }
65
66 public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) {
67 return checkNotNull(criterion(selector, type),
68 format("%s criterion cannot be null", type));
69 }
70
71 public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) {
72 return checkNotNull(criterion(criteria, type),
73 format("%s criterion cannot be null", type));
74 }
75
Wailok Shumfb7e7872021-06-18 17:30:08 +080076 public static Instruction instruction(TrafficTreatment treatment, Instruction.Type type) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080077 return treatment.allInstructions()
78 .stream()
79 .filter(inst -> inst.type() == type)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080080 .findFirst().orElse(null);
81 }
82
83 public static L2ModificationInstruction l2Instruction(
84 TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) {
85 return treatment.allInstructions().stream()
86 .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION))
87 .map(i -> (L2ModificationInstruction) i)
88 .filter(i -> i.subtype().equals(subType))
89 .findFirst().orElse(null);
90 }
91
Wailok Shumfb7e7872021-06-18 17:30:08 +080092 public static Instruction l2InstructionOrFail(
93 TrafficTreatment treatment,
94 L2ModificationInstruction.L2SubType subType, PiTableId tableId)
95 throws PiPipelineInterpreter.PiInterpreterException {
96 final Instruction inst = l2Instruction(treatment, subType);
97 if (inst == null) {
98 treatmentException(tableId, treatment, format("missing %s instruction", subType));
99 }
100 return inst;
101 }
102
Daniele Morof51d0c12019-07-30 10:43:10 -0700103 public static List<L2ModificationInstruction> l2Instructions(
104 TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) {
105 return treatment.allInstructions().stream()
106 .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION))
107 .map(i -> (L2ModificationInstruction) i)
108 .filter(i -> i.subtype().equals(subType))
109 .collect(Collectors.toList());
110 }
111
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800112 public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) {
Wailok Shumfb7e7872021-06-18 17:30:08 +0800113 return (Instructions.OutputInstruction) instruction(treatment, Instruction.Type.OUTPUT);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800114 }
115
116 public static PortNumber outputPort(TrafficTreatment treatment) {
117 final Instructions.OutputInstruction inst = outputInstruction(treatment);
118 return inst == null ? null : inst.port();
119 }
120
121 public static PortNumber outputPort(NextTreatment treatment) {
122 if (treatment.type() == NextTreatment.Type.TREATMENT) {
123 final DefaultNextTreatment t = (DefaultNextTreatment) treatment;
124 return outputPort(t.treatment());
125 }
126 return null;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200127 }
Wailok Shumfb7e7872021-06-18 17:30:08 +0800128
129 public static void treatmentException(
130 PiTableId tableId, TrafficTreatment treatment, String explanation)
131 throws PiPipelineInterpreter.PiInterpreterException {
132 throw new PiPipelineInterpreter.PiInterpreterException(format(
133 "Invalid treatment for table '%s', %s: %s", tableId, explanation, treatment));
134 }
pierventre4d29d3c2021-08-27 17:20:00 +0200135
136 /**
137 * Port type metadata conversion.
138 *
139 * @param obj the objective
140 * @return the port type associated to the metadata, null otherwise
141 */
142 public static Byte portType(Objective obj) {
143 Byte portType = null;
144 if (isSrMetadataSet(obj, PAIR_PORT) && METADATA_TO_PORT_TYPE.containsKey(PAIR_PORT)) {
145 portType = METADATA_TO_PORT_TYPE.get(PAIR_PORT);
146 } else if (isSrMetadataSet(obj, EDGE_PORT) && METADATA_TO_PORT_TYPE.containsKey(EDGE_PORT)) {
147 portType = METADATA_TO_PORT_TYPE.get(EDGE_PORT);
148 } else if (isSrMetadataSet(obj, INFRA_PORT) && METADATA_TO_PORT_TYPE.containsKey(INFRA_PORT)) {
149 portType = METADATA_TO_PORT_TYPE.get(INFRA_PORT);
150 }
151 return portType;
152 }
153
154 /**
155 * Check metadata passed from SegmentRouting app.
156 *
157 * @param obj the objective containing the metadata
158 * @return true if the objective contains valid metadata, false otherwise
159 */
160 public static boolean isValidSrMetadata(Objective obj) {
161 long meta = 0;
162 if (obj instanceof FilteringObjective) {
163 FilteringObjective filtObj = (FilteringObjective) obj;
164 if (filtObj.meta() == null) {
165 return true;
166 }
167 Instructions.MetadataInstruction metaIns = filtObj.meta().writeMetadata();
168 if (metaIns == null) {
169 return true;
170 }
171 meta = metaIns.metadata() & metaIns.metadataMask();
172 } else if (obj instanceof ForwardingObjective) {
173 ForwardingObjective fwdObj = (ForwardingObjective) obj;
174 if (fwdObj.meta() == null) {
175 return true;
176 }
177 MetadataCriterion metaCrit = (MetadataCriterion) fwdObj.meta().getCriterion(Criterion.Type.METADATA);
178 if (metaCrit == null) {
179 return true;
180 }
181 meta = metaCrit.metadata();
182 }
183 return meta != 0 && ((meta ^ METADATA_MASK) <= METADATA_MASK);
184 }
185
186 /**
187 * Verify if a given flag has been set into the metadata.
188 *
189 * @param obj the objective containing the metadata
190 * @param flag the flag to verify
191 * @return true if the flag is set, false otherwise
192 */
193 public static boolean isSrMetadataSet(Objective obj, long flag) {
194 long meta = 0;
195 if (obj instanceof FilteringObjective) {
196 FilteringObjective filtObj = (FilteringObjective) obj;
197 if (filtObj.meta() == null) {
198 return false;
199 }
200 Instructions.MetadataInstruction metaIns = filtObj.meta().writeMetadata();
201 if (metaIns == null) {
202 return false;
203 }
204 meta = metaIns.metadata() & metaIns.metadataMask();
205 } else if (obj instanceof ForwardingObjective) {
206 ForwardingObjective fwdObj = (ForwardingObjective) obj;
207 if (fwdObj.meta() == null) {
208 return false;
209 }
210 MetadataCriterion metaCrit = (MetadataCriterion) fwdObj.meta().getCriterion(Criterion.Type.METADATA);
211 if (metaCrit == null) {
212 return false;
213 }
214 meta = metaCrit.metadata();
215 }
216 return (meta & flag) == flag;
217 }
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200218}