blob: 792e838416d4fa62baa74d67cda197997c9d152a [file] [log] [blame]
Carmelo Cascone8d99b172017-07-18 17:26:31 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone8d99b172017-07-18 17:26:31 -04003 *
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.ctl;
18
19import com.google.common.collect.Lists;
20import com.google.common.testing.EqualsTester;
21import org.junit.Test;
22import org.onlab.util.ImmutableByteSequence;
23import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
24import org.onosproject.net.pi.model.DefaultPiPipeconf;
25import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.model.PiPipeconfId;
27import org.onosproject.net.pi.runtime.PiAction;
28import org.onosproject.net.pi.runtime.PiActionId;
29import org.onosproject.net.pi.runtime.PiActionParam;
30import org.onosproject.net.pi.runtime.PiActionParamId;
31import org.onosproject.net.pi.runtime.PiHeaderFieldId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020032import org.onosproject.net.pi.runtime.PiMatchKey;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040033import org.onosproject.net.pi.runtime.PiTableEntry;
34import org.onosproject.net.pi.runtime.PiTableId;
35import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020036import org.slf4j.Logger;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040037import p4.P4RuntimeOuterClass.Action;
38import p4.P4RuntimeOuterClass.TableEntry;
39
40import java.net.URL;
41import java.util.Collection;
42import java.util.Random;
43
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.is;
47import static org.onlab.util.ImmutableByteSequence.copyFrom;
48import static org.onlab.util.ImmutableByteSequence.fit;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020049import static org.onlab.util.ImmutableByteSequence.ofOnes;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040050import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
51import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
52import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decode;
53import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encode;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020054import static org.slf4j.LoggerFactory.getLogger;
55
56//import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
57//import org.onosproject.drivers.bmv2.Bmv2DefaultInterpreter;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040058
59public class TableEntryEncoderTest {
60
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020061 private final Logger log = getLogger(getClass());
62
Carmelo Cascone8d99b172017-07-18 17:26:31 -040063 private static final String TABLE_0 = "table0";
64 private static final String SET_EGRESS_PORT = "set_egress_port";
65 private static final String PORT = "port";
66 private static final String ETHERNET = "ethernet";
67 private static final String DST_ADDR = "dstAddr";
68 private static final String SRC_ADDR = "srcAddr";
69 private static final String STANDARD_METADATA = "standard_metadata";
70 private static final String INGRESS_PORT = "ingress_port";
71 private static final String ETHER_TYPE = "etherType";
72
73 private final Random rand = new Random();
74 private final URL p4InfoUrl = this.getClass().getResource("/default.p4info");
75 private final URL jsonUrl = this.getClass().getResource("/default.json");
76
77 private final PiPipeconf defaultPipeconf = DefaultPiPipeconf.builder()
78 .withId(new PiPipeconfId("mock"))
79 .withPipelineModel(Bmv2PipelineModelParser.parse(jsonUrl))
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020080// .addBehaviour(PiPipelineInterpreter.class, Bmv2DefaultInterpreter.class)
Carmelo Cascone8d99b172017-07-18 17:26:31 -040081 .addExtension(P4_INFO_TEXT, p4InfoUrl)
82 .addExtension(BMV2_JSON, jsonUrl)
83 .build();
84
85 private final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf);
86 private final ImmutableByteSequence ethAddr = fit(copyFrom(rand.nextInt()), 48);
87 private final ImmutableByteSequence portValue = copyFrom((short) rand.nextInt());
88 private final PiHeaderFieldId ethDstAddrFieldId = PiHeaderFieldId.of(ETHERNET, DST_ADDR);
89 private final PiHeaderFieldId ethSrcAddrFieldId = PiHeaderFieldId.of(ETHERNET, SRC_ADDR);
90 private final PiHeaderFieldId inPortFieldId = PiHeaderFieldId.of(STANDARD_METADATA, INGRESS_PORT);
91 private final PiHeaderFieldId ethTypeFieldId = PiHeaderFieldId.of(ETHERNET, ETHER_TYPE);
92 private final PiActionParamId portParamId = PiActionParamId.of(PORT);
93 private final PiActionId outActionId = PiActionId.of(SET_EGRESS_PORT);
94 private final PiTableId tableId = PiTableId.of(TABLE_0);
95
96 private final PiTableEntry piTableEntry = PiTableEntry
97 .builder()
98 .forTable(tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +020099 .withMatchKey(PiMatchKey.builder()
100 .addFieldMatch(new PiTernaryFieldMatch(ethDstAddrFieldId, ethAddr, ofOnes(6)))
101 .addFieldMatch(new PiTernaryFieldMatch(ethSrcAddrFieldId, ethAddr, ofOnes(6)))
102 .addFieldMatch(new PiTernaryFieldMatch(inPortFieldId, portValue, ofOnes(2)))
103 .addFieldMatch(new PiTernaryFieldMatch(ethTypeFieldId, portValue, ofOnes(2)))
104 .build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400105 .withAction(PiAction
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200106 .builder()
107 .withId(outActionId)
108 .withParameter(new PiActionParam(portParamId, portValue))
109 .build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400110 .withPriority(1)
111 .withCookie(2)
112 .build();
113
114 public TableEntryEncoderTest() throws ImmutableByteSequence.ByteSequenceTrimException {
115 }
116
117 @Test
118 public void testP4InfoBrowser() throws Exception {
119
120 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf);
121
122 assertThat(browser.tables().hasName(TABLE_0), is(true));
123 assertThat(browser.actions().hasName(SET_EGRESS_PORT), is(true));
124
125 int tableId = browser.tables().getByName(TABLE_0).getPreamble().getId();
126 int actionId = browser.actions().getByName(SET_EGRESS_PORT).getPreamble().getId();
127
128 assertThat(browser.matchFields(tableId).hasName(STANDARD_METADATA + "." + INGRESS_PORT), is(true));
129 assertThat(browser.actionParams(actionId).hasName(PORT), is(true));
130
131 // TODO: improve, assert browsing other entities (counters, meters, etc.)
132 }
133
134 @Test
135 public void testTableEntryEncoder()
136 throws P4InfoBrowser.NotFoundException, ImmutableByteSequence.ByteSequenceTrimException {
137
138 Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntry), defaultPipeconf);
139 assertThat(result, hasSize(1));
140
141 TableEntry tableEntryMsg = result.iterator().next();
142
143 Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf);
144 PiTableEntry decodedPiTableEntry = decodedResults.iterator().next();
145
146 // Test equality for decoded entry.
147 new EqualsTester()
148 .addEqualityGroup(piTableEntry, decodedPiTableEntry)
149 .testEquals();
150
151 // Table ID.
152 int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId();
153 int encodedTableId = tableEntryMsg.getTableId();
154 assertThat(encodedTableId, is(p4InfoTableId));
155
156 // Ternary match.
157 byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray();
158 assertThat(encodedTernaryMatchValue, is(ethAddr.asArray()));
159
160 Action actionMsg = tableEntryMsg.getAction().getAction();
161
162 // Action ID.
163 int p4InfoActionId = browser.actions().getByName(outActionId.name()).getPreamble().getId();
164 int encodedActionId = actionMsg.getActionId();
165 assertThat(encodedActionId, is(p4InfoActionId));
166
167 // Action param ID.
168 int p4InfoActionParamId = browser.actionParams(p4InfoActionId).getByName(portParamId.name()).getId();
169 int encodedActionParamId = actionMsg.getParams(0).getParamId();
170 assertThat(encodedActionParamId, is(p4InfoActionParamId));
171
172 // Action param value.
173 byte[] encodedActionParam = actionMsg.getParams(0).getValue().toByteArray();
174 assertThat(encodedActionParam, is(portValue.asArray()));
175
176 // TODO: improve, assert other field match types (ternary, LPM)
177 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400178}