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.List;
11 import java.util.Optional;
12 import java.util.concurrent.ExecutionException;
13 import java.util.stream.Collectors;
14 import java.util.stream.Stream;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.transportpce.common.Timeouts;
20 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
21 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
22 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
23 import org.opendaylight.transportpce.networkmodel.util.OpenRoadmFactory;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev170818.InitRoadmNodesInputBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.Network;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.Nodes;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.NodesKey;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.nodes.CpToDegree;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.nodes.Mapping;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev170929.Direction;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class R2RLinkDiscovery {
43 private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
45 private final DataBroker dataBroker;
46 private final NetworkTransactionService networkTransactionService;
47 private final DeviceTransactionManager deviceTransactionManager;
48 private final OpenRoadmFactory openRoadmFactory;
49 private final OpenRoadmInterfaces openRoadmInterfaces;
51 public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
52 OpenRoadmFactory openRoadmFactory, OpenRoadmInterfaces openRoadmInterfaces,
53 NetworkTransactionService networkTransactionService) {
54 this.dataBroker = dataBroker;
55 this.deviceTransactionManager = deviceTransactionManager;
56 this.openRoadmFactory = openRoadmFactory;
57 this.openRoadmInterfaces = openRoadmInterfaces;
58 this.networkTransactionService = networkTransactionService;
61 public boolean readLLDP(NodeId nodeId) {
62 InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
63 .child(Protocols.class);
64 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
65 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
66 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
67 if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
68 LOG.warn("LLDP subtree is missing : isolated openroadm device");
71 NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
72 LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
73 for (IfName ifName : nbrList.getIfName()) {
74 if (ifName.getRemoteSysName() == null) {
75 LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
76 nodeId.getValue(),ifName.getIfName());
78 Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName.getRemoteSysName());
79 if (!mps.isPresent()) {
80 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
81 // The controller raises a warning rather than an error because the first node to
82 // mount cannot see its neighbors yet. The link will be detected when processing
85 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
86 ifName.getRemotePortId())) {
87 LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName.getRemoteSysName());
96 public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
97 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
98 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
99 try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
100 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
102 if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
103 List<Mapping> mappingList = nodesObject.get().getMapping();
104 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
105 + degreeCounter)).collect(Collectors.toList());
106 if (mappingList.size() == 1) {
107 return Direction.Bidirectional;
108 } else if (mappingList.size() > 1) {
111 return Direction.NotApplicable;
114 } catch (InterruptedException | ExecutionException e) {
115 LOG.error("Failed getting Mapping data from portMapping",e);
117 return Direction.NotApplicable;
120 public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
121 String remoteInterfaceName) {
122 String srcTpTx = null;
123 String srcTpRx = null;
124 String destTpTx = null;
125 String destTpRx = null;
126 // Find which degree is associated with ethernet interface
127 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
128 if (srcDegId == null) {
129 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
132 // Check whether degree is Unidirectional or Bidirectional by counting
134 // circuit-packs under degree subtree
135 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
136 if (Direction.NotApplicable == sourceDirection) {
137 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
139 } else if (Direction.Bidirectional == sourceDirection) {
140 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
141 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
143 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
144 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
146 // Find degree for which Ethernet interface is created on other end
147 NodeId destNodeId = new NodeId(remoteSystemName);
148 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
149 if (destDegId == null) {
150 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
153 // Check whether degree is Unidirectional or Bidirectional by counting
155 // circuit-packs under degree subtree
156 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
157 if (Direction.NotApplicable == destinationDirection) {
158 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
160 } else if (Direction.Bidirectional == destinationDirection) {
161 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
162 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
164 destTpTx = "DEG" + destDegId + "-TTP-TX";
165 destTpRx = "DEG" + destDegId + "-TTP-RX";
169 "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
170 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
171 InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
172 r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
173 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
174 .setTerminationPointZ(destTpRx);
175 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.openRoadmFactory, this.dataBroker)) {
176 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
177 destNodeId.getValue());
182 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
183 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
184 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
186 InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
187 r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue()).setDegANum(destDegId.shortValue())
188 .setTerminationPointA(destTpTx).setRdmZNode(nodeId.getValue()).setDegZNum(srcDegId.shortValue())
189 .setTerminationPointZ(srcTpRx);
190 if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.openRoadmFactory, this.dataBroker)) {
191 LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
192 destNodeId.getValue(), nodeId.getValue());
198 public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
199 String remoteInterfaceName) {
200 String srcTpTx = null;
201 String srcTpRx = null;
202 String destTpTx = null;
203 String destTpRx = null;
204 // Find which degree is associated with ethernet interface
205 Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
206 if (srcDegId == null) {
207 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
210 // Check whether degree is Unidirectional or Bidirectional by counting number of
211 // circuit-packs under degree subtree
212 Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
213 if (Direction.NotApplicable == sourceDirection) {
214 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
216 } else if (Direction.Bidirectional == sourceDirection) {
217 srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
218 srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
220 srcTpTx = "DEG" + srcDegId + "-TTP-TX";
221 srcTpRx = "DEG" + srcDegId + "-TTP-RX";
223 // Find degree for which Ethernet interface is created on other end
224 NodeId destNodeId = new NodeId(remoteSystemName);
225 Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
226 if (destDegId == null) {
227 LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
230 // Check whether degree is Unidirectional or Bidirectional by counting number of
231 // circuit-packs under degree subtree
232 Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
233 if (Direction.NotApplicable == destinationDirection) {
234 LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
236 } else if (Direction.Bidirectional == destinationDirection) {
237 destTpTx = "DEG" + destDegId + "-TTP-TXRX";
238 destTpRx = "DEG" + destDegId + "-TTP-TXRX";
240 destTpTx = "DEG" + destDegId + "-TTP-TX";
241 destTpRx = "DEG" + destDegId + "-TTP-RX";
243 return this.openRoadmFactory.deleteLink(nodeId.getValue() + "-" + srcDegId.toString(),
244 destNodeId.getValue() + "-" + destDegId.toString(),
245 srcTpTx.toString(), destTpRx.toString(),networkTransactionService)
246 && this.openRoadmFactory.deleteLink(destNodeId.getValue() + "-" + destDegId.toString(),
247 nodeId.getValue() + "-" + srcDegId.toString(), destTpTx, srcTpRx,
248 networkTransactionService);
251 private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
252 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
253 .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
254 try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
255 Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
257 if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
258 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
259 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
260 .filter(cp -> cp.getInterfaceName().equals(interfaceName));
261 if (cpToDegStream != null) {
262 Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
263 if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
264 LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
265 return firstCpToDegree.get().getDegreeNumber().intValue();
267 LOG.debug("Not found so returning nothing");
271 LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
272 nodeId.getValue(),interfaceName);
275 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
278 } catch (InterruptedException | ExecutionException ex) {
279 LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);