Yi Tseng | f33c077 | 2017-06-06 14:56:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bmv2.model; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import org.onosproject.net.pi.model.PiHeaderFieldTypeModel; |
| 23 | import org.onosproject.net.pi.model.PiHeaderTypeModel; |
| 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * BMv2 header type model. |
| 34 | */ |
| 35 | @Beta |
| 36 | public 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 | } |