blob: b1d842e142eba44092fa1cd13d2f4f6b5d63722f [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 /**
Yi Tsengc4fb5ce2020-08-07 14:58:52 -070070 * Returns a new INT objective builder.
71 *
72 * @return INT objective builder
73 */
74 public static IntObjective.Builder builder() {
75 return new Builder();
76 }
77
78 /**
Jonghwan Hyun722275f2018-05-14 15:44:56 -070079 * An IntObjective builder.
80 */
81 public static final class Builder {
82 private TrafficSelector selector = DefaultTrafficSelector.emptySelector();
Carmelo Casconedefc74e2020-07-17 15:27:02 -070083 private final Set<IntMetadataType> metadataTypes = new HashSet<>();
Jonghwan Hyun722275f2018-05-14 15:44:56 -070084
85 /**
86 * Assigns a selector to the IntObjective.
87 *
88 * @param selector a traffic selector
89 * @return an IntObjective builder
90 */
91 public IntObjective.Builder withSelector(TrafficSelector selector) {
92 this.selector = selector;
93 return this;
94 }
95
96 /**
97 * Add a metadata type to the IntObjective.
98 *
99 * @param metadataTypes a set of metadata types
100 * @return an IntObjective builder
101 */
102 public IntObjective.Builder withMetadataTypes(Set<IntMetadataType> metadataTypes) {
103 this.metadataTypes.addAll(metadataTypes);
104 return this;
105 }
106
107 /**
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700108 * Builds the IntObjective.
109 *
110 * @return an IntObjective
111 */
112 public IntObjective build() {
113 checkArgument(!selector.criteria().isEmpty(), "Empty selector cannot match any flow.");
114 checkArgument(!metadataTypes.isEmpty(), "Metadata types cannot be empty");
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700115
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700116 return new IntObjective(selector, metadataTypes);
Jonghwan Hyun722275f2018-05-14 15:44:56 -0700117 }
118 }
119}