blob: 3bfca46f4474076589efbf0c09292af96d247e43 [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 Tsengd7716482018-10-31 15:34:30 -070044 private static final String GNMI_SERVER_ADDR_KEY = "gnmi_ip";
45 private static final String GNMI_SERVER_PORT_KEY = "gnmi_port";
Yi Tseng27851e32018-11-01 18:30:04 -070046
47 protected final Logger log = LoggerFactory.getLogger(getClass());
48 protected DeviceId deviceId;
49 protected DeviceService deviceService;
50 protected Device device;
51 protected GnmiController controller;
52 protected GnmiClient client;
53
54 protected boolean setupBehaviour() {
Yi Tseng27851e32018-11-01 18:30:04 -070055 deviceId = handler().data().deviceId();
Yi Tsengdbe3c7e2018-11-27 23:11:23 -080056 deviceService = handler().get(DeviceService.class);
Yi Tseng27851e32018-11-01 18:30:04 -070057 controller = handler().get(GnmiController.class);
58 client = controller.getClient(deviceId);
59
60 if (client == null) {
Yi Tseng5f7fef52018-11-05 11:30:47 -080061 log.warn("Unable to find client for {}, aborting operation", deviceId);
Yi Tseng27851e32018-11-01 18:30:04 -070062 return false;
63 }
64
65 return true;
66 }
67
Yi Tsengd7716482018-10-31 15:34:30 -070068 GnmiClient createClient() {
Yi Tseng27851e32018-11-01 18:30:04 -070069 deviceId = handler().data().deviceId();
70 controller = handler().get(GnmiController.class);
71
72 final String serverAddr = this.data().value(GNMI_SERVER_ADDR_KEY);
73 final String serverPortString = this.data().value(GNMI_SERVER_PORT_KEY);
74
75 if (serverAddr == null || serverPortString == null) {
Yi Tsengd7716482018-10-31 15:34:30 -070076 log.warn("Unable to create client for {}, missing driver data key (required is {} and {})",
Yi Tseng27851e32018-11-01 18:30:04 -070077 deviceId, GNMI_SERVER_ADDR_KEY, GNMI_SERVER_PORT_KEY);
78 return null;
79 }
80
81 final int serverPort;
82 try {
83 serverPort = Integer.parseUnsignedInt(serverPortString);
84 } catch (NumberFormatException e) {
Yi Tsengd7716482018-10-31 15:34:30 -070085 log.error("{} is not a valid port number", serverPortString);
Yi Tseng27851e32018-11-01 18:30:04 -070086 return null;
87 }
Yi Tsengd7716482018-10-31 15:34:30 -070088 GnmiClientKey clientKey = new GnmiClientKey(deviceId, serverAddr, serverPort);
Yi Tseng27851e32018-11-01 18:30:04 -070089 if (!controller.createClient(clientKey)) {
90 log.warn("Unable to create client for {}, aborting operation", deviceId);
91 return null;
92 }
93 return controller.getClient(deviceId);
94 }
Yi Tseng5f7fef52018-11-05 11:30:47 -080095
96 /**
97 * Returns the device request timeout driver property, or a default value
98 * if the property is not present or cannot be parsed.
99 *
100 * @return timeout value
101 */
102 private int getDeviceRequestTimeout() {
103 final String timeout = handler().driver()
104 .getProperty(DEVICE_REQ_TIMEOUT);
105 if (timeout == null) {
106 return DEFAULT_DEVICE_REQ_TIMEOUT;
107 } else {
108 try {
109 return Integer.parseInt(timeout);
110 } catch (NumberFormatException e) {
111 log.error("{} driver property '{}' is not a number, using default value {}",
112 DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT);
113 return DEFAULT_DEVICE_REQ_TIMEOUT;
114 }
115 }
116 }
117
118 /**
119 * Convenience method to get the result of a completable future while
120 * setting a timeout and checking for exceptions.
121 *
122 * @param future completable future
123 * @param opDescription operation description to use in log messages. Should
124 * be a sentence starting with a verb ending in -ing,
125 * e.g. "reading...", "writing...", etc.
126 * @param defaultValue value to return if operation fails
127 * @param <U> type of returned value
128 * @return future result or default value
129 */
130 <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription,
131 U defaultValue) {
132 try {
133 return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS);
134 } catch (InterruptedException e) {
135 log.error("Exception while {} on {}", opDescription, deviceId);
136 } catch (ExecutionException e) {
137 final Throwable cause = e.getCause();
138 if (cause instanceof StatusRuntimeException) {
139 final StatusRuntimeException grpcError = (StatusRuntimeException) cause;
140 log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage());
141 } else {
142 log.error("Exception while {} on {}", opDescription, deviceId, e.getCause());
143 }
144 } catch (TimeoutException e) {
145 log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId);
146 }
147 return defaultValue;
148 }
Yi Tseng27851e32018-11-01 18:30:04 -0700149}