Skip to content

Commit

Permalink
working version: set and get freq using gnmi from ONOS command line
Browse files Browse the repository at this point in the history
  • Loading branch information
juraul1 committed Apr 1, 2022
1 parent 4b90944 commit 8bfb6e6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,38 @@ public class FrequencyConfigCommand extends AbstractShellCommand {
@Completion(GnmiOperationCompleter.class)
private String operation = null;

@Argument(index = 1, name = "port name", description = "Port name",
@Argument(index = 1, name = "connection point", description = "{DeviceID}/{PortNumber}",
required = true, multiValued = false)
@Completion(OpticalConnectPointCompleter.class)
private String connectPoint = null;

@Argument(index = 2, name = "port name", description = "Port name",
required = true, multiValued = false)
private String portName = null;

@Argument(index = 2, name = "value", description = "frequency value. Unit: GHz",
@Argument(index = 3, name = "value", description = "frequency value. Unit: GHz",
required = false, multiValued = false)
private Double value = null;

@Override
protected void doExecute() throws Exception {
FrequencyConfig frequencyConfig = get(FrequencyConfig.class);
DeviceService deviceService = get(DeviceService.class);
ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPoint);
//Port port = deviceService.getPort(cp);
//if (port == null) {
// print("[WARNING] %s does not exist", cp);
//}
Device device = deviceService.getDevice(cp.deviceId());
FrequencyConfig frequencyConfig = device.as(FrequencyConfig.class);
if (operation.equals("get")) {
Optional<Double> val = frequencyConfig.getSfpFrequency(portName);
if (val.isPresent()) {
print("The frequency value in port %s is %f.",
long val = frequencyConfig.getSfpFrequency(portName);
if (val != 0) {
//double frequency = val.get();
print("The frequency value in port %s is %d.",
portName, val);
} else {
print("No value for frequency in port %s",
portName);
}
} else if (operation.equals("set")) {
checkNotNull(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public interface FrequencyConfig extends HandlerBehaviour {
* @param portName the port name
* @return frequency in Hz
*/
Optional<Double> getSfpFrequency(String portName);
long getSfpFrequency(String portName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.lang.*;
import java.io.*;


/**
Expand All @@ -59,6 +60,9 @@ public GnmiTofinoTerminalDeviceFrequencyConfig() {
public boolean setSfpFrequency(String portName, Double freq) {
// gNMI set
// /interfaces/interface[name=portName]/config/sfp-frequency
if (!setupBehaviour("setSfpFrequency")) {
return false;
}
Gnmi.Path path = GnmiPathBuilder.newBuilder()
.addElem("interfaces")
.addElem("interface").withKeyValue("name", portName)
Expand Down Expand Up @@ -86,17 +90,19 @@ public boolean setSfpFrequency(String portName, Double freq) {
}

// @Override
public Optional<Double> getSfpFrequency(String portName) {
public long getSfpFrequency(String portName) {
// Get value from path
// /interfaces/interface[name=portName]/config/sfp-frequency

if (!setupBehaviour("getSfpFrequency")) {
return 0;
}
// Query operational mode from device
Gnmi.Path path = GnmiPathBuilder.newBuilder()
.addElem("interfaces")
.addElem("interface").withKeyValue("name", portName)
.addElem("config")
.addElem("sfp-frequency")
.build();
.addElem("sfp-frequency")
.build();
Gnmi.GetRequest req = Gnmi.GetRequest.newBuilder()
.addPath(path)
.setEncoding(Gnmi.Encoding.PROTO)
Expand All @@ -107,27 +113,32 @@ public Optional<Double> getSfpFrequency(String portName) {
} catch (ExecutionException | InterruptedException e) {
log.warn("Unable to get frequency for port {}",
portName);
return Optional.empty();
return 0;
}
// Get operational mode value from gNMI get response
// Get frequency 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();
return 0;
}
Gnmi.Update update = resp.getNotification(0).getUpdate(0);
//Gnmi.TypedValue frequencyVal = update.getVal();
Gnmi.Decimal64 frequencyVal = update.getVal().getDecimalVal();
//if (frequencyVal == null) {
// log.warn("No frequency set or not a tunable transceiver");
// return Optional.empty();
//}
//return Optional.of(frequencyVal);
return Optional.of(decimal64ToDouble(frequencyVal));
Gnmi.TypedValue frequencyVal = update.getVal();
//Gnmi.uint64 frequencyVal = update.getVal().GetUintVal();
//Gnmi.Decimal64 frequencyVal = update.getVal().getDecimalVal();
if (frequencyVal == null) {
log.warn("No frequency set or not a tunable transceiver");
return 0;
}
long frequency = frequencyVal.getUintVal();
return frequency;
//return Optional.of(frequencyVal.getUintVal());
//return Optional.of(decimal64ToDouble(frequencyVal));
}

private Double decimal64ToDouble(Gnmi.Decimal64 value) {
System.out.println("decimal64 value: " + value);
double result = value.getDigits();
System.out.printf("double result: %f",result);
if (value.getPrecision() != 0) {
result = result / Math.pow(10, value.getPrecision());
}
Expand Down

0 comments on commit 8bfb6e6

Please sign in to comment.