blob: 84d3af8e21b443013abf90d06084cabec5440963 [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.ImmutableList;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.testing.EqualsTester;
23import org.junit.Test;
24import org.onosproject.net.pi.model.PiActionId;
25import org.onosproject.net.pi.model.PiActionModel;
26import org.onosproject.net.pi.model.PiActionParamId;
27import org.onosproject.net.pi.model.PiActionParamModel;
28import org.onosproject.net.pi.model.PiActionProfileId;
29import org.onosproject.net.pi.model.PiActionProfileModel;
30import org.onosproject.net.pi.model.PiControlMetadataId;
31import org.onosproject.net.pi.model.PiControlMetadataModel;
32import org.onosproject.net.pi.model.PiCounterId;
33import org.onosproject.net.pi.model.PiCounterModel;
34import org.onosproject.net.pi.model.PiCounterType;
35import org.onosproject.net.pi.model.PiMatchFieldId;
36import org.onosproject.net.pi.model.PiMatchFieldModel;
37import org.onosproject.net.pi.model.PiMatchType;
38import org.onosproject.net.pi.model.PiMeterId;
39import org.onosproject.net.pi.model.PiMeterModel;
40import org.onosproject.net.pi.model.PiMeterType;
41import org.onosproject.net.pi.model.PiPacketOperationModel;
42import org.onosproject.net.pi.model.PiPacketOperationType;
43import org.onosproject.net.pi.model.PiPipelineModel;
FrankWang2674e452018-05-24 17:13:35 +080044import org.onosproject.net.pi.model.PiRegisterId;
45import org.onosproject.net.pi.model.PiRegisterModel;
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030046import org.onosproject.net.pi.model.PiTableId;
47import org.onosproject.net.pi.model.PiTableModel;
48import org.onosproject.net.pi.model.PiTableType;
49
50import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
51
52/**
53 * Unit tests for P4PipelineModel class.
54 */
55public class P4PipelineModelTest {
56
57 /* Action Profiles */
58 private static final PiActionProfileId PI_ACTION_PROFILE_ID_1 = PiActionProfileId.of("Action1");
59 private static final PiActionProfileId PI_ACTION_PROFILE_ID_2 = PiActionProfileId.of("Action2");
60
61 private static final PiTableId ACTION_PI_TABLE_ID_1 = PiTableId.of("ActionTable1");
62 private static final PiTableId ACTION_PI_TABLE_ID_2 = PiTableId.of("ActionTable2");
63
64 private static final ImmutableSet<PiTableId> ACTION_TABLES_1 = new ImmutableSet.Builder<PiTableId>()
65 .add(ACTION_PI_TABLE_ID_1)
66 .build();
67
68 private static final ImmutableSet<PiTableId> ACTION_TABLES_2 = new ImmutableSet.Builder<PiTableId>()
69 .add(ACTION_PI_TABLE_ID_2)
70 .build();
71
72 private static final boolean ACTION_HAS_SELECTOR_1 = true;
73 private static final boolean ACTION_HAS_SELECTOR_2 = false;
74
75 private static final long ACTION_MAX_SIZE_1 = 100;
76 private static final long ACTION_MAX_SIZE_2 = 200;
77
78 private static final PiActionProfileModel P4_ACTION_PROFILE_MODEL_1 =
79 new P4ActionProfileModel(PI_ACTION_PROFILE_ID_1, ACTION_TABLES_1,
80 ACTION_HAS_SELECTOR_1, ACTION_MAX_SIZE_1);
81 private static final PiActionProfileModel P4_ACTION_PROFILE_MODEL_2 =
82 new P4ActionProfileModel(PI_ACTION_PROFILE_ID_2, ACTION_TABLES_2,
83 ACTION_HAS_SELECTOR_2, ACTION_MAX_SIZE_2);
84
85 /* Counters */
86 private static final PiCounterId PI_COUNTER_ID_1 = PiCounterId.of("Counter1");
87 private static final PiCounterId PI_COUNTER_ID_2 = PiCounterId.of("Counter2");
88
89 private static final PiCounterType PI_COUNTER_TYPE_1 = PiCounterType.DIRECT;
90 private static final PiCounterType PI_COUNTER_TYPE_2 = PiCounterType.INDIRECT;
91
92 private static final PiCounterModel.Unit COUNTER_UNIT_BYTES = P4CounterModel.Unit.BYTES;
93 private static final PiCounterModel.Unit COUNTER_UNIT_PACKETS = P4CounterModel.Unit.PACKETS;
94
95 private static final PiTableId COUNTER_PI_TABLE_ID_1 = PiTableId.of("CounterTable1");
96 private static final PiTableId COUNTER_PI_TABLE_ID_2 = PiTableId.of("CounterTable2");
97
98 private static final long COUNTER_SIZE_1 = 1000;
99 private static final long COUNTER_SIZE_2 = 2000;
100
101 private static final PiCounterModel P4_COUNTER_MODEL_1 =
102 new P4CounterModel(PI_COUNTER_ID_1, PI_COUNTER_TYPE_1,
103 COUNTER_UNIT_BYTES, COUNTER_PI_TABLE_ID_1, COUNTER_SIZE_1);
104 private static final PiCounterModel P4_COUNTER_MODEL_2 =
105 new P4CounterModel(PI_COUNTER_ID_2, PI_COUNTER_TYPE_2,
106 COUNTER_UNIT_PACKETS, COUNTER_PI_TABLE_ID_2, COUNTER_SIZE_2);
107
108 private static final ImmutableMap<PiCounterId, PiCounterModel> COUNTERS_1 =
109 new ImmutableMap.Builder<PiCounterId, PiCounterModel>()
110 .put(PI_COUNTER_ID_1, P4_COUNTER_MODEL_1)
111 .build();
112 private static final ImmutableMap<PiCounterId, PiCounterModel> COUNTERS_2 =
113 new ImmutableMap.Builder<PiCounterId, PiCounterModel>()
114 .put(PI_COUNTER_ID_2, P4_COUNTER_MODEL_2)
115 .build();
116
117 /* Meters */
118 private static final PiMeterId PI_METER_ID_1 = PiMeterId.of("Meter1");
119 private static final PiMeterId PI_METER_ID_2 = PiMeterId.of("Meter2");
120
121 private static final PiMeterType PI_METER_TYPE_1 = PiMeterType.DIRECT;
122 private static final PiMeterType PI_METER_TYPE_2 = PiMeterType.INDIRECT;
123
124 private static final PiMeterModel.Unit METER_UNIT_BYTES = P4MeterModel.Unit.BYTES;
125 private static final PiMeterModel.Unit METER_UNIT_PACKETS = P4MeterModel.Unit.PACKETS;
126
127 private static final PiTableId METER_PI_TABLE_ID_1 = PiTableId.of("MeterTable1");
128 private static final PiTableId METER_PI_TABLE_ID_2 = PiTableId.of("MeterTable2");
129
130 private static final long METER_SIZE_1 = 1000;
131 private static final long METER_SIZE_2 = 2000;
132
133 private static final PiMeterModel P4_METER_MODEL_1 =
134 new P4MeterModel(PI_METER_ID_1, PI_METER_TYPE_1, METER_UNIT_BYTES, METER_PI_TABLE_ID_1, METER_SIZE_1);
135 private static final PiMeterModel P4_METER_MODEL_2 =
136 new P4MeterModel(PI_METER_ID_2, PI_METER_TYPE_2, METER_UNIT_PACKETS, METER_PI_TABLE_ID_2, METER_SIZE_2);
137
138 private static final ImmutableMap<PiMeterId, PiMeterModel> METERS_1 =
139 new ImmutableMap.Builder<PiMeterId, PiMeterModel>()
140 .put(PI_METER_ID_1, P4_METER_MODEL_1)
141 .build();
142 private static final ImmutableMap<PiMeterId, PiMeterModel> METERS_2 =
143 new ImmutableMap.Builder<PiMeterId, PiMeterModel>()
144 .put(PI_METER_ID_2, P4_METER_MODEL_2)
145 .build();
146
FrankWang2674e452018-05-24 17:13:35 +0800147 /* Registers */
148 private static final PiRegisterId PI_REGISTER_ID_1 = PiRegisterId.of("Register1");
149 private static final PiRegisterId PI_REGISTER_ID_2 = PiRegisterId.of("Register2");
150
151 private static final long REGISTER_SIZE_1 = 1000;
152 private static final long REGISTER_SIZE_2 = 2000;
153
154 private static final P4RegisterModel P4_REGISTER_MODEL_1 = new P4RegisterModel(PI_REGISTER_ID_1, REGISTER_SIZE_1);
155 private static final P4RegisterModel P4_REGISTER_MODEL_2 = new P4RegisterModel(PI_REGISTER_ID_2, REGISTER_SIZE_2);
156
157 private static final ImmutableMap<PiRegisterId, PiRegisterModel> REGISTERS_1 =
158 new ImmutableMap.Builder<PiRegisterId, PiRegisterModel>()
159 .put(PI_REGISTER_ID_1, P4_REGISTER_MODEL_1)
160 .build();
161 private static final ImmutableMap<PiRegisterId, PiRegisterModel> REGISTERS_2 =
162 new ImmutableMap.Builder<PiRegisterId, PiRegisterModel>()
163 .put(PI_REGISTER_ID_2, P4_REGISTER_MODEL_2)
164 .build();
165
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300166 /* Match Fields */
167 private static final PiMatchFieldId PI_MATCH_FIELD_ID_1 = PiMatchFieldId.of("MatchField1");
168 private static final PiMatchFieldId PI_MATCH_FIELD_ID_2 = PiMatchFieldId.of("MatchField2");
169
170 private static final int MATCH_FIELD_BIT_WIDTH_1 = 8;
171 private static final int MATCH_FIELD_BIT_WIDTH_2 = 16;
172
173 private static final PiMatchType PI_MATCH_TYPE_1 = PiMatchType.EXACT;
174 private static final PiMatchType PI_MATCH_TYPE_2 = PiMatchType.TERNARY;
175
176 private static final PiMatchFieldModel P4_MATCH_FIELD_MODEL_1 =
177 new P4MatchFieldModel(PI_MATCH_FIELD_ID_1, MATCH_FIELD_BIT_WIDTH_1, PI_MATCH_TYPE_1);
178 private static final PiMatchFieldModel P4_MATCH_FIELD_MODEL_2 =
179 new P4MatchFieldModel(PI_MATCH_FIELD_ID_2, MATCH_FIELD_BIT_WIDTH_2, PI_MATCH_TYPE_2);
180
181 private static final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> MATCH_FIELDS_1 =
182 new ImmutableMap.Builder<PiMatchFieldId, PiMatchFieldModel>()
183 .put(PI_MATCH_FIELD_ID_1, P4_MATCH_FIELD_MODEL_1)
184 .build();
185 private static final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> MATCH_FIELDS_2 =
186 new ImmutableMap.Builder<PiMatchFieldId, PiMatchFieldModel>()
187 .put(PI_MATCH_FIELD_ID_2, P4_MATCH_FIELD_MODEL_2)
188 .build();
189
190 /* Actions */
191 private static final PiActionId PI_ACTION_ID_1 = PiActionId.of("Action1");
192 private static final PiActionId PI_ACTION_ID_2 = PiActionId.of("Action2");
193
194 private static final PiActionParamId PI_ACTION_PARAM_ID_1 = PiActionParamId.of("ActionParameter1");
195 private static final PiActionParamId PI_ACTION_PARAM_ID_2 = PiActionParamId.of("ActionParameter2");
196
197 private static final int ACTION_PARAM_BIT_WIDTH_1 = 8;
198 private static final int ACTION_PARAM_BIT_WIDTH_2 = 16;
199
200 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_1 =
201 new P4ActionParamModel(PI_ACTION_PARAM_ID_1, ACTION_PARAM_BIT_WIDTH_1);
202 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_2 =
203 new P4ActionParamModel(PI_ACTION_PARAM_ID_2, ACTION_PARAM_BIT_WIDTH_2);
204
205 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_1 =
206 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
207 .put(PI_ACTION_PARAM_ID_1, P4_ACTION_PARAM_MODEL_1)
208 .build();
209 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_2 =
210 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
211 .put(PI_ACTION_PARAM_ID_2, P4_ACTION_PARAM_MODEL_2)
212 .build();
213
214 private static final PiActionModel P4_ACTION_MODEL_1 = new P4ActionModel(PI_ACTION_ID_1, PI_ACTION_PARAMS_1);
215 private static final PiActionModel P4_ACTION_MODEL_2 = new P4ActionModel(PI_ACTION_ID_2, PI_ACTION_PARAMS_2);
216
217 private static final ImmutableMap<PiActionId, PiActionModel> ACTIONS_1 =
218 new ImmutableMap.Builder<PiActionId, PiActionModel>()
219 .put(PI_ACTION_ID_1, P4_ACTION_MODEL_1)
220 .build();
221
222 private static final ImmutableMap<PiActionId, PiActionModel> ACTIONS_2 =
223 new ImmutableMap.Builder<PiActionId, PiActionModel>()
224 .put(PI_ACTION_ID_2, P4_ACTION_MODEL_2)
225 .build();
226
227 /* Default Action */
228 private static final PiActionId PI_ACTION_ID_DEFAULT_1 = PiActionId.of("DefaultAction1");
229 private static final PiActionId PI_ACTION_ID_DEFAULT_2 = PiActionId.of("DefaultAction2");
230
231 private static final PiActionParamId PI_ACTION_PARAM_ID_DEFAULT_1 = PiActionParamId.of("DefaultActionParameter1");
232 private static final PiActionParamId PI_ACTION_PARAM_ID_DEFAULT_2 = PiActionParamId.of("DefaultActionParameter2");
233
234 private static final int ACTION_PARAM_BIT_WIDTH_DEFAULT_1 = 8;
235 private static final int ACTION_PARAM_BIT_WIDTH_DEFAULT_2 = 16;
236
237 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_DEFAULT_1 =
238 new P4ActionParamModel(PI_ACTION_PARAM_ID_DEFAULT_1, ACTION_PARAM_BIT_WIDTH_DEFAULT_1);
239 private static final PiActionParamModel P4_ACTION_PARAM_MODEL_DEFAULT_2 =
240 new P4ActionParamModel(PI_ACTION_PARAM_ID_DEFAULT_2, ACTION_PARAM_BIT_WIDTH_DEFAULT_2);
241
242 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_DEFAULT_1 =
243 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
244 .put(PI_ACTION_PARAM_ID_DEFAULT_1, P4_ACTION_PARAM_MODEL_DEFAULT_1)
245 .build();
246 private static final ImmutableMap<PiActionParamId, PiActionParamModel> PI_ACTION_PARAMS_DEFAULT_2 =
247 new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
248 .put(PI_ACTION_PARAM_ID_DEFAULT_2, P4_ACTION_PARAM_MODEL_DEFAULT_2)
249 .build();
250
251 private static final PiActionModel P4_ACTION_MODEL_DEFAULT_1 =
252 new P4ActionModel(PI_ACTION_ID_DEFAULT_1, PI_ACTION_PARAMS_DEFAULT_1);
253 private static final PiActionModel P4_ACTION_MODEL_DEFAULT_2 =
254 new P4ActionModel(PI_ACTION_ID_DEFAULT_2, PI_ACTION_PARAMS_DEFAULT_2);
255
256 /* Table Models */
257 private static final PiTableId PI_TABLE_ID_1 = PiTableId.of("Table1");
258 private static final PiTableId PI_TABLE_ID_2 = PiTableId.of("Table2");
259
260 private static final PiTableType PI_TABLE_TYPE_1 = PiTableType.DIRECT;
261 private static final PiTableType PI_TABLE_TYPE_2 = PiTableType.INDIRECT;
262
263 private static final long MAX_SIZE_1 = 10000;
264 private static final long MAX_SIZE_2 = 20000;
265
266 private static final boolean SUPPORT_AGING_1 = true;
267 private static final boolean SUPPORT_AGING_2 = false;
268
269 private static final boolean HAS_DEFAULT_MUTABLE_PARAMS_1 = true;
270 private static final boolean HAS_DEFAULT_MUTABLE_PARAMS_2 = false;
271
272 private static final PiTableModel P4_TABLE_MODEL_1 =
273 new P4TableModel(PI_TABLE_ID_1, PI_TABLE_TYPE_1, P4_ACTION_PROFILE_MODEL_1, MAX_SIZE_1, COUNTERS_1,
274 METERS_1, SUPPORT_AGING_1, MATCH_FIELDS_1, ACTIONS_1, P4_ACTION_MODEL_DEFAULT_1,
275 HAS_DEFAULT_MUTABLE_PARAMS_1);
276 private static final PiTableModel P4_TABLE_MODEL_2 =
277 new P4TableModel(PI_TABLE_ID_2, PI_TABLE_TYPE_2, P4_ACTION_PROFILE_MODEL_2, MAX_SIZE_2, COUNTERS_2,
278 METERS_2, SUPPORT_AGING_2, MATCH_FIELDS_2, ACTIONS_2, P4_ACTION_MODEL_DEFAULT_2,
279 HAS_DEFAULT_MUTABLE_PARAMS_2);
280
281 /* Packet operations */
282 private static final PiPacketOperationType PI_PACKET_OPERATION_TYPE_1 = PiPacketOperationType.PACKET_IN;
283 private static final PiPacketOperationType PI_PACKET_OPERATION_TYPE_2 = PiPacketOperationType.PACKET_OUT;
284
285 private static final PiControlMetadataId PI_CONTROL_METADATA_ID_1 = PiControlMetadataId.of("INGRESS PORT");
286 private static final PiControlMetadataId PI_CONTROL_METADATA_ID_2 = PiControlMetadataId.of("EGRESS PORT");
287
288 private static final int META_BIT_WIDTH_1 = 32;
289 private static final int META_BIT_WIDTH_2 = 64;
290
291 private static final PiControlMetadataModel P4_CONTROL_METADATA_MODEL_1 =
292 new P4ControlMetadataModel(PI_CONTROL_METADATA_ID_1, META_BIT_WIDTH_1);
293 private static final PiControlMetadataModel P4_CONTROL_METADATA_MODEL_2 =
294 new P4ControlMetadataModel(PI_CONTROL_METADATA_ID_2, META_BIT_WIDTH_2);
295
296 /* Pipeline Models */
297 private static final ImmutableMap<PiTableId, PiTableModel> TABLES_1 =
298 new ImmutableMap.Builder<PiTableId, PiTableModel>()
299 .put(PI_TABLE_ID_1, P4_TABLE_MODEL_1)
300 .build();
301 private static final ImmutableMap<PiTableId, PiTableModel> TABLES_2 =
302 new ImmutableMap.Builder<PiTableId, PiTableModel>()
303 .put(PI_TABLE_ID_2, P4_TABLE_MODEL_2)
304 .build();
305
306 private static final ImmutableMap<PiActionProfileId, PiActionProfileModel> ACTION_PROFILES_1 =
307 new ImmutableMap.Builder<PiActionProfileId, PiActionProfileModel>()
308 .put(PI_ACTION_PROFILE_ID_1, P4_ACTION_PROFILE_MODEL_1)
309 .build();
310 private static final ImmutableMap<PiActionProfileId, PiActionProfileModel> ACTION_PROFILES_2 =
311 new ImmutableMap.Builder<PiActionProfileId, PiActionProfileModel>()
312 .put(PI_ACTION_PROFILE_ID_2, P4_ACTION_PROFILE_MODEL_2)
313 .build();
314
315 private static final ImmutableList<PiControlMetadataModel> METADATAS_1 =
316 new ImmutableList.Builder<PiControlMetadataModel>()
317 .add(P4_CONTROL_METADATA_MODEL_1)
318 .build();
319 private static final ImmutableList<PiControlMetadataModel> METADATAS_2 =
320 new ImmutableList.Builder<PiControlMetadataModel>()
321 .add(P4_CONTROL_METADATA_MODEL_2)
322 .build();
323
324 private static final PiPacketOperationModel P4_PACKET_OPERATION_MODEL_1 =
325 new P4PacketOperationModel(PI_PACKET_OPERATION_TYPE_1, METADATAS_1);
326 private static final PiPacketOperationModel P4_PACKET_OPERATION_MODEL_2 =
327 new P4PacketOperationModel(PI_PACKET_OPERATION_TYPE_2, METADATAS_2);
328
329 private static final ImmutableMap<PiPacketOperationType, PiPacketOperationModel> PACKET_OPERATIONS_1 =
330 new ImmutableMap.Builder<PiPacketOperationType, PiPacketOperationModel>()
331 .put(PI_PACKET_OPERATION_TYPE_1, P4_PACKET_OPERATION_MODEL_1)
332 .build();
333 private static final ImmutableMap<PiPacketOperationType, PiPacketOperationModel> PACKET_OPERATIONS_2 =
334 new ImmutableMap.Builder<PiPacketOperationType, PiPacketOperationModel>()
335 .put(PI_PACKET_OPERATION_TYPE_2, P4_PACKET_OPERATION_MODEL_2)
336 .build();
337
338 private static final PiPipelineModel P4_PIPELINE_MODEL_1 =
FrankWang2674e452018-05-24 17:13:35 +0800339 new P4PipelineModel(TABLES_1, COUNTERS_1, METERS_1, REGISTERS_1, ACTION_PROFILES_1, PACKET_OPERATIONS_1);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300340 private static final PiPipelineModel SAME_AS_P4_PIPELINE_MODEL_1 =
FrankWang2674e452018-05-24 17:13:35 +0800341 new P4PipelineModel(TABLES_1, COUNTERS_1, METERS_1, REGISTERS_1, ACTION_PROFILES_1, PACKET_OPERATIONS_1);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300342 private static final PiPipelineModel P4_PIPELINE_MODEL_2 =
FrankWang2674e452018-05-24 17:13:35 +0800343 new P4PipelineModel(TABLES_2, COUNTERS_2, METERS_2, REGISTERS_1, ACTION_PROFILES_2, PACKET_OPERATIONS_2);
Mehmed Mustafa3e269df2017-11-24 17:19:49 +0300344
345 /**
346 * Checks that the P4PipelineModel class is immutable.
347 */
348 @Test
349 public void testImmutability() {
350 assertThatClassIsImmutable(P4PipelineModel.class);
351 }
352
353 /**
354 * Checks the operation of equals(), hashCode() and toString() methods.
355 */
356 @Test
357 public void testEquals() {
358 new EqualsTester()
359 .addEqualityGroup(P4_PIPELINE_MODEL_1, SAME_AS_P4_PIPELINE_MODEL_1)
360 .addEqualityGroup(P4_PIPELINE_MODEL_2)
361 .testEquals();
362 }
363}