blob: 4ce9f1866ffa0847ce7a2c40dd1963e70a6eb07b [file] [log] [blame]
Yi Tseng27851e32018-11-01 18:30:04 -07001/*
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
17package org.onosproject.drivers.gnmi;
18
Carmelo Cascone3977ea42019-02-28 13:43:42 -080019import com.google.common.base.Strings;
Yi Tseng5f7fef52018-11-05 11:30:47 -080020import io.grpc.StatusRuntimeException;
Yi Tseng27851e32018-11-01 18:30:04 -070021import org.onosproject.gnmi.api.GnmiClient;
22import org.onosproject.gnmi.api.GnmiClientKey;
23import org.onosproject.gnmi.api.GnmiController;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080026import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.BasicDeviceConfig;
Yi Tseng27851e32018-11-01 18:30:04 -070028import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.driver.AbstractHandlerBehaviour;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Carmelo Cascone3977ea42019-02-28 13:43:42 -080033import java.net.URI;
34import java.net.URISyntaxException;
Yi Tseng5f7fef52018-11-05 11:30:47 -080035import java.util.concurrent.CompletableFuture;
36import java.util.concurrent.ExecutionException;
37import java.util.concurrent.TimeUnit;
38import java.util.concurrent.TimeoutException;
39
Yi Tseng27851e32018-11-01 18:30:04 -070040/**
41 * Abstract implementation of a behaviour handler for a gNMI device.
42 */
43public class AbstractGnmiHandlerBehaviour extends AbstractHandlerBehaviour {
44
Yi Tseng5f7fef52018-11-05 11:30:47 -080045 // 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 Tseng27851e32018-11-01 18:30:04 -070049 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 Tseng27851e32018-11-01 18:30:04 -070057 deviceId = handler().data().deviceId();
Yi Tsengdbe3c7e2018-11-27 23:11:23 -080058 deviceService = handler().get(DeviceService.class);
Yi Tseng27851e32018-11-01 18:30:04 -070059 controller = handler().get(GnmiController.class);
60 client = controller.getClient(deviceId);
61
62 if (client == null) {
Yi Tseng5f7fef52018-11-05 11:30:47 -080063 log.warn("Unable to find client for {}, aborting operation", deviceId);
Yi Tseng27851e32018-11-01 18:30:04 -070064 return false;
65 }
66
67 return true;
68 }
69
Carmelo Cascone3977ea42019-02-28 13:43:42 -080070 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 Tseng27851e32018-11-01 18:30:04 -070079 deviceId = handler().data().deviceId();
Yi Tseng27851e32018-11-01 18:30:04 -070080
Carmelo Cascone3977ea42019-02-28 13:43:42 -080081 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 Tseng27851e32018-11-01 18:30:04 -070086 return null;
87 }
88
Yi Tseng27851e32018-11-01 18:30:04 -070089 try {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080090 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 Tseng27851e32018-11-01 18:30:04 -070095 return null;
96 }
Yi Tseng27851e32018-11-01 18:30:04 -070097 }
Yi Tseng5f7fef52018-11-05 11:30:47 -080098
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 Tseng27851e32018-11-01 18:30:04 -0700152}