blob: 189318c81d79ca03c6b1bd5273d5bb25f90ecf62 [file] [log] [blame]
FrankWangea70de32018-05-08 15:41:25 +08001/*
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
17package org.onosproject.net.pi.runtime.data;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import org.onosproject.net.pi.model.PiData;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * PiHeaderUnion entity in a protocol-independent pipeline.
28 */
29@Beta
30public final class PiHeaderUnion implements PiData {
31 private final String validHeaderName;
32 private final PiHeader header;
33 private final boolean isValid;
34
35 /**
36 * Creates a new protocol-independent header union instance for the given header name and header.
37 *
38 * @param isValid indicates whether this header union is valid
39 * @param validHeaderName header name
40 * @param header the header
41 */
42 private PiHeaderUnion(boolean isValid, String validHeaderName, PiHeader header) {
43 this.isValid = isValid;
44 this.validHeaderName = validHeaderName;
45 this.header = header;
46 }
47
48 /**
49 * Returns a new invalid protocol-independent header union.
50 *
51 * @return header union
52 */
53 public static PiHeaderUnion ofInvalid() {
54 return new PiHeaderUnion(false, null, null);
55 }
56
57 /**
58 * Returns a new valid protocol-independent header union.
59 * @param validHeaderName header name
60 * @param header the header
61 * @return header union
62 */
63 public static PiHeaderUnion of(String validHeaderName, PiHeader header) {
64 checkNotNull(validHeaderName);
65 checkArgument(!validHeaderName.isEmpty(), "The header name must not be empty");
66 checkNotNull(header);
67 return new PiHeaderUnion(true, validHeaderName, header);
68 }
69
70 /**
71 * Returns true if this header is valid, false otherwise.
72 *
73 * @return a boolean value
74 */
75 public boolean isValid() {
76 return this.isValid;
77 }
78
79 /**
80 * Return the header name.
81 *
82 * @return header name, return null if the header union invalid
83 */
84 public String headerName() {
85 return this.validHeaderName;
86 }
87
88 /**
89 * Return the header.
90 *
91 * @return header, return null if the header union invalid
92 */
93 public PiHeader header() {
94 return this.header;
95 }
96
97 @Override
98 public Type type() {
99 return Type.HEADERUNION;
100 }
101
102 @Override
103 public boolean equals(Object o) {
104 if (this == o) {
105 return true;
106 }
107 if (o == null || getClass() != o.getClass()) {
108 return false;
109 }
110 PiHeaderUnion headerUnion = (PiHeaderUnion) o;
111 return Objects.equal(validHeaderName, headerUnion.validHeaderName) &&
112 Objects.equal(header, headerUnion.header);
113 }
114
115 @Override
116 public int hashCode() {
117 return Objects.hashCode(validHeaderName, header);
118 }
119
120 @Override
121 public String toString() {
122 return !isValid ? "INVALID" : validHeaderName + ":" + header.toString();
123 }
124}