networkOptional;
try (ReadOnlyTransaction rtx = this.dataBroker.newReadOnlyTransaction()) {
//TODO change to constant from Timeouts class when it will be merged.
networkOptional = rtx.read(LogicalDatastoreType.CONFIGURATION, networkIID).get(Timeouts.DATASTORE_READ,
TimeUnit.MILLISECONDS).toJavaUtil();
} catch (InterruptedException | ExecutionException | TimeoutException e) {
LOG.warn("Read of {} topology failed", NetworkUtils.OVERLAY_NETWORK_ID);
return Collections.emptyList();
}
if (! networkOptional.isPresent()) {
LOG.warn("Network augmentation with links data is not present in {} topology.",
NetworkUtils.OVERLAY_NETWORK_ID);
return Collections.emptyList();
}
List networkLinks = networkOptional.get().getLink();
if ((networkLinks == null) || networkLinks.isEmpty()) {
LOG.warn("Links are not present in {} topology.", NetworkUtils.OVERLAY_NETWORK_ID);
return Collections.emptyList();
}
return networkLinks;
}
/**
* This method retrieves OTS PM from current PM list by nodeId and TPId: Steps:
*
*
* 1. Get OTS interface name from port mapping by TPId 2. Call getPm RPC to get OTS PM
*
*
*
* @param nodeId Node-id of the NE.
* @param tpID Termination point Name.
* @param pmName PM name which need to be retrieved
* @return reference to OtsPmHolder
*/
private OtsPmHolder getPmMeasurements(String nodeId, String tpID, String pmName) {
String realNodeId = getRealNodeId(nodeId);
Mapping mapping = this.portMapping.getMapping(realNodeId, tpID);
if (mapping == null) {
return null;
}
GetPmInput getPmInput = new GetPmInputBuilder().setNodeId(realNodeId)
.setResourceType(ResourceTypeEnum.Interface).setGranularity(PmGranularity._15min)
.setResourceIdentifier(
new ResourceIdentifierBuilder().setResourceName(mapping.getSupportingOts()).build())
.build();
GetPmOutput otsPmOutput = getPm(getPmInput);
if (otsPmOutput == null) {
LOG.info("OTS PM not found for NodeId: {} TP Id:{} PMName:{}", realNodeId, tpID, pmName);
return null;
}
try {
for (Measurements measurement : otsPmOutput.getMeasurements()) {
if (pmName.equals(measurement.getPmparameterName())) {
return new OtsPmHolder(pmName, Double.parseDouble(measurement.getPmparameterValue()),
mapping.getSupportingOts());
}
}
} catch (NumberFormatException e) {
LOG.warn("Unable to get PM for NodeId: {} TP Id:{} PMName:{}", realNodeId, tpID, pmName, e);
}
return null;
}
/**
* This method Sets Spanloss on A-End and Z-End OTS interface: Steps:
*
*
* 1. Read existing interface details
*
*
* 2. Set spanloss
*
* @param nodeId nodeId of NE on which spanloss need to be updated
* @param interfaceName OTS interface for NE on which spanloss is cacluated
* @param spanLoss calculated spanloss value
* @param direction for which spanloss is calculated.It can be either Tx or Rx
* @return true/false
*/
private boolean setSpanLoss(String nodeId, String interfaceName, BigDecimal spanLoss, String direction) {
String realNodeId = getRealNodeId(nodeId);
LOG.info("Setting Spanloss in device for {}, InterfaceName: {}", realNodeId, interfaceName);
switch (direction) {
case "TX":
LOG.info("Setting 'span-loss-transmit' in device: {}, Interface: {}", realNodeId, interfaceName);
break;
case "RX":
LOG.info("Setting 'span-loss-receive' in device: {}, Interface: {}", realNodeId, interfaceName);
break;
default:
LOG.error("Impossible to set spanloss in device: {}, Interface: {}", realNodeId, interfaceName);
break;
}
InstanceIdentifier interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
.child(Interface.class, new InterfaceKey(interfaceName));
com.google.common.base.Optional interfaceObject;
try {
Future> deviceTxFuture =
this.deviceTransactionManager.getDeviceTransaction(realNodeId);
java.util.Optional deviceTxOpt = deviceTxFuture.get();
DeviceTransaction deviceTx;
if (deviceTxOpt.isPresent()) {
deviceTx = deviceTxOpt.get();
} else {
LOG.error("Device transaction for device {} was not found!", nodeId);
return false;
}
interfaceObject = deviceTx.read(LogicalDatastoreType.CONFIGURATION, interfacesIID).get();
BigDecimal initialSpanloss = new BigDecimal(0);
RatioDB spanLossRx = new RatioDB(initialSpanloss);
RatioDB spanLossTx = new RatioDB(initialSpanloss);
if (interfaceObject.isPresent()) {
Interface intf = interfaceObject.get();
InterfaceBuilder interfaceBuilder = new InterfaceBuilder(intf);
OtsBuilder otsBuilder = new OtsBuilder();
if ((intf.augmentation(Interface1.class) != null)
&& (intf.augmentation(Interface1.class).getOts() != null)) {
Ots ots = intf.augmentation(Interface1.class).getOts();
otsBuilder.setFiberType(ots.getFiberType());
spanLossRx = ots.getSpanLossReceive();
spanLossTx = ots.getSpanLossTransmit();
} else {
spanLossRx = new RatioDB(spanLoss);
spanLossTx = new RatioDB(spanLoss);
}
Interface1Builder intf1Builder = new Interface1Builder();
if (direction.equals("TX")) {
otsBuilder.setSpanLossTransmit(new RatioDB(spanLoss));
otsBuilder.setSpanLossReceive(spanLossRx);
} else {
otsBuilder.setSpanLossTransmit(spanLossTx).setSpanLossReceive(new RatioDB(spanLoss));
}
interfaceBuilder.addAugmentation(Interface1.class, intf1Builder.setOts(otsBuilder.build()).build());
deviceTx.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, interfaceBuilder.build());
ListenableFuture submit =
deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
submit.get();
LOG.info("Spanloss Value update completed successfully");
return true;
}
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Unable to set spanloss", e);
}
return false;
}
/**
* This method calculates Spanloss by TranmistPower - Receive Power Steps:
*
*
* 1. Read PM measurement
*
*
* 2. Set Spanloss value for interface
*
* @param roadmLinks
* reference to list of RoadmLinks
* @return map with list of spans with their spanloss value
*/
private Map getLinkSpanloss(List roadmLinks) {
Map map = new HashMap();
LOG.info("Executing GetLinkSpanLoss");
BigDecimal spanLoss = new BigDecimal(0);
for (Link link : roadmLinks) {
String sourceNodeId = link.getSource().getSourceNode().getValue();
String sourceTpId = link.getSource().getSourceTp().toString();
String destNodeId = link.getDestination().getDestNode().getValue();
String destTpId = link.getDestination().getDestTp().toString();
OtsPmHolder srcOtsPmHoler = getPmMeasurements(sourceNodeId, sourceTpId, "OpticalPowerOutput");
OtsPmHolder destOtsPmHoler = getPmMeasurements(destNodeId, destTpId, "OpticalPowerInput");
spanLoss = new BigDecimal(srcOtsPmHoler.getOtsParameterVal() - destOtsPmHoler.getOtsParameterVal())
.setScale(0, RoundingMode.HALF_UP);
LOG.info("Spanloss Calculated as :" + spanLoss + "=" + srcOtsPmHoler.getOtsParameterVal() + "-"
+ destOtsPmHoler.getOtsParameterVal());
if ((spanLoss.doubleValue() < 28) && (spanLoss.doubleValue() > 0)) {
if (!setSpanLoss(sourceNodeId, srcOtsPmHoler.getOtsInterfaceName(), spanLoss, "TX")) {
LOG.info("Setting spanLoss failed for " + sourceNodeId);
return null;
}
if (!setSpanLoss(destNodeId, destOtsPmHoler.getOtsInterfaceName(), spanLoss, "RX")) {
LOG.info("Setting spanLoss failed for " + destNodeId);
return null;
}
map.put(link.getLinkId(), spanLoss);
}
}
return map;
}
private String getRealNodeId(String mappedNodeId) {
KeyedInstanceIdentifier mappedNodeII =
InstanceIdentifiers.OVERLAY_NETWORK_II.child(Node.class, new NodeKey(new NodeId(mappedNodeId)));
com.google.common.base.Optional realNode;
try (ReadOnlyTransaction readOnlyTransaction = this.dataBroker.newReadOnlyTransaction()) {
realNode = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, mappedNodeII).get();
} catch (InterruptedException | ExecutionException e) {
LOG.error(e.getMessage(), e);
throw new IllegalStateException(e);
}
if (!realNode.isPresent() || (realNode.get().getSupportingNode() == null)) {
throw new IllegalArgumentException(
String.format("Could not find node %s, or supporting node is not present", mappedNodeId));
}
List collect = realNode.get().getSupportingNode().stream()
.filter(node -> (node.getNetworkRef() != null)
&& NetworkUtils.UNDERLAY_NETWORK_ID.equals(node.getNetworkRef().getValue())
&& (node.getNodeRef() != null) && !Strings.isNullOrEmpty(node.getNodeRef().getValue()))
.collect(Collectors.toList());
if (collect.isEmpty() || (collect.size() > 1)) {
throw new IllegalArgumentException(String.format("Invalid support node count [%d] was found for node %s",
collect.size(), mappedNodeId));
}
return collect.iterator().next().getNodeRef().getValue();
}
}