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