Network topology and inventory init
[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,
54             DeviceTransactionManager deviceTransactionManager, OpenRoadmTopology openRoadmTopology,
55             OpenRoadmInterfaces openRoadmInterfaces) {
56         this.dataBroker = dataBroker;
57         this.deviceTransactionManager = deviceTransactionManager;
58         this.openRoadmTopology = openRoadmTopology;
59         this.openRoadmInterfaces = openRoadmInterfaces;
60     }
61
62     public boolean readLLDP(NodeId nodeId) {
63         InstanceIdentifier<Protocols> protocolsIID =
64                 InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Protocols.class);
65         Optional<Protocols> protocolObject =
66                 this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL,
67                     protocolsIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
68         if (!protocolObject.isPresent() || (protocolObject.get().getAugmentation(Protocols1.class) == null)) {
69             LOG.warn("LLDP subtree is missing : isolated openroadm device");
70             return false;
71         }
72         NbrList nbrList = protocolObject.get().getAugmentation(Protocols1.class).getLldp().getNbrList();
73         LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
74         for (IfName ifName : nbrList.getIfName()) {
75             if (ifName.getRemoteSysName() == null) {
76                 LOG.error("LLDP subtree is empty in the device for nodeId: {}",
77                         nodeId.getValue());
78                 return false;
79             }
80             Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName.getRemoteSysName());
81             if (!mps.isPresent()) {
82                 LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
83                 return false;
84             } else {
85                 if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
86                         ifName.getRemotePortId())) {
87                     LOG.warn("Link Creation failed between {} and {} nodes.", nodeId, ifName.getRemoteSysName());
88                     return false;
89                 }
90             }
91         }
92         return true;
93     }
94
95     public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
96         InstanceIdentifier<Degree> deviceIID =
97                 InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Degree.class, new DegreeKey(degreeCounter));
98         Optional<Degree> degreeObject =
99                 this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL,
100                     deviceIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
101         if (degreeObject.isPresent()) {
102             Integer connectionPortCount = degreeObject.get().getConnectionPorts().size();
103             if (connectionPortCount == 1) {
104                 return Direction.Bidirectional;
105             } else if (connectionPortCount > 1) {
106                 return Direction.Tx;
107             } else {
108                 return Direction.NotApplicable;
109             }
110         }  else {
111             LOG.error("Couldnt retrieve Degree object for nodeId: {} and DegreeNumbner: {}",
112                     nodeId.getValue(),degreeCounter);
113             return Direction.NotApplicable;
114         }
115     }
116
117     public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
118                                  String remoteInterfaceName) {
119         String srcTpTx = null;
120         String srcTpRx = null;
121         String destTpTx = null;
122         String destTpRx = null;
123         //Find which degree is associated with ethernet interface
124         //portmapping.getDegFromCP(nodeId,interfaceName);
125         Integer srcDegId = getDegFromCP(nodeId, interfaceName);
126         if (srcDegId == null) {
127             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
128             return false;
129         }
130         // Check whether degree is Unidirectional or Bidirectional by counting 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 number of
152         // circuit-packs under degree subtree
153         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
154         if (Direction.NotApplicable == destinationDirection) {
155             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
156             return false;
157         } else if (Direction.Bidirectional == destinationDirection) {
158             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
159             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
160         } else {
161             destTpTx = "DEG" + destDegId + "-TTP-TX";
162             destTpRx = "DEG" + destDegId + "-TTP-RX";
163         }
164         //A->Z
165         LOG.debug(
166             "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
167             nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
168         InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
169         r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue())
170                 .setDegANum(srcDegId.shortValue()).setTerminationPointA(srcTpTx)
171                 .setRdmZNode(destNodeId.getValue())
172                 .setDegZNum(destDegId.shortValue()).setTerminationPointZ(destTpRx);
173         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(),openRoadmTopology,
174                 dataBroker)) {
175             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction",
176                     nodeId.getValue(),destNodeId.getValue());
177             return false;
178         }
179         //Z->A
180         LOG.debug(
181                 "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
182                         + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
183                 destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
184
185         InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
186         r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue())
187                 .setDegANum(destDegId.shortValue()).setTerminationPointA(destTpTx)
188                 .setRdmZNode(nodeId.getValue())
189                 .setDegZNum(srcDegId.shortValue()).setTerminationPointZ(srcTpRx);
190         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(),openRoadmTopology,
191                 dataBroker)) {
192             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
193                     destNodeId.getValue(),nodeId.getValue());
194             return false;
195         }
196         return true;
197     }
198
199     public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
200             String remoteInterfaceName) {
201         String srcTpTx = null;
202         String srcTpRx = null;
203         String destTpTx = null;
204         String destTpRx = null;
205         //Find which degree is associated with ethernet interface
206         //portmapping.getDegFromCP(nodeId,interfaceName);
207         Integer srcDegId = getDegFromCP(nodeId, interfaceName);
208         if (srcDegId == null) {
209             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
210             return false;
211         }
212         // Check whether degree is Unidirectional or Bidirectional by counting number of
213         // circuit-packs under degree subtree
214         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
215         if (Direction.NotApplicable == sourceDirection) {
216             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
217             return false;
218         } else if (Direction.Bidirectional == sourceDirection) {
219             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
220             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
221         } else {
222             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
223             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
224         }
225         // Find degree for which Ethernet interface is created on other end
226         NodeId destNodeId = new NodeId(remoteSystemName);
227         //portmapping.getDegFromCP(nodeId,interfaceName);
228         Integer destDegId = getDegFromCP(destNodeId, remoteInterfaceName);
229         if (destDegId == null) {
230             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
231             return false;
232         }
233         // Check whether degree is Unidirectional or Bidirectional by counting number of
234         // circuit-packs under degree subtree
235         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
236         if (Direction.NotApplicable == destinationDirection) {
237             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
238             return false;
239         } else if (Direction.Bidirectional == destinationDirection) {
240             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
241             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
242         } else {
243             destTpTx = "DEG" + destDegId + "-TTP-TX";
244             destTpRx = "DEG" + destDegId + "-TTP-RX";
245         }
246         return this.openRoadmTopology.deleteLink(nodeId.getValue(), destNodeId.getValue(), srcTpTx, destTpRx)
247                     && this.openRoadmTopology.deleteLink(destNodeId.getValue(), nodeId.getValue(), destTpTx, srcTpRx);
248     }
249
250     private CpToDegree getCPtoDegreeMapping(NodeId nodeId, String circuitPackName) {
251         InstanceIdentifier<CpToDegree> cptoDegMappingIID =
252                 InstanceIdentifier.builder(Network.class).child(Nodes.class,
253                         new NodesKey(nodeId.getValue()))
254                         .child(CpToDegree.class, new CpToDegreeKey(circuitPackName)).build();
255         LOG.debug("Input parameters are {},{}", nodeId.getValue(), circuitPackName);
256         try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
257             Optional<CpToDegree> cptpDegObject =
258                     readTx.read(LogicalDatastoreType.CONFIGURATION, cptoDegMappingIID).get().toJavaUtil();
259             if (cptpDegObject.isPresent()) {
260                 CpToDegree cpToDeg = cptpDegObject.get();
261                 LOG.debug("Found mapping for the Circuit Pack {}. Degree: {}", circuitPackName, cpToDeg);
262                 return cpToDeg;
263             } else {
264                 LOG.warn("Could not find mapping for Circuit Pack {} for nodeId {}", circuitPackName,
265                         nodeId.getValue());
266             }
267         } catch (InterruptedException | ExecutionException ex) {
268             LOG.error("Unable to read mapping for circuit pack : {} for nodeId {}", circuitPackName, nodeId, ex);
269         }
270         return null;
271     }
272
273     private Integer getDegFromParentCP(NodeId nodeId, String interfaceName, String supportingCircuitPack) {
274         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
275                 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPack));
276         Optional<CircuitPacks> circuitPackObject =
277                 this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL,
278                         circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
279         if (!circuitPackObject.isPresent()
280                 || (circuitPackObject.get().getParentCircuitPack().getCircuitPackName() == null)) {
281             LOG.warn("Parent circuitpack not found for NodeId: {} and Interface: {}", nodeId, interfaceName);
282             return null;
283         }
284         String parentCP = circuitPackObject.get().getParentCircuitPack().getCircuitPackName();
285         CpToDegree cpToDegree = getCPtoDegreeMapping(nodeId, parentCP);
286         if (cpToDegree == null) {
287             LOG.error("CP to Degree mapping not found even with parent circuitpack for NodeID: {}" + "and Interface {}",
288                     nodeId, interfaceName);
289             return null;
290         } else {
291             LOG.debug("CP to degree is {}", cpToDegree.getDegreeNumber());
292             return cpToDegree.getDegreeNumber().intValue();
293         }
294     }
295
296     private Integer getDegFromCP(NodeId nodeId, String interfaceName) {
297         try {
298             java.util.Optional<Interface> interfaceOpt = this.openRoadmInterfaces.getInterface(nodeId.getValue(),
299                     interfaceName);
300             if (!interfaceOpt.isPresent()) {
301                 LOG.warn("Interface with {} on node {} was not found!", interfaceName, nodeId.getValue());
302                 return null;
303             }
304             String supportingCircuitPack = interfaceOpt.get().getSupportingCircuitPackName();
305             LOG.debug("Supporting circuitpack name is :{}",
306                     interfaceOpt.get().getSupportingCircuitPackName());
307             CpToDegree cpToDegree = getCPtoDegreeMapping(nodeId, supportingCircuitPack);
308             // Currently devices have different ways to represent connection to Ethernet port and degree port
309             // If Circuit pack is not present under degree tree then read parent CP of given CP(Circuit pack)
310             if (cpToDegree != null) {
311                 return cpToDegree.getDegreeNumber().intValue();
312             } else {
313                 return getDegFromParentCP(nodeId, interfaceName, supportingCircuitPack);
314             }
315         } catch (OpenRoadmInterfaceException ex) {
316             LOG.error("Failed to get source interface {} from node {}!", interfaceName, nodeId, ex);
317             return null;
318         }
319     }
320 }