blob: 3f6c9ad16f773c1d83f4794cbb96290530e27a2d [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;
30import org.onosproject.p4runtime.api.P4RuntimeController;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Carmelo Casconee5b28722018-06-22 17:28:28 +020034import java.util.concurrent.CompletableFuture;
35import java.util.concurrent.ExecutionException;
36import java.util.concurrent.TimeUnit;
37import java.util.concurrent.TimeoutException;
38
Carmelo Cascone3da671a2018-02-12 10:43:35 -080039import static com.google.common.base.Preconditions.checkNotNull;
40
Carmelo Casconee3a7c742017-09-01 01:25:52 +020041/**
42 * Abstract implementation of a behaviour handler for a P4Runtime device.
43 */
44public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour {
45
Carmelo Cascone158b8c42018-07-04 19:42:37 +020046 // Default timeout in seconds for device operations.
47 private static final String DEVICE_REQ_TIMEOUT = "deviceRequestTimeout";
48 private static final int DEFAULT_DEVICE_REQ_TIMEOUT = 60;
Carmelo Casconee5b28722018-06-22 17:28:28 +020049
Carmelo Casconee3a7c742017-09-01 01:25:52 +020050 public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip";
51 public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port";
52 public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId";
53
54 protected final Logger log = LoggerFactory.getLogger(getClass());
55
56 // Initialized by setupBehaviour()
57 protected DeviceId deviceId;
58 protected DeviceService deviceService;
59 protected Device device;
60 protected P4RuntimeController controller;
61 protected PiPipeconf pipeconf;
62 protected P4RuntimeClient client;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020063 protected PiTranslationService piTranslationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020064
65 /**
Carmelo Cascone3da671a2018-02-12 10:43:35 -080066 * Initializes this behaviour attributes. Returns true if the operation was
67 * successful, false otherwise. This method assumes that the P4runtime
68 * controller already has a client for this device and that the device has
69 * been created in the core.
Carmelo Casconee3a7c742017-09-01 01:25:52 +020070 *
71 * @return true if successful, false otherwise
72 */
73 protected boolean setupBehaviour() {
74 deviceId = handler().data().deviceId();
75
76 deviceService = handler().get(DeviceService.class);
77 device = deviceService.getDevice(deviceId);
78 if (device == null) {
79 log.warn("Unable to find device with id {}, aborting operation", deviceId);
80 return false;
81 }
82
83 controller = handler().get(P4RuntimeController.class);
Carmelo Cascone158b8c42018-07-04 19:42:37 +020084 client = controller.getClient(deviceId);
85 if (client == null) {
Carmelo Casconee3a7c742017-09-01 01:25:52 +020086 log.warn("Unable to find client for {}, aborting operation", deviceId);
87 return false;
88 }
Carmelo Casconee3a7c742017-09-01 01:25:52 +020089
90 PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
Carmelo Casconee5b28722018-06-22 17:28:28 +020091 if (!piPipeconfService.ofDevice(deviceId).isPresent()) {
92 log.warn("Unable to get assigned pipeconf for {} (mapping " +
93 "missing in PiPipeconfService), aborting operation",
94 deviceId);
Carmelo Casconee3a7c742017-09-01 01:25:52 +020095 return false;
96 }
Carmelo Casconee5b28722018-06-22 17:28:28 +020097 PiPipeconfId pipeconfId = piPipeconfService.ofDevice(deviceId).get();
98 if (!piPipeconfService.getPipeconf(pipeconfId).isPresent()) {
99 log.warn("Cannot find any pipeconf with ID '{}' ({}), aborting operation", pipeconfId, deviceId);
100 return false;
101 }
102 pipeconf = piPipeconfService.getPipeconf(pipeconfId).get();
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200103
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200104 piTranslationService = handler().get(PiTranslationService.class);
105
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200106 return true;
107 }
108
109 /**
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200110 * Returns an instance of the interpreter implementation for this device,
111 * null if an interpreter cannot be retrieved.
112 *
113 * @return interpreter or null
114 */
115 PiPipelineInterpreter getInterpreter() {
116 if (!device.is(PiPipelineInterpreter.class)) {
117 log.warn("Unable to get interpreter for {}, missing behaviour",
118 deviceId);
119 return null;
120 }
121 return device.as(PiPipelineInterpreter.class);
122 }
123
124 /**
125 * Returns a P4Runtime client for this device, null if such client cannot be
126 * created.
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200127 *
Carmelo Casconee5b28722018-06-22 17:28:28 +0200128 * @return client or null
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200129 */
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200130 P4RuntimeClient createClient() {
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200131 deviceId = handler().data().deviceId();
132 controller = handler().get(P4RuntimeController.class);
133
Carmelo Cascone44448a52018-06-25 23:36:57 +0200134 final String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY);
135 final String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY);
136 final String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200137
138 if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) {
139 log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
140 deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200141 return null;
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200142 }
143
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200144 final int serverPort;
145 final long p4DeviceId;
Carmelo Casconee5b28722018-06-22 17:28:28 +0200146
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200147 try {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200148 serverPort = Integer.parseUnsignedInt(serverPortString);
149 } catch (NumberFormatException e) {
150 log.error("{} is not a valid P4Runtime port number", serverPortString);
151 return null;
152 }
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200153 try {
154 p4DeviceId = Long.parseUnsignedLong(p4DeviceIdString);
155 } catch (NumberFormatException e) {
156 log.error("{} is not a valid P4Runtime-internal device ID", p4DeviceIdString);
157 return null;
158 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200159
160 if (!controller.createClient(deviceId, serverAddr, serverPort, p4DeviceId)) {
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200161 log.warn("Unable to create client for {}, aborting operation", deviceId);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200162 return null;
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200163 }
164
Carmelo Casconee5b28722018-06-22 17:28:28 +0200165 return controller.getClient(deviceId);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200166 }
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800167
168 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +0200169 * Returns the value of the given driver property, if present, otherwise
170 * returns the given default value.
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800171 *
Carmelo Casconee5b28722018-06-22 17:28:28 +0200172 * @param propName property name
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800173 * @param defaultVal default value
174 * @return boolean
175 */
176 protected boolean driverBoolProperty(String propName, boolean defaultVal) {
177 checkNotNull(propName);
178 if (handler().driver().getProperty(propName) == null) {
179 return defaultVal;
180 } else {
181 return Boolean.parseBoolean(handler().driver().getProperty(propName));
182 }
183 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200184
185 /**
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200186 * Returns the device request timeout driver property, or a default value
187 * if the property is not present or cannot be parsed.
188 *
189 * @return timeout value
190 */
191 private int getDeviceRequestTimeout() {
192 final String timeout = handler().driver()
193 .getProperty(DEVICE_REQ_TIMEOUT);
194 if (timeout == null) {
195 return DEFAULT_DEVICE_REQ_TIMEOUT;
196 } else {
197 try {
198 return Integer.parseInt(timeout);
199 } catch (NumberFormatException e) {
200 log.error("{} driver property '{}' is not a number, using default value {}",
201 DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT);
202 return DEFAULT_DEVICE_REQ_TIMEOUT;
203 }
204 }
205 }
206
207 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +0200208 * Convenience method to get the result of a completable future while
209 * setting a timeout and checking for exceptions.
210 *
211 * @param future completable future
212 * @param opDescription operation description to use in log messages. Should
213 * be a sentence starting with a verb ending in -ing,
214 * e.g. "reading...", "writing...", etc.
215 * @param defaultValue value to return if operation fails
216 * @param <U> type of returned value
217 * @return future result or default value
218 */
219 <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription,
220 U defaultValue) {
221 try {
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200222 return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200223 } catch (InterruptedException e) {
224 log.error("Exception while {} on {}", opDescription, deviceId);
225 } catch (ExecutionException e) {
226 final Throwable cause = e.getCause();
227 if (cause instanceof StatusRuntimeException) {
228 final StatusRuntimeException grpcError = (StatusRuntimeException) cause;
229 log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage());
230 } else {
231 log.error("Exception while {} on {}", opDescription, deviceId, e.getCause());
232 }
233 } catch (TimeoutException e) {
234 log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId);
235 }
236 return defaultValue;
237 }
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200238}