update portmapping YANG to store SAPI/DAPI hashmap
[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 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.MountPoint;
21 import org.opendaylight.mdsal.binding.api.ReadTransaction;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.transportpce.common.Timeouts;
24 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
25 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
26 import org.opendaylight.transportpce.networkmodel.util.TopologyUtils;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev170818.InitRoadmNodesInputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.Network;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.Nodes;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.NodesKey;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.CpToDegree;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.Mapping;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev170929.Direction;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.NbrList;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.Uint8;
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
53     public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
54         NetworkTransactionService networkTransactionService) {
55         this.dataBroker = dataBroker;
56         this.deviceTransactionManager = deviceTransactionManager;
57         this.networkTransactionService = networkTransactionService;
58     }
59
60     public boolean readLLDP(NodeId nodeId, String nodeVersion) {
61
62         if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_1_2_1)) {
63             InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
64                     .child(Protocols.class);
65             Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
66                 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
67                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
68             if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
69                 LOG.warn("LLDP subtree is missing : isolated openroadm device");
70                 return false;
71             }
72             NbrList nbrList = protocolObject.get().augmentation(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.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
77                         nodeId.getValue(),ifName.getIfName());
78                 } else {
79                     Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
80                         .getRemoteSysName());
81                     if (!mps.isPresent()) {
82                         LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
83                         // The controller raises a warning rather than an error because the first node to
84                         // mount cannot see its neighbors yet. The link will be detected when processing
85                         // the neighbor node.
86                     } else {
87                         if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
88                             ifName.getRemotePortId())) {
89                             LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
90                                 ifName.getRemoteSysName());
91                             return false;
92                         }
93                     }
94                 }
95             }
96             return true;
97         }
98         else if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_2_2_1)) {
99             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
100                 .container.org.openroadm.device.Protocols> protocolsIID = InstanceIdentifier.create(org.opendaylight
101                 .yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
102                 .OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
103                 .org.openroadm.device.container.org.openroadm.device.Protocols.class);
104             Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
105                 .container.org.openroadm.device.Protocols> protocolObject = this.deviceTransactionManager
106                 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocolsIID,
107                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
108             if (!protocolObject.isPresent() || (protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org
109                 .openroadm.lldp.rev181019.Protocols1.class) == null)) {
110                 LOG.warn("LLDP subtree is missing : isolated openroadm device");
111                 return false;
112             }
113             org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList nbrList
114                 = protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019
115                 .Protocols1.class).getLldp().getNbrList();
116             LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
117             for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
118                 ifName : nbrList.getIfName()) {
119                 if (ifName.getRemoteSysName() == null) {
120                     LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
121                         nodeId.getValue(),ifName.getIfName());
122                 } else {
123                     Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
124                         .getRemoteSysName());
125                     if (!mps.isPresent()) {
126                         LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
127                         // The controller raises a warning rather than an error because the first node to
128                         // mount cannot see its neighbors yet. The link will be detected when processing
129                         // the neighbor node.
130                     } else {
131                         if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
132                             ifName.getRemotePortId())) {
133                             LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName
134                                 .getRemoteSysName());
135                             return false;
136                         }
137                     }
138                 }
139             }
140             return true;
141         }
142         else {
143             LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
144             return false;
145         }
146     }
147
148     public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
149         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
150             .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
151         try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
152             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
153             if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
154                 List<Mapping> mappingList = nodesObject.get().getMapping();
155                 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
156                     + degreeCounter)).collect(Collectors.toList());
157                 if (mappingList.size() == 1) {
158                     return Direction.Bidirectional;
159                 } else if (mappingList.size() > 1) {
160                     return Direction.Tx;
161                 } else {
162                     return Direction.NotApplicable;
163                 }
164             }
165         } catch (InterruptedException | ExecutionException e) {
166             LOG.error("Failed getting Mapping data from portMapping",e);
167         }
168         return Direction.NotApplicable;
169     }
170
171     public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
172                                  String remoteInterfaceName) {
173         String srcTpTx = null;
174         String srcTpRx = null;
175         String destTpTx = null;
176         String destTpRx = null;
177         // Find which degree is associated with ethernet interface
178         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
179         if (srcDegId == null) {
180             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
181             return false;
182         }
183         // Check whether degree is Unidirectional or Bidirectional by counting
184         // number of
185         // circuit-packs under degree subtree
186         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
187         if (Direction.NotApplicable == sourceDirection) {
188             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
189             return false;
190         } else if (Direction.Bidirectional == sourceDirection) {
191             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
192             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
193         } else {
194             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
195             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
196         }
197         // Find degree for which Ethernet interface is created on other end
198         NodeId destNodeId = new NodeId(remoteSystemName);
199         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
200         if (destDegId == null) {
201             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
202             return false;
203         }
204         // Check whether degree is Unidirectional or Bidirectional by counting
205         // number of
206         // circuit-packs under degree subtree
207         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
208         if (Direction.NotApplicable == destinationDirection) {
209             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
210             return false;
211         } else if (Direction.Bidirectional == destinationDirection) {
212             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
213             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
214         } else {
215             destTpTx = "DEG" + destDegId + "-TTP-TX";
216             destTpRx = "DEG" + destDegId + "-TTP-RX";
217         }
218         // A->Z
219         LOG.debug(
220             "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
221             nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
222         InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
223         r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(Uint8.valueOf(srcDegId))
224             .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(Uint8.valueOf(destDegId))
225             .setTerminationPointZ(destTpRx);
226         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.dataBroker)) {
227             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
228                 destNodeId.getValue());
229             return false;
230         }
231         // Z->A
232         LOG.debug(
233             "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
234                 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
235             destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
236
237         InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder()
238             .setRdmANode(destNodeId.getValue())
239             .setDegANum(Uint8.valueOf(destDegId))
240             .setTerminationPointA(destTpTx)
241             .setRdmZNode(nodeId.getValue())
242             .setDegZNum(Uint8.valueOf(srcDegId))
243             .setTerminationPointZ(srcTpRx);
244         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), 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 TopologyUtils.deleteLink(nodeId.getValue() + "-" + srcDegId, destNodeId.getValue() + "-" + destDegId,
298             srcTpTx, destTpRx, networkTransactionService)
299             && TopologyUtils.deleteLink(destNodeId.getValue() + "-" + destDegId, nodeId.getValue() + "-" + srcDegId,
300                 destTpTx, srcTpRx, networkTransactionService);
301     }
302
303     private Integer getDegFromInterface(NodeId nodeId, String interfaceName) {
304         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
305             .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
306         try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
307             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
308             if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
309                 List<CpToDegree> cpToDeg = nodesObject.get().getCpToDegree();
310                 Stream cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
311                     .filter(cp -> cp.getInterfaceName().equals(interfaceName));
312                 if (cpToDegStream != null) {
313                     @SuppressWarnings("unchecked") Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
314                     if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
315                         LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
316                         return firstCpToDegree.get().getDegreeNumber().intValue();
317                     } else {
318                         LOG.debug("Not found so returning nothing");
319                         return null;
320                     }
321                 } else {
322                     LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
323                         nodeId.getValue(),interfaceName);
324                 }
325             } else {
326                 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
327                     nodeId.getValue());
328             }
329         } catch (InterruptedException | ExecutionException ex) {
330             LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);
331         }
332         return null;
333     }
334 }