blob: 277323011fb0616d75575b0faa71ad062895a6e2 [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.PiHeaderModel;
22import org.onosproject.net.pi.model.PiHeaderTypeModel;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25import static com.google.common.base.Preconditions.*;
26
27/**
28 * BMv2 header model.
29 */
30@Beta
31public final class Bmv2HeaderModel implements PiHeaderModel {
32 private final String name;
33 private final int id;
34 private final int index;
35 private final Bmv2HeaderTypeModel type;
36 private final boolean isMetadata;
37
38 /**
39 * Builds a new BMv2 header model with given information.
40 *
41 * @param name the name of this header mdoel
42 * @param id the id of this header model
43 * @param index the header index
44 * @param type the type of this header model
45 * @param metadata if the header is metadata
46 */
47 public Bmv2HeaderModel(String name, int id, int index, Bmv2HeaderTypeModel type, boolean metadata) {
48 checkNotNull(name, "Model name can't be null.");
49 checkArgument(index >= 0, "Index should be a positive integer");
50 checkNotNull(type, "Header type can't be null.");
51 this.name = name;
52 this.id = id;
53 this.index = index;
54 this.type = type;
55 this.isMetadata = metadata;
56 }
57
58 /**
59 * Gets the name of this header model.
60 *
61 * @return name of this model
62 */
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020063 @Override
Yi Tsengf33c0772017-06-06 14:56:18 -070064 public String name() {
65 return name;
66 }
67
68 /**
69 * Gets the id of this header model.
70 *
71 * @return if of this header model
72 */
73 public int id() {
74 return id;
75 }
76
77 @Override
78 public int index() {
79 return index;
80 }
81
82 @Override
83 public PiHeaderTypeModel type() {
84 return type;
85 }
86
87 @Override
88 public boolean isMetadata() {
89 return isMetadata;
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hashCode(id, type, isMetadata);
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) {
100 return true;
101 }
102 if (obj == null || getClass() != obj.getClass()) {
103 return false;
104 }
105 final Bmv2HeaderModel other = (Bmv2HeaderModel) obj;
106 return Objects.equal(this.id, other.id)
107 && Objects.equal(this.type, other.type)
108 && Objects.equal(this.isMetadata, other.isMetadata);
109 }
110
111 @Override
112 public String toString() {
113 return toStringHelper(this)
114 .add("index", id)
115 .add("type", type)
116 .add("isMetadata", isMetadata)
117 .toString();
118 }
119}