Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.p4runtime.model; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.testing.EqualsTester; |
| 21 | import com.google.protobuf.ExtensionRegistry; |
| 22 | import com.google.protobuf.TextFormat; |
| 23 | import org.hamcrest.collection.IsIterableContainingInAnyOrder; |
| 24 | import org.hamcrest.collection.IsIterableContainingInOrder; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.net.pi.model.PiActionId; |
| 27 | import org.onosproject.net.pi.model.PiActionModel; |
| 28 | import org.onosproject.net.pi.model.PiActionParamId; |
| 29 | import org.onosproject.net.pi.model.PiActionParamModel; |
| 30 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 31 | import org.onosproject.net.pi.model.PiActionProfileModel; |
| 32 | import org.onosproject.net.pi.model.PiCounterId; |
| 33 | import org.onosproject.net.pi.model.PiCounterModel; |
| 34 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 35 | import org.onosproject.net.pi.model.PiMatchFieldModel; |
| 36 | import org.onosproject.net.pi.model.PiMatchType; |
| 37 | import org.onosproject.net.pi.model.PiMeterModel; |
| 38 | import org.onosproject.net.pi.model.PiPacketOperationModel; |
| 39 | import org.onosproject.net.pi.model.PiPacketOperationType; |
| 40 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 41 | import org.onosproject.net.pi.model.PiTableId; |
| 42 | import org.onosproject.net.pi.model.PiTableModel; |
| 43 | import p4.config.P4InfoOuterClass.Table; |
| 44 | import p4.config.P4InfoOuterClass.P4Info; |
| 45 | import p4.config.P4InfoOuterClass.MatchField; |
| 46 | import p4.config.P4InfoOuterClass.ActionRef; |
| 47 | |
| 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
| 50 | import java.io.InputStreamReader; |
| 51 | import java.net.MalformedURLException; |
| 52 | import java.net.URL; |
| 53 | import java.util.ArrayList; |
| 54 | import java.util.Collection; |
| 55 | import java.util.List; |
| 56 | |
| 57 | import static org.hamcrest.MatcherAssert.assertThat; |
| 58 | import static org.hamcrest.Matchers.*; |
| 59 | import static org.hamcrest.core.IsNull.notNullValue; |
| 60 | |
| 61 | /** |
| 62 | * Test for P4Info Parser. |
| 63 | */ |
| 64 | public class P4InfoParserTest { |
| 65 | private static final String PATH = "basic.p4info"; |
| 66 | |
| 67 | private final URL p4InfoUrl = P4InfoParserTest.class.getResource(PATH); |
| 68 | |
| 69 | private static final Long DEFAULT_MAX_TABLE_SIZE = 1024L; |
| 70 | private static final Long DEFAULT_MAX_ACTION_PROFILE_SIZE = 64L; |
| 71 | |
| 72 | public P4InfoParserTest() throws MalformedURLException { } |
| 73 | |
| 74 | /** |
| 75 | * Tests parse method. |
| 76 | * @throws Exception if equality group objects dose not match as expected |
| 77 | */ |
| 78 | @Test |
| 79 | public void testParse() throws Exception { |
| 80 | // Generate two PiPipelineModels from p4Info file |
| 81 | PiPipelineModel model = P4InfoParser.parse(p4InfoUrl); |
| 82 | PiPipelineModel model2 = P4InfoParser.parse(p4InfoUrl); |
| 83 | |
| 84 | // Check equality |
| 85 | new EqualsTester().addEqualityGroup(model, model2).testEquals(); |
| 86 | |
| 87 | // Generate a P4Info object from the file |
| 88 | final P4Info p4info; |
| 89 | try { |
| 90 | p4info = getP4InfoMessage(p4InfoUrl); |
| 91 | } catch (IOException e) { |
| 92 | throw new P4InfoParserException("Unable to parse protobuf " + p4InfoUrl.toString(), e); |
| 93 | } |
| 94 | |
| 95 | List<Table> tableMsgs = p4info.getTablesList(); |
| 96 | PiTableId table0Id = PiTableId.of(tableMsgs.get(0).getPreamble().getName()); |
| 97 | PiTableId wcmpTableId = PiTableId.of(tableMsgs.get(1).getPreamble().getName()); |
| 98 | |
| 99 | //parse tables |
| 100 | PiTableModel table0Model = model.table(table0Id).orElse(null); |
| 101 | PiTableModel wcmpTableModel = model.table(wcmpTableId).orElse(null); |
| 102 | PiTableModel table0Model2 = model2.table(table0Id).orElse(null); |
| 103 | PiTableModel wcmpTableModel2 = model2.table(wcmpTableId).orElse(null); |
| 104 | |
| 105 | new EqualsTester().addEqualityGroup(table0Model, table0Model2) |
| 106 | .addEqualityGroup(wcmpTableModel, wcmpTableModel2).testEquals(); |
| 107 | |
| 108 | // Check existence |
| 109 | assertThat("model parsed value is null", table0Model, notNullValue()); |
| 110 | assertThat("model parsed value is null", wcmpTableModel, notNullValue()); |
| 111 | assertThat("Incorrect size for table0 size", table0Model.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE))); |
| 112 | assertThat("Incorrect size for wcmp_table size", wcmpTableModel.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE))); |
| 113 | |
| 114 | // Check matchFields |
| 115 | List<MatchField> matchFieldList = tableMsgs.get(0).getMatchFieldsList(); |
| 116 | List<PiMatchFieldModel> piMatchFieldList = new ArrayList<>(); |
| 117 | |
| 118 | for (MatchField matchFieldIter : matchFieldList) { |
| 119 | int matchTypeNumber = matchFieldIter.getMatchType().getNumber(); |
| 120 | PiMatchType piMatchType = PiMatchType.VALID; |
| 121 | switch (matchTypeNumber) { |
| 122 | case 1: piMatchType = PiMatchType.VALID; break; |
| 123 | case 2: piMatchType = PiMatchType.EXACT; break; |
| 124 | case 3: piMatchType = PiMatchType.LPM; break; |
| 125 | case 4: piMatchType = piMatchType.TERNARY; break; |
| 126 | case 5: piMatchType = piMatchType.RANGE; break; |
| 127 | default: piMatchType = PiMatchType.VALID; break; |
| 128 | } |
| 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 | |
| 197 | PiActionModel table0DefaultAction = table0Model.defaultAction().orElse(null); |
| 198 | |
| 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, |
| 209 | true, DEFAULT_MAX_ACTION_PROFILE_SIZE); |
| 210 | PiActionProfileModel wcmpSelector = model.actionProfiles(actionProfileId).orElse(null); |
| 211 | PiActionProfileModel wcmpSelector2 = model2.actionProfiles(actionProfileId).orElse(null); |
| 212 | |
| 213 | new EqualsTester().addEqualityGroup(wcmpSelector, wcmpSelector2, wcmpSelector3).testEquals(); |
| 214 | |
| 215 | // Check existence |
| 216 | assertThat("model parsed value is null", wcmpSelector, notNullValue()); |
| 217 | assertThat("Incorrect value for actions profiles", model.actionProfiles(), containsInAnyOrder(wcmpSelector)); |
| 218 | // ActionProfiles size |
| 219 | assertThat("Incorrect size for action profiles", model.actionProfiles().size(), is(equalTo(1))); |
| 220 | |
| 221 | //parse counters |
| 222 | PiCounterModel ingressPortCounterModel = |
| 223 | model.counter(PiCounterId.of("port_counters_ingress.ingress_port_counter")).orElse(null); |
| 224 | PiCounterModel egressPortCounterModel = |
| 225 | model.counter(PiCounterId.of("port_counters_egress.egress_port_counter")).orElse(null); |
| 226 | PiCounterModel table0CounterModel = |
| 227 | model.counter(PiCounterId.of("table0_control.table0_counter")).orElse(null); |
| 228 | PiCounterModel wcmpTableCounterModel = |
| 229 | model.counter(PiCounterId.of("wcmp_control.wcmp_table_counter")).orElse(null); |
| 230 | |
| 231 | PiCounterModel ingressPortCounterModel2 = |
| 232 | model2.counter(PiCounterId.of("port_counters_ingress.ingress_port_counter")).orElse(null); |
| 233 | PiCounterModel egressPortCounterModel2 = |
| 234 | model2.counter(PiCounterId.of("port_counters_egress.egress_port_counter")).orElse(null); |
| 235 | PiCounterModel table0CounterModel2 = |
| 236 | model2.counter(PiCounterId.of("table0_control.table0_counter")).orElse(null); |
| 237 | PiCounterModel wcmpTableCounterModel2 = |
| 238 | model2.counter(PiCounterId.of("wcmp_control.wcmp_table_counter")).orElse(null); |
| 239 | |
| 240 | new EqualsTester() |
| 241 | .addEqualityGroup(ingressPortCounterModel, ingressPortCounterModel2) |
| 242 | .addEqualityGroup(egressPortCounterModel, egressPortCounterModel2) |
| 243 | .addEqualityGroup(table0CounterModel, table0CounterModel2) |
| 244 | .addEqualityGroup(wcmpTableCounterModel, wcmpTableCounterModel2) |
| 245 | .testEquals(); |
| 246 | |
| 247 | assertThat("model parsed value is null", ingressPortCounterModel, notNullValue()); |
| 248 | assertThat("model parsed value is null", egressPortCounterModel, notNullValue()); |
| 249 | assertThat("model parsed value is null", table0CounterModel, notNullValue()); |
| 250 | assertThat("model parsed value is null", wcmpTableCounterModel, notNullValue()); |
| 251 | |
| 252 | //Parse meters |
| 253 | Collection<PiMeterModel> meterModel = model.meters(); |
| 254 | Collection<PiMeterModel> meterModel2 = model2.meters(); |
| 255 | |
| 256 | assertThat("model pased meter collaction should be empty", meterModel.isEmpty(), is(true)); |
| 257 | assertThat("model pased meter collaction should be empty", meterModel2.isEmpty(), is(true)); |
| 258 | |
| 259 | //parse packet operations |
| 260 | PiPacketOperationModel packetInOperationalModel = |
| 261 | model.packetOperationModel(PiPacketOperationType.PACKET_IN).orElse(null); |
| 262 | PiPacketOperationModel packetOutOperationalModel = |
| 263 | model.packetOperationModel(PiPacketOperationType.PACKET_OUT).orElse(null); |
| 264 | |
| 265 | PiPacketOperationModel packetInOperationalModel2 = |
| 266 | model2.packetOperationModel(PiPacketOperationType.PACKET_IN).orElse(null); |
| 267 | PiPacketOperationModel packetOutOperationalModel2 = |
| 268 | model2.packetOperationModel(PiPacketOperationType.PACKET_OUT).orElse(null); |
| 269 | |
| 270 | new EqualsTester() |
| 271 | .addEqualityGroup(packetInOperationalModel, packetInOperationalModel2) |
| 272 | .addEqualityGroup(packetOutOperationalModel, packetOutOperationalModel2) |
| 273 | .testEquals(); |
| 274 | |
| 275 | // Check existence |
| 276 | assertThat("model parsed value is null", packetInOperationalModel, notNullValue()); |
| 277 | assertThat("model parsed value is null", packetOutOperationalModel, notNullValue()); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Gets P4Info message from the URL. |
| 282 | * @param p4InfoUrl link to the p4Info file |
| 283 | * @return a P4Info object |
| 284 | * @throws IOException if any problem occurs while reading from the URL connection. |
| 285 | */ |
| 286 | private P4Info getP4InfoMessage(URL p4InfoUrl) throws IOException { |
| 287 | InputStream p4InfoStream = p4InfoUrl.openStream(); |
| 288 | P4Info.Builder p4InfoBuilder = P4Info.newBuilder(); |
| 289 | TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), |
| 290 | ExtensionRegistry.getEmptyRegistry(), |
| 291 | p4InfoBuilder); |
| 292 | return p4InfoBuilder.build(); |
| 293 | } |
| 294 | } |