blob: b3e9a1eda24b60301fa41365d6a472232951974b [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;
Yi Tseng82512da2017-08-16 19:46:36 -070021import org.easymock.EasyMock;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040022import org.junit.Test;
23import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040024import org.onosproject.net.pi.model.DefaultPiPipeconf;
25import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tseng82512da2017-08-16 19:46:36 -070027import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040028import org.onosproject.net.pi.runtime.PiAction;
29import org.onosproject.net.pi.runtime.PiActionId;
30import org.onosproject.net.pi.runtime.PiActionParam;
31import org.onosproject.net.pi.runtime.PiActionParamId;
32import org.onosproject.net.pi.runtime.PiHeaderFieldId;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020033import org.onosproject.net.pi.runtime.PiMatchKey;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040034import org.onosproject.net.pi.runtime.PiTableEntry;
35import org.onosproject.net.pi.runtime.PiTableId;
36import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
37import 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.P4_INFO_TEXT;
51import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decode;
52import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encode;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020053
Yi Tseng82512da2017-08-16 19:46:36 -070054/**
55 * Test for P4 runtime table entry encoder.
56 */
Carmelo Cascone8d99b172017-07-18 17:26:31 -040057public class TableEntryEncoderTest {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040058 private static final String TABLE_0 = "table0";
59 private static final String SET_EGRESS_PORT = "set_egress_port";
60 private static final String PORT = "port";
61 private static final String ETHERNET = "ethernet";
62 private static final String DST_ADDR = "dstAddr";
63 private static final String SRC_ADDR = "srcAddr";
64 private static final String STANDARD_METADATA = "standard_metadata";
65 private static final String INGRESS_PORT = "ingress_port";
66 private static final String ETHER_TYPE = "etherType";
67
68 private final Random rand = new Random();
69 private final URL p4InfoUrl = this.getClass().getResource("/default.p4info");
Carmelo Cascone8d99b172017-07-18 17:26:31 -040070
71 private final PiPipeconf defaultPipeconf = DefaultPiPipeconf.builder()
72 .withId(new PiPipeconfId("mock"))
Yi Tseng82512da2017-08-16 19:46:36 -070073 .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class))
Carmelo Cascone8d99b172017-07-18 17:26:31 -040074 .addExtension(P4_INFO_TEXT, p4InfoUrl)
Carmelo Cascone8d99b172017-07-18 17:26:31 -040075 .build();
76
77 private final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf);
78 private final ImmutableByteSequence ethAddr = fit(copyFrom(rand.nextInt()), 48);
79 private final ImmutableByteSequence portValue = copyFrom((short) rand.nextInt());
80 private final PiHeaderFieldId ethDstAddrFieldId = PiHeaderFieldId.of(ETHERNET, DST_ADDR);
81 private final PiHeaderFieldId ethSrcAddrFieldId = PiHeaderFieldId.of(ETHERNET, SRC_ADDR);
82 private final PiHeaderFieldId inPortFieldId = PiHeaderFieldId.of(STANDARD_METADATA, INGRESS_PORT);
83 private final PiHeaderFieldId ethTypeFieldId = PiHeaderFieldId.of(ETHERNET, ETHER_TYPE);
84 private final PiActionParamId portParamId = PiActionParamId.of(PORT);
85 private final PiActionId outActionId = PiActionId.of(SET_EGRESS_PORT);
86 private final PiTableId tableId = PiTableId.of(TABLE_0);
87
88 private final PiTableEntry piTableEntry = PiTableEntry
89 .builder()
90 .forTable(tableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +020091 .withMatchKey(PiMatchKey.builder()
92 .addFieldMatch(new PiTernaryFieldMatch(ethDstAddrFieldId, ethAddr, ofOnes(6)))
93 .addFieldMatch(new PiTernaryFieldMatch(ethSrcAddrFieldId, ethAddr, ofOnes(6)))
94 .addFieldMatch(new PiTernaryFieldMatch(inPortFieldId, portValue, ofOnes(2)))
95 .addFieldMatch(new PiTernaryFieldMatch(ethTypeFieldId, portValue, ofOnes(2)))
96 .build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -040097 .withAction(PiAction
Carmelo Cascone0e896a02017-07-31 07:22:27 +020098 .builder()
99 .withId(outActionId)
100 .withParameter(new PiActionParam(portParamId, portValue))
101 .build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400102 .withPriority(1)
103 .withCookie(2)
104 .build();
105
106 public TableEntryEncoderTest() throws ImmutableByteSequence.ByteSequenceTrimException {
107 }
108
109 @Test
110 public void testP4InfoBrowser() throws Exception {
111
112 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(defaultPipeconf);
113
114 assertThat(browser.tables().hasName(TABLE_0), is(true));
115 assertThat(browser.actions().hasName(SET_EGRESS_PORT), is(true));
116
117 int tableId = browser.tables().getByName(TABLE_0).getPreamble().getId();
118 int actionId = browser.actions().getByName(SET_EGRESS_PORT).getPreamble().getId();
119
120 assertThat(browser.matchFields(tableId).hasName(STANDARD_METADATA + "." + INGRESS_PORT), is(true));
121 assertThat(browser.actionParams(actionId).hasName(PORT), is(true));
122
123 // TODO: improve, assert browsing other entities (counters, meters, etc.)
124 }
125
126 @Test
127 public void testTableEntryEncoder()
128 throws P4InfoBrowser.NotFoundException, ImmutableByteSequence.ByteSequenceTrimException {
129
130 Collection<TableEntry> result = encode(Lists.newArrayList(piTableEntry), defaultPipeconf);
131 assertThat(result, hasSize(1));
132
133 TableEntry tableEntryMsg = result.iterator().next();
134
135 Collection<PiTableEntry> decodedResults = decode(Lists.newArrayList(tableEntryMsg), defaultPipeconf);
136 PiTableEntry decodedPiTableEntry = decodedResults.iterator().next();
137
138 // Test equality for decoded entry.
139 new EqualsTester()
140 .addEqualityGroup(piTableEntry, decodedPiTableEntry)
141 .testEquals();
142
143 // Table ID.
144 int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId();
145 int encodedTableId = tableEntryMsg.getTableId();
146 assertThat(encodedTableId, is(p4InfoTableId));
147
148 // Ternary match.
149 byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray();
150 assertThat(encodedTernaryMatchValue, is(ethAddr.asArray()));
151
152 Action actionMsg = tableEntryMsg.getAction().getAction();
153
154 // Action ID.
155 int p4InfoActionId = browser.actions().getByName(outActionId.name()).getPreamble().getId();
156 int encodedActionId = actionMsg.getActionId();
157 assertThat(encodedActionId, is(p4InfoActionId));
158
159 // Action param ID.
160 int p4InfoActionParamId = browser.actionParams(p4InfoActionId).getByName(portParamId.name()).getId();
161 int encodedActionParamId = actionMsg.getParams(0).getParamId();
162 assertThat(encodedActionParamId, is(p4InfoActionParamId));
163
164 // Action param value.
165 byte[] encodedActionParam = actionMsg.getParams(0).getValue().toByteArray();
166 assertThat(encodedActionParam, is(portValue.asArray()));
167
168 // TODO: improve, assert other field match types (ternary, LPM)
169 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400170}