blob: ac4856ae76f5acf5fa47f46d0915533196df3984 [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 Cascone39c28ca2017-11-15 13:03:57 -080026import org.onosproject.net.pi.service.PiPipeconfService;
27import org.onosproject.net.pi.service.PiTranslationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020028import org.onosproject.p4runtime.api.P4RuntimeClient;
29import org.onosproject.p4runtime.api.P4RuntimeController;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Carmelo Casconee5b28722018-06-22 17:28:28 +020033import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutionException;
35import java.util.concurrent.TimeUnit;
36import java.util.concurrent.TimeoutException;
37
Carmelo Cascone3da671a2018-02-12 10:43:35 -080038import static com.google.common.base.Preconditions.checkNotNull;
39
Carmelo Casconee3a7c742017-09-01 01:25:52 +020040/**
41 * Abstract implementation of a behaviour handler for a P4Runtime device.
42 */
43public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour {
44
Carmelo Casconee5b28722018-06-22 17:28:28 +020045 // Timeout in seconds for device operations.
46 // TODO make configurable via driver properties
47 private static final int DEVICE_OP_TIMEOUT = 5;
48
Carmelo Casconee3a7c742017-09-01 01:25:52 +020049 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 Cascone87b9b392017-10-02 18:33:20 +020062 protected PiTranslationService piTranslationService;
Carmelo Casconee3a7c742017-09-01 01:25:52 +020063
64 /**
Carmelo Cascone3da671a2018-02-12 10:43:35 -080065 * 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 Casconee3a7c742017-09-01 01:25:52 +020069 *
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 Casconee5b28722018-06-22 17:28:28 +020090 if (!piPipeconfService.ofDevice(deviceId).isPresent()) {
91 log.warn("Unable to get assigned pipeconf for {} (mapping " +
92 "missing in PiPipeconfService), aborting operation",
93 deviceId);
Carmelo Casconee3a7c742017-09-01 01:25:52 +020094 return false;
95 }
Carmelo Casconee5b28722018-06-22 17:28:28 +020096 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 Casconee3a7c742017-09-01 01:25:52 +0200102
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200103 piTranslationService = handler().get(PiTranslationService.class);
104
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200105 return true;
106 }
107
108 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +0200109 * Returns a P4Runtime client for this device, null if such client cannot
110 * be created.
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200111 *
Carmelo Casconee5b28722018-06-22 17:28:28 +0200112 * @return client or null
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200113 */
Carmelo Casconee5b28722018-06-22 17:28:28 +0200114 P4RuntimeClient createClient() {
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200115 deviceId = handler().data().deviceId();
116 controller = handler().get(P4RuntimeController.class);
117
Carmelo Cascone44448a52018-06-25 23:36:57 +0200118 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 Casconee3a7c742017-09-01 01:25:52 +0200121
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 Casconee5b28722018-06-22 17:28:28 +0200125 return null;
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200126 }
127
Carmelo Casconee5b28722018-06-22 17:28:28 +0200128 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 Casconee3a7c742017-09-01 01:25:52 +0200145 log.warn("Unable to create client for {}, aborting operation", deviceId);
Carmelo Casconee5b28722018-06-22 17:28:28 +0200146 return null;
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200147 }
148
Carmelo Casconee5b28722018-06-22 17:28:28 +0200149 return controller.getClient(deviceId);
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200150 }
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800151
152 /**
Carmelo Casconee5b28722018-06-22 17:28:28 +0200153 * Returns the value of the given driver property, if present, otherwise
154 * returns the given default value.
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800155 *
Carmelo Casconee5b28722018-06-22 17:28:28 +0200156 * @param propName property name
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800157 * @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 Casconee5b28722018-06-22 17:28:28 +0200168
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 Casconee3a7c742017-09-01 01:25:52 +0200200}