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.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.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.OpenRoadmFactory;
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.rev170228.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.NodesKey;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.nodes.CpToDegree;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.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;
52 private final OpenRoadmFactory openRoadmFactory;
54 public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
55 OpenRoadmFactory openRoadmFactory, NetworkTransactionService networkTransactionService) {
56 this.dataBroker = dataBroker;
57 this.deviceTransactionManager = deviceTransactionManager;
58 this.openRoadmFactory = openRoadmFactory;
59 this.networkTransactionService = networkTransactionService;
62 public boolean readLLDP(NodeId nodeId, String nodeVersion) {
64 if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_1_2_1)) {
65 InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
66 .child(Protocols.class);
67 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
68 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
69 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
70 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
71 LOG.warn("LLDP subtree is missing : isolated openroadm device");
74 NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
75 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
76 for (IfName ifName : nbrList.getIfName()) {
77 if (ifName.getRemoteSysName() == null) {
78 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
79 nodeId.getValue(),ifName.getIfName());
81 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
83 if (!mps.isPresent()) {
84 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
85 // The controller raises a warning rather than an error because the first node to
86 // mount cannot see its neighbors yet. The link will be detected when processing
89 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
90 ifName.getRemotePortId())) {
91 LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
92 ifName.getRemoteSysName());
100 else if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_2_2_1)) {
101 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
102 .container.org.openroadm.device.Protocols> protocolsIID = InstanceIdentifier.create(org.opendaylight
103 .yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
104 .OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
105 .org.openroadm.device.container.org.openroadm.device.Protocols.class);
106 Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
107 .container.org.openroadm.device.Protocols> protocolObject = this.deviceTransactionManager
108 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocolsIID,
109 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
110 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org
111 .openroadm.lldp.rev181019.Protocols1.class) == null)) {
112 LOG.warn("LLDP subtree is missing : isolated openroadm device");
115 org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList nbrList
116 = protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019
117 .Protocols1.class).getLldp().getNbrList();
118 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
119 for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
120 ifName : nbrList.getIfName()) {
121 if (ifName.getRemoteSysName() == null) {
122 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
123 nodeId.getValue(),ifName.getIfName());
125 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
126 .getRemoteSysName());
127 if (!mps.isPresent()) {
128 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
129 // The controller raises a warning rather than an error because the first node to
130 // mount cannot see its neighbors yet. The link will be detected when processing
131 // the neighbor node.
133 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
134 ifName.getRemotePortId())) {
135 LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName
136 .getRemoteSysName());
145 LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
150 public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
151 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
152 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
153 try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
154 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
156 if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
157 List<Mapping> mappingList = nodesObject.get().getMapping();
158 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
159 + degreeCounter)).collect(Collectors.toList());
160 if (mappingList.size() == 1) {
161 return Direction.Bidirectional;
162 } else if (mappingList.size() > 1) {
165 return Direction.NotApplicable;
168 } catch (InterruptedException | ExecutionException e) {
169 LOG.error("Failed getting Mapping data from portMapping",e);
171 return Direction.NotApplicable;
174 public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
175 String remoteInterfaceName) {
176 String srcTpTx = null;
177 String srcTpRx = null;
178 String destTpTx = null;
179 String destTpRx = null;
180 // Find which degree is associated with ethernet interface
181 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
182 if (srcDegId == null) {
183 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
186 // Check whether degree is Unidirectional or Bidirectional by counting
188 // circuit-packs under degree subtree
189 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
190 if (Direction.NotApplicable == sourceDirection) {
191 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
193 } else if (Direction.Bidirectional == sourceDirection) {
194 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
195 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
197 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
198 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
200 // Find degree for which Ethernet interface is created on other end
201 NodeId destNodeId = new NodeId(remoteSystemName);
202 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
203 if (destDegId == null) {
204 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
207 // Check whether degree is Unidirectional or Bidirectional by counting
209 // circuit-packs under degree subtree
210 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
211 if (Direction.NotApplicable == destinationDirection) {
212 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
214 } else if (Direction.Bidirectional == destinationDirection) {
215 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
216 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
218 destTpTx = "DEG" + destDegId + "-TTP-TX";
219 destTpRx = "DEG" + destDegId + "-TTP-RX";
223 "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
224 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
225 InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
226 r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
227 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
228 .setTerminationPointZ(destTpRx);
229 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.openRoadmFactory, this.dataBroker)) {
230 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
231 destNodeId.getValue());
236 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
237 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
238 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
240 InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
241 r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue()).setDegANum(destDegId.shortValue())
242 .setTerminationPointA(destTpTx).setRdmZNode(nodeId.getValue()).setDegZNum(srcDegId.shortValue())
243 .setTerminationPointZ(srcTpRx);
244 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.openRoadmFactory, 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 this.openRoadmFactory.deleteLink(nodeId.getValue() + "-" + srcDegId.toString(),
298 destNodeId.getValue() + "-" + destDegId.toString(),
299 srcTpTx.toString(), destTpRx.toString(),networkTransactionService)
300 && this.openRoadmFactory.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 (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
309 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
311 if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
312 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
313 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
314 .filter(cp -> cp.getInterfaceName().equals(interfaceName));
315 if (cpToDegStream != null) {
316 Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
317 if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
318 LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
319 return firstCpToDegree.get().getDegreeNumber().intValue();
321 LOG.debug("Not found so returning nothing");
325 LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
326 nodeId.getValue(),interfaceName);
329 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
332 } catch (InterruptedException | ExecutionException ex) {
333 LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);