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