blob: afeb4388727500344a130d8c5398bde6901f3f89 [file] [log] [blame]
Yi Tseng82512da2017-08-16 19:46:36 -07001/*
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.ctl;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Lists;
22import com.google.protobuf.ByteString;
23import io.grpc.ManagedChannel;
24import io.grpc.Server;
25import io.grpc.inprocess.InProcessChannelBuilder;
26import io.grpc.inprocess.InProcessServerBuilder;
27import io.grpc.internal.AbstractServerImplBuilder;
28import org.easymock.EasyMock;
29import org.junit.AfterClass;
30import org.junit.Before;
31import org.junit.BeforeClass;
32import org.junit.Test;
33import org.onlab.util.ImmutableByteSequence;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.pi.model.DefaultPiPipeconf;
Carmelo Cascone87892e22017-11-13 16:01:29 -080036import org.onosproject.net.pi.model.PiActionId;
37import org.onosproject.net.pi.model.PiActionParamId;
38import org.onosproject.net.pi.model.PiActionProfileId;
Yi Tseng82512da2017-08-16 19:46:36 -070039import org.onosproject.net.pi.model.PiPipeconf;
40import org.onosproject.net.pi.model.PiPipeconfId;
41import org.onosproject.net.pi.model.PiPipelineModel;
Yi Tseng82512da2017-08-16 19:46:36 -070042import org.onosproject.net.pi.runtime.PiAction;
43import org.onosproject.net.pi.runtime.PiActionGroup;
44import org.onosproject.net.pi.runtime.PiActionGroupId;
45import org.onosproject.net.pi.runtime.PiActionGroupMember;
46import org.onosproject.net.pi.runtime.PiActionGroupMemberId;
Yi Tseng82512da2017-08-16 19:46:36 -070047import org.onosproject.net.pi.runtime.PiActionParam;
Yi Tseng82512da2017-08-16 19:46:36 -070048import p4.P4RuntimeOuterClass.ActionProfileGroup;
49import p4.P4RuntimeOuterClass.ActionProfileMember;
50import p4.P4RuntimeOuterClass.Entity;
Yi Tseng3e7f1452017-10-20 10:31:53 -070051import p4.P4RuntimeOuterClass.Uint128;
Yi Tseng82512da2017-08-16 19:46:36 -070052import p4.P4RuntimeOuterClass.Update;
53import p4.P4RuntimeOuterClass.WriteRequest;
54
55import java.io.IOException;
56import java.net.URL;
57import java.util.Collection;
58import java.util.List;
59import java.util.concurrent.CompletableFuture;
60import java.util.concurrent.TimeUnit;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020061import java.util.stream.Collectors;
Yi Tseng82512da2017-08-16 19:46:36 -070062
Carmelo Cascone87b9b392017-10-02 18:33:20 +020063import static org.easymock.EasyMock.niceMock;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070064import static org.junit.Assert.assertEquals;
65import static org.junit.Assert.assertNotNull;
66import static org.junit.Assert.assertTrue;
Carmelo Cascone87892e22017-11-13 16:01:29 -080067import static org.onosproject.net.pi.model.PiActionGroupType.SELECT;
Yi Tseng82512da2017-08-16 19:46:36 -070068import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
Yi Tseng82512da2017-08-16 19:46:36 -070069import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020070import static p4.P4RuntimeOuterClass.Action;
71import static p4.P4RuntimeOuterClass.ReadResponse;
Yi Tseng82512da2017-08-16 19:46:36 -070072
73/**
74 * Tests for P4 Runtime Action Profile Group support.
75 */
76public class P4RuntimeGroupTest {
77 private static final String PIPECONF_ID = "p4runtime-mock-pipeconf";
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070078 private static final String P4INFO_PATH = "/test.p4info";
Yi Tseng82512da2017-08-16 19:46:36 -070079 private static final PiPipeconf PIPECONF = buildPipeconf();
80 private static final int P4_INFO_ACT_PROF_ID = 285227860;
81 private static final PiActionProfileId ACT_PROF_ID = PiActionProfileId.of("ecmp_selector");
82 private static final PiActionGroupId GROUP_ID = PiActionGroupId.of(1);
83 private static final int DEFAULT_MEMBER_WEIGHT = 1;
84 private static final PiActionId EGRESS_PORT_ACTION_ID = PiActionId.of("set_egress_port");
85 private static final PiActionParamId PORT_PARAM_ID = PiActionParamId.of("port");
86 private static final int BASE_MEM_ID = 65535;
87 private static final List<Integer> MEMBER_IDS = ImmutableList.of(65536, 65537, 65538);
88 private static final Collection<PiActionGroupMember> GROUP_MEMBERS =
89 ImmutableSet.of(
90 outputMember((short) 1),
91 outputMember((short) 2),
92 outputMember((short) 3)
93 );
94 private static final PiActionGroup GROUP = PiActionGroup.builder()
95 .withId(GROUP_ID)
96 .addMembers(GROUP_MEMBERS)
97 .withActionProfileId(ACT_PROF_ID)
98 .withType(SELECT)
99 .build();
100 private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:p4runtime:1");
101 private static final int P4_DEVICE_ID = 1;
102 private static final int SET_EGRESS_PORT_ID = 16794308;
103 private static final String GRPC_SERVER_NAME = "P4RuntimeGroupTest";
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200104 private static final long DEFAULT_TIMEOUT_TIME = 10;
Yi Tseng3e7f1452017-10-20 10:31:53 -0700105 private static final Uint128 DEFAULT_ELECTION_ID = Uint128.newBuilder().setLow(1).build();
Yi Tseng82512da2017-08-16 19:46:36 -0700106
107 private P4RuntimeClientImpl client;
108 private P4RuntimeControllerImpl controller;
109 private static MockP4RuntimeServer p4RuntimeServerImpl = new MockP4RuntimeServer();
110 private static Server grpcServer;
111 private static ManagedChannel grpcChannel;
112
113 private static PiActionGroupMember outputMember(short portNum) {
114 PiActionParam param = new PiActionParam(PORT_PARAM_ID,
115 ImmutableByteSequence.copyFrom(portNum));
116 PiAction piAction = PiAction.builder()
117 .withId(EGRESS_PORT_ACTION_ID)
118 .withParameter(param).build();
119
120 return PiActionGroupMember.builder()
121 .withAction(piAction)
122 .withId(PiActionGroupMemberId.of(BASE_MEM_ID + portNum))
123 .withWeight(DEFAULT_MEMBER_WEIGHT)
124 .build();
125 }
126
127 private static PiPipeconf buildPipeconf() {
128 final URL p4InfoUrl = P4RuntimeGroupTest.class.getResource(P4INFO_PATH);
129 return DefaultPiPipeconf.builder()
130 .withId(new PiPipeconfId(PIPECONF_ID))
131 .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class))
132 .addExtension(P4_INFO_TEXT, p4InfoUrl)
133 .build();
134 }
135
136 @BeforeClass
137 public static void globalSetup() throws IOException {
138 AbstractServerImplBuilder builder = InProcessServerBuilder
139 .forName(GRPC_SERVER_NAME).directExecutor();
140 builder.addService(p4RuntimeServerImpl);
141 grpcServer = builder.build().start();
142 grpcChannel = InProcessChannelBuilder.forName(GRPC_SERVER_NAME)
143 .directExecutor()
144 .usePlaintext(true)
145 .build();
146 }
147
148 @AfterClass
149 public static void globalTeerDown() {
150 grpcServer.shutdown();
151 grpcChannel.shutdown();
152 }
153
154
155 @Before
156 public void setup() {
157 controller = niceMock(P4RuntimeControllerImpl.class);
158 client = new P4RuntimeClientImpl(DEVICE_ID, P4_DEVICE_ID,
159 grpcChannel,
160 controller);
Yi Tseng3e7f1452017-10-20 10:31:53 -0700161 client.p4RuntimeElectionId = DEFAULT_ELECTION_ID;
Yi Tseng82512da2017-08-16 19:46:36 -0700162 }
163
164 @Test
165 public void testInsertPiActionGroup() throws Exception {
166 CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1);
167 client.writeActionGroup(GROUP, INSERT, PIPECONF);
168 complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
169 WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0);
170 assertEquals(1, result.getDeviceId());
171 assertEquals(1, result.getUpdatesCount());
Yi Tseng3e7f1452017-10-20 10:31:53 -0700172 assertEquals(DEFAULT_ELECTION_ID, result.getElectionId());
Yi Tseng82512da2017-08-16 19:46:36 -0700173
174 Update update = result.getUpdatesList().get(0);
175 assertEquals(Update.Type.INSERT, update.getType());
176
177 Entity entity = update.getEntity();
178 ActionProfileGroup actionProfileGroup = entity.getActionProfileGroup();
179 assertNotNull(actionProfileGroup);
180
181 assertEquals(P4_INFO_ACT_PROF_ID, actionProfileGroup.getActionProfileId());
182 assertEquals(3, actionProfileGroup.getMembersCount());
183 List<ActionProfileGroup.Member> members = actionProfileGroup.getMembersList();
184
185 for (ActionProfileGroup.Member member : members) {
186 // XXX: We can't guarantee the order of member, just make sure we
187 // have these member ids
188 assertTrue(MEMBER_IDS.contains(member.getMemberId()));
189 assertEquals(DEFAULT_MEMBER_WEIGHT, member.getWeight());
190 }
191 }
192
193 @Test
194 public void testInsertPiActionMembers() throws Exception {
195 CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1);
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200196 client.writeActionGroupMembers(GROUP, INSERT, PIPECONF);
Yi Tseng82512da2017-08-16 19:46:36 -0700197 complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
198 WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0);
199 assertEquals(1, result.getDeviceId());
200 assertEquals(3, result.getUpdatesCount());
Yi Tseng3e7f1452017-10-20 10:31:53 -0700201 assertEquals(DEFAULT_ELECTION_ID, result.getElectionId());
Yi Tseng82512da2017-08-16 19:46:36 -0700202
203 List<Update> updates = result.getUpdatesList();
204 for (Update update : updates) {
205 assertEquals(Update.Type.INSERT, update.getType());
206 Entity entity = update.getEntity();
207 ActionProfileMember member = entity.getActionProfileMember();
208 assertNotNull(member);
209 assertEquals(P4_INFO_ACT_PROF_ID, member.getActionProfileId());
210 assertTrue(MEMBER_IDS.contains(member.getMemberId()));
211 Action action = member.getAction();
212 assertEquals(SET_EGRESS_PORT_ID, action.getActionId());
213 assertEquals(1, action.getParamsCount());
214 Action.Param param = action.getParamsList().get(0);
215 assertEquals(1, param.getParamId());
216 byte outPort = (byte) (member.getMemberId() - BASE_MEM_ID);
217 ByteString bs = ByteString.copyFrom(new byte[]{0, outPort});
218 assertEquals(bs, param.getValue());
219 }
220 }
221
222 @Test
223 public void testReadGroups() throws Exception {
224 ActionProfileGroup.Builder group = ActionProfileGroup.newBuilder()
225 .setGroupId(GROUP_ID.id())
226 .setType(ActionProfileGroup.Type.SELECT)
227 .setActionProfileId(P4_INFO_ACT_PROF_ID);
228
229 List<ActionProfileMember> members = Lists.newArrayList();
230
231 MEMBER_IDS.forEach(id -> {
232 ActionProfileGroup.Member member = ActionProfileGroup.Member.newBuilder()
233 .setMemberId(id)
234 .setWeight(DEFAULT_MEMBER_WEIGHT)
235 .build();
236 group.addMembers(member);
237
238 byte outPort = (byte) (id - BASE_MEM_ID);
239 ByteString bs = ByteString.copyFrom(new byte[]{0, outPort});
240 Action.Param param = Action.Param.newBuilder()
241 .setParamId(1)
242 .setValue(bs)
243 .build();
244
245 Action action = Action.newBuilder()
246 .setActionId(SET_EGRESS_PORT_ID)
247 .addParams(param)
248 .build();
249
Yi Tseng82512da2017-08-16 19:46:36 -0700250 ActionProfileMember actProfMember =
251 ActionProfileMember.newBuilder()
252 .setMemberId(id)
253 .setAction(action)
254 .build();
255 members.add(actProfMember);
256 });
257
258 List<ReadResponse> responses = Lists.newArrayList();
259 responses.add(ReadResponse.newBuilder()
260 .addEntities(Entity.newBuilder().setActionProfileGroup(group))
261 .build()
262 );
263
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200264 responses.add(ReadResponse.newBuilder()
265 .addAllEntities(members.stream()
266 .map(m -> Entity.newBuilder().setActionProfileMember(m).build())
267 .collect(Collectors.toList()))
268 .build());
Yi Tseng82512da2017-08-16 19:46:36 -0700269
270 p4RuntimeServerImpl.willReturnReadResult(responses);
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200271 CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(2);
Yi Tseng82512da2017-08-16 19:46:36 -0700272 CompletableFuture<Collection<PiActionGroup>> groupsComplete = client.dumpGroups(ACT_PROF_ID, PIPECONF);
273 complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
274
275 Collection<PiActionGroup> groups = groupsComplete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
276 assertEquals(1, groups.size());
277 PiActionGroup piActionGroup = groups.iterator().next();
278 assertEquals(ACT_PROF_ID, piActionGroup.actionProfileId());
279 assertEquals(GROUP_ID, piActionGroup.id());
280 assertEquals(SELECT, piActionGroup.type());
281 assertEquals(3, piActionGroup.members().size());
282 assertTrue(GROUP_MEMBERS.containsAll(piActionGroup.members()));
283 assertTrue(piActionGroup.members().containsAll(GROUP_MEMBERS));
284 }
285}