blob: 23181b4c09079e59cf82b2ff4c0eaafc907e2337 [file] [log] [blame]
oleksandr.yashchuk@plvision.eu3dbcaaf2019-03-13 14:44:46 +02001/*
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.drivers.gnoi;
18
19import com.google.common.base.Strings;
20import org.onosproject.gnoi.api.GnoiClient;
21import org.onosproject.gnoi.api.GnoiClientKey;
22import org.onosproject.gnoi.api.GnoiController;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.config.NetworkConfigService;
26import org.onosproject.net.config.basics.BasicDeviceConfig;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.net.URI;
33import java.net.URISyntaxException;
34
35/**
36 * Abstract implementation of a behaviour handler for a gNOI device.
37 */
38public 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}