blob: 6c838c4d8049feeebd2ba35736283319b77b0dee [file] [log] [blame]
Yi Tsengf33c0772017-06-06 14:56:18 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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.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 */
63 public String name() {
64 return name;
65 }
66
67 /**
68 * Gets the id of this header model.
69 *
70 * @return if of this header model
71 */
72 public int id() {
73 return id;
74 }
75
76 @Override
77 public int index() {
78 return index;
79 }
80
81 @Override
82 public PiHeaderTypeModel type() {
83 return type;
84 }
85
86 @Override
87 public boolean isMetadata() {
88 return isMetadata;
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hashCode(id, type, isMetadata);
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
101 if (obj == null || getClass() != obj.getClass()) {
102 return false;
103 }
104 final Bmv2HeaderModel other = (Bmv2HeaderModel) obj;
105 return Objects.equal(this.id, other.id)
106 && Objects.equal(this.type, other.type)
107 && Objects.equal(this.isMetadata, other.isMetadata);
108 }
109
110 @Override
111 public String toString() {
112 return toStringHelper(this)
113 .add("index", id)
114 .add("type", type)
115 .add("isMetadata", isMetadata)
116 .toString();
117 }
118}