blob: 655f88d3d4fce1c66aa40c19c4863a7e7b57ffa6 [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;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020028
Carmelo Casconeb5324e72018-11-25 02:26:32 -080029import java.util.Collection;
Daniele Morof51d0c12019-07-30 10:43:10 -070030import java.util.List;
31import java.util.stream.Collectors;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080032
33import static com.google.common.base.Preconditions.checkNotNull;
34import static java.lang.String.format;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020035
36/**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080037 * Utility class with methods common to fabric pipeconf operations.
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020038 */
39public final class FabricUtils {
40
41 private FabricUtils() {
42 // Hides constructor.
43 }
44
Carmelo Casconeb5324e72018-11-25 02:26:32 -080045 public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) {
46 return criteria.stream()
47 .filter(c -> c.type().equals(type))
48 .findFirst().orElse(null);
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020049 }
50
Carmelo Casconeb5324e72018-11-25 02:26:32 -080051 public static Criterion criterion(TrafficSelector selector, Criterion.Type type) {
52 return selector.getCriterion(type);
53 }
54
55 public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) {
56 return checkNotNull(criterion(selector, type),
57 format("%s criterion cannot be null", type));
58 }
59
60 public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) {
61 return checkNotNull(criterion(criteria, type),
62 format("%s criterion cannot be null", type));
63 }
64
65 public static Instructions.OutputInstruction instruction(TrafficTreatment treatment, Instruction.Type type) {
66 return treatment.allInstructions()
67 .stream()
68 .filter(inst -> inst.type() == type)
69 .map(inst -> (Instructions.OutputInstruction) inst)
70 .findFirst().orElse(null);
71 }
72
73 public static L2ModificationInstruction l2Instruction(
74 TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) {
75 return treatment.allInstructions().stream()
76 .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION))
77 .map(i -> (L2ModificationInstruction) i)
78 .filter(i -> i.subtype().equals(subType))
79 .findFirst().orElse(null);
80 }
81
Daniele Morof51d0c12019-07-30 10:43:10 -070082 public static List<L2ModificationInstruction> l2Instructions(
83 TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) {
84 return treatment.allInstructions().stream()
85 .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION))
86 .map(i -> (L2ModificationInstruction) i)
87 .filter(i -> i.subtype().equals(subType))
88 .collect(Collectors.toList());
89 }
90
Carmelo Casconeb5324e72018-11-25 02:26:32 -080091 public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) {
92 return instruction(treatment, Instruction.Type.OUTPUT);
93 }
94
95 public static PortNumber outputPort(TrafficTreatment treatment) {
96 final Instructions.OutputInstruction inst = outputInstruction(treatment);
97 return inst == null ? null : inst.port();
98 }
99
100 public static PortNumber outputPort(NextTreatment treatment) {
101 if (treatment.type() == NextTreatment.Type.TREATMENT) {
102 final DefaultNextTreatment t = (DefaultNextTreatment) treatment;
103 return outputPort(t.treatment());
104 }
105 return null;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200106 }
107}