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