blob: b73e9c2636beab9df2c4f0f678916d7eb24e1bc6 [file] [log] [blame]
Jonghwan Hyun722275f2018-05-14 15:44:56 -07001/*
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 */
Carmelo Casconedefc74e2020-07-17 15:27:02 -070016package org.onosproject.net.behaviour.inbandtelemetry;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070017
Carmelo Casconedefc74e2020-07-17 15:27:02 -070018import com.google.common.collect.ImmutableSet;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070019import org.onosproject.net.flow.DefaultTrafficSelector;
20import org.onosproject.net.flow.TrafficSelector;
21
22import java.util.HashSet;
23import java.util.Set;
24
25import static com.google.common.base.Preconditions.checkArgument;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070026
Carmelo Casconedefc74e2020-07-17 15:27:02 -070027/**
28 * Represents a device-level objective to collect INT metadata for packets
29 * identified by a traffic selector.
30 */
Jonghwan Hyun722275f2018-05-14 15:44:56 -070031public final class IntObjective {
32
33 private static final int DEFAULT_PRIORITY = 10;
34
35 // TrafficSelector to describe target flows to monitor
36 private final TrafficSelector selector;
37 // Set of metadata types to collect
Carmelo Casconedefc74e2020-07-17 15:27:02 -070038 private final ImmutableSet<IntMetadataType> metadataTypes;
Jonghwan Hyun722275f2018-05-14 15:44:56 -070039
40 /**
41 * Creates an IntObjective.
42 *
43 * @param selector the traffic selector that identifies traffic to enable INT
44 * @param metadataTypes a set of metadata types to collect
Jonghwan Hyun722275f2018-05-14 15:44:56 -070045 */
Carmelo Casconedefc74e2020-07-17 15:27:02 -070046 private IntObjective(TrafficSelector selector, Set<IntMetadataType> metadataTypes) {
Jonghwan Hyun722275f2018-05-14 15:44:56 -070047 this.selector = selector;
Carmelo Casconedefc74e2020-07-17 15:27:02 -070048 this.metadataTypes = ImmutableSet.copyOf(metadataTypes);
Jonghwan Hyun722275f2018-05-14 15:44:56 -070049 }
50
51 /**
52 * Returns traffic selector of this objective.
53 *
54 * @return traffic selector
55 */
56 public TrafficSelector selector() {
57 return selector;
58 }
59
60 /**
61 * Returns a set of metadata types specified in this objective.
62 *
63 * @return instruction bitmap
64 */
65 public Set<IntMetadataType> metadataTypes() {
66 return metadataTypes;
67 }
68
69 /**
Jonghwan Hyun722275f2018-05-14 15:44:56 -070070 * An IntObjective builder.
71 */
72 public static final class Builder {
73 private TrafficSelector selector = DefaultTrafficSelector.emptySelector();
Carmelo Casconedefc74e2020-07-17 15:27:02 -070074 private final Set<IntMetadataType> metadataTypes = new HashSet<>();
Jonghwan Hyun722275f2018-05-14 15:44:56 -070075
76 /**
77 * Assigns a selector to the IntObjective.
78 *
79 * @param selector a traffic selector
80 * @return an IntObjective builder
81 */
82 public IntObjective.Builder withSelector(TrafficSelector selector) {
83 this.selector = selector;
84 return this;
85 }
86
87 /**
88 * Add a metadata type to the IntObjective.
89 *
90 * @param metadataTypes a set of metadata types
91 * @return an IntObjective builder
92 */
93 public IntObjective.Builder withMetadataTypes(Set<IntMetadataType> metadataTypes) {
94 this.metadataTypes.addAll(metadataTypes);
95 return this;
96 }
97
98 /**
Jonghwan Hyun722275f2018-05-14 15:44:56 -070099 * Builds the IntObjective.
100 *
101 * @return an IntObjective
102 */
103 public IntObjective build() {
104 checkArgument(!selector.criteria().isEmpty(), "Empty selector cannot match any flow.");
105 checkArgument(!metadataTypes.isEmpty(), "Metadata types cannot be empty");
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700106
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700107 return new IntObjective(selector, metadataTypes);
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700108 }
109 }
110}