oleksandr.yashchuk@plvision.eu | 3dbcaaf | 2019-03-13 14:44:46 +0200 | [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.drivers.gnoi; |
| 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import org.onosproject.gnoi.api.GnoiClient; |
| 21 | import org.onosproject.gnoi.api.GnoiClientKey; |
| 22 | import org.onosproject.gnoi.api.GnoiController; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.config.NetworkConfigService; |
| 26 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.net.URI; |
| 33 | import java.net.URISyntaxException; |
| 34 | |
| 35 | /** |
| 36 | * Abstract implementation of a behaviour handler for a gNOI device. |
| 37 | */ |
| 38 | public class AbstractGnoiHandlerBehaviour extends AbstractHandlerBehaviour { |
| 39 | |
| 40 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 41 | protected DeviceId deviceId; |
| 42 | protected DeviceService deviceService; |
| 43 | protected Device device; |
| 44 | protected GnoiController controller; |
| 45 | protected GnoiClient client; |
| 46 | |
| 47 | protected boolean setupBehaviour() { |
| 48 | deviceId = handler().data().deviceId(); |
| 49 | deviceService = handler().get(DeviceService.class); |
| 50 | controller = handler().get(GnoiController.class); |
| 51 | client = controller.getClient(deviceId); |
| 52 | |
| 53 | if (client == null) { |
| 54 | log.warn("Unable to find client for {}, aborting operation", deviceId); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | GnoiClient getClientByKey() { |
| 62 | final GnoiClientKey clientKey = clientKey(); |
| 63 | if (clientKey == null) { |
| 64 | return null; |
| 65 | } |
| 66 | return handler().get(GnoiController.class).getClient(clientKey); |
| 67 | } |
| 68 | |
| 69 | protected GnoiClientKey clientKey() { |
| 70 | deviceId = handler().data().deviceId(); |
| 71 | |
| 72 | final BasicDeviceConfig cfg = handler().get(NetworkConfigService.class) |
| 73 | .getConfig(deviceId, BasicDeviceConfig.class); |
| 74 | if (cfg == null || Strings.isNullOrEmpty(cfg.managementAddress())) { |
| 75 | log.error("Missing or invalid config for {}, cannot derive " + |
| 76 | "gNOI server endpoints", deviceId); |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | try { |
| 81 | return new GnoiClientKey( |
| 82 | deviceId, new URI(cfg.managementAddress())); |
| 83 | } catch (URISyntaxException e) { |
| 84 | log.error("Management address of {} is not a valid URI: {}", |
| 85 | deviceId, cfg.managementAddress()); |
| 86 | return null; |
| 87 | } |
| 88 | } |
| 89 | } |