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