blob: 4238c924e797edcef97839c915be55dbe8588963 [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.collect.ImmutableList;
21import com.google.common.collect.ImmutableMap;
22import org.onosproject.net.pi.model.PiHeaderFieldTypeModel;
23import org.onosproject.net.pi.model.PiHeaderTypeModel;
24
25import java.util.List;
26import java.util.Objects;
27import java.util.Optional;
28
29import static com.google.common.base.MoreObjects.toStringHelper;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * BMv2 header type model.
34 */
35@Beta
36public final class Bmv2HeaderTypeModel implements PiHeaderTypeModel {
37 private final String name;
38 private final int id;
39 private final ImmutableMap<String, PiHeaderFieldTypeModel> fields;
40
41 /**
42 * Builds a BMv2 header type model by given information.
43 *
44 * @param name the name
45 * @param id the id
46 * @param fields the fields
47 */
48 public Bmv2HeaderTypeModel(String name,
49 int id,
50 List<Bmv2HeaderFieldTypeModel> fields) {
51 checkNotNull(name, "Type name can't be null");
52 checkNotNull(fields, "Fields can't be null");
53 this.name = name;
54 this.id = id;
55 ImmutableMap.Builder<String, PiHeaderFieldTypeModel> mapBuilder = ImmutableMap.builder();
56 fields.forEach(field -> mapBuilder.put(field.name(), field));
57 this.fields = mapBuilder.build();
58 }
59
60 /**
61 * Gets id of this header type model.
62 *
63 * @return the id
64 */
65 public int id() {
66 return id;
67 }
68
69 @Override
70 public String name() {
71 return name;
72 }
73
74 @Override
75 public Optional<PiHeaderFieldTypeModel> field(String fieldName) {
76 return Optional.ofNullable(fields.get(fieldName));
77 }
78
79 @Override
80 public List<PiHeaderFieldTypeModel> fields() {
81 return (ImmutableList<PiHeaderFieldTypeModel>) fields.values();
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(name, fields);
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94 if (obj == null || getClass() != obj.getClass()) {
95 return false;
96 }
97 final Bmv2HeaderTypeModel other = (Bmv2HeaderTypeModel) obj;
98 return Objects.equals(this.name, other.name)
99 && Objects.equals(this.fields, other.fields);
100 }
101
102 @Override
103 public String toString() {
104 return toStringHelper(this)
105 .add("name", name)
106 .add("fields", fields)
107 .toString();
108 }
109}