Jonghwan Hyun | 722275f | 2018-05-14 15:44:56 -0700 | [diff] [blame^] | 1 | /* |
| 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 | package org.onosproject.inbandtelemetry.api; |
| 17 | |
| 18 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 19 | import org.onosproject.net.flow.TrafficSelector; |
| 20 | |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.onosproject.inbandtelemetry.api.IntIntent.IntMetadataType; |
| 27 | import static org.onosproject.inbandtelemetry.api.IntIntent.IntHeaderType; |
| 28 | |
| 29 | public final class IntObjective { |
| 30 | |
| 31 | private static final int DEFAULT_PRIORITY = 10; |
| 32 | |
| 33 | // TrafficSelector to describe target flows to monitor |
| 34 | private final TrafficSelector selector; |
| 35 | // Set of metadata types to collect |
| 36 | private final Set<IntMetadataType> metadataTypes; |
| 37 | // Type of header (either hop-by-hop or destination) |
| 38 | private final IntHeaderType headerType; |
| 39 | |
| 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 |
| 45 | * @param headerType the type of INT header |
| 46 | */ |
| 47 | private IntObjective(TrafficSelector selector, Set<IntMetadataType> metadataTypes, |
| 48 | IntHeaderType headerType) { |
| 49 | this.selector = selector; |
| 50 | this.metadataTypes = metadataTypes; |
| 51 | this.headerType = headerType; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns traffic selector of this objective. |
| 56 | * |
| 57 | * @return traffic selector |
| 58 | */ |
| 59 | public TrafficSelector selector() { |
| 60 | return selector; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns a set of metadata types specified in this objective. |
| 65 | * |
| 66 | * @return instruction bitmap |
| 67 | */ |
| 68 | public Set<IntMetadataType> metadataTypes() { |
| 69 | return metadataTypes; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns a INT header type specified in this objective. |
| 74 | * |
| 75 | * @return INT header type |
| 76 | */ |
| 77 | public IntHeaderType headerType() { |
| 78 | return headerType; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * An IntObjective builder. |
| 83 | */ |
| 84 | public static final class Builder { |
| 85 | private TrafficSelector selector = DefaultTrafficSelector.emptySelector(); |
| 86 | private Set<IntMetadataType> metadataTypes = new HashSet<>(); |
| 87 | private IntHeaderType headerType = IntHeaderType.HOP_BY_HOP; |
| 88 | |
| 89 | /** |
| 90 | * Assigns a selector to the IntObjective. |
| 91 | * |
| 92 | * @param selector a traffic selector |
| 93 | * @return an IntObjective builder |
| 94 | */ |
| 95 | public IntObjective.Builder withSelector(TrafficSelector selector) { |
| 96 | this.selector = selector; |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Add a metadata type to the IntObjective. |
| 102 | * |
| 103 | * @param metadataTypes a set of metadata types |
| 104 | * @return an IntObjective builder |
| 105 | */ |
| 106 | public IntObjective.Builder withMetadataTypes(Set<IntMetadataType> metadataTypes) { |
| 107 | this.metadataTypes.addAll(metadataTypes); |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Assigns a header type to the IntObjective. |
| 113 | * |
| 114 | * @param headerType a header type |
| 115 | * @return an IntObjective builder |
| 116 | */ |
| 117 | public IntObjective.Builder withHeaderType(IntHeaderType headerType) { |
| 118 | this.headerType = headerType; |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Builds the IntObjective. |
| 124 | * |
| 125 | * @return an IntObjective |
| 126 | */ |
| 127 | public IntObjective build() { |
| 128 | checkArgument(!selector.criteria().isEmpty(), "Empty selector cannot match any flow."); |
| 129 | checkArgument(!metadataTypes.isEmpty(), "Metadata types cannot be empty"); |
| 130 | checkNotNull(headerType, "Header type cannot be null."); |
| 131 | |
| 132 | return new IntObjective(selector, metadataTypes, headerType); |
| 133 | } |
| 134 | } |
| 135 | } |