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