| /* |
| * Copyright 2018-present Open Networking Foundation |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.onosproject.pipelines.fabric; |
| |
| import org.onosproject.net.PortNumber; |
| import org.onosproject.net.flow.TrafficSelector; |
| import org.onosproject.net.flow.TrafficTreatment; |
| import org.onosproject.net.flow.criteria.Criterion; |
| import org.onosproject.net.flow.instructions.Instruction; |
| import org.onosproject.net.flow.instructions.Instructions; |
| import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| import org.onosproject.net.flowobjective.DefaultNextTreatment; |
| import org.onosproject.net.flowobjective.NextTreatment; |
| |
| import java.util.Collection; |
| |
| import static com.google.common.base.Preconditions.checkNotNull; |
| import static java.lang.String.format; |
| |
| /** |
| * Utility class with methods common to fabric pipeconf operations. |
| */ |
| public final class FabricUtils { |
| |
| private FabricUtils() { |
| // Hides constructor. |
| } |
| |
| public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) { |
| return criteria.stream() |
| .filter(c -> c.type().equals(type)) |
| .findFirst().orElse(null); |
| } |
| |
| public static Criterion criterion(TrafficSelector selector, Criterion.Type type) { |
| return selector.getCriterion(type); |
| } |
| |
| public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) { |
| return checkNotNull(criterion(selector, type), |
| format("%s criterion cannot be null", type)); |
| } |
| |
| public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) { |
| return checkNotNull(criterion(criteria, type), |
| format("%s criterion cannot be null", type)); |
| } |
| |
| public static Instructions.OutputInstruction instruction(TrafficTreatment treatment, Instruction.Type type) { |
| return treatment.allInstructions() |
| .stream() |
| .filter(inst -> inst.type() == type) |
| .map(inst -> (Instructions.OutputInstruction) inst) |
| .findFirst().orElse(null); |
| } |
| |
| public static L2ModificationInstruction l2Instruction( |
| TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) { |
| return treatment.allInstructions().stream() |
| .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION)) |
| .map(i -> (L2ModificationInstruction) i) |
| .filter(i -> i.subtype().equals(subType)) |
| .findFirst().orElse(null); |
| } |
| |
| public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) { |
| return instruction(treatment, Instruction.Type.OUTPUT); |
| } |
| |
| public static PortNumber outputPort(TrafficTreatment treatment) { |
| final Instructions.OutputInstruction inst = outputInstruction(treatment); |
| return inst == null ? null : inst.port(); |
| } |
| |
| public static PortNumber outputPort(NextTreatment treatment) { |
| if (treatment.type() == NextTreatment.Type.TREATMENT) { |
| final DefaultNextTreatment t = (DefaultNextTreatment) treatment; |
| return outputPort(t.treatment()); |
| } |
| return null; |
| } |
| } |