Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.gnmi; |
| 18 | |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 19 | import io.grpc.StatusRuntimeException; |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 20 | import org.onosproject.gnmi.api.GnmiClient; |
| 21 | import org.onosproject.gnmi.api.GnmiClientKey; |
| 22 | import org.onosproject.gnmi.api.GnmiController; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.ExecutionException; |
| 32 | import java.util.concurrent.TimeUnit; |
| 33 | import java.util.concurrent.TimeoutException; |
| 34 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Abstract implementation of a behaviour handler for a gNMI device. |
| 37 | */ |
| 38 | public class AbstractGnmiHandlerBehaviour extends AbstractHandlerBehaviour { |
| 39 | |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 40 | // Default timeout in seconds for device operations. |
| 41 | private static final String DEVICE_REQ_TIMEOUT = "deviceRequestTimeout"; |
| 42 | private static final int DEFAULT_DEVICE_REQ_TIMEOUT = 60; |
| 43 | |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 44 | public static final String GNMI_SERVER_ADDR_KEY = "gnmi_ip"; |
| 45 | public static final String GNMI_SERVER_PORT_KEY = "gnmi_port"; |
| 46 | private static final String GNMI_SERVICE_NAME = "gnmi"; |
| 47 | |
| 48 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 49 | protected DeviceId deviceId; |
| 50 | protected DeviceService deviceService; |
| 51 | protected Device device; |
| 52 | protected GnmiController controller; |
| 53 | protected GnmiClient client; |
| 54 | |
| 55 | protected boolean setupBehaviour() { |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 56 | deviceId = handler().data().deviceId(); |
Yi Tseng | dbe3c7e | 2018-11-27 23:11:23 -0800 | [diff] [blame^] | 57 | deviceService = handler().get(DeviceService.class); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 58 | controller = handler().get(GnmiController.class); |
| 59 | client = controller.getClient(deviceId); |
| 60 | |
| 61 | if (client == null) { |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 62 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | protected GnmiClient createClient() { |
| 70 | deviceId = handler().data().deviceId(); |
| 71 | controller = handler().get(GnmiController.class); |
| 72 | |
| 73 | final String serverAddr = this.data().value(GNMI_SERVER_ADDR_KEY); |
| 74 | final String serverPortString = this.data().value(GNMI_SERVER_PORT_KEY); |
| 75 | |
| 76 | if (serverAddr == null || serverPortString == null) { |
| 77 | log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})", |
| 78 | deviceId, GNMI_SERVER_ADDR_KEY, GNMI_SERVER_PORT_KEY); |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | final int serverPort; |
| 83 | try { |
| 84 | serverPort = Integer.parseUnsignedInt(serverPortString); |
| 85 | } catch (NumberFormatException e) { |
| 86 | log.error("{} is not a valid gNMI port number", serverPortString); |
| 87 | return null; |
| 88 | } |
| 89 | GnmiClientKey clientKey = |
| 90 | new GnmiClientKey(GNMI_SERVICE_NAME, deviceId, serverAddr, serverPort); |
| 91 | if (!controller.createClient(clientKey)) { |
| 92 | log.warn("Unable to create client for {}, aborting operation", deviceId); |
| 93 | return null; |
| 94 | } |
| 95 | return controller.getClient(deviceId); |
| 96 | } |
Yi Tseng | 5f7fef5 | 2018-11-05 11:30:47 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Returns the device request timeout driver property, or a default value |
| 100 | * if the property is not present or cannot be parsed. |
| 101 | * |
| 102 | * @return timeout value |
| 103 | */ |
| 104 | private int getDeviceRequestTimeout() { |
| 105 | final String timeout = handler().driver() |
| 106 | .getProperty(DEVICE_REQ_TIMEOUT); |
| 107 | if (timeout == null) { |
| 108 | return DEFAULT_DEVICE_REQ_TIMEOUT; |
| 109 | } else { |
| 110 | try { |
| 111 | return Integer.parseInt(timeout); |
| 112 | } catch (NumberFormatException e) { |
| 113 | log.error("{} driver property '{}' is not a number, using default value {}", |
| 114 | DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT); |
| 115 | return DEFAULT_DEVICE_REQ_TIMEOUT; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Convenience method to get the result of a completable future while |
| 122 | * setting a timeout and checking for exceptions. |
| 123 | * |
| 124 | * @param future completable future |
| 125 | * @param opDescription operation description to use in log messages. Should |
| 126 | * be a sentence starting with a verb ending in -ing, |
| 127 | * e.g. "reading...", "writing...", etc. |
| 128 | * @param defaultValue value to return if operation fails |
| 129 | * @param <U> type of returned value |
| 130 | * @return future result or default value |
| 131 | */ |
| 132 | <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription, |
| 133 | U defaultValue) { |
| 134 | try { |
| 135 | return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS); |
| 136 | } catch (InterruptedException e) { |
| 137 | log.error("Exception while {} on {}", opDescription, deviceId); |
| 138 | } catch (ExecutionException e) { |
| 139 | final Throwable cause = e.getCause(); |
| 140 | if (cause instanceof StatusRuntimeException) { |
| 141 | final StatusRuntimeException grpcError = (StatusRuntimeException) cause; |
| 142 | log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage()); |
| 143 | } else { |
| 144 | log.error("Exception while {} on {}", opDescription, deviceId, e.getCause()); |
| 145 | } |
| 146 | } catch (TimeoutException e) { |
| 147 | log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId); |
| 148 | } |
| 149 | return defaultValue; |
| 150 | } |
Yi Tseng | 27851e3 | 2018-11-01 18:30:04 -0700 | [diff] [blame] | 151 | } |