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