Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.grpc.utils; |
| 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.grpc.api.GrpcClient; |
| 22 | import org.onosproject.grpc.api.GrpcClientController; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.config.NetworkConfigService; |
| 25 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.net.URI; |
| 32 | import java.net.URISyntaxException; |
| 33 | import 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 | */ |
| 41 | public 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 | } |