blob: be1b71c2aea4f20ccfee55e08bd1b57f286cb8b6 [file] [log] [blame]
Carmelo Cascone87892e22017-11-13 16:01:29 -08001/*
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
17package org.onosproject.p4runtime.model;
18
19import com.google.common.collect.ImmutableMap;
20import org.onosproject.net.pi.model.PiActionId;
21import org.onosproject.net.pi.model.PiActionModel;
22import org.onosproject.net.pi.model.PiActionProfileModel;
23import org.onosproject.net.pi.model.PiCounterId;
24import org.onosproject.net.pi.model.PiCounterModel;
25import org.onosproject.net.pi.model.PiMatchFieldId;
26import org.onosproject.net.pi.model.PiMatchFieldModel;
27import org.onosproject.net.pi.model.PiMeterId;
28import org.onosproject.net.pi.model.PiMeterModel;
29import org.onosproject.net.pi.model.PiTableId;
30import org.onosproject.net.pi.model.PiTableModel;
31import org.onosproject.net.pi.model.PiTableType;
32
33import java.util.Collection;
34import java.util.Objects;
35import java.util.Optional;
36
pierventre69329172021-01-10 17:29:03 -080037import static com.google.common.base.MoreObjects.toStringHelper;
38
Carmelo Cascone87892e22017-11-13 16:01:29 -080039/**
40 * Implementation of PiTableModel for P4Runtime.
41 */
42final class P4TableModel implements PiTableModel {
43
44 private final PiTableId id;
45 private final PiTableType tableType;
46 private final PiActionProfileModel actionProfile;
47 private final long maxSize;
48 private final ImmutableMap<PiCounterId, PiCounterModel> counters;
49 private final ImmutableMap<PiMeterId, PiMeterModel> meters;
50 private final boolean supportAging;
51 private final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields;
52 private final ImmutableMap<PiActionId, PiActionModel> actions;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070053 private final PiActionModel constDefaultAction;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070054 private final boolean isConstTable;
Carmelo Cascone87892e22017-11-13 16:01:29 -080055
56 P4TableModel(PiTableId id, PiTableType tableType,
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070057 PiActionProfileModel actionProfile, long maxSize,
58 ImmutableMap<PiCounterId, PiCounterModel> counters,
59 ImmutableMap<PiMeterId, PiMeterModel> meters, boolean supportAging,
60 ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields,
61 ImmutableMap<PiActionId, PiActionModel> actions,
Carmelo Casconee45902b2018-12-18 13:30:45 -080062 PiActionModel constDefaultAction,
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070063 boolean isConstTable) {
Carmelo Cascone87892e22017-11-13 16:01:29 -080064 this.id = id;
65 this.tableType = tableType;
66 this.actionProfile = actionProfile;
67 this.maxSize = maxSize;
68 this.counters = counters;
69 this.meters = meters;
70 this.supportAging = supportAging;
71 this.matchFields = matchFields;
72 this.actions = actions;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070073 this.constDefaultAction = constDefaultAction;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070074 this.isConstTable = isConstTable;
Carmelo Cascone87892e22017-11-13 16:01:29 -080075 }
76
77 @Override
78 public PiTableId id() {
79 return id;
80 }
81
82 @Override
83 public PiTableType tableType() {
84 return tableType;
85 }
86
87 @Override
88 public PiActionProfileModel actionProfile() {
89 return actionProfile;
90 }
91
92 @Override
93 public long maxSize() {
94 return maxSize;
95 }
96
97 @Override
98 public Collection<PiCounterModel> counters() {
99 return counters.values();
100 }
101
102 @Override
103 public Collection<PiMeterModel> meters() {
104 return meters.values();
105 }
106
107 @Override
108 public boolean supportsAging() {
109 return supportAging;
110 }
111
112 @Override
113 public Collection<PiMatchFieldModel> matchFields() {
114 return matchFields.values();
115 }
116
117 @Override
118 public Collection<PiActionModel> actions() {
119 return actions.values();
120 }
121
122 @Override
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700123 public Optional<PiActionModel> constDefaultAction() {
124 return Optional.ofNullable(constDefaultAction);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800125 }
126
127 @Override
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700128 public boolean isConstantTable() {
129 return isConstTable;
130 }
131
132 @Override
Carmelo Cascone87892e22017-11-13 16:01:29 -0800133 public Optional<PiActionModel> action(PiActionId actionId) {
134 return Optional.ofNullable(actions.get(actionId));
135 }
136
137 @Override
138 public Optional<PiMatchFieldModel> matchField(PiMatchFieldId matchFieldId) {
139 return Optional.ofNullable(matchFields.get(matchFieldId));
140 }
141
142 @Override
143 public int hashCode() {
144 return Objects.hash(id, tableType, actionProfile, maxSize, counters,
145 meters, supportAging, matchFields, actions,
Carmelo Casconee45902b2018-12-18 13:30:45 -0800146 constDefaultAction);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800147 }
148
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj) {
152 return true;
153 }
154 if (obj == null || getClass() != obj.getClass()) {
155 return false;
156 }
157 final P4TableModel other = (P4TableModel) obj;
158 return Objects.equals(this.id, other.id)
159 && Objects.equals(this.tableType, other.tableType)
160 && Objects.equals(this.actionProfile, other.actionProfile)
161 && Objects.equals(this.maxSize, other.maxSize)
162 && Objects.equals(this.counters, other.counters)
163 && Objects.equals(this.meters, other.meters)
164 && Objects.equals(this.supportAging, other.supportAging)
165 && Objects.equals(this.matchFields, other.matchFields)
166 && Objects.equals(this.actions, other.actions)
Carmelo Casconee45902b2018-12-18 13:30:45 -0800167 && Objects.equals(this.constDefaultAction, other.constDefaultAction);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800168 }
pierventre69329172021-01-10 17:29:03 -0800169
170 @Override
171 public String toString() {
172 return toStringHelper(this)
173 .add("id", id)
174 .add("tableType", tableType)
175 .add("actionProfile", actionProfile)
176 .add("maxSize", maxSize)
177 .add("counters", counters)
178 .add("meters", meters)
179 .add("supportAging", supportAging)
180 .add("matchFields", matchFields)
181 .add("actions", actions)
182 .add("constDefaultAction", constDefaultAction)
183 .add("isConstTable", isConstTable)
184 .toString();
185 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800186}