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