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