blob: 53503401ca267177d1020dcf9a0ffa66bd43287d [file] [log] [blame]
Carmelo Cascone5899c132016-04-06 22:09:08 -07001/*
Carmelo Casconeaa8b6292016-04-13 14:27:06 -07002 * Copyright 2016-present Open Networking Laboratory
Carmelo Cascone5899c132016-04-06 22:09:08 -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
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070017package org.onosproject.bmv2.api.context;
Carmelo Cascone5899c132016-04-06 22:09:08 -070018
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070019import com.google.common.annotations.Beta;
Carmelo Cascone5899c132016-04-06 22:09:08 -070020import com.google.common.base.Objects;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.Maps;
23
24import java.util.LinkedHashMap;
25import java.util.List;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
28
29/**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070030 * BMv2 header type model.
Carmelo Cascone5899c132016-04-06 22:09:08 -070031 */
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070032@Beta
33public final class Bmv2HeaderTypeModel {
Carmelo Cascone5899c132016-04-06 22:09:08 -070034
35 private final String name;
36 private final int id;
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070037 private final LinkedHashMap<String, Bmv2FieldTypeModel> fields = Maps.newLinkedHashMap();
Carmelo Cascone5899c132016-04-06 22:09:08 -070038
39 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070040 * Creates a new header type model.
Carmelo Cascone5899c132016-04-06 22:09:08 -070041 *
42 * @param name name
43 * @param id id
44 * @param fieldTypes fields
45 */
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070046 protected Bmv2HeaderTypeModel(String name, int id, List<Bmv2FieldTypeModel> fieldTypes) {
Carmelo Cascone5899c132016-04-06 22:09:08 -070047 this.name = name;
48 this.id = id;
49 fieldTypes.forEach(f -> this.fields.put(f.name(), f));
50 }
51
52 /**
53 * Returns this header type name.
54 *
55 * @return name
56 */
57 public String name() {
58 return name;
59 }
60
61 /**
62 * Returns this header type id.
63 *
64 * @return id
65 */
66 public int id() {
67 return id;
68 }
69
70 /**
71 * Returns this header type's field defined by the passed name, null if
72 * not present.
73 *
74 * @param fieldName field name
75 * @return field or null
76 */
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070077 public Bmv2FieldTypeModel field(String fieldName) {
Carmelo Cascone5899c132016-04-06 22:09:08 -070078 return fields.get(fieldName);
79 }
80
81 /**
82 * Return and immutable list of header fields for this header
83 * type. The list is ordered according to the values defined in the
84 * model.
85 *
86 * @return list of fields
87 */
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070088 public List<Bmv2FieldTypeModel> fields() {
Carmelo Cascone5899c132016-04-06 22:09:08 -070089 return ImmutableList.copyOf(fields.values());
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hashCode(name, id, fields);
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 }
Carmelo Cascone17fc9e42016-05-31 11:29:21 -0700105 final Bmv2HeaderTypeModel other = (Bmv2HeaderTypeModel) obj;
Carmelo Cascone5899c132016-04-06 22:09:08 -0700106 return Objects.equal(this.name, other.name)
107 && Objects.equal(this.id, other.id)
108 && Objects.equal(this.fields, other.fields);
109 }
110
111 @Override
112 public String toString() {
113 return toStringHelper(this)
114 .add("name", name)
115 .add("id", id)
116 .add("fields", fields)
117 .toString();
118 }
119}