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.base.MoreObjects; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onosproject.net.pi.model.PiActionModel; |
| 23 | import org.onosproject.net.pi.model.PiTableMatchFieldModel; |
| 24 | import org.onosproject.net.pi.model.PiTableModel; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.Objects; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.*; |
| 31 | |
| 32 | /** |
| 33 | * BMv2 table model. |
| 34 | */ |
| 35 | @Beta |
| 36 | public final class Bmv2TableModel implements PiTableModel { |
| 37 | private final String name; |
| 38 | private final int id; |
| 39 | private final int maxSize; |
| 40 | private final boolean hasCounters; |
| 41 | private final boolean supportAging; |
| 42 | private final Set<PiTableMatchFieldModel> matchFields; |
| 43 | private final Set<PiActionModel> actions; |
| 44 | |
| 45 | /** |
| 46 | * Creates new BMv2 table model. |
| 47 | * |
| 48 | * @param name the name of table model |
| 49 | * @param id the table id |
| 50 | * @param maxSize the max size of table model |
| 51 | * @param hasCounters true if the table model has counter; null otherwise |
| 52 | * @param supportAging true if the table model supports aging; null otherwise |
| 53 | * @param matchFields the match fields of table model |
| 54 | * @param actions the actions of table model |
| 55 | */ |
| 56 | public Bmv2TableModel(String name, int id, |
| 57 | int maxSize, boolean hasCounters, |
| 58 | boolean supportAging, |
| 59 | Set<Bmv2TableMatchFieldModel> matchFields, |
| 60 | Set<Bmv2ActionModel> actions) { |
| 61 | checkNotNull(name, "Model name can't be null"); |
| 62 | checkArgument(maxSize >= 0, "Max size should more than 0"); |
| 63 | checkNotNull(matchFields, "Match fields can't be null"); |
| 64 | checkNotNull(actions, "Actions can't be null"); |
| 65 | this.name = name; |
| 66 | this.id = id; |
| 67 | this.maxSize = maxSize; |
| 68 | this.hasCounters = hasCounters; |
| 69 | this.supportAging = supportAging; |
| 70 | this.matchFields = ImmutableSet.copyOf(matchFields); |
| 71 | this.actions = ImmutableSet.copyOf(actions); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gets table model id. |
| 76 | * |
| 77 | * @return table model id |
| 78 | */ |
| 79 | public int id() { |
| 80 | return id; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String name() { |
| 85 | return name; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int maxSize() { |
| 90 | return maxSize; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean hasCounters() { |
| 95 | return hasCounters; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean supportsAging() { |
| 100 | return supportAging; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Collection<PiTableMatchFieldModel> matchFields() { |
| 105 | return matchFields; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Collection<PiActionModel> actions() { |
| 110 | return actions; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int hashCode() { |
| 115 | return Objects.hash(name, id, maxSize, hasCounters, supportAging, |
| 116 | matchFields, actions); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
| 121 | if (obj == this) { |
| 122 | return true; |
| 123 | } |
| 124 | if (!(obj instanceof Bmv2TableModel)) { |
| 125 | return false; |
| 126 | } |
| 127 | Bmv2TableModel that = (Bmv2TableModel) obj; |
| 128 | return Objects.equals(name, that.name) && |
| 129 | Objects.equals(id, that.id) && |
| 130 | Objects.equals(maxSize, that.maxSize) && |
| 131 | Objects.equals(hasCounters, that.hasCounters) && |
| 132 | Objects.equals(supportAging, that.supportAging) && |
| 133 | Objects.equals(matchFields, that.matchFields) && |
| 134 | Objects.equals(actions, that.actions); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String toString() { |
| 139 | return MoreObjects.toStringHelper(getClass()) |
| 140 | .add("name", name) |
| 141 | .add("id", id) |
| 142 | .add("maxSize", maxSize) |
| 143 | .add("hasCounters", hasCounters) |
| 144 | .add("supportAging", supportAging) |
| 145 | .add("matchFields", matchFields) |
| 146 | .add("actions", actions) |
| 147 | .toString(); |
| 148 | } |
| 149 | } |