Reintroduce (ordm 2.2.1) Rdm2Rdm links in topo
[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
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.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.OpenRoadmFactory;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev170818.InitRoadmNodesInputBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.NodesKey;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.nodes.CpToDegree;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev170228.network.nodes.Mapping;
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.org.openroadm.device.container.OrgOpenroadmDevice;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class R2RLinkDiscovery {
46
47     private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
48
49     private final DataBroker dataBroker;
50     private final NetworkTransactionService networkTransactionService;
51     private final DeviceTransactionManager deviceTransactionManager;
52     private final OpenRoadmFactory openRoadmFactory;
53
54     public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
55         OpenRoadmFactory openRoadmFactory, NetworkTransactionService networkTransactionService) {
56         this.dataBroker = dataBroker;
57         this.deviceTransactionManager = deviceTransactionManager;
58         this.openRoadmFactory = openRoadmFactory;
59         this.networkTransactionService = networkTransactionService;
60     }
61
62     public boolean readLLDP(NodeId nodeId, String nodeVersion) {
63
64         if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_1_2_1)) {
65             InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
66                     .child(Protocols.class);
67             Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
68                 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
69                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
70             if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
71                 LOG.warn("LLDP subtree is missing : isolated openroadm device");
72                 return false;
73             }
74             NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
75             LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
76             for (IfName ifName : nbrList.getIfName()) {
77                 if (ifName.getRemoteSysName() == null) {
78                     LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
79                         nodeId.getValue(),ifName.getIfName());
80                 } else {
81                     Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
82                         .getRemoteSysName());
83                     if (!mps.isPresent()) {
84                         LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
85                         // The controller raises a warning rather than an error because the first node to
86                         // mount cannot see its neighbors yet. The link will be detected when processing
87                         // the neighbor node.
88                     } else {
89                         if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
90                             ifName.getRemotePortId())) {
91                             LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
92                                 ifName.getRemoteSysName());
93                             return false;
94                         }
95                     }
96                 }
97             }
98             return true;
99         }
100         else if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_2_2_1)) {
101             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
102                 .container.org.openroadm.device.Protocols> protocolsIID = InstanceIdentifier.create(org.opendaylight
103                 .yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
104                 .OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
105                 .org.openroadm.device.container.org.openroadm.device.Protocols.class);
106             Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
107                 .container.org.openroadm.device.Protocols> protocolObject = this.deviceTransactionManager
108                 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocolsIID,
109                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
110             if (!protocolObject.isPresent() || (protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org
111                 .openroadm.lldp.rev181019.Protocols1.class) == null)) {
112                 LOG.warn("LLDP subtree is missing : isolated openroadm device");
113                 return false;
114             }
115             org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList nbrList
116                 = protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019
117                 .Protocols1.class).getLldp().getNbrList();
118             LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
119             for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
120                 ifName : nbrList.getIfName()) {
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                             return false;
138                         }
139                     }
140                 }
141             }
142             return true;
143         }
144         else {
145             LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
146             return false;
147         }
148     }
149
150     public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
151         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
152             .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
153         try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
154             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
155                 .get().toJavaUtil();
156             if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
157                 List<Mapping> mappingList = nodesObject.get().getMapping();
158                 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
159                     + degreeCounter)).collect(Collectors.toList());
160                 if (mappingList.size() == 1) {
161                     return Direction.Bidirectional;
162                 } else if (mappingList.size() > 1) {
163                     return Direction.Tx;
164                 } else {
165                     return Direction.NotApplicable;
166                 }
167             }
168         } catch (InterruptedException | ExecutionException e) {
169             LOG.error("Failed getting Mapping data from portMapping",e);
170         }
171         return Direction.NotApplicable;
172     }
173
174     public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
175                                  String remoteInterfaceName) {
176         String srcTpTx = null;
177         String srcTpRx = null;
178         String destTpTx = null;
179         String destTpRx = null;
180         // Find which degree is associated with ethernet interface
181         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
182         if (srcDegId == null) {
183             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
184             return false;
185         }
186         // Check whether degree is Unidirectional or Bidirectional by counting
187         // number of
188         // circuit-packs under degree subtree
189         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
190         if (Direction.NotApplicable == sourceDirection) {
191             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
192             return false;
193         } else if (Direction.Bidirectional == sourceDirection) {
194             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
195             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
196         } else {
197             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
198             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
199         }
200         // Find degree for which Ethernet interface is created on other end
201         NodeId destNodeId = new NodeId(remoteSystemName);
202         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
203         if (destDegId == 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 destinationDirection = getDegreeDirection(destDegId, destNodeId);
211         if (Direction.NotApplicable == destinationDirection) {
212             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
213             return false;
214         } else if (Direction.Bidirectional == destinationDirection) {
215             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
216             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
217         } else {
218             destTpTx = "DEG" + destDegId + "-TTP-TX";
219             destTpRx = "DEG" + destDegId + "-TTP-RX";
220         }
221         // A->Z
222         LOG.debug(
223             "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
224             nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
225         InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
226         r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
227             .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
228             .setTerminationPointZ(destTpRx);
229         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.openRoadmFactory, this.dataBroker)) {
230             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
231                 destNodeId.getValue());
232             return false;
233         }
234         // Z->A
235         LOG.debug(
236             "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
237                 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
238             destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
239
240         InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
241         r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue()).setDegANum(destDegId.shortValue())
242             .setTerminationPointA(destTpTx).setRdmZNode(nodeId.getValue()).setDegZNum(srcDegId.shortValue())
243             .setTerminationPointZ(srcTpRx);
244         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.openRoadmFactory, this.dataBroker)) {
245             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
246                 destNodeId.getValue(), nodeId.getValue());
247             return false;
248         }
249         return true;
250     }
251
252     public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
253                                  String remoteInterfaceName) {
254         String srcTpTx = null;
255         String srcTpRx = null;
256         String destTpTx = null;
257         String destTpRx = null;
258         // Find which degree is associated with ethernet interface
259         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
260         if (srcDegId == null) {
261             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
262             return false;
263         }
264         // Check whether degree is Unidirectional or Bidirectional by counting number of
265         // circuit-packs under degree subtree
266         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
267         if (Direction.NotApplicable == sourceDirection) {
268             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
269             return false;
270         } else if (Direction.Bidirectional == sourceDirection) {
271             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
272             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
273         } else {
274             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
275             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
276         }
277         // Find degree for which Ethernet interface is created on other end
278         NodeId destNodeId = new NodeId(remoteSystemName);
279         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
280         if (destDegId == null) {
281             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
282             return false;
283         }
284         // Check whether degree is Unidirectional or Bidirectional by counting number of
285         // circuit-packs under degree subtree
286         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
287         if (Direction.NotApplicable == destinationDirection) {
288             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
289             return false;
290         } else if (Direction.Bidirectional == destinationDirection) {
291             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
292             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
293         } else {
294             destTpTx = "DEG" + destDegId + "-TTP-TX";
295             destTpRx = "DEG" + destDegId + "-TTP-RX";
296         }
297         return this.openRoadmFactory.deleteLink(nodeId.getValue() + "-" + srcDegId.toString(),
298                 destNodeId.getValue() + "-" + destDegId.toString(),
299                 srcTpTx.toString(), destTpRx.toString(),networkTransactionService)
300             && this.openRoadmFactory.deleteLink(destNodeId.getValue() + "-" + destDegId.toString(),
301                 nodeId.getValue() + "-" + srcDegId.toString(), destTpTx, srcTpRx,
302                 networkTransactionService);
303     }
304
305     private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
306         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
307             .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
308         try (ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
309             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID)
310                 .get().toJavaUtil();
311             if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
312                 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
313                 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
314                     .filter(cp -> cp.getInterfaceName().equals(interfaceName));
315                 if (cpToDegStream != null) {
316                     Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
317                     if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
318                         LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
319                         return firstCpToDegree.get().getDegreeNumber().intValue();
320                     } else {
321                         LOG.debug("Not found so returning nothing");
322                         return null;
323                     }
324                 } else {
325                     LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
326                         nodeId.getValue(),interfaceName);
327                 }
328             } else {
329                 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
330                     nodeId.getValue());
331             }
332         } catch (InterruptedException | ExecutionException ex) {
333             LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);
334         }
335         return null;
336     }
337 }