Update portmapping YANG model
[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.Collection;
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.rev210315.Network;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.cp.to.degree.CpToDegree;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.network.Nodes;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.network.NodesKey;
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             boolean success = true;
75             for (IfName ifName : nbrList.nonnullIfName().values()) {
76                 if (ifName.getRemoteSysName() == null) {
77                     LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
78                         nodeId.getValue(),ifName.getIfName());
79                 } else {
80                     Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
81                         .getRemoteSysName());
82                     if (!mps.isPresent()) {
83                         LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
84                         // The controller raises a warning rather than an error because the first node to
85                         // mount cannot see its neighbors yet. The link will be detected when processing
86                         // the neighbor node.
87                     } else {
88                         if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
89                             ifName.getRemotePortId())) {
90                             LOG.error("Link Creation failed between {} and {} nodes.", nodeId.getValue(),
91                                 ifName.getRemoteSysName());
92                             success = false;
93                         }
94                     }
95                 }
96             }
97             return success;
98         }
99         else if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_2_2_1)) {
100             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
101                 .container.org.openroadm.device.Protocols> protocolsIID = InstanceIdentifier.create(org.opendaylight
102                 .yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
103                 .OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019
104                 .org.openroadm.device.container.org.openroadm.device.Protocols.class);
105             Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
106                 .container.org.openroadm.device.Protocols> protocolObject = this.deviceTransactionManager
107                 .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocolsIID,
108                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
109             if (!protocolObject.isPresent() || (protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org
110                 .openroadm.lldp.rev181019.Protocols1.class) == null)) {
111                 LOG.warn("LLDP subtree is missing : isolated openroadm device");
112                 return false;
113             }
114             org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList nbrList
115                 = protocolObject.get().augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019
116                 .Protocols1.class).getLldp().getNbrList();
117             LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
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         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                 Collection<Mapping> mappingList = nodesObject.get().nonnullMapping().values();
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(Uint8.valueOf(srcDegId))
226             .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(Uint8.valueOf(destDegId))
227             .setTerminationPointZ(destTpRx);
228         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), 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             .setRdmANode(destNodeId.getValue())
241             .setDegANum(Uint8.valueOf(destDegId))
242             .setTerminationPointA(destTpTx)
243             .setRdmZNode(nodeId.getValue())
244             .setDegZNum(Uint8.valueOf(srcDegId))
245             .setTerminationPointZ(srcTpRx);
246         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.dataBroker)) {
247             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
248                 destNodeId.getValue(), nodeId.getValue());
249             return false;
250         }
251         return true;
252     }
253
254     public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
255                                  String remoteInterfaceName) {
256         String srcTpTx = null;
257         String srcTpRx = null;
258         String destTpTx = null;
259         String destTpRx = null;
260         // Find which degree is associated with ethernet interface
261         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
262         if (srcDegId == null) {
263             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
264             return false;
265         }
266         // Check whether degree is Unidirectional or Bidirectional by counting number of
267         // circuit-packs under degree subtree
268         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
269         if (Direction.NotApplicable == sourceDirection) {
270             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
271             return false;
272         } else if (Direction.Bidirectional == sourceDirection) {
273             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
274             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
275         } else {
276             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
277             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
278         }
279         // Find degree for which Ethernet interface is created on other end
280         NodeId destNodeId = new NodeId(remoteSystemName);
281         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
282         if (destDegId == null) {
283             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
284             return false;
285         }
286         // Check whether degree is Unidirectional or Bidirectional by counting number of
287         // circuit-packs under degree subtree
288         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
289         if (Direction.NotApplicable == destinationDirection) {
290             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
291             return false;
292         } else if (Direction.Bidirectional == destinationDirection) {
293             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
294             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
295         } else {
296             destTpTx = "DEG" + destDegId + "-TTP-TX";
297             destTpRx = "DEG" + destDegId + "-TTP-RX";
298         }
299         return TopologyUtils.deleteLink(nodeId.getValue() + "-" + srcDegId, destNodeId.getValue() + "-" + destDegId,
300             srcTpTx, destTpRx, networkTransactionService)
301             && TopologyUtils.deleteLink(destNodeId.getValue() + "-" + destDegId, nodeId.getValue() + "-" + srcDegId,
302                 destTpTx, srcTpRx, 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 (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
309             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
310             if (nodesObject.isPresent() && (nodesObject.get().getCpToDegree() != null)) {
311                 Collection<CpToDegree> cpToDeg = nodesObject.get().nonnullCpToDegree().values();
312                 Stream<CpToDegree> cpToDegStream = cpToDeg.stream().filter(cp -> cp.getInterfaceName() != null)
313                     .filter(cp -> cp.getInterfaceName().equals(interfaceName));
314                 if (cpToDegStream != null) {
315                     @SuppressWarnings("unchecked") Optional<CpToDegree> firstCpToDegree = cpToDegStream.findFirst();
316                     if (firstCpToDegree.isPresent() && (firstCpToDegree != null)) {
317                         LOG.debug("Found and returning {}",firstCpToDegree.get().getDegreeNumber().intValue());
318                         return firstCpToDegree.get().getDegreeNumber().intValue();
319                     } else {
320                         LOG.debug("Not found so returning nothing");
321                         return null;
322                     }
323                 } else {
324                     LOG.warn("CircuitPack stream couldnt find anything for nodeId: {} and interfaceName: {}",
325                         nodeId.getValue(),interfaceName);
326                 }
327             } else {
328                 LOG.warn("Could not find mapping for Interface {} for nodeId {}", interfaceName,
329                     nodeId.getValue());
330             }
331         } catch (InterruptedException | ExecutionException ex) {
332             LOG.error("Unable to read mapping for Interface : {} for nodeId {}", interfaceName, nodeId, ex);
333         }
334         return null;
335     }
336 }