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