Bug corrections in topo and portmapping modules
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / R2RLinkDiscovery.java
1 /*
2  * Copyright © 2016 AT&T and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.networkmodel;
9
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.transportpce.common.Timeouts;
18 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
19 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
20 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
21 import org.opendaylight.transportpce.networkmodel.util.OpenRoadmTopology;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev170929.Direction;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Degree;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.networkutils.rev170818.InitRoadmNodesInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.Network;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.NodesKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.CpToDegree;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.CpToDegreeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class R2RLinkDiscovery {
45
46     private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
47
48     private final DataBroker dataBroker;
49     private final DeviceTransactionManager deviceTransactionManager;
50     private final OpenRoadmTopology openRoadmTopology;
51     private final OpenRoadmInterfaces openRoadmInterfaces;
52
53     public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
54             OpenRoadmTopology openRoadmTopology, OpenRoadmInterfaces openRoadmInterfaces) {
55         this.dataBroker = dataBroker;
56         this.deviceTransactionManager = deviceTransactionManager;
57         this.openRoadmTopology = openRoadmTopology;
58         this.openRoadmInterfaces = openRoadmInterfaces;
59     }
60
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().getAugmentation(Protocols1.class) == null)) {
68             LOG.warn("LLDP subtree is missing : isolated openroadm device");
69             return false;
70         }
71         NbrList nbrList = protocolObject.get().getAugmentation(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.error("LLDP subtree is empty in the device for nodeId: {}", nodeId.getValue());
76                 return false;
77             }
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
83                 // the neighbor node.
84             } else {
85                 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(), ifName.getRemotePortId())) {
86                     LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName.getRemoteSysName());
87                     return false;
88                 }
89             }
90         }
91         return true;
92     }
93
94     public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
95         InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Degree.class,
96                 new DegreeKey(degreeCounter));
97         Optional<Degree> degreeObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
98                 LogicalDatastoreType.OPERATIONAL, deviceIID, Timeouts.DEVICE_READ_TIMEOUT,
99                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
100         if (degreeObject.isPresent()) {
101             Integer connectionPortCount = degreeObject.get().getConnectionPorts().size();
102             if (connectionPortCount == 1) {
103                 return Direction.Bidirectional;
104             } else if (connectionPortCount > 1) {
105                 return Direction.Tx;
106             } else {
107                 return Direction.NotApplicable;
108             }
109         } else {
110             LOG.error("Couldnt retrieve Degree object for nodeId: {} and DegreeNumbner: {}", nodeId.getValue(),
111                     degreeCounter);
112             return Direction.NotApplicable;
113         }
114     }
115
116     public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
117             String remoteInterfaceName) {
118         String srcTpTx = null;
119         String srcTpRx = null;
120         String destTpTx = null;
121         String destTpRx = null;
122         // Find which degree is associated with ethernet interface
123         // portmapping.getDegFromCP(nodeId,interfaceName);
124         Integer srcDegId = getDegFromCP(nodeId, interfaceName);
125         if (srcDegId == null) {
126             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
127             return false;
128         }
129         // Check whether degree is Unidirectional or Bidirectional by counting
130         // number of
131         // circuit-packs under degree subtree
132         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
133         if (Direction.NotApplicable == sourceDirection) {
134             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
135             return false;
136         } else if (Direction.Bidirectional == sourceDirection) {
137             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
138             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
139         } else {
140             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
141             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
142         }
143         // Find degree for which Ethernet interface is created on other end
144         NodeId destNodeId = new NodeId(remoteSystemName);
145         Integer destDegId = getDegFromCP(destNodeId, remoteInterfaceName);
146         // portmapping.getDegFromCP(nodeId,interfaceName);
147         if (destDegId == null) {
148             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
149             return false;
150         }
151         // Check whether degree is Unidirectional or Bidirectional by counting
152         // number of
153         // circuit-packs under degree subtree
154         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
155         if (Direction.NotApplicable == destinationDirection) {
156             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
157             return false;
158         } else if (Direction.Bidirectional == destinationDirection) {
159             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
160             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
161         } else {
162             destTpTx = "DEG" + destDegId + "-TTP-TX";
163             destTpRx = "DEG" + destDegId + "-TTP-RX";
164         }
165         // A->Z
166         LOG.debug(
167             "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
168                 nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
169         InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
170         r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
171                 .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
172                 .setTerminationPointZ(destTpRx);
173         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), openRoadmTopology, dataBroker)) {
174             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
175                     destNodeId.getValue());
176             return false;
177         }
178         // Z->A
179         LOG.debug(
180                 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
181                         + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
182                 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
183
184         InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
185         r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue()).setDegANum(destDegId.shortValue())
186                 .setTerminationPointA(destTpTx).setRdmZNode(nodeId.getValue()).setDegZNum(srcDegId.shortValue())
187                 .setTerminationPointZ(srcTpRx);
188         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), openRoadmTopology, dataBroker)) {
189             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
190                     destNodeId.getValue(), nodeId.getValue());
191             return false;
192         }
193         return true;
194     }
195
196     public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
197             String remoteInterfaceName) {
198         String srcTpTx = null;
199         String srcTpRx = null;
200         String destTpTx = null;
201         String destTpRx = null;
202         // Find which degree is associated with ethernet interface
203         // portmapping.getDegFromCP(nodeId,interfaceName);
204         Integer srcDegId = getDegFromCP(nodeId, interfaceName);
205         if (srcDegId == null) {
206             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
207             return false;
208         }
209         // Check whether degree is Unidirectional or Bidirectional by counting number of
210         // circuit-packs under degree subtree
211         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
212         if (Direction.NotApplicable == sourceDirection) {
213             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
214             return false;
215         } else if (Direction.Bidirectional == sourceDirection) {
216             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
217             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
218         } else {
219             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
220             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
221         }
222         // Find degree for which Ethernet interface is created on other end
223         NodeId destNodeId = new NodeId(remoteSystemName);
224         // portmapping.getDegFromCP(nodeId,interfaceName);
225         Integer destDegId = getDegFromCP(destNodeId, remoteInterfaceName);
226         if (destDegId == null) {
227             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
228             return false;
229         }
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);
235             return false;
236         } else if (Direction.Bidirectional == destinationDirection) {
237             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
238             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
239         } else {
240             destTpTx = "DEG" + destDegId + "-TTP-TX";
241             destTpRx = "DEG" + destDegId + "-TTP-RX";
242         }
243         return this.openRoadmTopology.deleteLink(nodeId.getValue(), destNodeId.getValue(), srcTpTx, destTpRx)
244                 && this.openRoadmTopology.deleteLink(destNodeId.getValue(), nodeId.getValue(), destTpTx, srcTpRx);
245     }
246
247     private CpToDegree getCPtoDegreeMapping(NodeId nodeId, String circuitPackName) {
248         InstanceIdentifier<CpToDegree> cptoDegMappingIID = InstanceIdentifier.builder(Network.class)
249                 .child(Nodes.class, new NodesKey(nodeId.getValue()))
250                 .child(CpToDegree.class, new CpToDegreeKey(circuitPackName)).build();
251         LOG.debug("Input parameters are {},{}", nodeId.getValue(), circuitPackName);
252         try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
253             Optional<CpToDegree> cptpDegObject = readTx.read(LogicalDatastoreType.CONFIGURATION, cptoDegMappingIID)
254                     .get().toJavaUtil();
255             if (cptpDegObject.isPresent()) {
256                 CpToDegree cpToDeg = cptpDegObject.get();
257                 LOG.debug("Found mapping for the Circuit Pack {}. Degree: {}", circuitPackName, cpToDeg);
258                 return cpToDeg;
259             } else {
260                 LOG.warn("Could not find mapping for Circuit Pack {} for nodeId {}", circuitPackName,
261                         nodeId.getValue());
262             }
263         } catch (InterruptedException | ExecutionException ex) {
264             LOG.error("Unable to read mapping for circuit pack : {} for nodeId {}", circuitPackName, nodeId, ex);
265         }
266         return null;
267     }
268
269     private Integer getDegFromParentCP(NodeId nodeId, String interfaceName, String supportingCircuitPack) {
270         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
271                 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPack));
272         Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
273                 LogicalDatastoreType.OPERATIONAL, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
274                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
275         if (!circuitPackObject.isPresent()
276                 || (circuitPackObject.get().getParentCircuitPack().getCircuitPackName() == null)) {
277             LOG.warn("Parent circuitpack not found for NodeId: {} and Interface: {}", nodeId, interfaceName);
278             return null;
279         }
280         String parentCP = circuitPackObject.get().getParentCircuitPack().getCircuitPackName();
281         CpToDegree cpToDegree = getCPtoDegreeMapping(nodeId, parentCP);
282         if (cpToDegree == null) {
283             LOG.error("CP to Degree mapping not found even with parent circuitpack for NodeID: {}" + "and Interface {}",
284                     nodeId, interfaceName);
285             return null;
286         } else {
287             LOG.debug("CP to degree is {}", cpToDegree.getDegreeNumber());
288             return cpToDegree.getDegreeNumber().intValue();
289         }
290     }
291
292     private Integer getDegFromCP(NodeId nodeId, String interfaceName) {
293         try {
294             java.util.Optional<Interface> interfaceOpt = this.openRoadmInterfaces.getInterface(nodeId.getValue(),
295                     interfaceName);
296             if (!interfaceOpt.isPresent()) {
297                 LOG.warn("Interface with {} on node {} was not found!", interfaceName, nodeId.getValue());
298                 return null;
299             }
300             String supportingCircuitPack = interfaceOpt.get().getSupportingCircuitPackName();
301             LOG.debug("Supporting circuitpack name is :{}", interfaceOpt.get().getSupportingCircuitPackName());
302             CpToDegree cpToDegree = getCPtoDegreeMapping(nodeId, supportingCircuitPack);
303             // Currently devices have different ways to represent connection to Ethernet port
304             // and degree port.
305             // If Circuit pack is not present under degree tree then read parent CP of given
306             // CP (Circuit pack).
307             if (cpToDegree != null) {
308                 return cpToDegree.getDegreeNumber().intValue();
309             } else {
310                 return getDegFromParentCP(nodeId, interfaceName, supportingCircuitPack);
311             }
312         } catch (OpenRoadmInterfaceException ex) {
313             LOG.error("Failed to get source interface {} from node {}!", interfaceName, nodeId, ex);
314             return null;
315         }
316     }
317 }