blob: c05a84cd624dc6a6c64dcca7b225e3e6447996c0 [file] [log] [blame]
Ekber Aziz123ad5d2017-11-27 12:36:35 -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 */
16package org.onosproject.p4runtime.model;
17
18import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.testing.EqualsTester;
21import com.google.protobuf.ExtensionRegistry;
22import com.google.protobuf.TextFormat;
23import org.hamcrest.collection.IsIterableContainingInAnyOrder;
24import org.hamcrest.collection.IsIterableContainingInOrder;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020025import org.junit.Assert;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080026import org.junit.Test;
27import org.onosproject.net.pi.model.PiActionId;
28import org.onosproject.net.pi.model.PiActionModel;
29import org.onosproject.net.pi.model.PiActionParamId;
30import org.onosproject.net.pi.model.PiActionParamModel;
31import org.onosproject.net.pi.model.PiActionProfileId;
32import org.onosproject.net.pi.model.PiActionProfileModel;
33import org.onosproject.net.pi.model.PiCounterId;
34import org.onosproject.net.pi.model.PiCounterModel;
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.PiMeterModel;
39import org.onosproject.net.pi.model.PiPacketOperationModel;
40import org.onosproject.net.pi.model.PiPacketOperationType;
41import org.onosproject.net.pi.model.PiPipelineModel;
42import org.onosproject.net.pi.model.PiTableId;
43import org.onosproject.net.pi.model.PiTableModel;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020044import p4.config.v1.P4InfoOuterClass.ActionRef;
45import p4.config.v1.P4InfoOuterClass.MatchField;
46import p4.config.v1.P4InfoOuterClass.P4Info;
47import p4.config.v1.P4InfoOuterClass.Table;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080048
49import java.io.IOException;
50import java.io.InputStream;
51import java.io.InputStreamReader;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080052import java.net.URL;
53import java.util.ArrayList;
54import java.util.Collection;
55import java.util.List;
56
57import static org.hamcrest.MatcherAssert.assertThat;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020058import static org.hamcrest.Matchers.containsInAnyOrder;
59import static org.hamcrest.Matchers.equalTo;
60import static org.hamcrest.Matchers.is;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080061import static org.hamcrest.core.IsNull.notNullValue;
62
63/**
64 * Test for P4Info Parser.
65 */
66public class P4InfoParserTest {
67 private static final String PATH = "basic.p4info";
68
69 private final URL p4InfoUrl = P4InfoParserTest.class.getResource(PATH);
70
71 private static final Long DEFAULT_MAX_TABLE_SIZE = 1024L;
72 private static final Long DEFAULT_MAX_ACTION_PROFILE_SIZE = 64L;
Carmelo Casconea3635ab2019-03-19 12:55:34 -070073 private static final int DEFAULT_MAX_GROUP_SIZE = 16;
Ekber Aziz123ad5d2017-11-27 12:36:35 -080074
Ekber Aziz123ad5d2017-11-27 12:36:35 -080075 /**
76 * Tests parse method.
77 * @throws Exception if equality group objects dose not match as expected
78 */
79 @Test
80 public void testParse() throws Exception {
81 // Generate two PiPipelineModels from p4Info file
82 PiPipelineModel model = P4InfoParser.parse(p4InfoUrl);
83 PiPipelineModel model2 = P4InfoParser.parse(p4InfoUrl);
84
85 // Check equality
86 new EqualsTester().addEqualityGroup(model, model2).testEquals();
87
88 // Generate a P4Info object from the file
89 final P4Info p4info;
90 try {
91 p4info = getP4InfoMessage(p4InfoUrl);
92 } catch (IOException e) {
93 throw new P4InfoParserException("Unable to parse protobuf " + p4InfoUrl.toString(), e);
94 }
95
96 List<Table> tableMsgs = p4info.getTablesList();
97 PiTableId table0Id = PiTableId.of(tableMsgs.get(0).getPreamble().getName());
98 PiTableId wcmpTableId = PiTableId.of(tableMsgs.get(1).getPreamble().getName());
99
100 //parse tables
101 PiTableModel table0Model = model.table(table0Id).orElse(null);
102 PiTableModel wcmpTableModel = model.table(wcmpTableId).orElse(null);
103 PiTableModel table0Model2 = model2.table(table0Id).orElse(null);
104 PiTableModel wcmpTableModel2 = model2.table(wcmpTableId).orElse(null);
105
106 new EqualsTester().addEqualityGroup(table0Model, table0Model2)
107 .addEqualityGroup(wcmpTableModel, wcmpTableModel2).testEquals();
108
109 // Check existence
110 assertThat("model parsed value is null", table0Model, notNullValue());
111 assertThat("model parsed value is null", wcmpTableModel, notNullValue());
112 assertThat("Incorrect size for table0 size", table0Model.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE)));
113 assertThat("Incorrect size for wcmp_table size", wcmpTableModel.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE)));
114
115 // Check matchFields
116 List<MatchField> matchFieldList = tableMsgs.get(0).getMatchFieldsList();
117 List<PiMatchFieldModel> piMatchFieldList = new ArrayList<>();
118
119 for (MatchField matchFieldIter : matchFieldList) {
Carmelo Cascone6af4e172018-06-15 16:01:30 +0200120 MatchField.MatchType matchType = matchFieldIter.getMatchType();
121 PiMatchType piMatchType;
122 switch (matchType) {
123 case EXACT: piMatchType = PiMatchType.EXACT; break;
124 case LPM: piMatchType = PiMatchType.LPM; break;
125 case TERNARY: piMatchType = PiMatchType.TERNARY; break;
126 case RANGE: piMatchType = PiMatchType.RANGE; break;
127 default: Assert.fail(); return;
Ekber Aziz123ad5d2017-11-27 12:36:35 -0800128 }
129 piMatchFieldList.add(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldIter.getName()),
130 matchFieldIter.getBitwidth(), piMatchType));
131 }
132 // Check MatchFields size
133 assertThat("Incorrect size for matchFields", table0Model.matchFields().size(), is(equalTo(9)));
134 // Check if matchFields are in order
135 assertThat("Incorrect order for matchFields", table0Model.matchFields(), IsIterableContainingInOrder.contains(
136 piMatchFieldList.get(0), piMatchFieldList.get(1),
137 piMatchFieldList.get(2), piMatchFieldList.get(3),
138 piMatchFieldList.get(4), piMatchFieldList.get(5),
139 piMatchFieldList.get(6), piMatchFieldList.get(7),
140 piMatchFieldList.get(8)));
141
142 assertThat("Incorrect size for matchFields", wcmpTableModel.matchFields().size(), is(equalTo(1)));
143
144 // check if matchFields are in order
145 matchFieldList = tableMsgs.get(1).getMatchFieldsList();
146 assertThat("Incorrect order for matchFields",
147 wcmpTableModel.matchFields(), IsIterableContainingInOrder.contains(
148 new P4MatchFieldModel(PiMatchFieldId.of(matchFieldList.get(0).getName()),
149 matchFieldList.get(0).getBitwidth(), PiMatchType.EXACT)));
150
151 //check table0 actionsRefs
152 List<ActionRef> actionRefList = tableMsgs.get(0).getActionRefsList();
153 assertThat("Incorrect size for actionRefs", actionRefList.size(), is(equalTo(4)));
154
155 //create action instances
156 PiActionId actionId = PiActionId.of("set_egress_port");
157 PiActionParamId piActionParamId = PiActionParamId.of("port");
158 int bitWitdth = 9;
159 PiActionParamModel actionParamModel = new P4ActionParamModel(piActionParamId, bitWitdth);
160 ImmutableMap<PiActionParamId, PiActionParamModel> params = new
161 ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
162 .put(piActionParamId, actionParamModel).build();
163
164 PiActionModel setEgressPortAction = new P4ActionModel(actionId, params);
165
166 actionId = PiActionId.of("send_to_cpu");
167 PiActionModel sendToCpuAction =
168 new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
169
170 actionId = PiActionId.of("_drop");
171 PiActionModel dropAction =
172 new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
173
174 actionId = PiActionId.of("NoAction");
175 PiActionModel noAction =
176 new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
177
178 actionId = PiActionId.of("table0_control.set_next_hop_id");
179 piActionParamId = PiActionParamId.of("next_hop_id");
180 bitWitdth = 16;
181 actionParamModel = new P4ActionParamModel(piActionParamId, bitWitdth);
182 params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>()
183 .put(piActionParamId, actionParamModel).build();
184
185 PiActionModel setNextHopIdAction = new P4ActionModel(actionId, params);
186
187 //check table0 actions
188 assertThat("action dose not match",
189 table0Model.actions(), IsIterableContainingInAnyOrder.containsInAnyOrder(
190 setEgressPortAction, sendToCpuAction, setNextHopIdAction, dropAction));
191
192 //check wcmp_table actions
193 assertThat("actions dose not match",
194 wcmpTableModel.actions(), IsIterableContainingInAnyOrder.containsInAnyOrder(
195 setEgressPortAction, noAction));
196
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700197 PiActionModel table0DefaultAction = table0Model.constDefaultAction().orElse(null);
Ekber Aziz123ad5d2017-11-27 12:36:35 -0800198
199 new EqualsTester().addEqualityGroup(table0DefaultAction, dropAction).testEquals();
200
201 // Check existence
202 assertThat("model parsed value is null", table0DefaultAction, notNullValue());
203
204 //parse action profiles
205 PiTableId tableId = PiTableId.of("wcmp_control.wcmp_table");
206 ImmutableSet<PiTableId> tableIds = new ImmutableSet.Builder<PiTableId>().add(tableId).build();
207 PiActionProfileId actionProfileId = PiActionProfileId.of("wcmp_control.wcmp_selector");
208 PiActionProfileModel wcmpSelector3 = new P4ActionProfileModel(actionProfileId, tableIds,
Carmelo Cascone99c59db2019-01-17 15:39:35 -0800209 true, DEFAULT_MAX_ACTION_PROFILE_SIZE,
210 DEFAULT_MAX_GROUP_SIZE);
Ekber Aziz123ad5d2017-11-27 12:36:35 -0800211 PiActionProfileModel wcmpSelector = model.actionProfiles(actionProfileId).orElse(null);
212 PiActionProfileModel wcmpSelector2 = model2.actionProfiles(actionProfileId).orElse(null);
213
214 new EqualsTester().addEqualityGroup(wcmpSelector, wcmpSelector2, wcmpSelector3).testEquals();
215
216 // Check existence
217 assertThat("model parsed value is null", wcmpSelector, notNullValue());
218 assertThat("Incorrect value for actions profiles", model.actionProfiles(), containsInAnyOrder(wcmpSelector));
219 // ActionProfiles size
220 assertThat("Incorrect size for action profiles", model.actionProfiles().size(), is(equalTo(1)));
221
222 //parse counters
223 PiCounterModel ingressPortCounterModel =
224 model.counter(PiCounterId.of("port_counters_ingress.ingress_port_counter")).orElse(null);
225 PiCounterModel egressPortCounterModel =
226 model.counter(PiCounterId.of("port_counters_egress.egress_port_counter")).orElse(null);
227 PiCounterModel table0CounterModel =
228 model.counter(PiCounterId.of("table0_control.table0_counter")).orElse(null);
229 PiCounterModel wcmpTableCounterModel =
230 model.counter(PiCounterId.of("wcmp_control.wcmp_table_counter")).orElse(null);
231
232 PiCounterModel ingressPortCounterModel2 =
233 model2.counter(PiCounterId.of("port_counters_ingress.ingress_port_counter")).orElse(null);
234 PiCounterModel egressPortCounterModel2 =
235 model2.counter(PiCounterId.of("port_counters_egress.egress_port_counter")).orElse(null);
236 PiCounterModel table0CounterModel2 =
237 model2.counter(PiCounterId.of("table0_control.table0_counter")).orElse(null);
238 PiCounterModel wcmpTableCounterModel2 =
239 model2.counter(PiCounterId.of("wcmp_control.wcmp_table_counter")).orElse(null);
240
241 new EqualsTester()
242 .addEqualityGroup(ingressPortCounterModel, ingressPortCounterModel2)
243 .addEqualityGroup(egressPortCounterModel, egressPortCounterModel2)
244 .addEqualityGroup(table0CounterModel, table0CounterModel2)
245 .addEqualityGroup(wcmpTableCounterModel, wcmpTableCounterModel2)
246 .testEquals();
247
248 assertThat("model parsed value is null", ingressPortCounterModel, notNullValue());
249 assertThat("model parsed value is null", egressPortCounterModel, notNullValue());
250 assertThat("model parsed value is null", table0CounterModel, notNullValue());
251 assertThat("model parsed value is null", wcmpTableCounterModel, notNullValue());
252
253 //Parse meters
254 Collection<PiMeterModel> meterModel = model.meters();
255 Collection<PiMeterModel> meterModel2 = model2.meters();
256
257 assertThat("model pased meter collaction should be empty", meterModel.isEmpty(), is(true));
258 assertThat("model pased meter collaction should be empty", meterModel2.isEmpty(), is(true));
259
260 //parse packet operations
261 PiPacketOperationModel packetInOperationalModel =
262 model.packetOperationModel(PiPacketOperationType.PACKET_IN).orElse(null);
263 PiPacketOperationModel packetOutOperationalModel =
264 model.packetOperationModel(PiPacketOperationType.PACKET_OUT).orElse(null);
265
266 PiPacketOperationModel packetInOperationalModel2 =
267 model2.packetOperationModel(PiPacketOperationType.PACKET_IN).orElse(null);
268 PiPacketOperationModel packetOutOperationalModel2 =
269 model2.packetOperationModel(PiPacketOperationType.PACKET_OUT).orElse(null);
270
271 new EqualsTester()
272 .addEqualityGroup(packetInOperationalModel, packetInOperationalModel2)
273 .addEqualityGroup(packetOutOperationalModel, packetOutOperationalModel2)
274 .testEquals();
275
276 // Check existence
277 assertThat("model parsed value is null", packetInOperationalModel, notNullValue());
278 assertThat("model parsed value is null", packetOutOperationalModel, notNullValue());
279 }
280
281 /**
282 * Gets P4Info message from the URL.
283 * @param p4InfoUrl link to the p4Info file
284 * @return a P4Info object
285 * @throws IOException if any problem occurs while reading from the URL connection.
286 */
287 private P4Info getP4InfoMessage(URL p4InfoUrl) throws IOException {
288 InputStream p4InfoStream = p4InfoUrl.openStream();
289 P4Info.Builder p4InfoBuilder = P4Info.newBuilder();
290 TextFormat.getParser().merge(new InputStreamReader(p4InfoStream),
291 ExtensionRegistry.getEmptyRegistry(),
292 p4InfoBuilder);
293 return p4InfoBuilder.build();
294 }
295}