blob: 99f7e759fc7239104012693a9c968ef7cab76418 [file] [log] [blame]
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone1022a4e2017-05-25 00:16:18 -04003 *
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
17package org.onosproject.net.pi.runtime;
18
19import org.onlab.util.Identifier;
20
Carmelo Cascone7b821702017-06-19 11:26:08 +090021import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040024/**
25 * Identifier of a packet's header field.
26 */
27public final class PiHeaderFieldId extends Identifier<String> {
28
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020029 // 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 Cascone1022a4e2017-05-25 00:16:18 -040033 private final String headerName;
34 private final String fieldName;
35 private final int index;
36
Carmelo Cascone7b821702017-06-19 11:26:08 +090037 private PiHeaderFieldId(String headerName, String fieldName, int index) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040038 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 Cascone7b821702017-06-19 11:26:08 +090047 * 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 Cascone1022a4e2017-05-25 00:16:18 -040052 *
53 * @param headerName header name
54 * @param fieldName field name
Carmelo Cascone7b821702017-06-19 11:26:08 +090055 * @param index index
56 * @return header field identifier
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040057 */
Carmelo Cascone7b821702017-06-19 11:26:08 +090058 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 Cascone1022a4e2017-05-25 00:16:18 -040077 }
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}