upgrade portmapping YANG to introduce OTN
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion221.java
1 /*
2  * Copyright © 2017 Orange, Inc. 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.common.mapping;
10
11 import com.google.common.util.concurrent.FluentFuture;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.Comparator;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.WriteTransaction;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.transportpce.common.StringConstants;
27 import org.opendaylight.transportpce.common.Timeouts;
28 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
29 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
30 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.Network;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.NetworkBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.Nodes;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.NodesBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.NodesKey;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.CpToDegree;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.CpToDegreeBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.CpToDegreeKey;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.Mapping;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.MappingBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.MappingKey;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.NodeInfo;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.NodeInfo.OpenroadmVersion;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200113.network.nodes.NodeInfoBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.Direction;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.PortQual;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.CircuitPack;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsKey;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.degree.ConnectionPorts;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.ConnectionMap;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Degree;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.DegreeKey;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Info;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Protocols;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.SharedRiskGroup;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.SharedRiskGroupKey;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Xponder;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.xponder.XpdrPort;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.InterfaceType;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpenROADMOpticalMultiplex;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpticalTransport;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.Lldp;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.PortConfig;
72 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76 public class PortMappingVersion221 {
77     private static final Logger LOG = LoggerFactory.getLogger(PortMappingImpl.class);
78
79     private final DataBroker dataBroker;
80     private final DeviceTransactionManager deviceTransactionManager;
81     private final OpenRoadmInterfaces openRoadmInterfaces;
82
83     public PortMappingVersion221(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
84         OpenRoadmInterfaces openRoadmInterfaces) {
85         this.dataBroker = dataBroker;
86         this.deviceTransactionManager = deviceTransactionManager;
87         this.openRoadmInterfaces = openRoadmInterfaces;
88     }
89
90     public boolean createMappingData(String nodeId) {
91         LOG.info("Create Mapping Data for node 2.2.1 {}", nodeId);
92         List<Mapping> portMapList = new ArrayList<>();
93         InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
94         Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType
95             .OPERATIONAL, infoIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
96         Info deviceInfo;
97         NodeInfo nodeInfo;
98         if (deviceInfoOptional.isPresent()) {
99             deviceInfo = deviceInfoOptional.get();
100             nodeInfo = createNodeInfo(deviceInfo);
101             if (nodeInfo == null) {
102                 return false;
103             } else {
104                 postPortMapping(nodeId, nodeInfo, null, null);
105             }
106         } else {
107             LOG.warn("Device info subtree is absent for {}", nodeId);
108             return false;
109         }
110
111         switch (deviceInfo.getNodeType()) {
112
113             case Rdm:
114                 // Get TTP port mapping
115                 if (!createTtpPortMapping(nodeId, deviceInfo, portMapList)) {
116                     // return false if mapping creation for TTP's failed
117                     LOG.warn("Unable to create mapping for TTP's on node {}", nodeId);
118                     return false;
119                 }
120
121                 // Get PP port mapping
122                 if (!createPpPortMapping(nodeId, deviceInfo, portMapList)) {
123                     // return false if mapping creation for PP's failed
124                     LOG.warn("Unable to create mapping for PP's on node {}", nodeId);
125                     return false;
126                 }
127                 break;
128             case Xpdr:
129                 if (!createXpdrPortMapping(nodeId, portMapList)) {
130                     LOG.warn("Unable to create mapping for Xponder on node {}", nodeId);
131                     return false;
132                 }
133                 break;
134             default:
135                 LOG.error("Unable to create mapping for node {} : unknown nodetype ", nodeId);
136                 break;
137
138         }
139         return postPortMapping(nodeId, nodeInfo, portMapList, null);
140     }
141
142     public boolean updateMapping(String nodeId, Mapping oldMapping) {
143         LOG.info("Updating Mapping Data {} for node {}", oldMapping, nodeId);
144         InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
145             .child(CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName()))
146             .child(Ports.class, new PortsKey(oldMapping.getSupportingPort()));
147         if ((oldMapping != null) && (nodeId != null)) {
148             try {
149                 Optional<Ports> portObject = deviceTransactionManager.getDataFromDevice(nodeId,
150                     LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
151                     Timeouts.DEVICE_READ_TIMEOUT_UNIT);
152                 if (portObject.isPresent()) {
153                     Ports port = portObject.get();
154                     Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
155                         oldMapping.getLogicalConnectionPoint());
156
157                     final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
158                     InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class)
159                         .child(Nodes.class, new NodesKey(nodeId))
160                         .child(Mapping.class, new MappingKey(oldMapping.getLogicalConnectionPoint()));
161                     writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
162                     FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
163                     commit.get();
164                     return true;
165                 }
166                 return false;
167             } catch (InterruptedException | ExecutionException e) {
168                 LOG.error("Error updating Mapping {} for node {}", oldMapping.getLogicalConnectionPoint(), nodeId, e);
169                 return false;
170             }
171         } else {
172             LOG.error("Impossible to update mapping");
173             return false;
174         }
175     }
176
177     private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
178         // Creating for Xponder Line and Client Ports
179         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
180         Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
181             LogicalDatastoreType.OPERATIONAL, deviceIID,
182             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
183         OrgOpenroadmDevice device = null;
184         if (deviceObject.isPresent()) {
185             device = deviceObject.get();
186         } else {
187             LOG.error("Impossible to get device configuration for node {}", nodeId);
188             return false;
189         }
190         // Variable to keep track of number of line ports
191         int line = 1;
192         // Variable to keep track of number of client ports
193         int client = 1;
194         Map<String, String> lcpMap = new HashMap<>();
195         Map<String, Mapping> mappingMap = new HashMap<>();
196
197         List<CircuitPacks> circuitPackList = null;
198         if (device.getCircuitPacks() == null) {
199             LOG.warn("Circuit Packs are not present for {}", nodeId);
200             return false;
201         } else {
202             circuitPackList = deviceObject.get().getCircuitPacks();
203             circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
204         }
205         String xpdrPreFix = "XPDR";
206         if (device.getXponder() == null) {
207             LOG.warn("{} configuration does not contain a list of xponders", nodeId);
208             for (CircuitPacks cp : circuitPackList) {
209                 String circuitPackName = cp.getCircuitPackName();
210                 if (cp.getPorts() == null) {
211                     LOG.warn("Ports were not found for circuit pack: {}", circuitPackName);
212                     continue;
213                 }
214                 List<Ports> portList = cp.getPorts();
215                 portList.sort(Comparator.comparing(Ports::getPortName));
216                 for (Ports port : portList) {
217                     if (port.getPortQual() == null) {
218                         LOG.warn("PortQual was not found for port {} on circuit pack: {}", port.getPortName(),
219                             circuitPackName);
220                         continue;
221                     }
222                     if (PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
223                         && port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
224                         String lcp = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
225                         lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
226                         mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null,
227                             null));
228                         line++;
229                     } else if (PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
230                         && port.getPortDirection().getIntValue() != Direction.Bidirectional.getIntValue()
231                         && port.getPartnerPort() != null
232                         && port.getPartnerPort().getCircuitPackName() != null
233                         && port.getPartnerPort().getPortName() != null) {
234                         if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
235                             continue;
236                         }
237                         String lcp1 = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
238                         Optional<CircuitPacks> cpOpt = circuitPackList.stream().filter(cP -> cP.getCircuitPackName()
239                             .equals(port.getPartnerPort().getCircuitPackName())).findFirst();
240                         if (cpOpt.isPresent()) {
241                             Optional<Ports> poOpt = cpOpt.get().getPorts().stream().filter(p -> p.getPortName()
242                                 .equals(port.getPartnerPort().getPortName().toString())).findFirst();
243                             if (poOpt.isPresent()) {
244                                 Ports port2 = poOpt.get();
245                                 if (checkPartnerPort(circuitPackName, port, port2)) {
246                                     String lcp2 = new StringBuilder("XPDR1-").append(StringConstants.NETWORK_TOKEN)
247                                         .append(line + 1).toString();
248                                     if (!lcpMap.containsKey(lcp1) && !lcpMap.containsKey(lcp2)) {
249                                         lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
250                                         lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
251                                         mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName,
252                                             lcp1, lcp2, null, null));
253                                         mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2, cpOpt.get()
254                                             .getCircuitPackName(), lcp2, lcp1, null, null));
255                                     } else {
256                                         LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
257                                     }
258                                     line += 2;
259                                 } else {
260                                     LOG.error("port {} on {} is not a correct partner port of {} on  {}",
261                                         port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
262                                         circuitPackName);
263                                 }
264                             } else {
265                                 LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
266                                     port.getPartnerPort().getCircuitPackName(), nodeId);
267                             }
268                         } else {
269                             LOG.error("Error fetching circuit-pack {} for {}", port.getPartnerPort()
270                                 .getCircuitPackName(), nodeId);
271                         }
272                     } else if (PortQual.XpdrClient.getIntValue() == port.getPortQual().getIntValue()) {
273                         String lcp = "XPDR1-" + StringConstants.CLIENT_TOKEN + client;
274                         lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
275                         mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null,
276                             null));
277                         client++;
278                     } else {
279                         LOG.warn("Error in the configuration of port {} of {} for {}", port.getPortName(),
280                             circuitPackName,
281                             nodeId);
282                     }
283                 }
284             }
285         } else {
286             LOG.info("{} configuration contains a list of xponders", nodeId);
287             for (Xponder xponder:deviceObject.get().getXponder()) {
288                 xpdrPreFix += xponder.getXpdrNumber();
289                 for (XpdrPort xpdrPort : xponder.getXpdrPort()) {
290                     CircuitPacks cp = circuitPackList.stream().filter(x -> x.getCircuitPackName()
291                             .equals(xpdrPort.getCircuitPackName()))
292                             .collect(Collectors.toList()).get(0);
293                     String circuitPackName = cp.getCircuitPackName();
294                     Boolean isExisting = false;
295                     for (String keys : lcpMap.keySet()) {
296                         LOG.info("Keys are {}", keys);
297                         if (keys.contains(cp.getCircuitPackName() + "+" + xpdrPort.getPortName().toString())) {
298                             isExisting = true;
299                             break;
300                         }
301                     }
302                     if (isExisting) {
303                         continue;
304                     }
305                     List<Ports> portList = cp.getPorts();
306                     portList.sort(Comparator.comparing(Ports::getPortName));
307                     for (Ports port : portList) {
308                         if (port.getPortQual() == null) {
309                             LOG.warn("PortQual was not found for port {} on circuit pack: {}", port.getPortName(),
310                                     circuitPackName);
311                             continue;
312                         }
313                         if ((PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
314                             || PortQual.SwitchNetwork.getIntValue() == port.getPortQual().getIntValue())
315                                 && port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
316                             String lcp = xpdrPreFix + "-" + StringConstants.NETWORK_TOKEN + line;
317                             lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
318                             mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null,
319                                 null));
320                             line++;
321                         } else if (PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
322                                 && port.getPortDirection().getIntValue() != Direction.Bidirectional.getIntValue()
323                                 && port.getPartnerPort() != null
324                                 && port.getPartnerPort().getCircuitPackName() != null
325                                 && port.getPartnerPort().getPortName() != null) {
326                             if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
327                                 continue;
328                             }
329                             String lcp1 = xpdrPreFix + "-" + StringConstants.NETWORK_TOKEN + line;
330                             Optional<CircuitPacks> cpOpt = circuitPackList.stream().filter(cP -> cP.getCircuitPackName()
331                                     .equals(port.getPartnerPort().getCircuitPackName())).findFirst();
332                             if (cpOpt.isPresent()) {
333                                 Optional<Ports> poOpt = cpOpt.get().getPorts().stream().filter(p -> p.getPortName()
334                                     .equals(port.getPartnerPort().getPortName().toString())).findFirst();
335                                 if (poOpt.isPresent()) {
336                                     Ports port2 = poOpt.get();
337                                     if (checkPartnerPort(circuitPackName, port, port2)) {
338                                         String lcp2 = new StringBuilder(xpdrPreFix)
339                                             .append(StringConstants.NETWORK_TOKEN)
340                                             .append(line + 1)
341                                             .toString();
342                                         if (!lcpMap.containsKey(lcp1) && !lcpMap.containsKey(lcp2)) {
343                                             lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
344                                             lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(),
345                                                 lcp2);
346                                             mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName,
347                                                 lcp1, lcp2, null, null));
348                                             mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2, cpOpt.get()
349                                                     .getCircuitPackName(), lcp2, lcp1, null, null));
350                                         } else {
351                                             LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
352                                         }
353                                         line += 2;
354                                     } else {
355                                         LOG.error("port {} on {} is not a correct partner port of {} on  {}",
356                                             port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
357                                             circuitPackName);
358                                     }
359                                 } else {
360                                     LOG.error("Error fetching port {} on {} for {}",
361                                         port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName(),
362                                         nodeId);
363                                 }
364                             } else {
365                                 LOG.error("Error fetching circuit-pack {} for {}",
366                                     port.getPartnerPort().getCircuitPackName(), nodeId);
367                             }
368                         } else if (PortQual.XpdrClient.getIntValue() == port.getPortQual().getIntValue()
369                             || PortQual.SwitchClient.getIntValue() == port.getPortQual().getIntValue()) {
370                             String lcp = xpdrPreFix + "-" + StringConstants.CLIENT_TOKEN + client;
371                             lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
372                             mappingMap.put(lcp,
373                                 createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
374                             client++;
375                         } else {
376                             LOG.warn("Error in the configuration of port {} of {} for {}",
377                                 port.getPortName(), circuitPackName, nodeId);
378                         }
379                     }
380                 }
381             }
382         }
383
384         if (device.getConnectionMap() != null) {
385             List<ConnectionMap> connectionMap = deviceObject.get().getConnectionMap();
386             String slcp = null;
387             String dlcp = null;
388             for (ConnectionMap cm : connectionMap) {
389                 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
390                 if (lcpMap.containsKey(skey)) {
391                     slcp = lcpMap.get(skey);
392                 }
393                 String dkey = cm.getDestination().get(0).getCircuitPackName() + "+"
394                     + cm.getDestination().get(0).getPortName();
395                 if (lcpMap.containsKey(dkey)) {
396                     dlcp = lcpMap.get(dkey);
397                 }
398                 if (slcp != null) {
399                     Mapping mapping = mappingMap.get(slcp);
400                     mappingMap.remove(slcp);
401                     portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
402                 } else {
403                     LOG.error("Error in connection-map analysis");
404                 }
405             }
406         } else {
407             LOG.warn("No connection-map inside device configuration");
408         }
409         if (!mappingMap.isEmpty()) {
410             for (Mapping m : mappingMap.values()) {
411                 portMapList.add(m);
412             }
413         }
414         return true;
415     }
416
417     private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
418         if ((Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
419             && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
420             && port2.getPartnerPort() != null
421             && port2.getPartnerPort().getCircuitPackName() != null
422             && port2.getPartnerPort().getPortName() != null
423             && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
424             && port2.getPartnerPort().getPortName().equals(port1.getPortName()))
425             ||
426             (Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
427             && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
428             && port2.getPartnerPort() != null
429             && port2.getPartnerPort().getCircuitPackName() != null
430             && port2.getPartnerPort().getPortName() != null
431             && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
432             && port2.getPartnerPort().getPortName().equals(port1.getPortName()))) {
433             return true;
434         } else {
435             return false;
436         }
437     }
438
439     private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg
440         .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
441         HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg
442             .CircuitPacks>> cpPerSrg = new HashMap<>();
443         Integer maxSrg;
444         // Get value for max Srg from info subtree, required for iteration
445         // if not present assume to be 20 (temporary)
446         if (ordmInfo.getMaxSrgs() != null) {
447             maxSrg = ordmInfo.getMaxSrgs();
448         } else {
449             maxSrg = 20;
450         }
451         for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
452             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks> srgCps
453                 = new ArrayList<>();
454             LOG.info("Getting Circuitpacks for Srg Number {}", srgCounter);
455             InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
456                 .child(SharedRiskGroup.class, new SharedRiskGroupKey(srgCounter));
457             Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
458                 LogicalDatastoreType.OPERATIONAL, srgIID,
459                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
460             if (ordmSrgObject.isPresent()) {
461                 srgCps.addAll(ordmSrgObject.get().getCircuitPacks());
462                 cpPerSrg.put(ordmSrgObject.get().getSrgNumber(), srgCps);
463             }
464         }
465         LOG.info("Device {} has {} Srg", deviceId, cpPerSrg.size());
466         return cpPerSrg;
467     }
468
469     //last LOG info message in this method is too long
470     @SuppressWarnings("checkstyle:linelength")
471     private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
472         // Creating mapping data for SRG's PP
473         HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks>> srgCps
474             = getSrgCps(nodeId, deviceInfo);
475
476         for (Integer k : srgCps.keySet()) {
477             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks> cpList =
478                 srgCps.get(k);
479             List<String> keys = new ArrayList<>();
480             for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks cp : cpList) {
481                 String circuitPackName = cp.getCircuitPackName();
482                 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
483                     .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
484                 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
485                     LogicalDatastoreType.OPERATIONAL, cpIID,
486                     Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
487
488                 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
489                     LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
490                     continue;
491                 }
492                 List<Ports> portList = circuitPackObject.get().getPorts();
493                 Collections.sort(portList, new SortPort221ByName());
494                 int portIndex = 1;
495                 for (Ports port : portList) {
496                     String currentKey = circuitPackName + "-" + port.getPortName();
497                     if (port.getPortQual() == null) {
498                         continue;
499                     } else if (PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
500                         && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()
501                         && !keys.contains(currentKey)) {
502                         String logicalConnectionPoint = createLogicalConnectionPort(port, k, portIndex);
503                         LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
504                             port.getPortName(), logicalConnectionPoint);
505                         portMapList.add(createMappingObject(nodeId, port, circuitPackName, logicalConnectionPoint));
506                         portIndex++;
507                         keys.add(currentKey);
508                     } else if (PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
509                         && (Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
510                         || Direction.Tx.getIntValue() == port.getPortDirection().getIntValue())
511                         && !keys.contains(currentKey)
512                         && port.getPartnerPort() != null) {
513                         String logicalConnectionPoint1 = createLogicalConnectionPort(port, k, portIndex);
514                         LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
515                             port.getPortName(), logicalConnectionPoint1);
516                         InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
517                             .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
518                             .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
519                         Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
520                             LogicalDatastoreType.OPERATIONAL, port2ID,
521                             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
522                         if (port2Object.isPresent()
523                             && port2Object.get().getPortQual().getIntValue()
524                                 == PortQual.RoadmExternal.getIntValue()) {
525                             Ports port2 = port2Object.get();
526                             if (checkPartnerPort(circuitPackName, port, port2)) {
527                                 String logicalConnectionPoint2 = createLogicalConnectionPort(port2, k, portIndex);
528                                 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
529                                     port2.getPortName(), logicalConnectionPoint2);
530                                 portMapList.add(createMappingObject(nodeId, port, circuitPackName,
531                                     logicalConnectionPoint1));
532                                 portMapList.add(createMappingObject(nodeId,port2,
533                                     port.getPartnerPort().getCircuitPackName(), logicalConnectionPoint2));
534                                 portIndex++;
535                                 keys.add(currentKey);
536                                 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
537                             } else {
538                                 LOG.error("Error with partner port configuration for port {} of  {} - {}",
539                                     port.getPortName(), circuitPackName, nodeId);
540                                 portIndex++;
541                             }
542                         } else {
543                             LOG.error("error getting partner port {} of  {} - {}",
544                                 port.getPartnerPort().getPortName().toString(),
545                                 port.getPartnerPort().getCircuitPackName(), nodeId);
546                             continue;
547                         }
548                     } else {
549                         LOG.info("{} : port {} on {} is not roadm-external or has already been handled."
550                             + " No logicalConnectionPoint assignment for this port.",
551                             nodeId, port.getPortName(), circuitPackName);
552                     }
553                 }
554             }
555         }
556         return true;
557     }
558
559     private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
560         String lcp = null;
561         switch (port.getPortDirection()) {
562             case Tx:
563                 lcp = "SRG" + index + "-PP" + portIndex + "-TX";
564                 break;
565             case Rx:
566                 lcp = "SRG" + index + "-PP" + portIndex + "-RX";
567                 break;
568             case Bidirectional:
569                 lcp = "SRG" + index + "-PP" + portIndex + "-TXRX";
570                 break;
571             default:
572                 LOG.error("Unsupported port direction for port {} : {}", port, port.getPortDirection());
573         }
574         return lcp;
575     }
576
577     private List<Degree> getDegrees(String deviceId, Info ordmInfo) {
578         List<Degree> degrees = new ArrayList<>();
579         Integer maxDegree;
580
581         // Get value for max degree from info subtree, required for iteration
582         // if not present assume to be 20 (temporary)
583         if (ordmInfo.getMaxDegrees() != null) {
584             maxDegree = ordmInfo.getMaxDegrees();
585         } else {
586             maxDegree = 20;
587         }
588
589         for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
590             LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
591             InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
592                 .child(Degree.class, new DegreeKey(degreeCounter));
593             Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
594                 LogicalDatastoreType.OPERATIONAL, deviceIID,
595                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
596             if (ordmDegreeObject.isPresent()) {
597                 degrees.add(ordmDegreeObject.get());
598             }
599         }
600         LOG.info("Device {} has {} degree", degrees.size());
601         return degrees;
602     }
603
604     private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
605         Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
606         Integer maxDegree;
607
608         if (ordmInfo.getMaxDegrees() != null) {
609             maxDegree = ordmInfo.getMaxDegrees();
610         } else {
611             maxDegree = 20;
612         }
613         for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
614             LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
615             InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
616                 .child(Degree.class, new DegreeKey(degreeCounter));
617             Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
618                 LogicalDatastoreType.OPERATIONAL, deviceIID,
619                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
620             if (ordmDegreeObject.isPresent()) {
621                 conPortMap.put(degreeCounter, ordmDegreeObject.get().getConnectionPorts());
622             }
623         }
624         LOG.info("Device {} has {} degree", deviceId, conPortMap.size());
625         return conPortMap;
626     }
627
628     private Map<String, String> getEthInterfaceList(String nodeId) {
629         LOG.info("It is calling get ethernet interface");
630         Map<String, String> cpToInterfaceMap = new HashMap<>();
631         InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
632             .child(Protocols.class);
633         Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
634             LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
635             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
636         if (protocolObject.isPresent() && protocolObject.get().augmentation(Protocols1.class).getLldp() != null) {
637             Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
638             for (PortConfig portConfig : lldp.getPortConfig()) {
639                 if (portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
640                     InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
641                         .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
642                     Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
643                         LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
644                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
645                     if (interfaceObject.isPresent() && (interfaceObject.get().getSupportingCircuitPackName() != null)) {
646                         String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
647                         cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
648                         InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
649                             .create(OrgOpenroadmDevice.class)
650                             .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
651                         Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
652                             nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
653                             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
654                         if (circuitPackObject.isPresent() && (circuitPackObject.get().getParentCircuitPack() != null)) {
655                             cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
656                                 portConfig.getIfName());
657                         }
658                     }
659                 }
660             }
661         } else {
662             LOG.warn("Couldnt find port config under LLDP for Node : {}", nodeId);
663         }
664         LOG.info("Processiong is done.. now returning..");
665         return cpToInterfaceMap;
666     }
667
668     private List<CpToDegree> getCpToDegreeList(List<Degree> degrees, String nodeId,
669         Map<String, String> interfaceList) {
670         List<CpToDegree> cpToDegreeList = new ArrayList<>();
671         for (Degree degree : degrees) {
672             if (degree.getCircuitPacks() != null) {
673                 LOG.info("Inside CP to degree list");
674                 cpToDegreeList.addAll(degree.getCircuitPacks().stream()
675                     .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
676                         degree.getDegreeNumber().toString(), nodeId, interfaceList))
677                     .collect(Collectors.toList()));
678             }
679         }
680         return cpToDegreeList;
681     }
682
683     private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
684         List<CpToDegree> cp2DegreeList) {
685         NodesBuilder nodesBldr = new NodesBuilder();
686         nodesBldr.withKey(new NodesKey(nodeId)).setNodeId(nodeId);
687         if (nodeInfo != null) {
688             nodesBldr.setNodeInfo(nodeInfo);
689         }
690         if (portMapList != null) {
691             nodesBldr.setMapping(portMapList);
692         }
693         if (cp2DegreeList != null) {
694             nodesBldr.setCpToDegree(cp2DegreeList);
695         }
696
697         List<Nodes> nodesList = new ArrayList<>();
698         nodesList.add(nodesBldr.build());
699
700         NetworkBuilder nwBldr = new NetworkBuilder();
701         nwBldr.setNodes(nodesList);
702
703         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
704         InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
705         Network network = nwBldr.build();
706         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
707         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
708         try {
709             commit.get();
710             return true;
711
712         } catch (InterruptedException | ExecutionException e) {
713             LOG.warn("Failed to post {}", network, e);
714             return false;
715         }
716     }
717
718     private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber, String nodeId,
719         Map<String, String> interfaceList) {
720         String interfaceName = null;
721         if (interfaceList.get(circuitPackName) != null) {
722             interfaceName = interfaceList.get(circuitPackName);
723         }
724         return new CpToDegreeBuilder().withKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
725             .setDegreeNumber(new Long(degreeNumber)).setInterfaceName(interfaceName).build();
726     }
727
728     private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
729         String logicalConnectionPoint) {
730         MappingBuilder mpBldr = new MappingBuilder();
731         mpBldr.withKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
732             .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName())
733             .setPortDirection(port.getPortDirection().getName());
734
735         // Get OMS and OTS interface provisioned on the TTP's
736         if (logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) && (port.getInterfaces() != null)) {
737             for (Interfaces interfaces : port.getInterfaces()) {
738                 try {
739                     Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
740                         interfaces.getInterfaceName());
741                     if (openRoadmInterface.isPresent()) {
742                         Class<? extends InterfaceType> interfaceType
743                             = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
744                         // Check if interface type is OMS or OTS
745                         if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
746                             mpBldr.setSupportingOms(interfaces.getInterfaceName());
747                         }
748                         if (interfaceType.equals(OpticalTransport.class)) {
749                             mpBldr.setSupportingOts(interfaces.getInterfaceName());
750                         }
751                     } else {
752                         LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
753                     }
754                 } catch (OpenRoadmInterfaceException ex) {
755                     LOG.warn("Error while getting interface {} from node {}!", interfaces.getInterfaceName(), nodeId,
756                         ex);
757                 }
758             }
759         }
760         return mpBldr.build();
761     }
762
763     private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
764         String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
765         MappingBuilder mpBldr;
766         if (mapping != null && assoLcp != null) {
767             // update existing mapping
768             mpBldr = new MappingBuilder(mapping);
769             mpBldr.setConnectionMapLcp(assoLcp);
770         } else {
771             // create a new mapping
772             mpBldr = new MappingBuilder();
773             mpBldr.withKey(new MappingKey(logicalConnectionPoint))
774                 .setLogicalConnectionPoint(logicalConnectionPoint)
775                 .setSupportingCircuitPackName(circuitPackName)
776                 .setSupportingPort(port.getPortName())
777                 .setPortDirection(port.getPortDirection().getName());
778             if (port.getPortQual() != null) {
779                 mpBldr.setPortQual(port.getPortQual().getName());
780             }
781             if (partnerLcp != null) {
782                 mpBldr.setPartnerLcp(partnerLcp);
783             }
784         }
785         return mpBldr.build();
786     }
787
788     //some LOG messages are too long
789     @SuppressWarnings("checkstyle:linelength")
790     private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
791         // Creating mapping data for degree TTP's
792         List<Degree> degrees = getDegrees(nodeId, deviceInfo);
793         Map<String, String> interfaceList = getEthInterfaceList(nodeId);
794         List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, nodeId, interfaceList);
795         LOG.info("Map looks like this {}", interfaceList);
796         postPortMapping(nodeId, null, null, cpToDegreeList);
797
798         Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
799         for (Integer k : connectionPortMap.keySet()) {
800             switch (connectionPortMap.get(k).size()) {
801                 case 1:
802                     // port is bidirectional
803                     InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
804                         .child(CircuitPacks.class, new CircuitPacksKey(connectionPortMap.get(k).get(0)
805                             .getCircuitPackName()))
806                         .child(Ports.class, new PortsKey(connectionPortMap.get(k).get(0)
807                             .getPortName().toString()));
808                     LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap.get(k).get(0)
809                         .getPortName().toString(), connectionPortMap.get(k).get(0).getCircuitPackName());
810                     Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
811                         LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
812                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
813                     if (portObject.isPresent()) {
814                         Ports port = portObject.get();
815                         if (port.getPortQual() == null) {
816                             continue;
817                         } else if (PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
818                             && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()) {
819                             String logicalConnectionPoint = new StringBuilder("DEG").append(k).append("-TTP-TXRX")
820                                 .toString();
821                             LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
822                                 connectionPortMap.get(k).get(0).getCircuitPackName(), port.getPortName(),
823                                 logicalConnectionPoint);
824                             portMapList.add(createMappingObject(nodeId, port,
825                                 connectionPortMap.get(k).get(0).getCircuitPackName(), logicalConnectionPoint));
826                         } else {
827                             LOG.error(
828                                 "Impossible to create logical connection point for port {} of {} on node {}"
829                                 + "- Error in configuration with port-qual or port-direction",
830                                 port.getPortName(), connectionPortMap.get(k).get(0).getCircuitPackName(), nodeId);
831                         }
832                     } else {
833                         LOG.error("No port {} on circuit pack {} for node {}",
834                             connectionPortMap.get(k).get(0).getPortName().toString(),
835                             connectionPortMap.get(k).get(0).getCircuitPackName(), nodeId);
836                         return false;
837                     }
838                     break;
839                 case 2:
840                     // ports are unidirectionals
841                     String cp1Name = connectionPortMap.get(k).get(0).getCircuitPackName();
842                     String cp2Name = connectionPortMap.get(k).get(1).getCircuitPackName();
843                     InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
844                         .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
845                         .child(Ports.class, new PortsKey(connectionPortMap.get(k).get(0).getPortName().toString()));
846                     LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap.get(k).get(0)
847                         .getPortName().toString(), cp1Name);
848                     Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
849                         LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
850                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
851                     InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
852                         .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
853                         .child(Ports.class, new PortsKey(connectionPortMap.get(k).get(1).getPortName().toString()));
854                     LOG.info("Fetching connection-port {} at circuit pack {}",
855                         connectionPortMap.get(k).get(1).getPortName().toString(), cp2Name);
856                     Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
857                         LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
858                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
859                     if (port1Object.isPresent() && port2Object.isPresent()) {
860                         Ports port1 = port1Object.get();
861                         Ports port2 = port2Object.get();
862                         if (port1.getPortQual() == null || port2.getPortQual() == null) {
863                             continue;
864                         } else if ((PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
865                                 && PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
866                                 && Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
867                                 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
868                                 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
869                                 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
870                                 && port1.getPartnerPort().getPortName().equals(port2.getPortName().toString())
871                                 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
872                                 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))
873                                 ||
874                                 (PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
875                                 && PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
876                                 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
877                                 && Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
878                                 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
879                                 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
880                                 && port1.getPartnerPort().getPortName().equals(port2.getPortName().toString())
881                                 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
882                                 && port2.getPartnerPort().getPortName().equals(port1.getPortName().toString()))) {
883                             String logicalConnectionPoint1 = new StringBuilder("DEG").append(k).append("-TTP-")
884                                 .append(port1.getPortDirection().getName().toUpperCase()).toString();
885                             LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
886                                 connectionPortMap.get(k).get(0).getCircuitPackName(), port1.getPortName(),
887                                 logicalConnectionPoint1);
888                             portMapList.add(createMappingObject(nodeId, port1, connectionPortMap.get(k).get(0)
889                                 .getCircuitPackName(), logicalConnectionPoint1));
890                             String logicalConnectionPoint2 = new StringBuilder("DEG").append(k).append("-TTP-")
891                                 .append(port2.getPortDirection().getName().toUpperCase()).toString();
892                             LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
893                                 connectionPortMap.get(k).get(1).getCircuitPackName(), port2.getPortName(),
894                                 logicalConnectionPoint2);
895                             portMapList.add(createMappingObject(nodeId, port2, connectionPortMap.get(k).get(1)
896                                 .getCircuitPackName(), logicalConnectionPoint2));
897                         } else {
898                             LOG.error(
899                                 "impossible to create logical connection point for port {} or port {} on node {} - "
900                                 + "Error in configuration with port-qual, port-direction or partner-port configuration",
901                                 port1.getPortName(), port2.getPortName(), nodeId);
902                         }
903                     } else {
904                         LOG.error("No port {} on circuit pack {} for node {}",
905                             connectionPortMap.get(k).get(0).getPortName().toString(),
906                             connectionPortMap.get(k).get(0).getCircuitPackName(), nodeId);
907                         return false;
908                     }
909
910                     break;
911                 default:
912                     LOG.error("Number of connection port for DEG{} on {} is incorrect", k, nodeId);
913                     continue;
914             }
915         }
916         return true;
917     }
918
919     private NodeInfo createNodeInfo(Info deviceInfo) {
920         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder();
921         if (deviceInfo.getNodeType() != null) {
922             nodeInfoBldr = new NodeInfoBuilder()
923                 .setOpenroadmVersion(OpenroadmVersion._221)
924                 .setNodeType(deviceInfo.getNodeType());
925             if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
926                 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
927             } else {
928                 nodeInfoBldr.setNodeClli("defaultCLLI");
929             }
930             if (deviceInfo.getModel() != null) {
931                 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
932             }
933             if (deviceInfo.getVendor() != null) {
934                 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
935             }
936             if (deviceInfo.getIpAddress() != null) {
937                 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
938             }
939         } else {
940          // TODO make mandatory in yang
941             LOG.error("Node type field is missing");
942             return null;
943         }
944         return nodeInfoBldr.build();
945     }
946
947 }