Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [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 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.protobuf.ByteString; |
| 22 | import io.grpc.ManagedChannel; |
| 23 | import io.grpc.Server; |
| 24 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 25 | import io.grpc.inprocess.InProcessServerBuilder; |
| 26 | import io.grpc.internal.AbstractServerImplBuilder; |
| 27 | import org.easymock.EasyMock; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 28 | import org.junit.After; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 29 | import org.junit.AfterClass; |
| 30 | import org.junit.Before; |
| 31 | import org.junit.BeforeClass; |
| 32 | import org.junit.Test; |
| 33 | import org.onlab.util.ImmutableByteSequence; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 36 | import org.onosproject.net.pi.model.PiActionId; |
| 37 | import org.onosproject.net.pi.model.PiActionParamId; |
| 38 | import org.onosproject.net.pi.model.PiActionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 39 | import org.onosproject.net.pi.model.PiPipeconf; |
| 40 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 41 | import org.onosproject.net.pi.model.PiPipelineModel; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiAction; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 43 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 44 | import org.onosproject.net.pi.runtime.PiActionProfileGroup; |
| 45 | import org.onosproject.net.pi.runtime.PiActionProfileGroupId; |
| 46 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 47 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
Miguel Borges de Freitas | 757307b | 2021-10-20 17:12:02 +0100 | [diff] [blame^] | 48 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 49 | import org.onosproject.p4runtime.ctl.client.P4RuntimeClientImpl; |
| 50 | import org.onosproject.p4runtime.ctl.controller.P4RuntimeControllerImpl; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 51 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup; |
| 52 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
| 53 | import p4.v1.P4RuntimeOuterClass.Entity; |
| 54 | import p4.v1.P4RuntimeOuterClass.Uint128; |
| 55 | import p4.v1.P4RuntimeOuterClass.Update; |
| 56 | import p4.v1.P4RuntimeOuterClass.WriteRequest; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 57 | |
| 58 | import java.io.IOException; |
| 59 | import java.net.URL; |
| 60 | import java.util.Collection; |
| 61 | import java.util.List; |
| 62 | import java.util.concurrent.CompletableFuture; |
| 63 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 64 | import java.util.stream.Collectors; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 65 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 66 | import static org.easymock.EasyMock.niceMock; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 67 | import static org.junit.Assert.assertEquals; |
| 68 | import static org.junit.Assert.assertNotNull; |
| 69 | import static org.junit.Assert.assertTrue; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 70 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 71 | import static p4.v1.P4RuntimeOuterClass.Action; |
| 72 | import static p4.v1.P4RuntimeOuterClass.ReadResponse; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Tests for P4 Runtime Action Profile Group support. |
| 76 | */ |
| 77 | public class P4RuntimeGroupTest { |
| 78 | private static final String PIPECONF_ID = "p4runtime-mock-pipeconf"; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 79 | private static final String P4INFO_PATH = "/test.p4info"; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 80 | private static final PiPipeconf PIPECONF = buildPipeconf(); |
| 81 | private static final int P4_INFO_ACT_PROF_ID = 285227860; |
| 82 | private static final PiActionProfileId ACT_PROF_ID = PiActionProfileId.of("ecmp_selector"); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 83 | private static final PiActionProfileGroupId GROUP_ID = PiActionProfileGroupId.of(1); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 84 | private static final int DEFAULT_MEMBER_WEIGHT = 1; |
| 85 | private static final PiActionId EGRESS_PORT_ACTION_ID = PiActionId.of("set_egress_port"); |
| 86 | private static final PiActionParamId PORT_PARAM_ID = PiActionParamId.of("port"); |
| 87 | private static final int BASE_MEM_ID = 65535; |
| 88 | private static final List<Integer> MEMBER_IDS = ImmutableList.of(65536, 65537, 65538); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 89 | private static final List<PiActionProfileMember> GROUP_MEMBER_INSTANCES = |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 90 | Lists.newArrayList( |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 91 | outputMember((short) 1), |
| 92 | outputMember((short) 2), |
| 93 | outputMember((short) 3) |
| 94 | ); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 95 | private static final List<PiActionProfileGroup.WeightedMember> GROUP_WEIGHTED_MEMBERS = |
| 96 | GROUP_MEMBER_INSTANCES.stream() |
| 97 | .map(m -> new PiActionProfileGroup.WeightedMember(m, DEFAULT_MEMBER_WEIGHT)) |
| 98 | .collect(Collectors.toList()); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 99 | private static final PiActionProfileGroup GROUP = PiActionProfileGroup.builder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 100 | .withId(GROUP_ID) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 101 | .addMembers(GROUP_MEMBER_INSTANCES) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 102 | .withActionProfileId(ACT_PROF_ID) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 103 | .build(); |
| 104 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:p4runtime:1"); |
| 105 | private static final int P4_DEVICE_ID = 1; |
| 106 | private static final int SET_EGRESS_PORT_ID = 16794308; |
| 107 | private static final String GRPC_SERVER_NAME = "P4RuntimeGroupTest"; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 108 | private static final long DEFAULT_TIMEOUT_TIME = 10; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 109 | private static final Uint128 DEFAULT_ELECTION_ID = Uint128.getDefaultInstance(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 110 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 111 | private org.onosproject.p4runtime.ctl.client.P4RuntimeClientImpl client; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 112 | private P4RuntimeControllerImpl controller; |
| 113 | private static MockP4RuntimeServer p4RuntimeServerImpl = new MockP4RuntimeServer(); |
| 114 | private static Server grpcServer; |
| 115 | private static ManagedChannel grpcChannel; |
| 116 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 117 | private static PiActionProfileMember outputMember(short portNum) { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 118 | PiActionParam param = new PiActionParam(PORT_PARAM_ID, |
| 119 | ImmutableByteSequence.copyFrom(portNum)); |
| 120 | PiAction piAction = PiAction.builder() |
| 121 | .withId(EGRESS_PORT_ACTION_ID) |
| 122 | .withParameter(param).build(); |
| 123 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 124 | return PiActionProfileMember.builder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 125 | .forActionProfile(ACT_PROF_ID) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 126 | .withAction(piAction) |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 127 | .withId(PiActionProfileMemberId.of(BASE_MEM_ID + portNum)) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 128 | .build(); |
| 129 | } |
| 130 | |
| 131 | private static PiPipeconf buildPipeconf() { |
| 132 | final URL p4InfoUrl = P4RuntimeGroupTest.class.getResource(P4INFO_PATH); |
| 133 | return DefaultPiPipeconf.builder() |
| 134 | .withId(new PiPipeconfId(PIPECONF_ID)) |
| 135 | .withPipelineModel(EasyMock.niceMock(PiPipelineModel.class)) |
| 136 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 137 | .build(); |
| 138 | } |
| 139 | |
| 140 | @BeforeClass |
| 141 | public static void globalSetup() throws IOException { |
| 142 | AbstractServerImplBuilder builder = InProcessServerBuilder |
| 143 | .forName(GRPC_SERVER_NAME).directExecutor(); |
| 144 | builder.addService(p4RuntimeServerImpl); |
| 145 | grpcServer = builder.build().start(); |
| 146 | grpcChannel = InProcessChannelBuilder.forName(GRPC_SERVER_NAME) |
| 147 | .directExecutor() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 148 | .build(); |
| 149 | } |
| 150 | |
| 151 | @AfterClass |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 152 | public static void globalTearDown() { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 153 | grpcServer.shutdown(); |
| 154 | grpcChannel.shutdown(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | @Before |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 159 | public void setup() { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 160 | controller = niceMock(org.onosproject.p4runtime.ctl.controller.P4RuntimeControllerImpl.class); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 161 | client = new P4RuntimeClientImpl( |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 162 | DEVICE_ID, grpcChannel, controller, new MockPipeconfService(), |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 163 | new MockMasterElectionIdStore()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 166 | @After |
| 167 | public void teardown() { |
| 168 | client.shutdown(); |
| 169 | } |
| 170 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 171 | @Test |
Miguel Borges de Freitas | 757307b | 2021-10-20 17:12:02 +0100 | [diff] [blame^] | 172 | public void testInvalidPiActionProfileMember() { |
| 173 | PiActionParam param = new PiActionParam(PORT_PARAM_ID, "invalidString"); |
| 174 | PiAction piAction = PiAction.builder() |
| 175 | .withId(EGRESS_PORT_ACTION_ID) |
| 176 | .withParameter(param).build(); |
| 177 | PiActionProfileMember actionProfileMember = PiActionProfileMember.builder() |
| 178 | .forActionProfile(ACT_PROF_ID) |
| 179 | .withAction(piAction) |
| 180 | .withId(PiActionProfileMemberId.of(BASE_MEM_ID + 1)) |
| 181 | .build(); |
| 182 | P4RuntimeWriteClient.WriteRequest writeRequest = client.write(P4_DEVICE_ID, PIPECONF); |
| 183 | writeRequest.insert(actionProfileMember); |
| 184 | P4RuntimeWriteClient.WriteResponse response = writeRequest.submitSync(); |
| 185 | |
| 186 | assertEquals(false, response.isSuccess()); |
| 187 | assertEquals(1, response.all().size()); |
| 188 | assertEquals("Wrong size for param 'port' of action 'set_egress_port', expected 2 bytes, but found 13", |
| 189 | response.all().iterator().next().explanation()); |
| 190 | } |
| 191 | |
| 192 | @Test |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 193 | public void testInsertPiActionProfileGroup() throws Exception { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 194 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 195 | client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP).submitSync(); |
| 196 | assertTrue(client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP).submitSync().isSuccess()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 197 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
| 198 | WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0); |
| 199 | assertEquals(1, result.getDeviceId()); |
| 200 | assertEquals(1, result.getUpdatesCount()); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 201 | assertEquals(DEFAULT_ELECTION_ID, result.getElectionId()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 202 | |
| 203 | Update update = result.getUpdatesList().get(0); |
| 204 | assertEquals(Update.Type.INSERT, update.getType()); |
| 205 | |
| 206 | Entity entity = update.getEntity(); |
| 207 | ActionProfileGroup actionProfileGroup = entity.getActionProfileGroup(); |
| 208 | assertNotNull(actionProfileGroup); |
| 209 | |
| 210 | assertEquals(P4_INFO_ACT_PROF_ID, actionProfileGroup.getActionProfileId()); |
| 211 | assertEquals(3, actionProfileGroup.getMembersCount()); |
| 212 | List<ActionProfileGroup.Member> members = actionProfileGroup.getMembersList(); |
| 213 | |
| 214 | for (ActionProfileGroup.Member member : members) { |
| 215 | // XXX: We can't guarantee the order of member, just make sure we |
| 216 | // have these member ids |
| 217 | assertTrue(MEMBER_IDS.contains(member.getMemberId())); |
| 218 | assertEquals(DEFAULT_MEMBER_WEIGHT, member.getWeight()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Test |
| 223 | public void testInsertPiActionMembers() throws Exception { |
| 224 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 225 | assertTrue(client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP_MEMBER_INSTANCES) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 226 | .submitSync().isSuccess()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 227 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
| 228 | WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0); |
| 229 | assertEquals(1, result.getDeviceId()); |
| 230 | assertEquals(3, result.getUpdatesCount()); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 231 | assertEquals(DEFAULT_ELECTION_ID, result.getElectionId()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 232 | |
| 233 | List<Update> updates = result.getUpdatesList(); |
| 234 | for (Update update : updates) { |
| 235 | assertEquals(Update.Type.INSERT, update.getType()); |
| 236 | Entity entity = update.getEntity(); |
| 237 | ActionProfileMember member = entity.getActionProfileMember(); |
| 238 | assertNotNull(member); |
| 239 | assertEquals(P4_INFO_ACT_PROF_ID, member.getActionProfileId()); |
| 240 | assertTrue(MEMBER_IDS.contains(member.getMemberId())); |
| 241 | Action action = member.getAction(); |
| 242 | assertEquals(SET_EGRESS_PORT_ID, action.getActionId()); |
| 243 | assertEquals(1, action.getParamsCount()); |
| 244 | Action.Param param = action.getParamsList().get(0); |
| 245 | assertEquals(1, param.getParamId()); |
| 246 | byte outPort = (byte) (member.getMemberId() - BASE_MEM_ID); |
| 247 | ByteString bs = ByteString.copyFrom(new byte[]{0, outPort}); |
| 248 | assertEquals(bs, param.getValue()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void testReadGroups() throws Exception { |
| 254 | ActionProfileGroup.Builder group = ActionProfileGroup.newBuilder() |
| 255 | .setGroupId(GROUP_ID.id()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 256 | .setActionProfileId(P4_INFO_ACT_PROF_ID); |
| 257 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 258 | MEMBER_IDS.forEach(id -> { |
| 259 | ActionProfileGroup.Member member = ActionProfileGroup.Member.newBuilder() |
| 260 | .setMemberId(id) |
| 261 | .setWeight(DEFAULT_MEMBER_WEIGHT) |
| 262 | .build(); |
| 263 | group.addMembers(member); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 264 | }); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 265 | |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 266 | List<ReadResponse> responses = Lists.newArrayList(); |
| 267 | responses.add(ReadResponse.newBuilder() |
| 268 | .addEntities(Entity.newBuilder().setActionProfileGroup(group)) |
| 269 | .build() |
| 270 | ); |
| 271 | |
| 272 | p4RuntimeServerImpl.willReturnReadResult(responses); |
| 273 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 274 | Collection<PiActionProfileGroup> groups = client.read(P4_DEVICE_ID, PIPECONF) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 275 | .actionProfileGroups(ACT_PROF_ID) |
| 276 | .submitSync().all(PiActionProfileGroup.class); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 277 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 278 | assertEquals(1, groups.size()); |
| 279 | PiActionProfileGroup piActionGroup = groups.iterator().next(); |
| 280 | assertEquals(ACT_PROF_ID, piActionGroup.actionProfile()); |
| 281 | assertEquals(GROUP_ID, piActionGroup.id()); |
| 282 | assertEquals(3, piActionGroup.members().size()); |
| 283 | assertTrue(GROUP_WEIGHTED_MEMBERS.containsAll(piActionGroup.members())); |
| 284 | assertTrue(piActionGroup.members().containsAll(GROUP_WEIGHTED_MEMBERS)); |
| 285 | } |
| 286 | |
| 287 | @Test |
| 288 | public void testReadMembers() throws Exception { |
| 289 | List<ActionProfileMember> members = Lists.newArrayList(); |
| 290 | |
| 291 | MEMBER_IDS.forEach(id -> { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 292 | byte outPort = (byte) (id - BASE_MEM_ID); |
| 293 | ByteString bs = ByteString.copyFrom(new byte[]{0, outPort}); |
| 294 | Action.Param param = Action.Param.newBuilder() |
| 295 | .setParamId(1) |
| 296 | .setValue(bs) |
| 297 | .build(); |
| 298 | |
| 299 | Action action = Action.newBuilder() |
| 300 | .setActionId(SET_EGRESS_PORT_ID) |
| 301 | .addParams(param) |
| 302 | .build(); |
| 303 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 304 | ActionProfileMember actProfMember = |
| 305 | ActionProfileMember.newBuilder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 306 | .setActionProfileId(P4_INFO_ACT_PROF_ID) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 307 | .setMemberId(id) |
| 308 | .setAction(action) |
| 309 | .build(); |
| 310 | members.add(actProfMember); |
| 311 | }); |
| 312 | |
| 313 | List<ReadResponse> responses = Lists.newArrayList(); |
| 314 | responses.add(ReadResponse.newBuilder() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 315 | .addAllEntities(members.stream() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 316 | .map(m -> Entity.newBuilder() |
| 317 | .setActionProfileMember(m).build()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 318 | .collect(Collectors.toList())) |
| 319 | .build()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 320 | |
| 321 | p4RuntimeServerImpl.willReturnReadResult(responses); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 322 | CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 323 | Collection<PiActionProfileMember> piMembers = client.read(P4_DEVICE_ID, PIPECONF) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 324 | .actionProfileMembers(ACT_PROF_ID).submitSync() |
| 325 | .all(PiActionProfileMember.class); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 326 | complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS); |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 327 | assertEquals(3, piMembers.size()); |
| 328 | assertTrue(GROUP_MEMBER_INSTANCES.containsAll(piMembers)); |
| 329 | assertTrue(piMembers.containsAll(GROUP_MEMBER_INSTANCES)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 330 | } |
| 331 | } |