Add gNMI-based ODTN driver
Change-Id: Ic1df82797ee9f60956f46f4c3431e8c74f4548b2
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceDiscovery.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceDiscovery.java
new file mode 100644
index 0000000..b64aaea
--- /dev/null
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceDiscovery.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.odtn.openconfig;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Streams;
+import gnmi.Gnmi;
+import org.onosproject.drivers.gnmi.OpenConfigGnmiDeviceDescriptionDiscovery;
+import org.onosproject.gnmi.api.GnmiUtils.GnmiPathBuilder;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.OduSignalType;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.optical.device.OchPortHelper;
+import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.onosproject.gnmi.api.GnmiUtils.pathToString;
+
+/**
+ * A ODTN device discovery behaviour based on gNMI and OpenConfig model.
+ *
+ * This behavior is based on the origin gNMI OpenConfig device description discovery
+ * with additional logic to discover optical ports for this device.
+ *
+ * To find all optical port name and info, it queries all components with path:
+ * /components/component[name=*]
+ * And it uses components with type "OPTICAL_CHANNEL" to find optical ports
+ *
+ */
+public class GnmiTerminalDeviceDiscovery
+ extends OpenConfigGnmiDeviceDescriptionDiscovery
+ implements OdtnDeviceDescriptionDiscovery {
+
+ private static final Logger log = LoggerFactory.getLogger(GnmiTerminalDeviceDiscovery.class);
+ private static final String COMPONENT_TYPE_PATH_TEMPLATE =
+ "/components/component[name=%s]/state/type";
+ private static final String LINE_PORT_PATH_TEMPLATE =
+ "/components/component[name=%s]/optical-channel/config/line-port";
+
+ @Override
+ public DeviceDescription discoverDeviceDetails() {
+ return new DefaultDeviceDescription(super.discoverDeviceDetails(),
+ Device.Type.TERMINAL_DEVICE);
+ }
+
+ @Override
+ public List<PortDescription> discoverPortDetails() {
+ if (!setupBehaviour("discoverPortDetails()")) {
+ return Collections.emptyList();
+ }
+
+ // Get all components
+ Gnmi.Path path = GnmiPathBuilder.newBuilder()
+ .addElem("components")
+ .addElem("component").withKeyValue("name", "*")
+ .build();
+
+ Gnmi.GetRequest req = Gnmi.GetRequest.newBuilder()
+ .addPath(path)
+ .setEncoding(Gnmi.Encoding.PROTO)
+ .build();
+ Gnmi.GetResponse resp;
+ try {
+ resp = client.get(req).get();
+ } catch (ExecutionException | InterruptedException e) {
+ log.warn("unable to get components via gNMI: {}", e.getMessage());
+ return Collections.emptyList();
+ }
+
+ Multimap<String, Gnmi.Update> componentUpdates = HashMultimap.create();
+ resp.getNotificationList().stream()
+ .map(Gnmi.Notification::getUpdateList)
+ .flatMap(List::stream)
+ .forEach(u -> {
+ // Get component name
+ // /components/component[name=?]
+ Gnmi.Path p = u.getPath();
+ if (p.getElemCount() < 2) {
+ // Invalid path
+ return;
+ }
+ String name = p.getElem(1)
+ .getKeyOrDefault("name", null);
+
+ // Collect gNMI updates for the component.
+ // name -> a set of gNMI updates
+ if (name != null) {
+ componentUpdates.put(name, u);
+ }
+ });
+
+ Stream<PortDescription> normalPorts = super.discoverPortDetails().stream();
+ Stream<PortDescription> opticalPorts = componentUpdates.keySet().stream()
+ .map(name -> convertComponentToOdtnPortDesc(name, componentUpdates.get(name)))
+ .filter(Objects::nonNull);
+ return Streams.concat(normalPorts, opticalPorts)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Converts gNMI updates to ODTN port description.
+ *
+ * Paths we expected per optical port component:
+ * /components/component/state/type
+ * /components/component/optical-channel/config/line-port
+ *
+ * @param name component name
+ * @param updates gNMI updates
+ * @return port description, null if it is not a valid component config/state
+ */
+ private PortDescription
+ convertComponentToOdtnPortDesc(String name, Collection<Gnmi.Update> updates) {
+ Map<String, Gnmi.TypedValue> pathValue = Maps.newHashMap();
+ updates.forEach(u -> pathValue.put(pathToString(u.getPath()), u.getVal()));
+
+ String componentTypePathStr =
+ String.format(COMPONENT_TYPE_PATH_TEMPLATE, name);
+ Gnmi.TypedValue componentType =
+ pathValue.get(componentTypePathStr);
+
+ if (componentType == null ||
+ !componentType.getStringVal().equals("OPTICAL_CHANNEL")) {
+ // Ignore the component which is not a optical channel type.
+ return null;
+ }
+
+ Map<String, String> annotations = Maps.newHashMap();
+ annotations.put(OC_NAME, name);
+ annotations.put(OC_TYPE, componentType.getStringVal());
+
+ String linePortPathStr =
+ String.format(LINE_PORT_PATH_TEMPLATE, name);
+ Gnmi.TypedValue linePort = pathValue.get(linePortPathStr);
+
+ // Invalid optical port
+ if (linePort == null) {
+ return null;
+ }
+
+ // According to CassiniTerminalDevice class, we expected to received a string with
+ // this format: port-[port id].
+ // And we use "port id" from the string as the port number.
+ // However, if we can't get port id from line port value, we will use
+ // hash number of the port name. (According to TerminalDeviceDiscovery class)
+ String linePortString = linePort.getStringVal();
+ long portId = name.hashCode();
+ if (linePortString.contains("-") && !linePortString.endsWith("-")) {
+ try {
+ portId = Long.parseUnsignedLong(linePortString.split("-")[1]);
+ } catch (NumberFormatException e) {
+ log.warn("Invalid line port string: {}, use {}", linePortString, portId);
+ }
+ }
+
+ annotations.put(AnnotationKeys.PORT_NAME, linePortString);
+ annotations.putIfAbsent(PORT_TYPE,
+ OdtnDeviceDescriptionDiscovery.OdtnPortType.LINE.value());
+ annotations.putIfAbsent(ONOS_PORT_INDEX, Long.toString(portId));
+ annotations.putIfAbsent(CONNECTION_ID, "connection-" + portId);
+
+ OchSignal signalId = OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1);
+ return OchPortHelper.ochPortDescription(
+ PortNumber.portNumber(portId, name),
+ true,
+ OduSignalType.ODU4, // TODO: discover type via gNMI if possible
+ true,
+ signalId,
+ DefaultAnnotations.builder().putAll(annotations).build());
+ }
+}
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceFlowRuleProgrammable.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceFlowRuleProgrammable.java
new file mode 100644
index 0000000..7d9cae5
--- /dev/null
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceFlowRuleProgrammable.java
@@ -0,0 +1,193 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.odtn.openconfig;
+
+import com.google.common.collect.ImmutableList;
+import gnmi.Gnmi;
+import org.onlab.util.Frequency;
+import org.onosproject.drivers.odtn.impl.DeviceConnectionCache;
+import org.onosproject.drivers.odtn.impl.FlowRuleParser;
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.gnmi.api.GnmiUtils.GnmiPathBuilder;
+import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleProgrammable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME;
+
+/**
+ * A FlowRuleProgrammable behavior which converts flow rules to gNMI calls that sets
+ * frequency for the optical component.
+ */
+public class GnmiTerminalDeviceFlowRuleProgrammable
+ extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
+ implements FlowRuleProgrammable {
+
+ private static final Logger log =
+ LoggerFactory.getLogger(GnmiTerminalDeviceFlowRuleProgrammable.class);
+
+ public GnmiTerminalDeviceFlowRuleProgrammable() {
+ super(GnmiController.class);
+ }
+
+ @Override
+ public Collection<FlowEntry> getFlowEntries() {
+ // TODO: currently, we store flow rules in a cluster store. Should check if rule/config exists via gNMI.
+ if (!setupBehaviour("getFlowEntries")) {
+ return Collections.emptyList();
+ }
+ DeviceConnectionCache cache = getConnectionCache();
+ Set<FlowRule> cachedRules = cache.get(deviceId);
+ if (cachedRules == null) {
+ return ImmutableList.of();
+ }
+
+ return cachedRules.stream()
+ .filter(Objects::nonNull)
+ .map(r -> new DefaultFlowEntry(r, FlowEntry.FlowEntryState.ADDED, 0, 0, 0))
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
+ if (!setupBehaviour("applyFlowRules")) {
+ return Collections.emptyList();
+ }
+ List<FlowRule> added = new ArrayList<>();
+ for (FlowRule r : rules) {
+ String connectionId = applyFlowRule(r);
+ if (connectionId != null) {
+ getConnectionCache().add(deviceId, connectionId, r);
+ added.add(r);
+ }
+ }
+ return added;
+ }
+
+ @Override
+ public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
+ if (!setupBehaviour("removeFlowRules")) {
+ return Collections.emptyList();
+ }
+ List<FlowRule> removed = new ArrayList<>();
+ for (FlowRule r : rules) {
+ String connectionId = removeFlowRule(r);
+ if (connectionId != null) {
+ getConnectionCache().remove(deviceId, connectionId);
+ removed.add(r);
+ }
+ }
+ return removed;
+ }
+
+ private String applyFlowRule(FlowRule r) {
+ FlowRuleParser frp = new FlowRuleParser(r);
+ if (!frp.isReceiver()) {
+ String opticalPortName = getOpticalPortName(frp.getPortNumber());
+ if (opticalPortName == null) {
+ log.warn("[Apply] No optical port name found from port {}, skipped",
+ frp.getPortNumber());
+ return null;
+ }
+ if (!setOpticalPortFrequency(opticalPortName, frp.getCentralFrequency())) {
+ // Already logged in setOpticalChannelFrequency function
+ return null;
+ }
+ return opticalPortName + ":" + frp.getCentralFrequency().asGHz();
+ }
+ return String.valueOf(frp.getCentralFrequency().asGHz());
+
+ }
+
+ private String removeFlowRule(FlowRule r) {
+ FlowRuleParser frp = new FlowRuleParser(r);
+ if (!frp.isReceiver()) {
+ String opticalPortName = getOpticalPortName(frp.getPortNumber());
+ if (opticalPortName == null) {
+ log.warn("[Remove] No optical port name found from port {}, skipped",
+ frp.getPortNumber());
+ return null;
+ }
+ if (!setOpticalPortFrequency(opticalPortName, Frequency.ofMHz(0))) {
+ // Already logged in setOpticalChannelFrequency function
+ return null;
+ }
+ return opticalPortName + ":" + frp.getCentralFrequency().asGHz();
+ }
+ return String.valueOf(frp.getCentralFrequency().asGHz());
+ }
+
+ private boolean setOpticalPortFrequency(String opticalPortName, Frequency freq) {
+ // gNMI set
+ // /components/component[name=opticalPortName]/optical-channel/config/frequency
+ Gnmi.Path path = GnmiPathBuilder.newBuilder()
+ .addElem("components")
+ .addElem("component").withKeyValue("name", opticalPortName)
+ .addElem("optical-channel")
+ .addElem("config")
+ .addElem("frequency")
+ .build();
+ Gnmi.TypedValue val = Gnmi.TypedValue.newBuilder()
+ .setUintVal((long) freq.asMHz())
+ .build();
+ Gnmi.Update update = Gnmi.Update.newBuilder()
+ .setPath(path)
+ .setVal(val)
+ .build();
+ Gnmi.SetRequest req = Gnmi.SetRequest.newBuilder()
+ .addUpdate(update)
+ .build();
+ try {
+ client.set(req).get();
+ return true;
+ } catch (ExecutionException | InterruptedException e) {
+ log.warn("Got exception when performing gNMI set operation: {}", e.getMessage());
+ log.warn("{}", req);
+ }
+ return false;
+ }
+
+ private String getOpticalPortName(PortNumber portNumber) {
+ Port clientPort = handler().get(DeviceService.class).getPort(deviceId, portNumber);
+ if (clientPort == null) {
+ log.warn("Unable to get port from device {}, port {}", deviceId, portNumber);
+ return null;
+ }
+ return clientPort.annotations().value(OC_NAME);
+ }
+
+ private DeviceConnectionCache getConnectionCache() {
+ return DeviceConnectionCache.init();
+ }
+}
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceModulationConfig.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceModulationConfig.java
new file mode 100644
index 0000000..5911293
--- /dev/null
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDeviceModulationConfig.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.odtn.openconfig;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.ImmutableBiMap;
+import gnmi.Gnmi;
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.gnmi.api.GnmiUtils.GnmiPathBuilder;
+import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
+import org.onosproject.net.ModulationScheme;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.ModulationConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * Modulation Config behavior for gNMI and OpenConfig model based device.
+ */
+public class GnmiTerminalDeviceModulationConfig<T>
+ extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
+ implements ModulationConfig<T> {
+
+ public static Logger log = LoggerFactory.getLogger(GnmiTerminalDeviceModulationConfig.class);
+
+ private static final BiMap<Long, ModulationScheme> OPERATIONAL_MODE_TO_MODULATION =
+ ImmutableBiMap.<Long, ModulationScheme>builder()
+ .put(1L, ModulationScheme.DP_QPSK)
+ .put(2L, ModulationScheme.DP_16QAM)
+ .put(3L, ModulationScheme.DP_8QAM)
+ .build();
+
+ public GnmiTerminalDeviceModulationConfig() {
+ super(GnmiController.class);
+ }
+
+ @Override
+ public Optional<ModulationScheme> getModulationScheme(PortNumber portNumber, T component) {
+ if (!setupBehaviour("getModulationScheme")) {
+ return Optional.empty();
+ }
+ // Get value from path
+ // /components/component[name=]/optical-channel/state/operational-mode
+ // And convert operational mode (uint64 bit mask) to ModulationScheme enum
+
+ // First we need to find component name (from port annotation)
+ String ocName = getOcName(portNumber);
+
+ // Query operational mode from device
+ Gnmi.Path path = GnmiPathBuilder.newBuilder()
+ .addElem("components")
+ .addElem("component").withKeyValue("name", ocName)
+ .addElem("optical-channel")
+ .addElem("state")
+ .addElem("operational-mode")
+ .build();
+
+ Gnmi.GetRequest req = Gnmi.GetRequest.newBuilder()
+ .addPath(path)
+ .setEncoding(Gnmi.Encoding.PROTO)
+ .build();
+
+ Gnmi.GetResponse resp;
+ try {
+ resp = client.get(req).get();
+ } catch (ExecutionException | InterruptedException e) {
+ log.warn("Unable to get operational mode from device {}, port {}: {}",
+ deviceId, portNumber, e.getMessage());
+ return Optional.empty();
+ }
+
+ // Get operational mode value from gNMI get response
+ // Here we assume we get only one response
+ if (resp.getNotificationCount() == 0 || resp.getNotification(0).getUpdateCount() == 0) {
+ log.warn("No update message found");
+ return Optional.empty();
+ }
+
+ Gnmi.Update update = resp.getNotification(0).getUpdate(0);
+ Gnmi.TypedValue operationalModeVal = update.getVal();
+
+ if (operationalModeVal == null) {
+ log.warn("No operational mode found");
+ return Optional.empty();
+ }
+
+ return Optional.ofNullable(
+ OPERATIONAL_MODE_TO_MODULATION.getOrDefault(operationalModeVal.getUintVal(), null));
+ }
+
+ @Override
+ public void setModulationScheme(PortNumber portNumber, T component, long bitRate) {
+ if (!setupBehaviour("getModulationScheme")) {
+ return;
+ }
+ // Sets value to path
+ // /components/component[name]/optical-channel/config/operational-mode
+
+ // First we convert bit rate to modulation scheme to operational mode
+ ModulationScheme modulationScheme = ModulationScheme.DP_16QAM;
+ // Use DP_QPSK if bit rate is less or equals to 100 Gbps
+ if (bitRate <= 100) {
+ modulationScheme = ModulationScheme.DP_QPSK;
+ }
+ long operationalMode = OPERATIONAL_MODE_TO_MODULATION.inverse().get(modulationScheme);
+
+ // Build gNMI set request
+ String ocName = getOcName(portNumber);
+ Gnmi.Path path = GnmiPathBuilder.newBuilder()
+ .addElem("components")
+ .addElem("component").withKeyValue("name", ocName)
+ .addElem("optical-channel")
+ .addElem("config")
+ .addElem("operational-mode")
+ .build();
+
+ Gnmi.TypedValue val = Gnmi.TypedValue.newBuilder()
+ .setUintVal(operationalMode)
+ .build();
+
+ Gnmi.Update update = Gnmi.Update.newBuilder()
+ .setPath(path)
+ .setVal(val)
+ .build();
+
+ Gnmi.SetRequest req = Gnmi.SetRequest.newBuilder()
+ .addUpdate(update)
+ .build();
+
+ try {
+ client.set(req).get();
+ } catch (ExecutionException | InterruptedException e) {
+ log.warn("Unable to set operational mode to device {}, port {}, mode: {}: {}",
+ deviceId, portNumber, operationalMode, e.getMessage());
+ }
+ }
+
+ private String getOcName(PortNumber portNumber) {
+ return deviceService.getPort(deviceId, portNumber).annotations().value("oc-name");
+ }
+}
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDevicePowerConfig.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDevicePowerConfig.java
new file mode 100644
index 0000000..792b46c
--- /dev/null
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/openconfig/GnmiTerminalDevicePowerConfig.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * This work was partially supported by EC H2020 project METRO-HAUL (761727).
+ */
+
+package org.onosproject.drivers.odtn.openconfig;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Range;
+import gnmi.Gnmi;
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.gnmi.api.GnmiUtils.GnmiPathBuilder;
+import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.PowerConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * PowerConfig behaviour for gNMI and OpenConfig model based device.
+ */
+public class GnmiTerminalDevicePowerConfig<T>
+ extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
+ implements PowerConfig<T> {
+
+ private static final Logger log = LoggerFactory.getLogger(GnmiTerminalDevicePowerConfig.class);
+ private static final int DEFAULT_OC_POWER_PRECISION = 2;
+ private static final Collection<Port.Type> OPTICAL_TYPES = ImmutableSet.of(Port.Type.FIBER,
+ Port.Type.PACKET,
+ Port.Type.ODUCLT,
+ Port.Type.OCH,
+ Port.Type.OMS,
+ Port.Type.OTU);
+
+ public GnmiTerminalDevicePowerConfig() {
+ super(GnmiController.class);
+ }
+
+ @Override
+ public Optional<Double> getTargetPower(PortNumber port, T component) {
+ if (!setupBehaviour("getTargetPower")) {
+ return Optional.empty();
+ }
+ if (!isOpticalPort(port)) {
+ return Optional.empty();
+ }
+ // path: /components/component[name=<name>]/optical-channel/config/target-output-power
+ return getValueFromPath(getOcName(port), "config/target-output-power");
+ }
+
+ @Override
+ public void setTargetPower(PortNumber port, T component, double power) {
+ if (!setupBehaviour("setTargetPower")) {
+ return;
+ }
+ if (!isOpticalPort(port)) {
+ return;
+ }
+ setValueToPath(getOcName(port), "config/target-output-power", power);
+ }
+
+ @Override
+ public Optional<Double> currentPower(PortNumber port, T component) {
+ if (!setupBehaviour("currentPower")) {
+ return Optional.empty();
+ }
+ if (!isOpticalPort(port)) {
+ return Optional.empty();
+ }
+ // path: /components/component[name=<name>]/optical-channel/state/output-power/instant
+ return getValueFromPath(getOcName(port), "state/output-power/instant");
+ }
+
+ @Override
+ public Optional<Double> currentInputPower(PortNumber port, T component) {
+ if (!setupBehaviour("currentInputPower")) {
+ return Optional.empty();
+ }
+ if (!isOpticalPort(port)) {
+ return Optional.empty();
+ }
+ // path: /components/component[name=<name>]/optical-channel/state/input-power/instant
+ return getValueFromPath(getOcName(port), "state/input-power/instant");
+ }
+
+ @Override
+ public Optional<Range<Double>> getTargetPowerRange(PortNumber port, Object component) {
+ if (!isOpticalPort(port)) {
+ return Optional.empty();
+ }
+
+ // From CassiniTerminalDevicePowerConfig
+ double targetMin = -30;
+ double targetMax = 1;
+ return Optional.of(Range.open(targetMin, targetMax));
+ }
+
+ @Override
+ public Optional<Range<Double>> getInputPowerRange(PortNumber port, Object component) {
+ if (!isOpticalPort(port)) {
+ return Optional.empty();
+ }
+
+ // From CassiniTerminalDevicePowerConfig
+ double targetMin = -30;
+ double targetMax = 1;
+ return Optional.of(Range.open(targetMin, targetMax));
+ }
+
+ private String getOcName(PortNumber portNumber) {
+ if (!setupBehaviour("getOcName")) {
+ return null;
+ }
+ return deviceService.getPort(deviceId, portNumber).annotations().value("oc-name");
+ }
+
+ private boolean isOpticalPort(PortNumber portNumber) {
+ if (!setupBehaviour("isOpticalPort")) {
+ return false;
+ }
+ return OPTICAL_TYPES.contains(deviceService.getPort(deviceId, portNumber).type());
+ }
+
+ private Optional<Double> getValueFromPath(String ocName, String subPath) {
+ Gnmi.GetRequest req = Gnmi.GetRequest.newBuilder()
+ .addPath(buildPathWithSubPath(ocName, subPath))
+ .setEncoding(Gnmi.Encoding.PROTO)
+ .build();
+ try {
+ Gnmi.GetResponse resp = client.get(req).get();
+ // Here we assume we have only one response
+ if (resp.getNotificationCount() == 0 || resp.getNotification(0).getUpdateCount() == 0) {
+ log.warn("Empty response for sub-path {}, component {}", subPath, ocName);
+ return Optional.empty();
+ }
+ Gnmi.Update update = resp.getNotification(0).getUpdate(0);
+ Gnmi.Decimal64 value = update.getVal().getDecimalVal();
+ return Optional.of(decimal64ToDouble(value));
+ } catch (ExecutionException | InterruptedException e) {
+ log.warn("Unable to get value from optical sub-path {} for component {}: {}",
+ subPath, ocName, e.getMessage());
+ return Optional.empty();
+ }
+ }
+
+ private void setValueToPath(String ocName, String subPath, Double value) {
+ Gnmi.TypedValue val = Gnmi.TypedValue.newBuilder()
+ .setDecimalVal(doubleToDecimal64(value, DEFAULT_OC_POWER_PRECISION))
+ .build();
+ Gnmi.Update update = Gnmi.Update.newBuilder()
+ .setPath(buildPathWithSubPath(ocName, subPath))
+ .setVal(val)
+ .build();
+ Gnmi.SetRequest req = Gnmi.SetRequest.newBuilder()
+ .addUpdate(update)
+ .build();
+ try {
+ client.set(req).get();
+ } catch (ExecutionException | InterruptedException e) {
+ log.warn("Unable to set optical sub-path {}, component {}, value {}: {}",
+ subPath, ocName, value, e.getMessage());
+ }
+ }
+
+ private Gnmi.Path buildPathWithSubPath(String ocName, String subPath) {
+ String[] elems = subPath.split("/");
+ GnmiPathBuilder pathBuilder = GnmiPathBuilder.newBuilder()
+ .addElem("components")
+ .addElem("component").withKeyValue("name", ocName)
+ .addElem("optical-channel");
+ for (String elem : elems) {
+ pathBuilder.addElem(elem);
+ }
+ return pathBuilder.build();
+ }
+
+ private Double decimal64ToDouble(Gnmi.Decimal64 value) {
+ double result = value.getDigits();
+ if (value.getPrecision() != 0) {
+ result = result / Math.pow(10, value.getPrecision());
+ }
+ return result;
+ }
+
+ private Gnmi.Decimal64 doubleToDecimal64(Double value, int precision) {
+ return Gnmi.Decimal64.newBuilder()
+ .setDigits((long) (value * Math.pow(10, precision)))
+ .setPrecision(precision)
+ .build();
+ }
+}
\ No newline at end of file
diff --git a/drivers/odtn-driver/src/main/resources/odtn-drivers.xml b/drivers/odtn-driver/src/main/resources/odtn-drivers.xml
index dfc418a..a9e110f 100644
--- a/drivers/odtn-driver/src/main/resources/odtn-drivers.xml
+++ b/drivers/odtn-driver/src/main/resources/odtn-drivers.xml
@@ -227,5 +227,20 @@
<behaviour api="org.onosproject.net.behaviour.ModulationConfig"
impl="org.onosproject.drivers.odtn.CassiniModulationOpenConfig"/>
</driver>
+
+ <driver name="gnmi-openconfig-terminal-device" manufacturer="OpenConfig" hwVersion="Unknown" swVersion="gNMI">
+ <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
+ impl="org.onosproject.drivers.odtn.openconfig.GnmiTerminalDeviceDiscovery"/>
+ <behaviour api="org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery"
+ impl="org.onosproject.drivers.odtn.openconfig.GnmiTerminalDeviceDiscovery"/>
+ <behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
+ impl="org.onosproject.drivers.odtn.openconfig.GnmiTerminalDeviceFlowRuleProgrammable"/>
+ <behaviour api="org.onosproject.net.behaviour.PowerConfig"
+ impl="org.onosproject.drivers.odtn.openconfig.GnmiTerminalDevicePowerConfig" />
+ <behaviour api="org.onosproject.net.behaviour.ModulationConfig"
+ impl="org.onosproject.drivers.odtn.openconfig.GnmiTerminalDeviceModulationConfig" />
+ <behaviour api="org.onosproject.net.device.DeviceHandshaker"
+ impl="org.onosproject.drivers.gnmi.GnmiHandshakerStandalone" />
+ </driver>
</drivers>