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 static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_1_2_1;
11 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_2_2_1;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.MountPoint;
22 import org.opendaylight.mdsal.binding.api.ReadTransaction;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.transportpce.common.Timeouts;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
27 import org.opendaylight.transportpce.networkmodel.util.TopologyUtils;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev170818.InitRoadmNodesInputBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.NodesKey;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.nodes.CpToDegree;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.nodes.Mapping;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev170929.Direction;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class R2RLinkDiscovery {
47 private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
49 private final DataBroker dataBroker;
50 private final NetworkTransactionService networkTransactionService;
51 private final DeviceTransactionManager deviceTransactionManager;
53 public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
54 NetworkTransactionService networkTransactionService) {
55 this.dataBroker = dataBroker;
56 this.deviceTransactionManager = deviceTransactionManager;
57 this.networkTransactionService = networkTransactionService;
60 public boolean readLLDP(NodeId nodeId, String nodeVersion) {
62 if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_1_2_1)) {
63 InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
64 .child(Protocols.class);
65 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
66 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
67 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
68 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
69 LOG.warn("LLDP subtree is missing : isolated openroadm device");
72 NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
73 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
74 for (IfName ifName : nbrList.getIfName()) {
75 if (ifName.getRemoteSysName() == null) {
76 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
77 nodeId.getValue(),ifName.getIfName());
79 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
81 if (!mps.isPresent()) {
82 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
83 // The controller raises a warning rather than an error because the first node to
84 // mount cannot see its neighbors yet. The link will be detected when processing
87 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
88 ifName.getRemotePortId())) {
89 LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
90 ifName.getRemoteSysName());
98 else if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_2_2_1)) {
99 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
100 .container.org.openroadm.device.Protocols> protocolsIID = InstanceIdentifier.create(org.opendaylight
101 .yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
102 .OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
103 .org.openroadm.device.container.org.openroadm.device.Protocols.class);
104 Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
105 .container.org.openroadm.device.Protocols> protocolObject = this.deviceTransactionManager
106 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocolsIID,
107 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
108 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org
109 .openroadm.lldp.rev181019.Protocols1.class) == null)) {
110 LOG.warn("LLDP subtree is missing : isolated openroadm device");
113 org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList nbrList
114 = protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019
115 .Protocols1.class).getLldp().getNbrList();
116 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
117 for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
118 ifName : nbrList.getIfName()) {
119 if (ifName.getRemoteSysName() == null) {
120 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
121 nodeId.getValue(),ifName.getIfName());
123 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
124 .getRemoteSysName());
125 if (!mps.isPresent()) {
126 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
127 // The controller raises a warning rather than an error because the first node to
128 // mount cannot see its neighbors yet. The link will be detected when processing
129 // the neighbor node.
131 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
132 ifName.getRemotePortId())) {
133 LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName
134 .getRemoteSysName());
143 LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
148 public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
149 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
150 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
151 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
152 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
153 if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
154 List<Mapping> mappingList = nodesObject.get().getMapping();
155 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
156 + degreeCounter)).collect(Collectors.toList());
157 if (mappingList.size() == 1) {
158 return Direction.Bidirectional;
159 } else if (mappingList.size() > 1) {
162 return Direction.NotApplicable;
165 } catch (InterruptedException | ExecutionException e) {
166 LOG.error("Failed getting Mapping data from portMapping",e);
168 return Direction.NotApplicable;
171 public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
172 String remoteInterfaceName) {
173 String srcTpTx = null;
174 String srcTpRx = null;
175 String destTpTx = null;
176 String destTpRx = null;
177 // Find which degree is associated with ethernet interface
178 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
179 if (srcDegId == null) {
180 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
183 // Check whether degree is Unidirectional or Bidirectional by counting
185 // circuit-packs under degree subtree
186 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
187 if (Direction.NotApplicable == sourceDirection) {
188 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
190 } else if (Direction.Bidirectional == sourceDirection) {
191 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
192 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
194 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
195 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
197 // Find degree for which Ethernet interface is created on other end
198 NodeId destNodeId = new NodeId(remoteSystemName);
199 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
200 if (destDegId == null) {
201 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
204 // Check whether degree is Unidirectional or Bidirectional by counting
206 // circuit-packs under degree subtree
207 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
208 if (Direction.NotApplicable == destinationDirection) {
209 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
211 } else if (Direction.Bidirectional == destinationDirection) {
212 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
213 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
215 destTpTx = "DEG" + destDegId + "-TTP-TX";
216 destTpRx = "DEG" + destDegId + "-TTP-RX";
220 "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
221 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
222 InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
223 r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
224 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
225 .setTerminationPointZ(destTpRx);
226 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.dataBroker)) {
227 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
228 destNodeId.getValue());
233 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
234 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
235 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
237 InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder()
238 .setRdmANode(destNodeId.getValue())
239 .setDegANum(destDegId.shortValue())
240 .setTerminationPointA(destTpTx)
241 .setRdmZNode(nodeId.getValue())
242 .setDegZNum(srcDegId.shortValue())
243 .setTerminationPointZ(srcTpRx);
244 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.dataBroker)) {
245 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
246 destNodeId.getValue(), nodeId.getValue());
252 public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
253 String remoteInterfaceName) {
254 String srcTpTx = null;
255 String srcTpRx = null;
256 String destTpTx = null;
257 String destTpRx = null;
258 // Find which degree is associated with ethernet interface
259 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
260 if (srcDegId == null) {
261 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
264 // Check whether degree is Unidirectional or Bidirectional by counting number of
265 // circuit-packs under degree subtree
266 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
267 if (Direction.NotApplicable == sourceDirection) {
268 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
270 } else if (Direction.Bidirectional == sourceDirection) {
271 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
272 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
274 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
275 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
277 // Find degree for which Ethernet interface is created on other end
278 NodeId destNodeId = new NodeId(remoteSystemName);
279 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
280 if (destDegId == null) {
281 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
284 // Check whether degree is Unidirectional or Bidirectional by counting number of
285 // circuit-packs under degree subtree
286 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
287 if (Direction.NotApplicable == destinationDirection) {
288 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
290 } else if (Direction.Bidirectional == destinationDirection) {
291 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
292 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
294 destTpTx = "DEG" + destDegId + "-TTP-TX";
295 destTpRx = "DEG" + destDegId + "-TTP-RX";
297 return TopologyUtils.deleteLink(nodeId.getValue() + "-" + srcDegId.toString(),
298 destNodeId.getValue() + "-" + destDegId.toString(),
299 srcTpTx.toString(), destTpRx.toString(),networkTransactionService)
300 && TopologyUtils.deleteLink(destNodeId.getValue() + "-" + destDegId.toString(),
301 nodeId.getValue() + "-" + srcDegId.toString(), destTpTx, srcTpRx,
302 networkTransactionService);
305 private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
306 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
307 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
308 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
309 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
310 if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
311 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
312 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
313 .filter(cp -> cp.getInterfaceName().equals(interfaceName));
314 if (cpToDegStream != null) {
315 @SuppressWarnings("unchecked") Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
316 if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
317 LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
318 return firstCpToDegree.get().getDegreeNumber().intValue();
320 LOG.debug("Not found so returning nothing");
324 LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
325 nodeId.getValue(),interfaceName);
328 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
331 } catch (InterruptedException | ExecutionException ex) {
332 LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);