4cb3c10e0d1bf2c0b46fbf7c4cb9a2c78a8ebb3e
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / mapping / PortMapping.java
1 /*
2  * Copyright © 2017 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
9 package org.opendaylight.transportpce.renderer.mapping;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
22 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaces;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.NodeTypes;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPorts;
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.Degree;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroup;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroupKey;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.port.Interfaces;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.InterfaceType;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpenROADMOpticalMultiplex;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpticalTransport;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.Network;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.NetworkBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.Nodes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.NodesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.NodesKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.Mapping;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.MappingBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.MappingKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 public class PortMapping {
66
67     private static final Logger LOG = LoggerFactory.getLogger(PortMapping.class);
68     private final DataBroker db;
69     private final MountPointService mps;
70     private final String nodeId;
71
72     public PortMapping(DataBroker db, MountPointService mps, String nodeId) {
73         this.db = db;
74         this.mps = mps;
75         this.nodeId = nodeId;
76
77     }
78
79     /**
80      * This method creates logical to physical port mapping for a given device.
81      * Instead of parsing all the circuit packs/ports in the device this methods
82      * does a selective read operation on degree/srg subtree to get circuit
83      * packs/ports that map to :
84      *
85      * <p>
86      * 1. DEGn-TTP-TX, DEGn-TTP-RX, DEGn-TTP-TXRX
87      *
88      * <p>
89      * 2. SRGn-PPp-TX, SRGn-PPp-RX, SRGn-PPp-TXRX
90      *
91      * <p>
92      * 3. LINEn
93      *
94      * <p>
95      * 4. CLNTn.
96      *
97      * <p>
98      * If the port is Mw it also store the OMS, OTS interface provisioned on the
99      * port. It skips the logical ports that are internal. If operation is
100      * successful the mapping gets stored in datastore corresponding to
101      * portmapping.yang data model.
102      *
103      * @return true/false based on status of operation
104      */
105     public boolean createMappingData() {
106
107         LOG.info("Create Mapping Data for node " + nodeId);
108         DataBroker deviceDb = getDeviceDataBroker(nodeId, mps);
109         List<Mapping> portMapList = new ArrayList<>();
110         Info deviceInfo;
111         Integer nodeType = 1;
112         if (deviceDb != null) {
113             deviceInfo = getDeviceInfo(deviceDb);
114             if (deviceInfo != null) {
115                 if (deviceInfo.getNodeType() == null) {
116                     LOG.info("Node type mandatory field is missing");
117                     return false;
118                 }
119                 nodeType = deviceInfo.getNodeType().getIntValue();
120                 // Create Mapping for Roadm Node
121                 if (nodeType == 1) {
122                     // Get TTP port mapping
123                     if (!createTtpPortMapping(deviceDb, deviceInfo, portMapList)) {
124                         // return false if mapping creation for TTP's failed
125                         LOG.info("Unable to create mapping for TTP's");
126                         return false;
127                     }
128
129                     // Get PP port mapping
130                     if (!createPpPortMapping(deviceDb, deviceInfo, portMapList)) {
131                         // return false if mapping creation for PP's failed
132                         LOG.info("Unable tp create mapping for PP's");
133                         return false;
134                     }
135                 }
136                 // Create Mapping for Xponder Node
137                 if (nodeType == 2) {
138                     if (!createXpdrPortMapping(deviceDb, deviceInfo, portMapList)) {
139                         LOG.info("Unable to create mapping for Xponder");
140                         return false;
141                     }
142                 }
143             } else {
144                 LOG.info("Device info subtree is absent for " + nodeId);
145                 return false;
146             }
147
148         } else {
149             LOG.info("Unable to get Data broker for node " + nodeId);
150             return false;
151         }
152         return postPortMapping(deviceInfo, portMapList, nodeType);
153     }
154
155     /**
156      * This method removes mapping data from the datastore after disconnecting
157      * ODL from a Netconf device.
158      */
159     public void deleteMappingData() {
160         LOG.info("Deleting Mapping Data corresponding at node " + nodeId);
161         WriteTransaction rw = db.newWriteOnlyTransaction();
162         InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.create(Network.class)
163             .child(Nodes.class, new NodesKey(nodeId));
164         rw.delete(LogicalDatastoreType.CONFIGURATION, nodesIID);
165         try {
166             rw.submit().get(1, TimeUnit.SECONDS);
167             LOG.info("Port mapping removal for node " + nodeId);
168         } catch (InterruptedException | ExecutionException | TimeoutException e) {
169             LOG.error("Error for removing port mapping infos for node " + nodeId);
170         }
171     }
172
173     /**
174      * This private method gets the list of external ports on a degree. For each
175      * port in the degree, it does a get on port subtree with
176      * circuit-pack-name/port-name as key in order to get the logical connection
177      * point name corresponding to it.
178      *
179      * @param deviceDb
180      *            Reference to device's databroker
181      * @param deviceInfo
182      *            Info subtree read from the device
183      * @param portMapList
184      *            Reference to the list containing the mapping to be pushed to
185      *            MD-SAL
186      *
187      * @return true/false based on status of operation
188      */
189     private boolean createTtpPortMapping(DataBroker deviceDb, Info deviceInfo, List<Mapping> portMapList) {
190         // Creating mapping data for degree TTP's
191         List<ConnectionPorts> degreeConPorts = getDegreePorts(deviceDb, deviceInfo);
192
193         // Getting circuit-pack-name/port-name corresponding to TTP's
194         for (ConnectionPorts cp : degreeConPorts) {
195             String circuitPackName = cp.getCircuitPackName();
196             String portName = cp.getPortName().toString();
197             InstanceIdentifier<Ports> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
198                 CircuitPacks.class, new CircuitPacksKey(circuitPackName)).child(Ports.class, new PortsKey(portName));
199             try {
200                 LOG.info("Fetching logical Connection Point value for port {} at circuit pack {}", portName,
201                     circuitPackName);
202                 ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
203                 Optional<Ports> portObject = rtx.read(LogicalDatastoreType.OPERATIONAL, portIID).get();
204                 if (portObject.isPresent()) {
205                     Ports port = portObject.get();
206                     if (port.getLogicalConnectionPoint() != null) {
207                         LOG.info("Logical Connection Point for {} {} is {}", circuitPackName, portName, port
208                             .getLogicalConnectionPoint());
209                         portMapList.add(createMappingObject(port, circuitPackName, port.getLogicalConnectionPoint(),
210                             deviceDb));
211                     } else {
212
213                         LOG.warn("Logical Connection Point value missing for {} {}", circuitPackName, port
214                             .getPortName());
215                     }
216                 }
217             } catch (InterruptedException | ExecutionException ex) {
218                 LOG.warn("Read failed for Logical Connection Point value missing for " + circuitPackName + " "
219                     + portName, ex);
220                 return false;
221             }
222         }
223         return true;
224     }
225
226     /**
227      * This private method gets the list of circuit packs on an Srg. For each
228      * circuit pack on an Srg, it does a get on circuit-pack subtree with
229      * circuit-pack-name as key in order to get the list of ports. It then
230      * iterates over the list of ports to get ports with port-qual as
231      * roadm-external. It appends a TX,RX,TXRX to the logical connection point
232      * name based on the direction of the port.
233      *
234      * @param deviceDb
235      *            Reference to device's databroker
236      * @param deviceInfo
237      *            Info subtree read from the device
238      * @param portMapList
239      *            Reference to the list containing the mapping to be pushed to
240      *            MD-SAL
241      *
242      * @return true/false based on status of operation
243      */
244
245     private boolean createPpPortMapping(DataBroker deviceDb, Info deviceInfo, List<Mapping> portMapList) {
246         // Creating mapping data for degree PP's
247
248         ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
249         Integer maxSrg;
250         // Get value for max Srg from subtree, required for iteration
251         // if it is not defined in the netconf node then set maxSrg to 20
252         if (deviceInfo.getMaxSrgs() != null) {
253             maxSrg = deviceInfo.getMaxSrgs();
254         } else {
255             maxSrg = 20;
256         }
257
258         Integer srgCounter = 1;
259         Integer nbSrg = 0;
260         while (srgCounter <= maxSrg) {
261             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps =
262                 new ArrayList<>();
263             LOG.info("Getting CircuitPacks for Srg Number {}", srgCounter);
264             InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
265                 SharedRiskGroup.class, new SharedRiskGroupKey(srgCounter));
266             try {
267                 Optional<SharedRiskGroup> ordmSrgObject = rtx.read(LogicalDatastoreType.CONFIGURATION, srgIID).get();
268
269                 if (ordmSrgObject.isPresent()) {
270                     srgCps.addAll(new ArrayList<>(ordmSrgObject.get().getCircuitPacks()));
271                     nbSrg++;
272                 }
273             } catch (InterruptedException | ExecutionException ex) {
274                 LOG.warn("Failed to read Srg {}", srgCounter, ex);
275                 break;
276             }
277
278             for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cps : srgCps) {
279                 String circuitPackName = cps.getCircuitPackName();
280                 try {
281                     InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
282                         CircuitPacks.class, new CircuitPacksKey(circuitPackName));
283                     Optional<CircuitPacks> circuitPackObject = rtx.read(LogicalDatastoreType.OPERATIONAL, cpIID).get();
284
285                     if (circuitPackObject.isPresent()) {
286                         CircuitPacks cp = circuitPackObject.get();
287                         if (cp.getPorts() == null) {
288                             LOG.warn("No port found for {} {}", deviceInfo.getNodeId(), circuitPackName);
289                         } else if (!cp.getPorts().isEmpty()) {
290                             for (Ports port : cp.getPorts()) {
291
292                                 if (port.getLogicalConnectionPoint() != null && port.getPortQual().getIntValue() == 2) {
293                                     String logicalConnectionPoint = null;
294                                     if (port.getPortDirection().getIntValue() == 1) {
295                                         // Port direction is transmit
296                                         logicalConnectionPoint = "SRG" + srgCounter + "-" + port
297                                             .getLogicalConnectionPoint() + "-TX";
298                                     }
299                                     if (port.getPortDirection().getIntValue() == 2) {
300                                         // Port direction is receive
301                                         logicalConnectionPoint = "SRG" + srgCounter + "-" + port
302                                             .getLogicalConnectionPoint() + "-RX";
303                                     }
304                                     if (port.getPortDirection().getIntValue() == 3) {
305                                         // port is bi-directional
306                                         logicalConnectionPoint = "SRG" + srgCounter + "-" + port
307                                             .getLogicalConnectionPoint() + "-TXRX";
308                                     }
309
310                                     LOG.info("Logical Connection Point for {} {} is {}", circuitPackName, port
311                                         .getPortName(), logicalConnectionPoint);
312
313                                     portMapList.add(createMappingObject(port, circuitPackName, logicalConnectionPoint,
314                                         deviceDb));
315
316                                 } else if (port.getPortQual().getIntValue() == 1) {
317
318                                     LOG.info("Port is internal, skipping Logical Connection Point missing for "
319                                         + circuitPackName + " " + port.getPortName());
320
321                                 } else if (port.getLogicalConnectionPoint() == null) {
322
323                                     LOG.info("Value missing, Skipping Logical Connection Point missing for {} {}",
324                                         circuitPackName, port.getPortName());
325                                 }
326
327                             }
328
329                         }
330
331                     }
332                 } catch (InterruptedException | ExecutionException ex) {
333                     LOG.warn("Read failed for {}", circuitPackName, ex);
334                     return false;
335                 }
336             }
337             srgCounter++;
338         }
339         LOG.info("Device has {} Srg", nbSrg);
340         return true;
341     }
342
343     /**
344      * This private method gets the list of circuit packs on a xponder. For each
345      * circuit pack on a Xponder, it does a get on circuit-pack subtree with
346      * circuit-pack-name as key in order to get the list of ports. It then
347      * iterates over the list of ports to get ports with port-qual as
348      * xpdr-network/xpdr-client. The line and client ports are saved as:
349      *
350      * <p>
351      * 1. LINEn
352      *
353      * <p>
354      * 2. CLNTn
355      *
356      * @param deviceDb
357      *            Reference to device's databroker
358      * @param deviceInfo
359      *            Info subtree read from the device
360      * @param portMapList
361      *            Reference to the list containing the mapping to be pushed to
362      *            MD-SAL
363      *
364      * @return true/false based on status of operation
365      */
366
367     private boolean createXpdrPortMapping(DataBroker deviceDb, Info deviceInfo, List<Mapping> portMapList) {
368         // Creating for Xponder Line and Client Ports
369         try {
370             InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
371             ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
372             Optional<OrgOpenroadmDevice> deviceObject = rtx.read(LogicalDatastoreType.OPERATIONAL, deviceIID).get();
373
374             // Variable to keep track of number of line ports
375             int line = 1;
376             // Variable to keep track of number of client ports
377             int client = 1;
378             if (deviceObject.isPresent()) {
379                 for (CircuitPacks cp : deviceObject.get().getCircuitPacks()) {
380                     String circuitPackName = cp.getCircuitPackName();
381                     if (cp.getPorts() == null) {
382                         LOG.warn("No port found for {}, circuit pack {}", deviceInfo.getNodeId(), circuitPackName);
383                     } else {
384                         for (Ports port : cp.getPorts()) {
385                             if (port.getPortQual() != null) {
386                                 if (port.getPortQual().getIntValue() == 3) {
387                                     // Port is xpdr-network
388                                     portMapList.add(createMappingObject(port, circuitPackName, "XPDR-LINE"
389                                         + line, deviceDb));
390                                     LOG.info("Logical Connection Point for {} {} is XPDR-LINE{}", circuitPackName, port
391                                         .getPortName(), line);
392                                     line++;
393                                 }
394                                 if (port.getPortQual().getIntValue() == 4) {
395                                     // port is xpdr-client
396                                     portMapList.add(createMappingObject(port, circuitPackName, "XPDR-CLNT"
397                                         + client, deviceDb));
398                                     LOG.info("Logical Connection Point for {} {} is XPDR-CLNT{}", circuitPackName, port
399                                         .getPortName(), client);
400                                     client++;
401                                 }
402                             } else {
403                                 LOG.info("no PortQual for port {} of circuit pack {}", port.getPortName(), cp
404                                     .getCircuitPackName());
405                             }
406                         }
407                     }
408                 }
409             } else {
410                 LOG.info("No deviceObject present for {}", nodeId);
411                 return false;
412             }
413
414         } catch (InterruptedException | ExecutionException ex) {
415             LOG.warn("Read failed for CircuitPacks of {}", nodeId, ex);
416             return false;
417         }
418         return true;
419     }
420
421     /**
422      * This private method builds the mapping object to be pushed in MD-SAL in
423      * order to save port mapping. In case of TTP ports, it also saves the
424      * OTS,OMS interfaces provisioned on the port.
425      *
426      * @param port
427      *            Reference to device's port subtree object.
428      * @param circuitPackName
429      *            Name of cp where port exists.
430      * @param logicalConnectionPoint
431      *            logical name of the port.
432      * @param deviceDb
433      *            Reference to device's databroker.
434      *
435      * @return true/false based on status of operation
436      */
437
438     private Mapping createMappingObject(Ports port, String circuitPackName, String logicalConnectionPoint,
439         DataBroker deviceDb) {
440         MappingBuilder mpBldr = new MappingBuilder();
441         mpBldr.setKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
442             .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName());
443
444         // Get OMS and OTS interface provisioned on the TTP's
445         if (logicalConnectionPoint.contains("TTP") && port.getInterfaces() != null) {
446             for (Interfaces interfaces : port.getInterfaces()) {
447                 Class<? extends InterfaceType> interfaceType = new OpenRoadmInterfaces(db, mps, nodeId,
448                     logicalConnectionPoint).getInterface(interfaces.getInterfaceName()).getType();
449                 // Check if interface type is OMS or OTS
450                 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
451                     String omsInterfaceName = interfaces.getInterfaceName();
452                     mpBldr.setSupportingOms(omsInterfaceName);
453                 }
454                 if (interfaceType.equals(OpticalTransport.class)) {
455                     String otsInterfaceName = interfaces.getInterfaceName();
456                     mpBldr.setSupportingOts(otsInterfaceName);
457                 }
458             }
459         }
460         return mpBldr.build();
461     }
462
463     /**
464      * This method does a get operation on info subtree of the netconf device's
465      * configuration datastore and returns info object.It is required to get
466      * device attributes such as maxDegrees,maxSrgs and node-type.
467      *
468      * @param deviceDb
469      *            Reference to device's databroker
470      * @return Info object
471      *
472      */
473     private Info getDeviceInfo(DataBroker deviceDb) {
474         ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
475         InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
476         try {
477             Optional<Info> ordmInfoObject = rtx.read(LogicalDatastoreType.OPERATIONAL, infoIID).get();
478             if (ordmInfoObject.isPresent()) {
479                 LOG.info("Info subtree is present {}", ordmInfoObject.get());
480                 return ordmInfoObject.get();
481             } else {
482                 LOG.error("Info subtree is not present");
483             }
484         } catch (NullPointerException ex) {
485             LOG.warn("Try to get Info from a non Open ROADM device {}", deviceDb);
486             return null;
487         } catch (InterruptedException | ExecutionException ex) {
488             LOG.error("Read failed on info subtree ", ex);
489             return null;
490         }
491         return null;
492     }
493
494     /**
495      * This method does a get operation on degree subtree of the netconf
496      * device's config datastore and returns a list of all connection port
497      * objects. It is required for doing a selective get on ports that
498      * correspond to logical connection points of interest.
499      *
500      * @param deviceDb
501      *            Reference to device's databroker
502      * @param ordmInfo
503      *            Info subtree from the device
504      * @return List of connection ports object belonging to- degree subtree
505      */
506     private List<ConnectionPorts> getDegreePorts(DataBroker deviceDb, Info ordmInfo) {
507
508         List<ConnectionPorts> degreeConPorts = new ArrayList<>();
509         ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
510         Integer maxDegree;
511
512         // Get value for max degree from info subtree, required for iteration
513         // if not present assume to be 20 (temporary)
514         if (ordmInfo.getMaxDegrees() != null) {
515             maxDegree = ordmInfo.getMaxDegrees();
516         } else {
517             maxDegree = 20;
518         }
519         Integer degreeCounter = 1;
520         while (degreeCounter <= maxDegree) {
521             LOG.info("Getting Connection ports for Degree Number " + degreeCounter);
522             InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
523                 Degree.class, new DegreeKey(degreeCounter));
524             try {
525                 Optional<Degree> ordmDegreeObject = rtx.read(LogicalDatastoreType.CONFIGURATION, deviceIID).get();
526
527                 if (ordmDegreeObject.isPresent()) {
528                     degreeConPorts.addAll(new ArrayList<>(ordmDegreeObject.get().getConnectionPorts()));
529
530                 } else {
531                     LOG.info("Device has " + (degreeCounter - 1) + " degree");
532                     break;
533                 }
534             } catch (InterruptedException | ExecutionException ex) {
535                 LOG.error("Failed to read degree " + degreeCounter, ex);
536                 break;
537
538             }
539             degreeCounter++;
540         }
541         return degreeConPorts;
542     }
543
544     /**
545      * This method for ports the portMapping corresponding to the
546      * portmapping.yang file to the MD-SAL datastore.
547      *
548      * <p>
549      * 1. Supporting circuit pack 2. Supporting port 3. Supporting OMS interface
550      * (if port on ROADM)
551      *
552      * @param deviceInfo
553      *            Info subtree from the device.
554      * @param portMapList
555      *            Reference to the list containing the mapping to be pushed to
556      *            MD-SAL.
557      *
558      * @return Result true/false based on status of operation.
559      */
560     private boolean postPortMapping(Info deviceInfo, List<Mapping> portMapList, Integer nodeType) {
561
562         NodesBuilder nodesBldr = new NodesBuilder();
563         nodesBldr.setKey(new NodesKey(deviceInfo.getNodeId())).setNodeId(deviceInfo.getNodeId());
564         nodesBldr.setNodeType(NodeTypes.forValue(nodeType));
565         nodesBldr.setMapping(portMapList);
566         List<Nodes> nodesList = new ArrayList<>();
567         nodesList.add(nodesBldr.build());
568         NetworkBuilder nwBldr = new NetworkBuilder();
569         nwBldr.setNodes(nodesList);
570         final WriteTransaction writeTransaction = db.newWriteOnlyTransaction();
571         InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
572         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, nwBldr.build());
573         CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
574         try {
575             submit.checkedGet();
576             return true;
577
578         } catch (TransactionCommitFailedException e) {
579             LOG.warn("Failed to post {} ", nwBldr.build(), e);
580             return false;
581
582         }
583     }
584
585     /**
586      * This method for a given node's termination point returns the Mapping
587      * object based on portmapping.yang model stored in the MD-SAL data store
588      * which is created when the node is connected for the first time. The
589      * mapping object basically contains the following attributes of interest:
590      *
591      * <p>
592      * 1. Supporting circuit pack
593      *
594      * <p>
595      * 2. Supporting port
596      *
597      * <p>
598      * 3. Supporting OMS interface (if port on ROADM) 4. Supporting OTS
599      * interface (if port on ROADM)
600      *
601      * @param nodeId
602      *            Unique Identifier for the node of interest.
603      * @param logicalConnPoint
604      *            Name of the logical point
605      * @param db
606      *            Databroker / MD-SAL data store
607      *
608      * @return Result Mapping object if success otherwise null.
609      */
610     public static Mapping getMapping(String nodeId, String logicalConnPoint, DataBroker db) {
611
612         /*
613          * Getting physical mapping corresponding to logical connection point
614          */
615         InstanceIdentifier<Mapping> portMapping = InstanceIdentifier.builder(Network.class).child(Nodes.class,
616             new NodesKey(nodeId)).child(Mapping.class, new MappingKey(logicalConnPoint)).build();
617         ReadOnlyTransaction readTx = db.newReadOnlyTransaction();
618         Optional<Mapping> mapObject;
619         try {
620             mapObject = readTx.read(LogicalDatastoreType.CONFIGURATION, portMapping).get();
621             if (mapObject.isPresent()) {
622                 LOG.info("Found mapping for the logical port " + mapObject.get().toString());
623                 return mapObject.get();
624             } else {
625                 LOG.info("Could not find mapping for logical connection point : " + logicalConnPoint + " for nodeId "
626                     + nodeId);
627                 return null;
628             }
629         } catch (InterruptedException | ExecutionException ex) {
630             LOG.error("Unable to read mapping for logical connection point : " + logicalConnPoint + " for nodeId "
631                 + nodeId, ex);
632         }
633         return null;
634     }
635
636     /**
637      * This static method returns the DataBroker for a netconf node.
638      *
639      * @param nodeId
640      *            Unique identifier for the mounted netconf- node
641      * @param mps
642      *            Reference to mount service
643      * @return Databroker for the given device
644      */
645     public static DataBroker getDeviceDataBroker(String nodeId, MountPointService mps) {
646         MountPoint netconfNode = getDeviceMountPoint(nodeId, mps);
647         if (netconfNode != null) {
648             DataBroker netconfNodeDataBroker = netconfNode.getService(DataBroker.class).get();
649             return netconfNodeDataBroker;
650         } else {
651             LOG.error("Device Data broker not found for :" + nodeId);
652             return null;
653         }
654     }
655
656     public static MountPoint getDeviceMountPoint(String nodeId, MountPointService mps) {
657         InstanceIdentifier<Node> netconfNodeIID = InstanceIdentifier.builder(NetworkTopology.class).child(
658             Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName()))).child(Node.class,
659                 new NodeKey(new NodeId(nodeId))).build();
660
661         // Get mount point for specified device
662         final Optional<MountPoint> netconfNodeOptional = mps.getMountPoint(netconfNodeIID);
663         if (netconfNodeOptional.isPresent()) {
664             MountPoint netconfNode = netconfNodeOptional.get();
665             return netconfNode;
666         } else {
667             LOG.error("Mount Point not found for :" + nodeId);
668             return null;
669         }
670
671     }
672 }