Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import org.onlab.util.Identifier; |
| 20 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 24 | /** |
| 25 | * Identifier of a packet's header field. |
| 26 | */ |
| 27 | public final class PiHeaderFieldId extends Identifier<String> { |
| 28 | |
| 29 | private final String headerName; |
| 30 | private final String fieldName; |
| 31 | private final int index; |
| 32 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 33 | private PiHeaderFieldId(String headerName, String fieldName, int index) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 34 | super(headerName + |
| 35 | (index > 0 ? "[" + String.valueOf(index) + "]" : "") + |
| 36 | "." + fieldName); |
| 37 | this.headerName = headerName; |
| 38 | this.fieldName = fieldName; |
| 39 | this.index = index; |
| 40 | } |
| 41 | |
| 42 | /** |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 43 | * Returns an header field identifier for the given header name, field name and index. |
| 44 | * <p> |
| 45 | * Index represents the position of this header in the packet w.r.t. to other headers of the |
| 46 | * same type. Index 0 points to the first instance of the header, 1 the second one, etc. Helpful |
| 47 | * when dealing with stacked headers, e.g. to match on the second MPLS label. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 48 | * |
| 49 | * @param headerName header name |
| 50 | * @param fieldName field name |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 51 | * @param index index |
| 52 | * @return header field identifier |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 53 | */ |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 54 | public static PiHeaderFieldId of(String headerName, String fieldName, int index) { |
| 55 | checkNotNull(headerName); |
| 56 | checkNotNull(fieldName); |
| 57 | checkArgument(!headerName.isEmpty(), "Header name can't be empty"); |
| 58 | checkArgument(!fieldName.isEmpty(), "Field name can't be empty"); |
| 59 | checkArgument(index >= 0, "Index must be a positive integer"); |
| 60 | return new PiHeaderFieldId(headerName, fieldName, index); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns an header field identifier for the given header name and field name. |
| 65 | * Index is set to default value 0. |
| 66 | * |
| 67 | * @param headerName header name |
| 68 | * @param fieldName field name |
| 69 | * @return header field identifier |
| 70 | */ |
| 71 | public static PiHeaderFieldId of(String headerName, String fieldName) { |
| 72 | return of(headerName, fieldName, 0); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the name of the header. |
| 77 | * |
| 78 | * @return a string value |
| 79 | */ |
| 80 | public String headerName() { |
| 81 | return headerName; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the name of the field. |
| 86 | * |
| 87 | * @return a string value |
| 88 | */ |
| 89 | public String fieldName() { |
| 90 | return fieldName; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the index of this header. |
| 95 | * |
| 96 | * @return an integer value. |
| 97 | */ |
| 98 | public int index() { |
| 99 | return index; |
| 100 | } |
| 101 | } |