Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.p4runtime.model; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onosproject.net.pi.model.PiActionId; |
| 21 | import org.onosproject.net.pi.model.PiActionModel; |
| 22 | import org.onosproject.net.pi.model.PiActionProfileModel; |
| 23 | import org.onosproject.net.pi.model.PiCounterId; |
| 24 | import org.onosproject.net.pi.model.PiCounterModel; |
| 25 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 26 | import org.onosproject.net.pi.model.PiMatchFieldModel; |
| 27 | import org.onosproject.net.pi.model.PiMeterId; |
| 28 | import org.onosproject.net.pi.model.PiMeterModel; |
| 29 | import org.onosproject.net.pi.model.PiTableId; |
| 30 | import org.onosproject.net.pi.model.PiTableModel; |
| 31 | import org.onosproject.net.pi.model.PiTableType; |
| 32 | |
| 33 | import java.util.Collection; |
| 34 | import java.util.Objects; |
| 35 | import java.util.Optional; |
| 36 | |
| 37 | /** |
| 38 | * Implementation of PiTableModel for P4Runtime. |
| 39 | */ |
| 40 | final class P4TableModel implements PiTableModel { |
| 41 | |
| 42 | private final PiTableId id; |
| 43 | private final PiTableType tableType; |
| 44 | private final PiActionProfileModel actionProfile; |
| 45 | private final long maxSize; |
| 46 | private final ImmutableMap<PiCounterId, PiCounterModel> counters; |
| 47 | private final ImmutableMap<PiMeterId, PiMeterModel> meters; |
| 48 | private final boolean supportAging; |
| 49 | private final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields; |
| 50 | private final ImmutableMap<PiActionId, PiActionModel> actions; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 51 | private final PiActionModel constDefaultAction; |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 52 | private final boolean isConstTable; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 53 | |
| 54 | P4TableModel(PiTableId id, PiTableType tableType, |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 55 | PiActionProfileModel actionProfile, long maxSize, |
| 56 | ImmutableMap<PiCounterId, PiCounterModel> counters, |
| 57 | ImmutableMap<PiMeterId, PiMeterModel> meters, boolean supportAging, |
| 58 | ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields, |
| 59 | ImmutableMap<PiActionId, PiActionModel> actions, |
Carmelo Cascone | e45902b | 2018-12-18 13:30:45 -0800 | [diff] [blame^] | 60 | PiActionModel constDefaultAction, |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 61 | boolean isConstTable) { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 62 | this.id = id; |
| 63 | this.tableType = tableType; |
| 64 | this.actionProfile = actionProfile; |
| 65 | this.maxSize = maxSize; |
| 66 | this.counters = counters; |
| 67 | this.meters = meters; |
| 68 | this.supportAging = supportAging; |
| 69 | this.matchFields = matchFields; |
| 70 | this.actions = actions; |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 71 | this.constDefaultAction = constDefaultAction; |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 72 | this.isConstTable = isConstTable; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public PiTableId id() { |
| 77 | return id; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public PiTableType tableType() { |
| 82 | return tableType; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public PiActionProfileModel actionProfile() { |
| 87 | return actionProfile; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public long maxSize() { |
| 92 | return maxSize; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Collection<PiCounterModel> counters() { |
| 97 | return counters.values(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Collection<PiMeterModel> meters() { |
| 102 | return meters.values(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean supportsAging() { |
| 107 | return supportAging; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Collection<PiMatchFieldModel> matchFields() { |
| 112 | return matchFields.values(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Collection<PiActionModel> actions() { |
| 117 | return actions.values(); |
| 118 | } |
| 119 | |
| 120 | @Override |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 121 | public Optional<PiActionModel> constDefaultAction() { |
| 122 | return Optional.ofNullable(constDefaultAction); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Carmelo Cascone | 33b27bc | 2018-09-09 22:56:14 -0700 | [diff] [blame] | 126 | public boolean isConstantTable() { |
| 127 | return isConstTable; |
| 128 | } |
| 129 | |
| 130 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 131 | public Optional<PiActionModel> action(PiActionId actionId) { |
| 132 | return Optional.ofNullable(actions.get(actionId)); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Optional<PiMatchFieldModel> matchField(PiMatchFieldId matchFieldId) { |
| 137 | return Optional.ofNullable(matchFields.get(matchFieldId)); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int hashCode() { |
| 142 | return Objects.hash(id, tableType, actionProfile, maxSize, counters, |
| 143 | meters, supportAging, matchFields, actions, |
Carmelo Cascone | e45902b | 2018-12-18 13:30:45 -0800 | [diff] [blame^] | 144 | constDefaultAction); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean equals(Object obj) { |
| 149 | if (this == obj) { |
| 150 | return true; |
| 151 | } |
| 152 | if (obj == null || getClass() != obj.getClass()) { |
| 153 | return false; |
| 154 | } |
| 155 | final P4TableModel other = (P4TableModel) obj; |
| 156 | return Objects.equals(this.id, other.id) |
| 157 | && Objects.equals(this.tableType, other.tableType) |
| 158 | && Objects.equals(this.actionProfile, other.actionProfile) |
| 159 | && Objects.equals(this.maxSize, other.maxSize) |
| 160 | && Objects.equals(this.counters, other.counters) |
| 161 | && Objects.equals(this.meters, other.meters) |
| 162 | && Objects.equals(this.supportAging, other.supportAging) |
| 163 | && Objects.equals(this.matchFields, other.matchFields) |
| 164 | && Objects.equals(this.actions, other.actions) |
Carmelo Cascone | e45902b | 2018-12-18 13:30:45 -0800 | [diff] [blame^] | 165 | && Objects.equals(this.constDefaultAction, other.constDefaultAction); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 166 | } |
| 167 | } |