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.opendaylight.yangtools.yang.common.Uint8;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class R2RLinkDiscovery {
48 private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
50 private final DataBroker dataBroker;
51 private final NetworkTransactionService networkTransactionService;
52 private final DeviceTransactionManager deviceTransactionManager;
54 public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
55 NetworkTransactionService networkTransactionService) {
56 this.dataBroker = dataBroker;
57 this.deviceTransactionManager = deviceTransactionManager;
58 this.networkTransactionService = networkTransactionService;
61 public boolean readLLDP(NodeId nodeId, String nodeVersion) {
63 if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_1_2_1)) {
64 InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
65 .child(Protocols.class);
66 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
67 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
68 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
69 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
70 LOG.warn("LLDP subtree is missing : isolated openroadm device");
73 NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
74 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
75 for (IfName ifName : nbrList.getIfName()) {
76 if (ifName.getRemoteSysName() == null) {
77 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
78 nodeId.getValue(),ifName.getIfName());
80 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
82 if (!mps.isPresent()) {
83 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
84 // The controller raises a warning rather than an error because the first node to
85 // mount cannot see its neighbors yet. The link will be detected when processing
88 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
89 ifName.getRemotePortId())) {
90 LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
91 ifName.getRemoteSysName());
99 else if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_2_2_1)) {
100 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
101 .container.org.openroadm.device.Protocols> protocolsIID = InstanceIdentifier.create(org.opendaylight
102 .yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
103 .OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
104 .org.openroadm.device.container.org.openroadm.device.Protocols.class);
105 Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
106 .container.org.openroadm.device.Protocols> protocolObject = this.deviceTransactionManager
107 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocolsIID,
108 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
109 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org
110 .openroadm.lldp.rev181019.Protocols1.class) == null)) {
111 LOG.warn("LLDP subtree is missing : isolated openroadm device");
114 org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList nbrList
115 = protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019
116 .Protocols1.class).getLldp().getNbrList();
117 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
118 for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
119 ifName : nbrList.getIfName()) {
120 if (ifName.getRemoteSysName() == null) {
121 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
122 nodeId.getValue(),ifName.getIfName());
124 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
125 .getRemoteSysName());
126 if (!mps.isPresent()) {
127 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
128 // The controller raises a warning rather than an error because the first node to
129 // mount cannot see its neighbors yet. The link will be detected when processing
130 // the neighbor node.
132 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
133 ifName.getRemotePortId())) {
134 LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName
135 .getRemoteSysName());
144 LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
149 public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
150 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
151 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
152 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
153 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
154 if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
155 List<Mapping> mappingList = nodesObject.get().getMapping();
156 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
157 + degreeCounter)).collect(Collectors.toList());
158 if (mappingList.size() == 1) {
159 return Direction.Bidirectional;
160 } else if (mappingList.size() > 1) {
163 return Direction.NotApplicable;
166 } catch (InterruptedException | ExecutionException e) {
167 LOG.error("Failed getting Mapping data from portMapping",e);
169 return Direction.NotApplicable;
172 public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
173 String remoteInterfaceName) {
174 String srcTpTx = null;
175 String srcTpRx = null;
176 String destTpTx = null;
177 String destTpRx = null;
178 // Find which degree is associated with ethernet interface
179 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
180 if (srcDegId == null) {
181 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
184 // Check whether degree is Unidirectional or Bidirectional by counting
186 // circuit-packs under degree subtree
187 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
188 if (Direction.NotApplicable == sourceDirection) {
189 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
191 } else if (Direction.Bidirectional == sourceDirection) {
192 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
193 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
195 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
196 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
198 // Find degree for which Ethernet interface is created on other end
199 NodeId destNodeId = new NodeId(remoteSystemName);
200 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
201 if (destDegId == null) {
202 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
205 // Check whether degree is Unidirectional or Bidirectional by counting
207 // circuit-packs under degree subtree
208 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
209 if (Direction.NotApplicable == destinationDirection) {
210 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
212 } else if (Direction.Bidirectional == destinationDirection) {
213 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
214 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
216 destTpTx = "DEG" + destDegId + "-TTP-TX";
217 destTpRx = "DEG" + destDegId + "-TTP-RX";
221 "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
222 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
223 InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
224 r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(Uint8.valueOf(srcDegId))
225 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(Uint8.valueOf(destDegId))
226 .setTerminationPointZ(destTpRx);
227 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.dataBroker)) {
228 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
229 destNodeId.getValue());
234 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
235 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
236 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
238 InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder()
239 .setRdmANode(destNodeId.getValue())
240 .setDegANum(Uint8.valueOf(destDegId))
241 .setTerminationPointA(destTpTx)
242 .setRdmZNode(nodeId.getValue())
243 .setDegZNum(Uint8.valueOf(srcDegId))
244 .setTerminationPointZ(srcTpRx);
245 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.dataBroker)) {
246 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
247 destNodeId.getValue(), nodeId.getValue());
253 public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
254 String remoteInterfaceName) {
255 String srcTpTx = null;
256 String srcTpRx = null;
257 String destTpTx = null;
258 String destTpRx = null;
259 // Find which degree is associated with ethernet interface
260 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
261 if (srcDegId == null) {
262 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
265 // Check whether degree is Unidirectional or Bidirectional by counting number of
266 // circuit-packs under degree subtree
267 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
268 if (Direction.NotApplicable == sourceDirection) {
269 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
271 } else if (Direction.Bidirectional == sourceDirection) {
272 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
273 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
275 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
276 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
278 // Find degree for which Ethernet interface is created on other end
279 NodeId destNodeId = new NodeId(remoteSystemName);
280 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
281 if (destDegId == null) {
282 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
285 // Check whether degree is Unidirectional or Bidirectional by counting number of
286 // circuit-packs under degree subtree
287 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
288 if (Direction.NotApplicable == destinationDirection) {
289 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
291 } else if (Direction.Bidirectional == destinationDirection) {
292 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
293 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
295 destTpTx = "DEG" + destDegId + "-TTP-TX";
296 destTpRx = "DEG" + destDegId + "-TTP-RX";
298 return TopologyUtils.deleteLink(nodeId.getValue() + "-" + srcDegId, destNodeId.getValue() + "-" + destDegId,
299 srcTpTx, destTpRx, networkTransactionService)
300 && TopologyUtils.deleteLink(destNodeId.getValue() + "-" + destDegId, nodeId.getValue() + "-" + srcDegId,
301 destTpTx, srcTpRx, networkTransactionService);
304 private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
305 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
306 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
307 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
308 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
309 if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
310 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
311 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
312 .filter(cp -> cp.getInterfaceName().equals(interfaceName));
313 if (cpToDegStream != null) {
314 @SuppressWarnings("unchecked") Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
315 if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
316 LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
317 return firstCpToDegree.get().getDegreeNumber().intValue();
319 LOG.debug("Not found so returning nothing");
323 LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
324 nodeId.getValue(),interfaceName);
327 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
330 } catch (InterruptedException | ExecutionException ex) {
331 LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);