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