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