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 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 19 | import io.grpc.StatusRuntimeException; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 25 | import org.onosproject.net.pi.model.PiPipeconfId; |
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 | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 33 | import java.util.concurrent.CompletableFuture; |
| 34 | import java.util.concurrent.ExecutionException; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | import java.util.concurrent.TimeoutException; |
| 37 | |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 40 | /** |
| 41 | * Abstract implementation of a behaviour handler for a P4Runtime device. |
| 42 | */ |
| 43 | public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour { |
| 44 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 45 | // Timeout in seconds for device operations. |
| 46 | // TODO make configurable via driver properties |
| 47 | private static final int DEVICE_OP_TIMEOUT = 5; |
| 48 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 49 | public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip"; |
| 50 | public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port"; |
| 51 | public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId"; |
| 52 | |
| 53 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
| 55 | // Initialized by setupBehaviour() |
| 56 | protected DeviceId deviceId; |
| 57 | protected DeviceService deviceService; |
| 58 | protected Device device; |
| 59 | protected P4RuntimeController controller; |
| 60 | protected PiPipeconf pipeconf; |
| 61 | protected P4RuntimeClient client; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 62 | protected PiTranslationService piTranslationService; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 63 | |
| 64 | /** |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 65 | * Initializes this behaviour attributes. Returns true if the operation was |
| 66 | * successful, false otherwise. This method assumes that the P4runtime |
| 67 | * controller already has a client for this device and that the device has |
| 68 | * been created in the core. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 69 | * |
| 70 | * @return true if successful, false otherwise |
| 71 | */ |
| 72 | protected boolean setupBehaviour() { |
| 73 | deviceId = handler().data().deviceId(); |
| 74 | |
| 75 | deviceService = handler().get(DeviceService.class); |
| 76 | device = deviceService.getDevice(deviceId); |
| 77 | if (device == null) { |
| 78 | log.warn("Unable to find device with id {}, aborting operation", deviceId); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | controller = handler().get(P4RuntimeController.class); |
| 83 | if (!controller.hasClient(deviceId)) { |
| 84 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
| 85 | return false; |
| 86 | } |
| 87 | client = controller.getClient(deviceId); |
| 88 | |
| 89 | PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 90 | if (!piPipeconfService.ofDevice(deviceId).isPresent()) { |
| 91 | log.warn("Unable to get assigned pipeconf for {} (mapping " + |
| 92 | "missing in PiPipeconfService), aborting operation", |
| 93 | deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 94 | return false; |
| 95 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 96 | PiPipeconfId pipeconfId = piPipeconfService.ofDevice(deviceId).get(); |
| 97 | if (!piPipeconfService.getPipeconf(pipeconfId).isPresent()) { |
| 98 | log.warn("Cannot find any pipeconf with ID '{}' ({}), aborting operation", pipeconfId, deviceId); |
| 99 | return false; |
| 100 | } |
| 101 | pipeconf = piPipeconfService.getPipeconf(pipeconfId).get(); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 102 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 103 | piTranslationService = handler().get(PiTranslationService.class); |
| 104 | |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 109 | * Returns a P4Runtime client for this device, null if such client cannot |
| 110 | * be created. |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 111 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 112 | * @return client or null |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 113 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 114 | P4RuntimeClient createClient() { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 115 | deviceId = handler().data().deviceId(); |
| 116 | controller = handler().get(P4RuntimeController.class); |
| 117 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 118 | final String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY); |
| 119 | final String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY); |
| 120 | final String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 121 | |
| 122 | if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) { |
| 123 | log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})", |
| 124 | deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 125 | return null; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 128 | final int serverPort; |
| 129 | final long p4DeviceId; |
| 130 | |
| 131 | try { |
| 132 | serverPort = Integer.parseUnsignedInt(serverPortString); |
| 133 | } catch (NumberFormatException e) { |
| 134 | log.error("{} is not a valid P4Runtime port number", serverPortString); |
| 135 | return null; |
| 136 | } |
| 137 | try { |
| 138 | p4DeviceId = Long.parseUnsignedLong(p4DeviceIdString); |
| 139 | } catch (NumberFormatException e) { |
| 140 | log.error("{} is not a valid P4Runtime-internal device ID", p4DeviceIdString); |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | if (!controller.createClient(deviceId, serverAddr, serverPort, p4DeviceId)) { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 145 | log.warn("Unable to create client for {}, aborting operation", deviceId); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 146 | return null; |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 149 | return controller.getClient(deviceId); |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 150 | } |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 151 | |
| 152 | /** |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 153 | * Returns the value of the given driver property, if present, otherwise |
| 154 | * returns the given default value. |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 155 | * |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 156 | * @param propName property name |
Carmelo Cascone | 3da671a | 2018-02-12 10:43:35 -0800 | [diff] [blame] | 157 | * @param defaultVal default value |
| 158 | * @return boolean |
| 159 | */ |
| 160 | protected boolean driverBoolProperty(String propName, boolean defaultVal) { |
| 161 | checkNotNull(propName); |
| 162 | if (handler().driver().getProperty(propName) == null) { |
| 163 | return defaultVal; |
| 164 | } else { |
| 165 | return Boolean.parseBoolean(handler().driver().getProperty(propName)); |
| 166 | } |
| 167 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame^] | 168 | |
| 169 | /** |
| 170 | * Convenience method to get the result of a completable future while |
| 171 | * setting a timeout and checking for exceptions. |
| 172 | * |
| 173 | * @param future completable future |
| 174 | * @param opDescription operation description to use in log messages. Should |
| 175 | * be a sentence starting with a verb ending in -ing, |
| 176 | * e.g. "reading...", "writing...", etc. |
| 177 | * @param defaultValue value to return if operation fails |
| 178 | * @param <U> type of returned value |
| 179 | * @return future result or default value |
| 180 | */ |
| 181 | <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription, |
| 182 | U defaultValue) { |
| 183 | try { |
| 184 | return future.get(DEVICE_OP_TIMEOUT, TimeUnit.SECONDS); |
| 185 | } catch (InterruptedException e) { |
| 186 | log.error("Exception while {} on {}", opDescription, deviceId); |
| 187 | } catch (ExecutionException e) { |
| 188 | final Throwable cause = e.getCause(); |
| 189 | if (cause instanceof StatusRuntimeException) { |
| 190 | final StatusRuntimeException grpcError = (StatusRuntimeException) cause; |
| 191 | log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage()); |
| 192 | } else { |
| 193 | log.error("Exception while {} on {}", opDescription, deviceId, e.getCause()); |
| 194 | } |
| 195 | } catch (TimeoutException e) { |
| 196 | log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId); |
| 197 | } |
| 198 | return defaultValue; |
| 199 | } |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 200 | } |