blob: 13677c0ceb836026b89734f95156318fc2c4ff4f [file] [log] [blame]
Mehmed Mustafa3e269df2017-11-24 17:19:49 +03001/*
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 com.google.common.collect.ImmutableSet;
21import com.google.common.testing.EqualsTester;
22import org.junit.Test;
23import org.onosproject.net.pi.model.PiActionId;
24import org.onosproject.net.pi.model.PiActionModel;
25import org.onosproject.net.pi.model.PiActionParamId;
26import org.onosproject.net.pi.model.PiActionParamModel;
27import org.onosproject.net.pi.model.PiActionProfileId;
28import org.onosproject.net.pi.model.PiActionProfileModel;
29import org.onosproject.net.pi.model.PiCounterId;
30import org.onosproject.net.pi.model.PiCounterModel;
31import org.onosproject.net.pi.model.PiCounterType;
32import org.onosproject.net.pi.model.PiMatchFieldId;
33import org.onosproject.net.pi.model.PiMatchFieldModel;
34import org.onosproject.net.pi.model.PiMatchType;
35import org.onosproject.net.pi.model.PiMeterId;
36import org.onosproject.net.pi.model.PiMeterModel;
37import org.onosproject.net.pi.model.PiMeterType;
38import org.onosproject.net.pi.model.PiTableId;
39import org.onosproject.net.pi.model.PiTableModel;
40import org.onosproject.net.pi.model.PiTableType;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070041
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030042import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
43
44/**
45 * Unit tests for P4TableModel class.
46 */
47public class P4TableModelTest {
48
49 /* Action Profiles */
50 private static final PiActionProfileId PI_ACTION_PROFILE_ID_1 = PiActionProfileId.of("Action1");
51 private static final PiActionProfileId PI_ACTION_PROFILE_ID_2 = PiActionProfileId.of("Action2");
52
53 private static final PiTableId ACTION_PI_TABLE_ID_1 = PiTableId.of("ActionTable1");
54 private static final PiTableId ACTION_PI_TABLE_ID_2 = PiTableId.of("ActionTable2");
55
56 private static final ImmutableSet<PiTableId> ACTION_TABLES_1 = new ImmutableSet.Builder<PiTableId>()
57 .add(ACTION_PI_TABLE_ID_1)
58 .build();
59
60 private static final ImmutableSet<PiTableId> ACTION_TABLES_2 = new ImmutableSet.Builder<PiTableId>()
61 .add(ACTION_PI_TABLE_ID_2)
62 .build();
63
64 private static final boolean ACTION_HAS_SELECTOR_1 = true;
65 private static final boolean ACTION_HAS_SELECTOR_2 = false;
66
67 private static final long ACTION_MAX_SIZE_1 = 100;
68 private static final long ACTION_MAX_SIZE_2 = 200;
69
Carmelo Cascone99c59db2019-01-17 15:39:35 -080070 private static final int ACTION_MAX_GROUP_SIZE_1 = 10;
71 private static final int ACTION_MAX_GROUP_SIZE_2 = 20;
72
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030073 private static final PiActionProfileModel P4_ACTION_PROFILE_MODEL_1 =
74 new P4ActionProfileModel(PI_ACTION_PROFILE_ID_1, ACTION_TABLES_1,
Carmelo Cascone99c59db2019-01-17 15:39:35 -080075 ACTION_HAS_SELECTOR_1, ACTION_MAX_SIZE_1,
76 ACTION_MAX_GROUP_SIZE_1);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030077 private static final PiActionProfileModel P4_ACTION_PROFILE_MODEL_2 =
78 new P4ActionProfileModel(PI_ACTION_PROFILE_ID_2, ACTION_TABLES_2,
Carmelo Cascone99c59db2019-01-17 15:39:35 -080079 ACTION_HAS_SELECTOR_2, ACTION_MAX_SIZE_2,
80 ACTION_MAX_GROUP_SIZE_2);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030081
82 /* Counters */
83 private static final PiCounterId PI_COUNTER_ID_1 = PiCounterId.of("Counter1");
84 private static final PiCounterId PI_COUNTER_ID_2 = PiCounterId.of("Counter2");
85
86 private static final PiCounterType PI_COUNTER_TYPE_1 = PiCounterType.DIRECT;
87 private static final PiCounterType PI_COUNTER_TYPE_2 = PiCounterType.INDIRECT;
88
89 private static final PiCounterModel.Unit COUNTER_UNIT_BYTES = P4CounterModel.Unit.BYTES;
90 private static final PiCounterModel.Unit COUNTER_UNIT_PACKETS = P4CounterModel.Unit.PACKETS;
91
92 private static final PiTableId COUNTER_PI_TABLE_ID_1 = PiTableId.of("CounterTable1");
93 private static final PiTableId COUNTER_PI_TABLE_ID_2 = PiTableId.of("CounterTable2");
94
95 private static final long COUNTER_SIZE_1 = 1000;
96 private static final long COUNTER_SIZE_2 = 2000;
97
98 private static final PiCounterModel P4_COUNTER_MODEL_1 =
99 new P4CounterModel(PI_COUNTER_ID_1, PI_COUNTER_TYPE_1, COUNTER_UNIT_BYTES,
100 COUNTER_PI_TABLE_ID_1, COUNTER_SIZE_1);
101 private static final PiCounterModel P4_COUNTER_MODEL_2 =
102 new P4CounterModel(PI_COUNTER_ID_2, PI_COUNTER_TYPE_2, COUNTER_UNIT_PACKETS,
103 COUNTER_PI_TABLE_ID_2, COUNTER_SIZE_2);
104
105 private static final ImmutableMap<PiCounterId, PiCounterModel> COUNTERS_1 =
106 new ImmutableMap.Builder<PiCounterId, PiCounterModel>()
107 .put(PI_COUNTER_ID_1, P4_COUNTER_MODEL_1)
108 .build();
109 private static final ImmutableMap<PiCounterId, PiCounterModel> COUNTERS_2 =
110 new ImmutableMap.Builder<PiCounterId, PiCounterModel>()
111 .put(PI_COUNTER_ID_2, P4_COUNTER_MODEL_2)
112 .build();
113
114 /* Meters */
115 private static final PiMeterId PI_METER_ID_1 = PiMeterId.of("Meter1");
116 private static final PiMeterId PI_METER_ID_2 = PiMeterId.of("Meter2");
117
118 private static final PiMeterType PI_METER_TYPE_1 = PiMeterType.DIRECT;
119 private static final PiMeterType PI_METER_TYPE_2 = PiMeterType.INDIRECT;
120
121 private static final PiMeterModel.Unit METER_UNIT_BYTES = P4MeterModel.Unit.BYTES;
122 private static final PiMeterModel.Unit METER_UNIT_PACKETS = P4MeterModel.Unit.PACKETS;
123
124 private static final PiTableId METER_PI_TABLE_ID_1 = PiTableId.of("MeterTable1");
125 private static final PiTableId METER_PI_TABLE_ID_2 = PiTableId.of("MeterTable2");
126
127 private static final long METER_SIZE_1 = 1000;
128 private static final long METER_SIZE_2 = 2000;
129
130 private static final PiMeterModel P4_METER_MODEL_1 =
131 new P4MeterModel(PI_METER_ID_1, PI_METER_TYPE_1, METER_UNIT_BYTES, METER_PI_TABLE_ID_1, METER_SIZE_1);
132 private static final PiMeterModel P4_METER_MODEL_2 =
133 new P4MeterModel(PI_METER_ID_2, PI_METER_TYPE_2, METER_UNIT_PACKETS, METER_PI_TABLE_ID_2, METER_SIZE_2);
134
135 private static final ImmutableMap<PiMeterId, PiMeterModel> METERS_1 =
136 new ImmutableMap.Builder<PiMeterId, PiMeterModel>()
137 .put(PI_METER_ID_1, P4_METER_MODEL_1)
138 .build();
139 private static final ImmutableMap<PiMeterId, PiMeterModel> METERS_2 =
140 new ImmutableMap.Builder<PiMeterId, PiMeterModel>()
141 .put(PI_METER_ID_2, P4_METER_MODEL_2)
142 .build();
143
144 /* Match Fields */
145 private static final PiMatchFieldId PI_MATCH_FIELD_ID_1 = PiMatchFieldId.of("MatchField1");
146 private static final PiMatchFieldId PI_MATCH_FIELD_ID_2 = PiMatchFieldId.of("MatchField2");
147
148 private static final int MATCH_FIELD_BIT_WIDTH_1 = 8;
149 private static final int MATCH_FIELD_BIT_WIDTH_2 = 16;
150
151 private static final PiMatchType PI_MATCH_TYPE_1 = PiMatchType.EXACT;
152 private static final PiMatchType PI_MATCH_TYPE_2 = PiMatchType.TERNARY;
153
154 private static final PiMatchFieldModel P4_MATCH_FIELD_MODEL_1 =
155 new P4MatchFieldModel(PI_MATCH_FIELD_ID_1, MATCH_FIELD_BIT_WIDTH_1, PI_MATCH_TYPE_1);
156 private static final PiMatchFieldModel P4_MATCH_FIELD_MODEL_2 =
157 new P4MatchFieldModel(PI_MATCH_FIELD_ID_2, MATCH_FIELD_BIT_WIDTH_2, PI_MATCH_TYPE_2);
158
159 private static final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> MATCH_FIELDS_1 =
160 new ImmutableMap.Builder<PiMatchFieldId, PiMatchFieldModel>()
161 .put(PI_MATCH_FIELD_ID_1, P4_MATCH_FIELD_MODEL_1)
162 .build();
163 private static final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> MATCH_FIELDS_2 =
164 new ImmutableMap.Builder<PiMatchFieldId, PiMatchFieldModel>()
165 .put(PI_MATCH_FIELD_ID_2, P4_MATCH_FIELD_MODEL_2)
166 .build();
167
168 /* Actions */
169 private static final PiActionId PI_ACTION_ID_1 = PiActionId.of("Action1");
170 private static final PiActionId PI_ACTION_ID_2 = PiActionId.of("Action2");
171
172 private static final PiActionParamId PI_ACTION_PARAM_ID_1 = PiActionParamId.of("ActionParameter1");
173 private static final PiActionParamId PI_ACTION_PARAM_ID_2 = PiActionParamId.of("ActionParameter2");
174
175 private static final int ACTION_PARAM_BIT_WIDTH_1 = 8;
176 private static final int ACTION_PARAM_BIT_WIDTH_2 = 16;
177
178 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_1 =
179 new P4ActionParamModel(PI_ACTION_PARAM_ID_1, ACTION_PARAM_BIT_WIDTH_1);
180 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_2 =
181 new P4ActionParamModel(PI_ACTION_PARAM_ID_2, ACTION_PARAM_BIT_WIDTH_2);
182
183 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_1 =
184 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
185 .put(PI_ACTION_PARAM_ID_1, P4_ACTION_PARAM_MODEL_1)
186 .build();
187 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_2 =
188 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
189 .put(PI_ACTION_PARAM_ID_2, P4_ACTION_PARAM_MODEL_2)
190 .build();
191
192 private static final PiActionModel P4_ACTION_MODEL_1 = new P4ActionModel(PI_ACTION_ID_1, PI_ACTION_PARAMS_1);
193 private static final PiActionModel P4_ACTION_MODEL_2 = new P4ActionModel(PI_ACTION_ID_2, PI_ACTION_PARAMS_2);
194
195 private static final ImmutableMap<PiActionId, PiActionModel> ACTIONS_1 =
196 new ImmutableMap.Builder<PiActionId, PiActionModel>()
197 .put(PI_ACTION_ID_1, P4_ACTION_MODEL_1)
198 .build();
199
200 private static final ImmutableMap<PiActionId, PiActionModel> ACTIONS_2 =
201 new ImmutableMap.Builder<PiActionId, PiActionModel>()
202 .put(PI_ACTION_ID_2, P4_ACTION_MODEL_2)
203 .build();
204
205 /* Default Action */
206 private static final PiActionId PI_ACTION_ID_DEFAULT_1 = PiActionId.of("DefaultAction1");
207 private static final PiActionId PI_ACTION_ID_DEFAULT_2 = PiActionId.of("DefaultAction2");
208
209 private static final PiActionParamId PI_ACTION_PARAM_ID_DEFAULT_1 = PiActionParamId.of("DefaultActionParameter1");
210 private static final PiActionParamId PI_ACTION_PARAM_ID_DEFAULT_2 = PiActionParamId.of("DefaultActionParameter2");
211
212 private static final int ACTION_PARAM_BIT_WIDTH_DEFAULT_1 = 8;
213 private static final int ACTION_PARAM_BIT_WIDTH_DEFAULT_2 = 16;
214
215 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_DEFAULT_1 =
216 new P4ActionParamModel(PI_ACTION_PARAM_ID_DEFAULT_1, ACTION_PARAM_BIT_WIDTH_DEFAULT_1);
217 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_DEFAULT_2 =
218 new P4ActionParamModel(PI_ACTION_PARAM_ID_DEFAULT_2, ACTION_PARAM_BIT_WIDTH_DEFAULT_2);
219
220 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_DEFAULT_1 =
221 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
222 .put(PI_ACTION_PARAM_ID_DEFAULT_1, P4_ACTION_PARAM_MODEL_DEFAULT_1)
223 .build();
224 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_DEFAULT_2 =
225 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
226 .put(PI_ACTION_PARAM_ID_DEFAULT_2, P4_ACTION_PARAM_MODEL_DEFAULT_2)
227 .build();
228
229 private static final PiActionModel P4_ACTION_MODEL_DEFAULT_1 =
230 new P4ActionModel(PI_ACTION_ID_DEFAULT_1, PI_ACTION_PARAMS_DEFAULT_1);
231 private static final PiActionModel P4_ACTION_MODEL_DEFAULT_2 =
232 new P4ActionModel(PI_ACTION_ID_DEFAULT_2, PI_ACTION_PARAMS_DEFAULT_2);
233
234 /* Table Models */
235 private static final PiTableId PI_TABLE_ID_1 = PiTableId.of("Table1");
236 private static final PiTableId PI_TABLE_ID_2 = PiTableId.of("Table2");
237
238 private static final PiTableType PI_TABLE_TYPE_1 = PiTableType.DIRECT;
239 private static final PiTableType PI_TABLE_TYPE_2 = PiTableType.INDIRECT;
240
241 private static final long MAX_SIZE_1 = 10000;
242 private static final long MAX_SIZE_2 = 20000;
243
244 private static final boolean SUPPORT_AGING_1 = true;
245 private static final boolean SUPPORT_AGING_2 = false;
246
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700247 private static final boolean IS_CONST_TABLE_1 = true;
248 private static final boolean IS_CONST_TABLE_2 = false;
249
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300250 private static final PiTableModel P4_TABLE_MODEL_1 =
251 new P4TableModel(PI_TABLE_ID_1, PI_TABLE_TYPE_1, P4_ACTION_PROFILE_MODEL_1, MAX_SIZE_1, COUNTERS_1,
252 METERS_1, SUPPORT_AGING_1, MATCH_FIELDS_1, ACTIONS_1, P4_ACTION_MODEL_DEFAULT_1,
Carmelo Casconee45902b2018-12-18 13:30:45 -0800253 IS_CONST_TABLE_1);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300254 private static final PiTableModel SAME_AS_P4_TABLE_MODEL_1 =
255 new P4TableModel(PI_TABLE_ID_1, PI_TABLE_TYPE_1, P4_ACTION_PROFILE_MODEL_1, MAX_SIZE_1, COUNTERS_1,
256 METERS_1, SUPPORT_AGING_1, MATCH_FIELDS_1, ACTIONS_1, P4_ACTION_MODEL_DEFAULT_1,
Carmelo Casconee45902b2018-12-18 13:30:45 -0800257 IS_CONST_TABLE_1);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300258 private static final PiTableModel P4_TABLE_MODEL_2 =
259 new P4TableModel(PI_TABLE_ID_2, PI_TABLE_TYPE_2, P4_ACTION_PROFILE_MODEL_2, MAX_SIZE_2, COUNTERS_2,
260 METERS_2, SUPPORT_AGING_2, MATCH_FIELDS_2, ACTIONS_2, P4_ACTION_MODEL_DEFAULT_2,
Carmelo Casconee45902b2018-12-18 13:30:45 -0800261 IS_CONST_TABLE_2);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300262
263 /**
264 * Checks that the P4TableModel class is immutable.
265 */
266 @Test
267 public void testImmutability() {
268 assertThatClassIsImmutable(P4TableModel.class);
269 }
270
271 /**
272 * Checks the operation of equals(), hashCode() and toString() methods.
273 */
274 @Test
275 public void testEquals() {
276 new EqualsTester()
277 .addEqualityGroup(P4_TABLE_MODEL_1, SAME_AS_P4_TABLE_MODEL_1)
278 .addEqualityGroup(P4_TABLE_MODEL_2)
279 .testEquals();
280 }
281}