blob: 7f9c8c661079021e8046d015502c610431c25eb7 [file] [log] [blame]
Carmelo Casconee3a7c742017-09-01 01:25:52 +02001/*
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.drivers.p4runtime;
18
Carmelo Casconee5b28722018-06-22 17:28:28 +020019import io.grpc.StatusRuntimeException;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020020import org.onosproject.net.Device;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.driver.AbstractHandlerBehaviour;
24import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Casconee5b28722018-06-22 17:28:28 +020025import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone158b8c42018-07-04 19:42:37 +020026import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080027import org.onosproject.net.pi.service.PiPipeconfService;
28import org.onosproject.net.pi.service.PiTranslationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020029import org.onosproject.p4runtime.api.P4RuntimeClient;
Yi Tseng2a340f72018-11-02 16:52:47 -070030import org.onosproject.p4runtime.api.P4RuntimeClientKey;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020031import org.onosproject.p4runtime.api.P4RuntimeController;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Carmelo Casconee5b28722018-06-22 17:28:28 +020035import java.util.concurrent.CompletableFuture;
36import java.util.concurrent.ExecutionException;
37import java.util.concurrent.TimeUnit;
38import java.util.concurrent.TimeoutException;
39
Carmelo Cascone3da671a2018-02-12 10:43:35 -080040import static com.google.common.base.Preconditions.checkNotNull;
41
Carmelo Casconee3a7c742017-09-01 01:25:52 +020042/**
43 * Abstract implementation of a behaviour handler for a P4Runtime device.
44 */
45public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour {
46
Carmelo Cascone158b8c42018-07-04 19:42:37 +020047 // Default timeout in seconds for device operations.
48 private static final String DEVICE_REQ_TIMEOUT = "deviceRequestTimeout";
49 private static final int DEFAULT_DEVICE_REQ_TIMEOUT = 60;
Carmelo Casconee5b28722018-06-22 17:28:28 +020050
Yi Tsengd7716482018-10-31 15:34:30 -070051 private static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip";
52 private static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port";
53 private static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId";
Carmelo Casconee3a7c742017-09-01 01:25:52 +020054
55 protected final Logger log = LoggerFactory.getLogger(getClass());
56
57 // Initialized by setupBehaviour()
58 protected DeviceId deviceId;
59 protected DeviceService deviceService;
60 protected Device device;
61 protected P4RuntimeController controller;
62 protected PiPipeconf pipeconf;
63 protected P4RuntimeClient client;
Yi Tsengd7716482018-10-31 15:34:30 -070064 protected PiTranslationService translationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020065
66 /**
Carmelo Cascone3da671a2018-02-12 10:43:35 -080067 * Initializes this behaviour attributes. Returns true if the operation was
68 * successful, false otherwise. This method assumes that the P4runtime
69 * controller already has a client for this device and that the device has
70 * been created in the core.
Carmelo Casconee3a7c742017-09-01 01:25:52 +020071 *
72 * @return true if successful, false otherwise
73 */
74 protected boolean setupBehaviour() {
75 deviceId = handler().data().deviceId();
76
77 deviceService = handler().get(DeviceService.class);
78 device = deviceService.getDevice(deviceId);
79 if (device == null) {
80 log.warn("Unable to find device with id {}, aborting operation", deviceId);
81 return false;
82 }
83
84 controller = handler().get(P4RuntimeController.class);
Carmelo Cascone158b8c42018-07-04 19:42:37 +020085 client = controller.getClient(deviceId);
86 if (client == null) {
Carmelo Casconee3a7c742017-09-01 01:25:52 +020087 log.warn("Unable to find client for {}, aborting operation", deviceId);
88 return false;
89 }
Carmelo Casconee3a7c742017-09-01 01:25:52 +020090
91 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
Carmelo Casconee5b28722018-06-22 17:28:28 +020092 if (!piPipeconfService.ofDevice(deviceId).isPresent()) {
93 log.warn("Unable to get assigned pipeconf for {} (mapping " +
94 "missing in PiPipeconfService), aborting operation",
95 deviceId);
Carmelo Casconee3a7c742017-09-01 01:25:52 +020096 return false;
97 }
Carmelo Casconee5b28722018-06-22 17:28:28 +020098 PiPipeconfId pipeconfId = piPipeconfService.ofDevice(deviceId).get();
99 if (!piPipeconfService.getPipeconf(pipeconfId).isPresent()) {
100 log.warn("Cannot find any pipeconf with ID '{}' ({}), aborting operation", pipeconfId, deviceId);
101 return false;
102 }
103 pipeconf = piPipeconfService.getPipeconf(pipeconfId).get();
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200104
Yi Tsengd7716482018-10-31 15:34:30 -0700105 translationService = handler().get(PiTranslationService.class);
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200106
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200107 return true;
108 }
109
110 /**
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200111 * Returns an instance of the interpreter implementation for this device,
112 * null if an interpreter cannot be retrieved.
113 *
114 * @return interpreter or null
115 */
116 PiPipelineInterpreter getInterpreter() {
117 if (!device.is(PiPipelineInterpreter.class)) {
118 log.warn("Unable to get interpreter for {}, missing behaviour",
119 deviceId);
120 return null;
121 }
122 return device.as(PiPipelineInterpreter.class);
123 }
124
125 /**
126 * Returns a P4Runtime client for this device, null if such client cannot be
127 * created.
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200128 *
Carmelo Casconee5b28722018-06-22 17:28:28 +0200129 * @return client or null
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200130 */
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200131 P4RuntimeClient createClient() {
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200132 deviceId = handler().data().deviceId();
133 controller = handler().get(P4RuntimeController.class);
134
Carmelo Cascone44448a52018-06-25 23:36:57 +0200135 final String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY);
136 final String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY);
137 final String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200138
139 if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) {
140 log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
141 deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200142 return null;
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200143 }
144
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200145 final int serverPort;
146 final long p4DeviceId;
Carmelo Casconee5b28722018-06-22 17:28:28 +0200147
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200148 try {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200149 serverPort = Integer.parseUnsignedInt(serverPortString);
150 } catch (NumberFormatException e) {
151 log.error("{} is not a valid P4Runtime port number", serverPortString);
152 return null;
153 }
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200154 try {
155 p4DeviceId = Long.parseUnsignedLong(p4DeviceIdString);
156 } catch (NumberFormatException e) {
157 log.error("{} is not a valid P4Runtime-internal device ID", p4DeviceIdString);
158 return null;
159 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200160
Yi Tsengd7716482018-10-31 15:34:30 -0700161 final P4RuntimeClientKey clientKey = new P4RuntimeClientKey(
162 deviceId, serverAddr, serverPort, p4DeviceId);
Yi Tseng2a340f72018-11-02 16:52:47 -0700163 if (!controller.createClient(clientKey)) {
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200164 log.warn("Unable to create client for {}, aborting operation", deviceId);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200165 return null;
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200166 }
167
Carmelo Casconee5b28722018-06-22 17:28:28 +0200168 return controller.getClient(deviceId);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200169 }
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800170
171 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +0200172 * Returns the value of the given driver property, if present, otherwise
173 * returns the given default value.
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800174 *
Carmelo Casconee5b28722018-06-22 17:28:28 +0200175 * @param propName property name
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800176 * @param defaultVal default value
177 * @return boolean
178 */
Yi Tsengd7716482018-10-31 15:34:30 -0700179 boolean driverBoolProperty(String propName, boolean defaultVal) {
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800180 checkNotNull(propName);
181 if (handler().driver().getProperty(propName) == null) {
182 return defaultVal;
183 } else {
184 return Boolean.parseBoolean(handler().driver().getProperty(propName));
185 }
186 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200187
188 /**
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200189 * Returns the device request timeout driver property, or a default value
190 * if the property is not present or cannot be parsed.
191 *
192 * @return timeout value
193 */
194 private int getDeviceRequestTimeout() {
195 final String timeout = handler().driver()
196 .getProperty(DEVICE_REQ_TIMEOUT);
197 if (timeout == null) {
198 return DEFAULT_DEVICE_REQ_TIMEOUT;
199 } else {
200 try {
201 return Integer.parseInt(timeout);
202 } catch (NumberFormatException e) {
203 log.error("{} driver property '{}' is not a number, using default value {}",
204 DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT);
205 return DEFAULT_DEVICE_REQ_TIMEOUT;
206 }
207 }
208 }
209
210 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +0200211 * Convenience method to get the result of a completable future while
212 * setting a timeout and checking for exceptions.
213 *
214 * @param future completable future
215 * @param opDescription operation description to use in log messages. Should
216 * be a sentence starting with a verb ending in -ing,
217 * e.g. "reading...", "writing...", etc.
218 * @param defaultValue value to return if operation fails
219 * @param <U> type of returned value
220 * @return future result or default value
221 */
222 <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription,
223 U defaultValue) {
224 try {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200225 return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200226 } catch (InterruptedException e) {
227 log.error("Exception while {} on {}", opDescription, deviceId);
228 } catch (ExecutionException e) {
229 final Throwable cause = e.getCause();
230 if (cause instanceof StatusRuntimeException) {
231 final StatusRuntimeException grpcError = (StatusRuntimeException) cause;
232 log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage());
233 } else {
234 log.error("Exception while {} on {}", opDescription, deviceId, e.getCause());
235 }
236 } catch (TimeoutException e) {
237 log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId);
238 }
239 return defaultValue;
240 }
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200241}