blob: c66dede96434a01018a1a4cb42def80863d2bc4e [file] [log] [blame]
Carmelo Casconec2be50a2019-04-10 00:15:39 -07001/*
2 * Copyright 2019-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.grpc.utils;
18
19import com.google.common.base.Strings;
20import com.google.common.collect.Maps;
21import org.onosproject.grpc.api.GrpcClient;
22import org.onosproject.grpc.api.GrpcClientController;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.config.NetworkConfigService;
25import org.onosproject.net.config.basics.BasicDeviceConfig;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.net.URI;
32import java.net.URISyntaxException;
33import java.util.concurrent.ConcurrentMap;
34
35/**
36 * Abstract implementation of HandlerBehaviour for gNMI-based devices.
37 *
38 * @param <CLIENT> gRPC client class
39 * @param <CTRL> gRPC controller class
40 */
41public class AbstractGrpcHandlerBehaviour
42 <CLIENT extends GrpcClient, CTRL extends GrpcClientController<CLIENT>>
43 extends AbstractHandlerBehaviour {
44
45 static final ConcurrentMap<DeviceId, URI> CHANNEL_URIS = Maps.newConcurrentMap();
46
47 protected final Logger log = LoggerFactory.getLogger(getClass());
48
49 final Class<CTRL> controllerClass;
50 protected DeviceId deviceId;
51 protected DeviceService deviceService;
52 protected CLIENT client;
53
54 public AbstractGrpcHandlerBehaviour(Class<CTRL> controllerClass) {
55 this.controllerClass = controllerClass;
56 }
57
58 protected boolean setupBehaviour(String opName) {
59 deviceId = handler().data().deviceId();
60 deviceService = handler().get(DeviceService.class);
61 client = getClientByNetcfg();
62 if (client == null) {
63 log.warn("Missing client for {}, aborting {}", deviceId, opName);
64 return false;
65 }
66
67 return true;
68 }
69
70 private CLIENT getClientByNetcfg() {
71 // Check if there's a channel for this device and if we created it with
72 // the same URI of that derived from the current netcfg. This makes sure
73 // we return null if the netcfg changed after we created the channel.
74 if (!CHANNEL_URIS.containsKey(data().deviceId()) ||
75 !CHANNEL_URIS.get(data().deviceId()).equals(mgmtUriFromNetcfg())) {
76 return null;
77 }
78 return handler().get(controllerClass).get(data().deviceId());
79 }
80
81 protected URI mgmtUriFromNetcfg() {
82 deviceId = handler().data().deviceId();
83
84 final BasicDeviceConfig cfg = handler().get(NetworkConfigService.class)
85 .getConfig(deviceId, BasicDeviceConfig.class);
86 if (cfg == null || Strings.isNullOrEmpty(cfg.managementAddress())) {
87 log.error("Missing or invalid config for {}, cannot derive " +
88 "gRPC server endpoints", deviceId);
89 return null;
90 }
91
92 try {
93 return new URI(cfg.managementAddress());
94 } catch (URISyntaxException e) {
95 log.error("Management address of {} is not a valid URI: {}",
96 deviceId, cfg.managementAddress());
97 return null;
98 }
99 }
100}