blob: e6d8b163cae4d380fc10f884ee4a0c9814e06777 [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
Yi Tseng5f7fef52018-11-05 11:30:47 -080019import io.grpc.StatusRuntimeException;
Yi Tseng27851e32018-11-01 18:30:04 -070020import org.onosproject.gnmi.api.GnmiClient;
21import org.onosproject.gnmi.api.GnmiClientKey;
22import org.onosproject.gnmi.api.GnmiController;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Yi Tseng5f7fef52018-11-05 11:30:47 -080030import java.util.concurrent.CompletableFuture;
31import java.util.concurrent.ExecutionException;
32import java.util.concurrent.TimeUnit;
33import java.util.concurrent.TimeoutException;
34
Yi Tseng27851e32018-11-01 18:30:04 -070035/**
36 * Abstract implementation of a behaviour handler for a gNMI device.
37 */
38public class AbstractGnmiHandlerBehaviour extends AbstractHandlerBehaviour {
39
Yi Tseng5f7fef52018-11-05 11:30:47 -080040 // 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 Tseng27851e32018-11-01 18:30:04 -070044 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 Tseng27851e32018-11-01 18:30:04 -070056 deviceId = handler().data().deviceId();
Yi Tsengdbe3c7e2018-11-27 23:11:23 -080057 deviceService = handler().get(DeviceService.class);
Yi Tseng27851e32018-11-01 18:30:04 -070058 controller = handler().get(GnmiController.class);
59 client = controller.getClient(deviceId);
60
61 if (client == null) {
Yi Tseng5f7fef52018-11-05 11:30:47 -080062 log.warn("Unable to find client for {}, aborting operation", deviceId);
Yi Tseng27851e32018-11-01 18:30:04 -070063 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 Tseng5f7fef52018-11-05 11:30:47 -080097
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 Tseng27851e32018-11-01 18:30:04 -0700151}