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;
12 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_7_1;
14 import java.util.Collection;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.stream.Collectors;
18 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.rev220630.InitRoadmNodesInputBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.cp.to.degree.CpToDegree;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.mapping.Mapping;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.network.Nodes;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.network.NodesKey;
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.OrgOpenroadmDeviceData;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.Uint8;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class R2RLinkDiscovery {
49 private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
51 private final DataBroker dataBroker;
52 private final NetworkTransactionService networkTransactionService;
53 private final DeviceTransactionManager deviceTransactionManager;
55 public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
56 NetworkTransactionService networkTransactionService) {
57 this.dataBroker = dataBroker;
58 this.deviceTransactionManager = deviceTransactionManager;
59 this.networkTransactionService = networkTransactionService;
62 public boolean readLLDP(NodeId nodeId, String nodeVersion) {
63 switch (nodeVersion) {
64 case OPENROADM_DEVICE_VERSION_1_2_1:
65 InstanceIdentifier<Protocols> protocols121IID = InstanceIdentifier
66 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
67 .child(Protocols.class)
69 Optional<Protocols> protocol121Object = this.deviceTransactionManager.getDataFromDevice(
70 nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocols121IID, Timeouts.DEVICE_READ_TIMEOUT,
71 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
72 if (!protocol121Object.isPresent()
73 || (protocol121Object.get().augmentation(Protocols1.class) == null)) {
74 LOG.warn("LLDP subtree is missing : isolated openroadm device");
78 NbrList nbr121List = protocol121Object.get().augmentation(Protocols1.class).getLldp().getNbrList();
79 LOG.info("LLDP subtree is present. Device has {} neighbours", nbr121List.getIfName().size());
80 // try to create rdm2rdm link
81 return rdm2rdmLinkCreatedv121(nodeId, nbr121List);
82 case OPENROADM_DEVICE_VERSION_2_2_1:
83 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
84 .container.org.openroadm.device.Protocols> protocols221IID = InstanceIdentifier
86 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceData.class,
87 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
88 .OrgOpenroadmDevice.class)
90 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
91 .org.openroadm.device.Protocols.class)
93 Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
94 .container.org.openroadm.device.Protocols> protocol221Object = this.deviceTransactionManager
95 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocols221IID,
96 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
97 if (!protocol221Object.isPresent() || (protocol221Object.get().augmentation(
98 org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class) == null)) {
99 LOG.warn("LLDP subtree is missing : isolated openroadm device");
102 org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList
103 nbr221List = protocol221Object.get().augmentation(org.opendaylight.yang.gen.v1.http
104 .org.openroadm.lldp.rev181019.Protocols1.class).getLldp().getNbrList();
105 LOG.info("LLDP subtree is present. Device has {} neighbours", nbr221List.getIfName().size());
106 return rdm2rdmLinkCreatedv221(nodeId, nbr221List);
107 case OPENROADM_DEVICE_VERSION_7_1:
108 LOG.info("Not yet implemented?");
111 LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
116 private boolean rdm2rdmLinkCreatedv221(NodeId nodeId,
117 org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.NbrList nbrList) {
118 boolean success = true;
119 for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
120 ifName : nbrList.nonnullIfName().values()) {
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 private boolean rdm2rdmLinkCreatedv121(NodeId nodeId, NbrList nbrList) {
146 boolean success = true;
147 for (IfName ifName : nbrList.nonnullIfName().values()) {
148 if (ifName.getRemoteSysName() == null) {
149 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
150 nodeId.getValue(),ifName.getIfName());
152 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
153 .getRemoteSysName());
154 if (!mps.isPresent()) {
155 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
156 // The controller raises a warning rather than an error because the first node to
157 // mount cannot see its neighbors yet. The link will be detected when processing
158 // the neighbor node.
160 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
161 ifName.getRemotePortId())) {
162 LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
163 ifName.getRemoteSysName());
172 public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
173 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
174 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
175 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
176 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
177 if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
178 Collection<Mapping> mappingList = nodesObject.get().nonnullMapping().values();
179 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
180 + degreeCounter)).collect(Collectors.toList());
181 if (mappingList.size() == 1) {
182 return Direction.Bidirectional;
183 } else if (mappingList.size() > 1) {
186 return Direction.NotApplicable;
189 } catch (InterruptedException | ExecutionException e) {
190 LOG.error("Failed getting Mapping data from portMapping",e);
192 return Direction.NotApplicable;
195 public boolean createR2RLink(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 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
203 if (srcDegId == 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 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 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
224 if (destDegId == null) {
225 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
228 // Check whether degree is Unidirectional or Bidirectional by counting
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";
244 "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
245 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
246 InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
247 r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(Uint8.valueOf(srcDegId))
248 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(Uint8.valueOf(destDegId))
249 .setTerminationPointZ(destTpRx);
250 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.dataBroker)) {
251 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
252 destNodeId.getValue());
257 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
258 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
259 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
261 InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder()
262 .setRdmANode(destNodeId.getValue())
263 .setDegANum(Uint8.valueOf(destDegId))
264 .setTerminationPointA(destTpTx)
265 .setRdmZNode(nodeId.getValue())
266 .setDegZNum(Uint8.valueOf(srcDegId))
267 .setTerminationPointZ(srcTpRx);
268 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.dataBroker)) {
269 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
270 destNodeId.getValue(), nodeId.getValue());
276 public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
277 String remoteInterfaceName) {
278 String srcTpTx = null;
279 String srcTpRx = null;
280 String destTpTx = null;
281 String destTpRx = null;
282 // Find which degree is associated with ethernet interface
283 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
284 if (srcDegId == null) {
285 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
288 // Check whether degree is Unidirectional or Bidirectional by counting number of
289 // circuit-packs under degree subtree
290 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
291 if (Direction.NotApplicable == sourceDirection) {
292 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
294 } else if (Direction.Bidirectional == sourceDirection) {
295 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
296 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
298 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
299 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
301 // Find degree for which Ethernet interface is created on other end
302 NodeId destNodeId = new NodeId(remoteSystemName);
303 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
304 if (destDegId == null) {
305 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
308 // Check whether degree is Unidirectional or Bidirectional by counting number of
309 // circuit-packs under degree subtree
310 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
311 if (Direction.NotApplicable == destinationDirection) {
312 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
314 } else if (Direction.Bidirectional == destinationDirection) {
315 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
316 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
318 destTpTx = "DEG" + destDegId + "-TTP-TX";
319 destTpRx = "DEG" + destDegId + "-TTP-RX";
321 return TopologyUtils.deleteLink(nodeId.getValue() + "-" + srcDegId, destNodeId.getValue() + "-" + destDegId,
322 srcTpTx, destTpRx, networkTransactionService)
323 && TopologyUtils.deleteLink(destNodeId.getValue() + "-" + destDegId, nodeId.getValue() + "-" + srcDegId,
324 destTpTx, srcTpRx, networkTransactionService);
327 private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
328 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
329 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
330 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
331 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
332 if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
333 Collection<CpToDegree> cpToDeg = nodesObject.get().nonnullCpToDegree().values();
334 Stream<CpToDegree> cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
335 .filter(cp -> cp.getInterfaceName().equals(interfaceName));
336 if (cpToDegStream != null) {
337 @SuppressWarnings("unchecked") Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
338 if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
339 LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
340 return firstCpToDegree.get().getDegreeNumber().intValue();
342 LOG.debug("Not found so returning nothing");
346 LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
347 nodeId.getValue(),interfaceName);
350 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
353 } catch (InterruptedException | ExecutionException ex) {
354 LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);