Carmelo Cascone | c8e8498 | 2017-07-26 15:34:42 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.test; |
| 18 | |
| 19 | import io.grpc.ManagedChannelBuilder; |
| 20 | import io.grpc.netty.NettyChannelBuilder; |
| 21 | import org.junit.Ignore; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.bmv2.model.Bmv2PipelineModelParser; |
| 24 | import org.onosproject.grpc.ctl.GrpcControllerImpl; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 27 | import org.onosproject.net.pi.model.PiPipeconf; |
| 28 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 29 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 30 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 31 | import org.onosproject.p4runtime.ctl.P4RuntimeControllerImpl; |
| 32 | |
| 33 | import java.net.URL; |
| 34 | import java.util.concurrent.ExecutionException; |
| 35 | |
| 36 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON; |
| 37 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 38 | |
| 39 | /** |
| 40 | * Class used for quick testing of P4Runtime with real devices. To be removed before release. |
| 41 | */ |
| 42 | public class P4RuntimeTest { |
| 43 | |
| 44 | private final URL p4InfoUrl = this.getClass().getResource("/default.p4info"); |
| 45 | private final URL jsonUrl = this.getClass().getResource("/default.json"); |
| 46 | |
| 47 | private final PiPipeconf bmv2DefaultPipeconf = DefaultPiPipeconf.builder() |
| 48 | .withId(new PiPipeconfId("mock")) |
| 49 | .withPipelineModel(Bmv2PipelineModelParser.parse(jsonUrl)) |
| 50 | // .addBehaviour(PiPipelineInterpreter.class, Bmv2DefaultInterpreter.class) |
| 51 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 52 | .addExtension(BMV2_JSON, jsonUrl) |
| 53 | .build(); |
| 54 | |
| 55 | @Test |
| 56 | @Ignore |
| 57 | public void testRuntime() throws ExecutionException, InterruptedException, |
| 58 | PiPipelineInterpreter.PiInterpreterException, IllegalAccessException, InstantiationException { |
| 59 | |
| 60 | // FIXME: remove me. |
| 61 | |
| 62 | P4RuntimeControllerImpl controller = new P4RuntimeControllerImpl(); |
| 63 | GrpcControllerImpl grpcController = new GrpcControllerImpl(); |
| 64 | controller.grpcController = grpcController; |
| 65 | GrpcControllerImpl.enableMessageLog = true; |
| 66 | grpcController.activate(); |
| 67 | DeviceId deviceId = DeviceId.deviceId("dummy:1"); |
| 68 | |
| 69 | ManagedChannelBuilder channelBuilder = NettyChannelBuilder |
| 70 | .forAddress("192.168.56.102", 59975) |
| 71 | .usePlaintext(true); |
| 72 | |
| 73 | assert (controller.createClient(deviceId, 1, channelBuilder)); |
| 74 | |
| 75 | P4RuntimeClient client = controller.getClient(deviceId); |
| 76 | |
| 77 | assert (client.setPipelineConfig(bmv2DefaultPipeconf, PiPipeconf.ExtensionType.BMV2_JSON).get()); |
| 78 | |
| 79 | assert (client.initStreamChannel().get()); |
| 80 | |
| 81 | // log.info("++++++++++++++++++++++++++++"); |
| 82 | // |
| 83 | // PiPipelineInterpreter interpreter = (PiPipelineInterpreter) defaultPipeconf |
| 84 | // .implementation(PiPipelineInterpreter.class) |
| 85 | // .orElse(null) |
| 86 | // .newInstance(); |
| 87 | // |
| 88 | // TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 89 | // .setOutput(PortNumber.portNumber(830L)).build(); |
| 90 | // byte[] payload = new byte[1000]; |
| 91 | //// payload[0] = 1; |
| 92 | // Arrays.fill( payload, (byte) 1 ); |
| 93 | // |
| 94 | // OutboundPacket packet = new DefaultOutboundPacket( |
| 95 | // deviceId, t, ByteBuffer.wrap(payload)); |
| 96 | // |
| 97 | // |
| 98 | // Collection<PiPacketOperation> operations = interpreter.mapOutboundPacket(packet,defaultPipeconf); |
| 99 | // log.info("{}", operations); |
| 100 | // operations.forEach(piPacketOperation -> { |
| 101 | // try { |
| 102 | // client.packetOut(piPacketOperation, defaultPipeconf).get(); |
| 103 | // } catch (InterruptedException | ExecutionException e) { |
| 104 | // log.error("{}",e); |
| 105 | // } |
| 106 | // }); |
| 107 | |
| 108 | // assert(client.dumpTable(PiTableId.of(TABLE_0), defaultPipeconf).get().size() == 0); |
| 109 | |
| 110 | // assert(client.writeTableEntries(Lists.newArrayList(piTableEntry), INSERT, defaultPipeconf).get()); |
| 111 | |
| 112 | // assert(client.dumpTable(PiTableId.of(TABLE_0), defaultPipeconf).get().size() == 1); |
| 113 | } |
| 114 | } |