blob: 6af08ef90d079764816e617f4d072d33580a5507 [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
29 private final String headerName;
30 private final String fieldName;
31 private final int index;
32
Carmelo Cascone7b821702017-06-19 11:26:08 +090033 private PiHeaderFieldId(String headerName, String fieldName, int index) {
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040034 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 Cascone7b821702017-06-19 11:26:08 +090043 * 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 Cascone1022a4e2017-05-25 00:16:18 -040048 *
49 * @param headerName header name
50 * @param fieldName field name
Carmelo Cascone7b821702017-06-19 11:26:08 +090051 * @param index index
52 * @return header field identifier
Carmelo Cascone1022a4e2017-05-25 00:16:18 -040053 */
Carmelo Cascone7b821702017-06-19 11:26:08 +090054 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 Cascone1022a4e2017-05-25 00:16:18 -040073 }
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}