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 | |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 29 | // FIXME: this abstraction is brittle and we should drop any string-composition logic. |
| 30 | // e.g. in P4_14 there is no scope for match fields. |
| 31 | // In light of ONOS-7066, the best solution seems to have IDs defined as arbitrary |
| 32 | // strings equal to the entity names defined in the P4Info. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 33 | private final String headerName; |
| 34 | private final String fieldName; |
| 35 | private final int index; |
| 36 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 37 | private PiHeaderFieldId(String headerName, String fieldName, int index) { |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 38 | super(headerName + |
| 39 | (index > 0 ? "[" + String.valueOf(index) + "]" : "") + |
| 40 | "." + fieldName); |
| 41 | this.headerName = headerName; |
| 42 | this.fieldName = fieldName; |
| 43 | this.index = index; |
| 44 | } |
| 45 | |
| 46 | /** |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 47 | * Returns an header field identifier for the given header name, field name and index. |
| 48 | * <p> |
| 49 | * Index represents the position of this header in the packet w.r.t. to other headers of the |
| 50 | * same type. Index 0 points to the first instance of the header, 1 the second one, etc. Helpful |
| 51 | * 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] | 52 | * |
| 53 | * @param headerName header name |
| 54 | * @param fieldName field name |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 55 | * @param index index |
| 56 | * @return header field identifier |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 57 | */ |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 58 | public static PiHeaderFieldId of(String headerName, String fieldName, int index) { |
| 59 | checkNotNull(headerName); |
| 60 | checkNotNull(fieldName); |
| 61 | checkArgument(!headerName.isEmpty(), "Header name can't be empty"); |
| 62 | checkArgument(!fieldName.isEmpty(), "Field name can't be empty"); |
| 63 | checkArgument(index >= 0, "Index must be a positive integer"); |
| 64 | return new PiHeaderFieldId(headerName, fieldName, index); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns an header field identifier for the given header name and field name. |
| 69 | * Index is set to default value 0. |
| 70 | * |
| 71 | * @param headerName header name |
| 72 | * @param fieldName field name |
| 73 | * @return header field identifier |
| 74 | */ |
| 75 | public static PiHeaderFieldId of(String headerName, String fieldName) { |
| 76 | return of(headerName, fieldName, 0); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the name of the header. |
| 81 | * |
| 82 | * @return a string value |
| 83 | */ |
| 84 | public String headerName() { |
| 85 | return headerName; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the name of the field. |
| 90 | * |
| 91 | * @return a string value |
| 92 | */ |
| 93 | public String fieldName() { |
| 94 | return fieldName; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the index of this header. |
| 99 | * |
| 100 | * @return an integer value. |
| 101 | */ |
| 102 | public int index() { |
| 103 | return index; |
| 104 | } |
| 105 | } |