blob: a8a9cad0a1629883a61461bf59d18ed565c184be [file] [log] [blame]
Yi Tsengf33c0772017-06-06 14:56:18 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengf33c0772017-06-06 14:56:18 -07003 *
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.bmv2.model;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import org.onosproject.net.pi.model.PiHeaderFieldTypeModel;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * BMv2 header field type model.
29 */
30@Beta
31public final class Bmv2HeaderFieldTypeModel implements PiHeaderFieldTypeModel {
32 private final String name;
33 private final int bitWidth;
34 private final boolean signed;
35
36 /**
37 * Builds a BMv2 header field type model with given information.
38 *
39 * @param name the name of field type
40 * @param bitWidth the bit width of field type
41 * @param signed header type field is signed or not
42 */
43 public Bmv2HeaderFieldTypeModel(String name, int bitWidth, boolean signed) {
44 checkNotNull(name, "Header field type name can't be null");
45 checkArgument(bitWidth > 0, "Bit width should be non-zero positive integer");
46 this.name = name;
47 this.bitWidth = bitWidth;
48 this.signed = signed;
49 }
50
51 @Override
52 public String name() {
53 return name;
54 }
55
56 @Override
57 public int bitWidth() {
58 return bitWidth;
59 }
60
61 /**
62 * Determine whether the header type field is signed or not.
63 *
64 * @return true if it is signed; otherwise false
65 */
66 public boolean signed() {
67 return signed;
68 }
69
70 @Override
71 public int hashCode() {
72 return Objects.hashCode(name, bitWidth);
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj) {
78 return true;
79 }
80 if (obj == null || getClass() != obj.getClass()) {
81 return false;
82 }
83 final Bmv2HeaderFieldTypeModel other = (Bmv2HeaderFieldTypeModel) obj;
84 return Objects.equal(this.name, other.name) &&
85 Objects.equal(this.bitWidth, other.bitWidth) &&
86 Objects.equal(this.signed, other.signed);
87 }
88
89 @Override
90 public String toString() {
91 return toStringHelper(this)
92 .add("name", name)
93 .add("bitWidth", bitWidth)
94 .toString();
95 }
96
97
98}