Fix spotbugs issues in network topologies module
[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.TopologyUtils;
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.rev200128.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.NodesKey;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.nodes.CpToDegree;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.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.opendaylight.yangtools.yang.common.Uint8;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class R2RLinkDiscovery {
47
48     private static final Logger LOG = LoggerFactory.getLogger(R2RLinkDiscovery.class);
49
50     private final DataBroker dataBroker;
51     private final NetworkTransactionService networkTransactionService;
52     private final DeviceTransactionManager deviceTransactionManager;
53
54     public R2RLinkDiscovery(final DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
55         NetworkTransactionService networkTransactionService) {
56         this.dataBroker = dataBroker;
57         this.deviceTransactionManager = deviceTransactionManager;
58         this.networkTransactionService = networkTransactionService;
59     }
60
61     public boolean readLLDP(NodeId nodeId, String nodeVersion) {
62
63         if (nodeVersion.equals(OPENROADM_DEVICE_VERSION_1_2_1)) {
64             InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
65                     .child(Protocols.class);
66             Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId.getValue(),
67                 LogicalDatastoreType.OPERATIONAL, protocolsIID, Timeouts.DEVICE_READ_TIMEOUT,
68                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
69             if (!protocolObject.isPresent() || (protocolObject.get().augmentation(Protocols1.class) == null)) {
70                 LOG.warn("LLDP subtree is missing : isolated openroadm device");
71                 return false;
72             }
73             NbrList nbrList = protocolObject.get().augmentation(Protocols1.class).getLldp().getNbrList();
74             LOG.info("LLDP subtree is present. Device has {} neighbours", nbrList.getIfName().size());
75             for (IfName ifName : nbrList.getIfName()) {
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                             return false;
93                         }
94                     }
95                 }
96             }
97             return true;
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             for (org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.nbr.list.IfName
119                 ifName : nbrList.getIfName()) {
120                 if (ifName.getRemoteSysName() == null) {
121                     LOG.warn("LLDP subtree neighbour is empty for nodeId: {}, ifName: {}",
122                         nodeId.getValue(),ifName.getIfName());
123                 } else {
124                     Optional<MountPoint> mps = this.deviceTransactionManager.getDeviceMountPoint(ifName
125                         .getRemoteSysName());
126                     if (!mps.isPresent()) {
127                         LOG.warn("Neighbouring nodeId: {} is not mounted yet", ifName.getRemoteSysName());
128                         // The controller raises a warning rather than an error because the first node to
129                         // mount cannot see its neighbors yet. The link will be detected when processing
130                         // the neighbor node.
131                     } else {
132                         if (!createR2RLink(nodeId, ifName.getIfName(), ifName.getRemoteSysName(),
133                             ifName.getRemotePortId())) {
134                             LOG.error("Link Creation failed between {} and {} nodes.", nodeId, ifName
135                                 .getRemoteSysName());
136                             return false;
137                         }
138                     }
139                 }
140             }
141             return true;
142         }
143         else {
144             LOG.error("Unable to read LLDP data for unmanaged openroadm device version");
145             return false;
146         }
147     }
148
149     public Direction getDegreeDirection(Integer degreeCounter, NodeId nodeId) {
150         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.builder(Network.class)
151             .child(Nodes.class, new NodesKey(nodeId.getValue())).build();
152         try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
153             Optional<Nodes> nodesObject = readTx.read(LogicalDatastoreType.CONFIGURATION, nodesIID).get();
154             if (nodesObject.isPresent() && (nodesObject.get().getMapping() != null)) {
155                 List<Mapping> mappingList = nodesObject.get().getMapping();
156                 mappingList = mappingList.stream().filter(mp -> mp.getLogicalConnectionPoint().contains("DEG"
157                     + degreeCounter)).collect(Collectors.toList());
158                 if (mappingList.size() == 1) {
159                     return Direction.Bidirectional;
160                 } else if (mappingList.size() > 1) {
161                     return Direction.Tx;
162                 } else {
163                     return Direction.NotApplicable;
164                 }
165             }
166         } catch (InterruptedException | ExecutionException e) {
167             LOG.error("Failed getting Mapping data from portMapping",e);
168         }
169         return Direction.NotApplicable;
170     }
171
172     public boolean createR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
173                                  String remoteInterfaceName) {
174         String srcTpTx = null;
175         String srcTpRx = null;
176         String destTpTx = null;
177         String destTpRx = null;
178         // Find which degree is associated with ethernet interface
179         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
180         if (srcDegId == null) {
181             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
182             return false;
183         }
184         // Check whether degree is Unidirectional or Bidirectional by counting
185         // number of
186         // circuit-packs under degree subtree
187         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
188         if (Direction.NotApplicable == sourceDirection) {
189             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
190             return false;
191         } else if (Direction.Bidirectional == sourceDirection) {
192             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
193             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
194         } else {
195             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
196             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
197         }
198         // Find degree for which Ethernet interface is created on other end
199         NodeId destNodeId = new NodeId(remoteSystemName);
200         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
201         if (destDegId == null) {
202             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
203             return false;
204         }
205         // Check whether degree is Unidirectional or Bidirectional by counting
206         // number of
207         // circuit-packs under degree subtree
208         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
209         if (Direction.NotApplicable == destinationDirection) {
210             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
211             return false;
212         } else if (Direction.Bidirectional == destinationDirection) {
213             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
214             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
215         } else {
216             destTpTx = "DEG" + destDegId + "-TTP-TX";
217             destTpRx = "DEG" + destDegId + "-TTP-RX";
218         }
219         // A->Z
220         LOG.debug(
221             "Found a neighbor SrcNodeId: {} , SrcDegId: {} , SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
222             nodeId.getValue(), srcDegId, srcTpTx, destNodeId, destDegId, destTpRx);
223         InitRoadmNodesInputBuilder r2rlinkBuilderAToZ = new InitRoadmNodesInputBuilder();
224         r2rlinkBuilderAToZ.setRdmANode(nodeId.getValue()).setDegANum(Uint8.valueOf(srcDegId))
225             .setTerminationPointA(srcTpTx).setRdmZNode(destNodeId.getValue()).setDegZNum(Uint8.valueOf(destDegId))
226             .setTerminationPointZ(destTpRx);
227         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderAToZ.build(), this.dataBroker)) {
228             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in A->Z direction", nodeId.getValue(),
229                 destNodeId.getValue());
230             return false;
231         }
232         // Z->A
233         LOG.debug(
234             "Found a neighbor SrcNodeId: {} , SrcDegId: {}"
235                 + ", SrcTPId: {}, DestNodeId:{} , DestDegId: {}, DestTPId: {}",
236             destNodeId, destDegId, destTpTx, nodeId.getValue(), srcDegId, srcTpRx);
237
238         InitRoadmNodesInputBuilder r2rlinkBuilderZToA = new InitRoadmNodesInputBuilder()
239             .setRdmANode(destNodeId.getValue())
240             .setDegANum(Uint8.valueOf(destDegId))
241             .setTerminationPointA(destTpTx)
242             .setRdmZNode(nodeId.getValue())
243             .setDegZNum(Uint8.valueOf(srcDegId))
244             .setTerminationPointZ(srcTpRx);
245         if (!OrdLink.createRdm2RdmLinks(r2rlinkBuilderZToA.build(), this.dataBroker)) {
246             LOG.error("OMS Link creation failed between node: {} and nodeId: {} in Z->A direction",
247                 destNodeId.getValue(), nodeId.getValue());
248             return false;
249         }
250         return true;
251     }
252
253     public boolean deleteR2RLink(NodeId nodeId, String interfaceName, String remoteSystemName,
254                                  String remoteInterfaceName) {
255         String srcTpTx = null;
256         String srcTpRx = null;
257         String destTpTx = null;
258         String destTpRx = null;
259         // Find which degree is associated with ethernet interface
260         Integer srcDegId = getDegFromInterface(nodeId, interfaceName);
261         if (srcDegId == null) {
262             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
263             return false;
264         }
265         // Check whether degree is Unidirectional or Bidirectional by counting number of
266         // circuit-packs under degree subtree
267         Direction sourceDirection = getDegreeDirection(srcDegId, nodeId);
268         if (Direction.NotApplicable == sourceDirection) {
269             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", nodeId, srcDegId);
270             return false;
271         } else if (Direction.Bidirectional == sourceDirection) {
272             srcTpTx = "DEG" + srcDegId + "-TTP-TXRX";
273             srcTpRx = "DEG" + srcDegId + "-TTP-TXRX";
274         } else {
275             srcTpTx = "DEG" + srcDegId + "-TTP-TX";
276             srcTpRx = "DEG" + srcDegId + "-TTP-RX";
277         }
278         // Find degree for which Ethernet interface is created on other end
279         NodeId destNodeId = new NodeId(remoteSystemName);
280         Integer destDegId = getDegFromInterface(destNodeId, remoteInterfaceName);
281         if (destDegId == null) {
282             LOG.error("Couldnt find degree connected to Ethernet interface for nodeId: {}", nodeId);
283             return false;
284         }
285         // Check whether degree is Unidirectional or Bidirectional by counting number of
286         // circuit-packs under degree subtree
287         Direction destinationDirection = getDegreeDirection(destDegId, destNodeId);
288         if (Direction.NotApplicable == destinationDirection) {
289             LOG.error("Couldnt find degree direction for nodeId: {} and degree: {}", destNodeId, destDegId);
290             return false;
291         } else if (Direction.Bidirectional == destinationDirection) {
292             destTpTx = "DEG" + destDegId + "-TTP-TXRX";
293             destTpRx = "DEG" + destDegId + "-TTP-TXRX";
294         } else {
295             destTpTx = "DEG" + destDegId + "-TTP-TX";
296             destTpRx = "DEG" + destDegId + "-TTP-RX";
297         }
298         return TopologyUtils.deleteLink(nodeId.getValue() + "-" + srcDegId, destNodeId.getValue() + "-" + destDegId,
299             srcTpTx, destTpRx, networkTransactionService)
300             && TopologyUtils.deleteLink(destNodeId.getValue() + "-" + destDegId, nodeId.getValue() + "-" + srcDegId,
301                 destTpTx, srcTpRx, 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 }