Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [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.drivers.p4runtime; |
| 18 | |
| 19 | import io.grpc.ManagedChannelBuilder; |
| 20 | import io.grpc.netty.NettyChannelBuilder; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 25 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 26 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 27 | import org.onosproject.net.pi.service.PiTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 28 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 29 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame^] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 35 | /** |
| 36 | * Abstract implementation of a behaviour handler for a P4Runtime device. |
| 37 | */ |
| 38 | public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour { |
| 39 | |
| 40 | public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip"; |
| 41 | public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port"; |
| 42 | public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId"; |
| 43 | |
| 44 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | // Initialized by setupBehaviour() |
| 47 | protected DeviceId deviceId; |
| 48 | protected DeviceService deviceService; |
| 49 | protected Device device; |
| 50 | protected P4RuntimeController controller; |
| 51 | protected PiPipeconf pipeconf; |
| 52 | protected P4RuntimeClient client; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 53 | protected PiTranslationService piTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 54 | |
| 55 | /** |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame^] | 56 | * Initializes this behaviour attributes. Returns true if the operation was |
| 57 | * successful, false otherwise. This method assumes that the P4runtime |
| 58 | * controller already has a client for this device and that the device has |
| 59 | * been created in the core. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 60 | * |
| 61 | * @return true if successful, false otherwise |
| 62 | */ |
| 63 | protected boolean setupBehaviour() { |
| 64 | deviceId = handler().data().deviceId(); |
| 65 | |
| 66 | deviceService = handler().get(DeviceService.class); |
| 67 | device = deviceService.getDevice(deviceId); |
| 68 | if (device == null) { |
| 69 | log.warn("Unable to find device with id {}, aborting operation", deviceId); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | controller = handler().get(P4RuntimeController.class); |
| 74 | if (!controller.hasClient(deviceId)) { |
| 75 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
| 76 | return false; |
| 77 | } |
| 78 | client = controller.getClient(deviceId); |
| 79 | |
| 80 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
| 81 | if (!piPipeconfService.ofDevice(deviceId).isPresent() || |
| 82 | !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 83 | log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId); |
| 84 | return false; |
| 85 | } |
| 86 | pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get(); |
| 87 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 88 | piTranslationService = handler().get(PiTranslationService.class); |
| 89 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame^] | 94 | * Create a P4Runtime client for this device. Returns true if the operation |
| 95 | * was successful, false otherwise. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 96 | * |
| 97 | * @return true if successful, false otherwise |
| 98 | */ |
| 99 | protected boolean createClient() { |
| 100 | deviceId = handler().data().deviceId(); |
| 101 | controller = handler().get(P4RuntimeController.class); |
| 102 | |
| 103 | String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY); |
| 104 | String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY); |
| 105 | String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY); |
| 106 | |
| 107 | if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) { |
| 108 | log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})", |
| 109 | deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | ManagedChannelBuilder channelBuilder = NettyChannelBuilder |
| 114 | .forAddress(serverAddr, Integer.valueOf(serverPortString)) |
| 115 | .usePlaintext(true); |
| 116 | |
| 117 | if (!controller.createClient(deviceId, Long.parseUnsignedLong(p4DeviceIdString), channelBuilder)) { |
| 118 | log.warn("Unable to create client for {}, aborting operation", deviceId); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame^] | 124 | |
| 125 | /** |
| 126 | * Returns the value of the given driver property, if present, |
| 127 | * otherwise returns the given default value. |
| 128 | * |
| 129 | * @param propName property name |
| 130 | * @param defaultVal default value |
| 131 | * @return boolean |
| 132 | */ |
| 133 | protected boolean driverBoolProperty(String propName, boolean defaultVal) { |
| 134 | checkNotNull(propName); |
| 135 | if (handler().driver().getProperty(propName) == null) { |
| 136 | return defaultVal; |
| 137 | } else { |
| 138 | return Boolean.parseBoolean(handler().driver().getProperty(propName)); |
| 139 | } |
| 140 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 141 | } |