Replace tpce-topology yang by existing ordmodels
[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 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;
13
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;
46
47 public class R2RLinkDiscovery {
48
49     private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
50
51     private final DataBroker dataBroker;
52     private final NetworkTransactionService networkTransactionService;
53     private final DeviceTransactionManager deviceTransactionManager;
54
55     public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
56         NetworkTransactionService networkTransactionService) {
57         this.dataBroker = dataBroker;
58         this.deviceTransactionManager = deviceTransactionManager;
59         this.networkTransactionService = networkTransactionService;
60     }
61
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)
68                     .build();
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");
75                     return false;
76                 }
77                 // get neighbor list
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
85                     .builderOfInherited(
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)
89                     .child(
90                         org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
91                             .org.openroadm.device.Protocols.class)
92                     .build();
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");
100                     return false;
101                 }
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?");
109                 return false;
110             default:
111                 LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
112                 return false;
113         }
114     }
115
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());
124             } else {
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.
132                 } else {
133                     if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
134                         ifName.getRemotePortId())) {
135                         LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName
136                             .getRemoteSysName());
137                         success = false;
138                     }
139                 }
140             }
141         }
142         return success;
143     }
144
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());
151             } else {
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.
159                 } else {
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());
164                         success = false;
165                     }
166                 }
167             }
168         }
169         return success;
170     }
171
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) {
184                     return Direction.Tx;
185                 } else {
186                     return Direction.NotApplicable;
187                 }
188             }
189         } catch (InterruptedException | ExecutionException e) {
190             LOG.error("Failed getting Mapping data from portMapping",e);
191         }
192         return Direction.NotApplicable;
193     }
194
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);
205             return false;
206         }
207         // Check whether degree is Unidirectional or Bidirectional by counting
208         // 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         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
224         if (destDegId == null) {
225             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
226             return false;
227         }
228         // Check whether degree is Unidirectional or Bidirectional by counting
229         // 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         // A->Z
243         LOG.debug(
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());
253             return false;
254         }
255         // Z->A
256         LOG.debug(
257             "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
258                 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
259             destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
260
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());
271             return false;
272         }
273         return true;
274     }
275
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);
286             return false;
287         }
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);
293             return false;
294         } else if (Direction.Bidirectional == sourceDirection) {
295             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
296             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
297         } else {
298             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
299             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
300         }
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);
306             return false;
307         }
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);
313             return false;
314         } else if (Direction.Bidirectional == destinationDirection) {
315             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
316             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
317         } else {
318             destTpTx = "DEG" + destDegId + "-TTP-TX";
319             destTpRx = "DEG" + destDegId + "-TTP-RX";
320         }
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);
325     }
326
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();
341                     } else {
342                         LOG.debug("Not found so returning nothing");
343                         return null;
344                     }
345                 } else {
346                     LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
347                         nodeId.getValue(),interfaceName);
348                 }
349             } else {
350                 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
351                     nodeId.getValue());
352             }
353         } catch (InterruptedException | ExecutionException ex) {
354             LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);
355         }
356         return null;
357     }
358 }