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