blob: 8e5caa1e333e94e2b96be2f6b52437576f86b1d5 [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
17package org.onosproject.pipelines.fabric;
18
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;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32import static java.lang.String.format;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020033
34/**
Carmelo Casconeb5324e72018-11-25 02:26:32 -080035 * Utility class with methods common to fabric pipeconf operations.
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020036 */
37public final class FabricUtils {
38
39 private FabricUtils() {
40 // Hides constructor.
41 }
42
Carmelo Casconeb5324e72018-11-25 02:26:32 -080043 public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) {
44 return criteria.stream()
45 .filter(c -> c.type().equals(type))
46 .findFirst().orElse(null);
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020047 }
48
Carmelo Casconeb5324e72018-11-25 02:26:32 -080049 public static Criterion criterion(TrafficSelector selector, Criterion.Type type) {
50 return selector.getCriterion(type);
51 }
52
53 public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) {
54 return checkNotNull(criterion(selector, type),
55 format("%s criterion cannot be null", type));
56 }
57
58 public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) {
59 return checkNotNull(criterion(criteria, type),
60 format("%s criterion cannot be null", type));
61 }
62
63 public static Instructions.OutputInstruction instruction(TrafficTreatment treatment, Instruction.Type type) {
64 return treatment.allInstructions()
65 .stream()
66 .filter(inst -> inst.type() == type)
67 .map(inst -> (Instructions.OutputInstruction) inst)
68 .findFirst().orElse(null);
69 }
70
71 public static L2ModificationInstruction l2Instruction(
72 TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) {
73 return treatment.allInstructions().stream()
74 .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION))
75 .map(i -> (L2ModificationInstruction) i)
76 .filter(i -> i.subtype().equals(subType))
77 .findFirst().orElse(null);
78 }
79
80 public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) {
81 return instruction(treatment, Instruction.Type.OUTPUT);
82 }
83
84 public static PortNumber outputPort(TrafficTreatment treatment) {
85 final Instructions.OutputInstruction inst = outputInstruction(treatment);
86 return inst == null ? null : inst.port();
87 }
88
89 public static PortNumber outputPort(NextTreatment treatment) {
90 if (treatment.type() == NextTreatment.Type.TREATMENT) {
91 final DefaultNextTreatment t = (DefaultNextTreatment) treatment;
92 return outputPort(t.treatment());
93 }
94 return null;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020095 }
96}