2 * Copyright © 2016 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.networkmodel;
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.transportpce.common.Timeouts;
17 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
18 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
19 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
20 import org.opendaylight.transportpce.networkmodel.util.OpenRoadmTopology;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev170929.Direction;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Degree;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.networkutils.rev170818.InitRoadmNodesInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.Network;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.NodesKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.CpToDegree;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.CpToDegreeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class R2RLinkDiscovery {
45 private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
47 private final DataBroker dataBroker;
48 private final DeviceTransactionManager deviceTransactionManager;
49 private final OpenRoadmTopology openRoadmTopology;
50 private final OpenRoadmInterfaces openRoadmInterfaces;
52 public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
53 OpenRoadmTopology openRoadmTopology, OpenRoadmInterfaces openRoadmInterfaces) {
54 this.dataBroker = dataBroker;
55 this.deviceTransactionManager = deviceTransactionManager;
56 this.openRoadmTopology = openRoadmTopology;
57 this.openRoadmInterfaces = openRoadmInterfaces;
60 public boolean readLLDP(NodeId nodeId) {
61 InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
62 .child(Protocols.class);
63 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
64 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
65 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
66 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
67 LOG.warn("LLDP subtree is missing : isolated openroadm device");
70 NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
71 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
72 for (IfName ifName : nbrList.getIfName()) {
73 if (ifName.getRemoteSysName() == null) {
74 LOG.error("LLDP subtree is empty in the device for nodeId: {}", nodeId.getValue());
77 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName.getRemoteSysName());
78 if (!mps.isPresent()) {
79 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
80 // The controller raises a warning rather than an error because the first node to
81 // mount cannot see its neighbors yet. The link will be detected when processing
84 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(), ifName.getRemotePortId())) {
85 LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName.getRemoteSysName());
93 public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
94 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Degree.class,
95 new DegreeKey(degreeCounter));
96 Optional<Degree> degreeObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
97 LogicalDatastoreType.OPERATIONAL, deviceIID, Timeouts.DEVICE_READ_TIMEOUT,
98 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
99 if (degreeObject.isPresent()) {
100 Integer connectionPortCount = degreeObject.get().getConnectionPorts().size();
101 if (connectionPortCount == 1) {
102 return Direction.Bidirectional;
103 } else if (connectionPortCount > 1) {
106 return Direction.NotApplicable;
109 LOG.error("Couldnt retrieve Degree object for nodeId: {} and DegreeNumbner: {}", nodeId.getValue(),
111 return Direction.NotApplicable;
115 public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
116 String remoteInterfaceName) {
117 String srcTpTx = null;
118 String srcTpRx = null;
119 String destTpTx = null;
120 String destTpRx = null;
121 // Find which degree is associated with ethernet interface
122 // portmapping.getDegFromCP(nodeId,interfaceName);
123 Integer srcDegId = getDegFromCP(nodeId, interfaceName);
124 if (srcDegId == null) {
125 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
128 // Check whether degree is Unidirectional or Bidirectional by counting
130 // circuit-packs under degree subtree
131 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
132 if (Direction.NotApplicable == sourceDirection) {
133 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
135 } else if (Direction.Bidirectional == sourceDirection) {
136 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
137 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
139 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
140 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
142 // Find degree for which Ethernet interface is created on other end
143 NodeId destNodeId = new NodeId(remoteSystemName);
144 Integer destDegId = getDegFromCP(destNodeId, remoteInterfaceName);
145 // portmapping.getDegFromCP(nodeId,interfaceName);
146 if (destDegId == null) {
147 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
150 // Check whether degree is Unidirectional or Bidirectional by counting
152 // circuit-packs under degree subtree
153 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
154 if (Direction.NotApplicable == destinationDirection) {
155 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
157 } else if (Direction.Bidirectional == destinationDirection) {
158 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
159 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
161 destTpTx = "DEG" + destDegId + "-TTP-TX";
162 destTpRx = "DEG" + destDegId + "-TTP-RX";
166 "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
167 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
168 InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
169 r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
170 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
171 .setTerminationPointZ(destTpRx);
172 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.openRoadmTopology, this.dataBroker)) {
173 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
174 destNodeId.getValue());
179 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
180 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
181 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
183 InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
184 r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue()).setDegANum(destDegId.shortValue())
185 .setTerminationPointA(destTpTx).setRdmZNode(nodeId.getValue()).setDegZNum(srcDegId.shortValue())
186 .setTerminationPointZ(srcTpRx);
187 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.openRoadmTopology, this.dataBroker)) {
188 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
189 destNodeId.getValue(), nodeId.getValue());
195 public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
196 String remoteInterfaceName) {
197 String srcTpTx = null;
198 String srcTpRx = null;
199 String destTpTx = null;
200 String destTpRx = null;
201 // Find which degree is associated with ethernet interface
202 // portmapping.getDegFromCP(nodeId,interfaceName);
203 Integer srcDegId = getDegFromCP(nodeId, interfaceName);
204 if (srcDegId == null) {
205 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
208 // Check whether degree is Unidirectional or Bidirectional by counting number of
209 // circuit-packs under degree subtree
210 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
211 if (Direction.NotApplicable == sourceDirection) {
212 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
214 } else if (Direction.Bidirectional == sourceDirection) {
215 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
216 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
218 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
219 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
221 // Find degree for which Ethernet interface is created on other end
222 NodeId destNodeId = new NodeId(remoteSystemName);
223 // portmapping.getDegFromCP(nodeId,interfaceName);
224 Integer destDegId = getDegFromCP(destNodeId, remoteInterfaceName);
225 if (destDegId == null) {
226 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
229 // Check whether degree is Unidirectional or Bidirectional by counting number of
230 // circuit-packs under degree subtree
231 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
232 if (Direction.NotApplicable == destinationDirection) {
233 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
235 } else if (Direction.Bidirectional == destinationDirection) {
236 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
237 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
239 destTpTx = "DEG" + destDegId + "-TTP-TX";
240 destTpRx = "DEG" + destDegId + "-TTP-RX";
242 return this.openRoadmTopology.deleteLink(nodeId.getValue(), destNodeId.getValue(), srcTpTx, destTpRx)
243 && this.openRoadmTopology.deleteLink(destNodeId.getValue(), nodeId.getValue(), destTpTx, srcTpRx);
246 private CpToDegree getCPtoDegreeMapping(NodeId nodeId, String circuitPackName) {
247 InstanceIdentifier<CpToDegree> cptoDegMappingIID = InstanceIdentifier.builder(Network.class)
248 .child(Nodes.class, new NodesKey(nodeId.getValue()))
249 .child(CpToDegree.class, new CpToDegreeKey(circuitPackName)).build();
250 LOG.debug("Input parameters are {},{}", nodeId.getValue(), circuitPackName);
251 try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
252 Optional<CpToDegree> cptpDegObject = readTx.read(LogicalDatastoreType.CONFIGURATION, cptoDegMappingIID)
254 if (cptpDegObject.isPresent()) {
255 CpToDegree cpToDeg = cptpDegObject.get();
256 LOG.debug("Found mapping for the Circuit Pack {}. Degree: {}", circuitPackName, cpToDeg);
259 LOG.warn("Could not find mapping for Circuit Pack {} for nodeId {}", circuitPackName,
262 } catch (InterruptedException | ExecutionException ex) {
263 LOG.error("Unable to read mapping for circuit pack : {} for nodeId {}", circuitPackName, nodeId, ex);
268 private Integer getDegFromParentCP(NodeId nodeId, String interfaceName, String supportingCircuitPack) {
269 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
270 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPack));
271 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
272 LogicalDatastoreType.OPERATIONAL, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
273 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
274 if (!circuitPackObject.isPresent()
275 || (circuitPackObject.get().getParentCircuitPack().getCircuitPackName() == null)) {
276 LOG.warn("Parent circuitpack not found for NodeId: {} and Interface: {}", nodeId, interfaceName);
279 String parentCP = circuitPackObject.get().getParentCircuitPack().getCircuitPackName();
280 CpToDegree cpToDegree = getCPtoDegreeMapping(nodeId, parentCP);
281 if (cpToDegree == null) {
282 LOG.error("CP to Degree mapping not found even with parent circuitpack for NodeID: {}" + "and Interface {}",
283 nodeId, interfaceName);
286 LOG.debug("CP to degree is {}", cpToDegree.getDegreeNumber());
287 return cpToDegree.getDegreeNumber().intValue();
291 private Integer getDegFromCP(NodeId nodeId, String interfaceName) {
293 java.util.Optional<Interface> interfaceOpt = this.openRoadmInterfaces.getInterface(nodeId.getValue(),
295 if (!interfaceOpt.isPresent()) {
296 LOG.warn("Interface with {} on node {} was not found!", interfaceName, nodeId.getValue());
299 String supportingCircuitPack = interfaceOpt.get().getSupportingCircuitPackName();
300 LOG.debug("Supporting circuitpack name is :{}", interfaceOpt.get().getSupportingCircuitPackName());
301 CpToDegree cpToDegree = getCPtoDegreeMapping(nodeId, supportingCircuitPack);
302 // Currently devices have different ways to represent connection to Ethernet port
304 // If Circuit pack is not present under degree tree then read parent CP of given
305 // CP (Circuit pack).
306 if (cpToDegree != null) {
307 return cpToDegree.getDegreeNumber().intValue();
309 return getDegFromParentCP(nodeId, interfaceName, supportingCircuitPack);
311 } catch (OpenRoadmInterfaceException ex) {
312 LOG.error("Failed to get source interface {} from node {}!", interfaceName, nodeId, ex);