upgrade portmapping YANG to introduce OTN
[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.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.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.rev200113.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.NodesKey;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.CpToDegree;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.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.rev180226.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 (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
154             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
155             if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
156                 List<Mapping> mappingList = nodesObject.get().getMapping();
157                 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
158                     + degreeCounter)).collect(Collectors.toList());
159                 if (mappingList.size() == 1) {
160                     return Direction.Bidirectional;
161                 } else if (mappingList.size() > 1) {
162                     return Direction.Tx;
163                 } else {
164                     return Direction.NotApplicable;
165                 }
166             }
167         } catch (InterruptedException | ExecutionException e) {
168             LOG.error("Failed getting Mapping data from portMapping",e);
169         }
170         return Direction.NotApplicable;
171     }
172
173     public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
174                                  String remoteInterfaceName) {
175         String srcTpTx = null;
176         String srcTpRx = null;
177         String destTpTx = null;
178         String destTpRx = null;
179         // Find which degree is associated with ethernet interface
180         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
181         if (srcDegId == null) {
182             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
183             return false;
184         }
185         // Check whether degree is Unidirectional or Bidirectional by counting
186         // number of
187         // circuit-packs under degree subtree
188         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
189         if (Direction.NotApplicable == sourceDirection) {
190             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
191             return false;
192         } else if (Direction.Bidirectional == sourceDirection) {
193             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
194             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
195         } else {
196             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
197             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
198         }
199         // Find degree for which Ethernet interface is created on other end
200         NodeId destNodeId = new NodeId(remoteSystemName);
201         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
202         if (destDegId == null) {
203             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
204             return false;
205         }
206         // Check whether degree is Unidirectional or Bidirectional by counting
207         // number of
208         // circuit-packs under degree subtree
209         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
210         if (Direction.NotApplicable == destinationDirection) {
211             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
212             return false;
213         } else if (Direction.Bidirectional == destinationDirection) {
214             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
215             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
216         } else {
217             destTpTx = "DEG" + destDegId + "-TTP-TX";
218             destTpRx = "DEG" + destDegId + "-TTP-RX";
219         }
220         // A->Z
221         LOG.debug(
222             "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
223             nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
224         InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
225         r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(srcDegId.shortValue())
226             .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(destDegId.shortValue())
227             .setTerminationPointZ(destTpRx);
228         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.openRoadmFactory, this.dataBroker)) {
229             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
230                 destNodeId.getValue());
231             return false;
232         }
233         // Z->A
234         LOG.debug(
235             "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
236                 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
237             destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
238
239         InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder();
240         r2rlinkBuilderZToA.setRdmANode(destNodeId.getValue()).setDegANum(destDegId.shortValue())
241             .setTerminationPointA(destTpTx).setRdmZNode(nodeId.getValue()).setDegZNum(srcDegId.shortValue())
242             .setTerminationPointZ(srcTpRx);
243         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.openRoadmFactory, this.dataBroker)) {
244             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
245                 destNodeId.getValue(), nodeId.getValue());
246             return false;
247         }
248         return true;
249     }
250
251     public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
252                                  String remoteInterfaceName) {
253         String srcTpTx = null;
254         String srcTpRx = null;
255         String destTpTx = null;
256         String destTpRx = null;
257         // Find which degree is associated with ethernet interface
258         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
259         if (srcDegId == null) {
260             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
261             return false;
262         }
263         // Check whether degree is Unidirectional or Bidirectional by counting number of
264         // circuit-packs under degree subtree
265         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
266         if (Direction.NotApplicable == sourceDirection) {
267             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
268             return false;
269         } else if (Direction.Bidirectional == sourceDirection) {
270             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
271             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
272         } else {
273             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
274             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
275         }
276         // Find degree for which Ethernet interface is created on other end
277         NodeId destNodeId = new NodeId(remoteSystemName);
278         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
279         if (destDegId == null) {
280             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
281             return false;
282         }
283         // Check whether degree is Unidirectional or Bidirectional by counting number of
284         // circuit-packs under degree subtree
285         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
286         if (Direction.NotApplicable == destinationDirection) {
287             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
288             return false;
289         } else if (Direction.Bidirectional == destinationDirection) {
290             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
291             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
292         } else {
293             destTpTx = "DEG" + destDegId + "-TTP-TX";
294             destTpRx = "DEG" + destDegId + "-TTP-RX";
295         }
296         return this.openRoadmFactory.deleteLink(nodeId.getValue() + "-" + srcDegId.toString(),
297                 destNodeId.getValue() + "-" + destDegId.toString(),
298                 srcTpTx.toString(), destTpRx.toString(),networkTransactionService)
299             && this.openRoadmFactory.deleteLink(destNodeId.getValue() + "-" + destDegId.toString(),
300                 nodeId.getValue() + "-" + srcDegId.toString(), destTpTx, srcTpRx,
301                 networkTransactionService);
302     }
303
304     private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
305         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
306             .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
307         try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
308             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
309             if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
310                 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
311                 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
312                     .filter(cp -> cp.getInterfaceName().equals(interfaceName));
313                 if (cpToDegStream != null) {
314                     @SuppressWarnings("unchecked") Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
315                     if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
316                         LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
317                         return firstCpToDegree.get().getDegreeNumber().intValue();
318                     } else {
319                         LOG.debug("Not found so returning nothing");
320                         return null;
321                     }
322                 } else {
323                     LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
324                         nodeId.getValue(),interfaceName);
325                 }
326             } else {
327                 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
328                     nodeId.getValue());
329             }
330         } catch (InterruptedException | ExecutionException ex) {
331             LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);
332         }
333         return null;
334     }
335 }