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