Align and improve Portmapping versions Step 2
[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             for (Mapping mapping: portMapList) {
617                 if (mapping == null) {
618                     continue;
619                 }
620                 mappingMap.put(mapping.key(), mapping);
621             }
622             nodesBldr.setMapping(mappingMap);
623         }
624         if (cp2DegreeList != null) {
625             Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
626             for (CpToDegree cp2Degree: cp2DegreeList) {
627                 if (cp2Degree == null) {
628                     continue;
629                 }
630                 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
631             }
632             nodesBldr.setCpToDegree(cpToDegreeMap);
633         }
634
635         Map<NodesKey,Nodes> nodesList = new HashMap<>();
636         Nodes nodes = nodesBldr.build();
637         nodesList.put(nodes.key(),nodes);
638
639         Network network = new NetworkBuilder().setNodes(nodesList).build();
640
641         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
642         InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
643         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
644         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
645         try {
646             commit.get();
647             return true;
648         } catch (InterruptedException | ExecutionException e) {
649             LOG.warn("Failed to post {}", network, e);
650             return false;
651         }
652     }
653
654     private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber, String nodeId,
655             Map<String, String> interfaceList) {
656         return new CpToDegreeBuilder()
657             .withKey(new CpToDegreeKey(circuitPackName))
658             .setCircuitPackName(circuitPackName)
659             .setDegreeNumber(Uint32.valueOf(degreeNumber))
660             .setInterfaceName(interfaceList.get(circuitPackName)).build();
661     }
662
663     private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
664             String logicalConnectionPoint) {
665         MappingBuilder mpBldr = new MappingBuilder()
666                 .withKey(new MappingKey(logicalConnectionPoint))
667                 .setLogicalConnectionPoint(logicalConnectionPoint)
668                 .setSupportingCircuitPackName(circuitPackName)
669                 .setSupportingPort(port.getPortName())
670                 .setPortDirection(port.getPortDirection().getName());
671
672         if (!logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) || (port.getInterfaces() == null)) {
673             return mpBldr.build();
674         }
675
676         // Get OMS and OTS interface provisioned on the TTP's
677         for (Interfaces interfaces : port.getInterfaces()) {
678             try {
679                 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
680                     interfaces.getInterfaceName());
681                 if (!openRoadmInterface.isPresent()) {
682                     LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
683                     continue;
684                 }
685                 Class<? extends InterfaceType> interfaceType
686                     = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
687                 // Check if interface type is OMS or OTS
688                 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
689                     mpBldr.setSupportingOms(interfaces.getInterfaceName());
690                 }
691                 if (interfaceType.equals(OpticalTransport.class)) {
692                     mpBldr.setSupportingOts(interfaces.getInterfaceName());
693                 }
694             } catch (OpenRoadmInterfaceException ex) {
695                 LOG.warn("Error while getting interface {} from node {}!",
696                     interfaces.getInterfaceName(), nodeId, ex);
697             }
698         }
699         return mpBldr.build();
700     }
701
702     private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
703             String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
704
705         if (mapping != null && assoLcp != null) {
706             // update existing mapping
707             return new MappingBuilder(mapping).setConnectionMapLcp(assoLcp).build();
708         }
709
710         // create a new mapping
711         String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
712         MappingBuilder mpBldr = new MappingBuilder()
713                 .withKey(new MappingKey(logicalConnectionPoint))
714                 .setLogicalConnectionPoint(logicalConnectionPoint)
715                 .setSupportingCircuitPackName(circuitPackName)
716                 .setSupportingPort(port.getPortName())
717                 .setPortDirection(port.getPortDirection().getName())
718                 .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
719         if (port.getPortQual() != null) {
720             mpBldr.setPortQual(port.getPortQual().getName());
721         }
722         if (partnerLcp != null) {
723             mpBldr.setPartnerLcp(partnerLcp);
724         }
725
726         return mpBldr.build();
727     }
728
729     private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
730         // Creating mapping data for degree TTP's
731         List<Degree> degrees = getDegrees(nodeId, deviceInfo);
732         Map<String, String> interfaceList = getEthInterfaceList(nodeId);
733         List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, nodeId, interfaceList);
734         LOG.info("Map looks like this {}", interfaceList);
735         postPortMapping(nodeId, null, null, cpToDegreeList);
736
737         Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
738         for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
739             switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
740                 case 1:
741                     // port is bidirectional
742                     InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
743                         .child(CircuitPacks.class,
744                             new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName()))
745                         .child(Ports.class,
746                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
747                     LOG.info("Fetching connection-port {} at circuit pack {}",
748                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
749                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
750                     Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
751                         LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
752                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
753                     if (!portObject.isPresent()) {
754                         LOG.error("No port {} on circuit pack {} for node {}",
755                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
756                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
757                         return false;
758                     }
759                     Ports port = portObject.get();
760                     if (port.getPortQual() == null) {
761                         continue;
762                     }
763                     if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()
764                         || Direction.Bidirectional.getIntValue() != port.getPortDirection().getIntValue()) {
765                         LOG.error(
766                             "Impossible to create logical connection point for port {} of {} on node {}"
767                             + " - Error in configuration with port-qual or port-direction",
768                             port.getPortName(),
769                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
770                         continue;
771                     }
772                     String logicalConnectionPoint = new StringBuilder("DEG")
773                         .append(cpMapEntry.getKey())
774                         .append("-TTP-TXRX")
775                         .toString();
776                     LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
777                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
778                         port.getPortName(), logicalConnectionPoint);
779                     portMapList.add(createMappingObject(nodeId, port,
780                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
781                         logicalConnectionPoint));
782                     break;
783                 case 2:
784                     // ports are unidirectionals
785                     String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
786                     String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
787                     InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
788                         .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
789                         .child(Ports.class,
790                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
791                     LOG.info("Fetching connection-port {} at circuit pack {}",
792                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
793                         cp1Name);
794                     Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
795                         LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
796                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
797                     InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
798                         .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
799                         .child(Ports.class,
800                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString()));
801                     LOG.info("Fetching connection-port {} at circuit pack {}",
802                         connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString(), cp2Name);
803                     Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
804                         LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
805                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
806                     if (!port1Object.isPresent() || !port2Object.isPresent()) {
807                         LOG.error("No port {} on circuit pack {} for node {}",
808                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
809                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
810                         return false;
811                     }
812
813                     Ports port1 = port1Object.get();
814                     Ports port2 = port2Object.get();
815                     if (port1.getPortQual() == null || port2.getPortQual() == null) {
816                         continue;
817                     }
818                     if (Port.PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()
819                         || Port.PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()
820                         || port1.getPartnerPort() == null
821                         || port2.getPartnerPort() == null
822                         || !port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
823                         || !port1.getPartnerPort().getPortName().equals(port2.getPortName())
824                         || !port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
825                         || !port2.getPartnerPort().getPortName().equals(port1.getPortName())
826                         || ((Direction.Rx.getIntValue() != port1.getPortDirection().getIntValue()
827                                 || Direction.Tx.getIntValue() != port2.getPortDirection().getIntValue())
828                             && (Direction.Rx.getIntValue() != port2.getPortDirection().getIntValue()
829                                 || Direction.Tx.getIntValue() != port1.getPortDirection().getIntValue()))) {
830                         LOG.error("Impossible to create logical connection point for port {} or port {} on node {} - "
831                                 + "Error in configuration with port-qual, port-direction or partner-port configuration",
832                             port1.getPortName(), port2.getPortName(), nodeId);
833                         continue;
834                     }
835
836                     String logicalConnectionPoint1 = new StringBuilder("DEG")
837                         .append(cpMapEntry.getKey())
838                         .append("-TTP-")
839                         .append(port1.getPortDirection().getName().toUpperCase(Locale.getDefault()))
840                         .toString();
841                     LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
842                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
843                         port1.getPortName(), logicalConnectionPoint1);
844                     portMapList.add(createMappingObject(nodeId, port1,
845                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
846                         logicalConnectionPoint1));
847                     String logicalConnectionPoint2 = new StringBuilder("DEG")
848                         .append(cpMapEntry.getKey())
849                         .append("-TTP-")
850                         .append(port2.getPortDirection().getName().toUpperCase(Locale.getDefault()))
851                         .toString();
852                     LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
853                         connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
854                         port2.getPortName(), logicalConnectionPoint2);
855                     portMapList.add(createMappingObject(nodeId, port2,
856                         connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
857                         logicalConnectionPoint2));
858                     break;
859                 default:
860                     LOG.error("Number of connection port for DEG{} on {} is incorrect", cpMapEntry.getKey(), nodeId);
861                     continue;
862             }
863         }
864         return true;
865     }
866
867     private NodeInfo createNodeInfo(Info deviceInfo) {
868
869         if (deviceInfo.getNodeType() == null) {
870             // TODO make mandatory in yang
871             LOG.error("Node type field is missing");
872             return null;
873         }
874
875         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
876                 .setOpenroadmVersion(OpenroadmVersion._121);
877         // TODO check if we can use here .setNodeType(NodeTypes.forValue(..) such as with 221
878         switch (deviceInfo.getNodeType().getIntValue()) {
879             case 1:
880             case 2:
881                 nodeInfoBldr.setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
882                 break;
883             default:
884                 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
885                 // TODO: is this protection useful ? it is not present in Portmapping 221
886         }
887         if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
888             nodeInfoBldr.setNodeClli(deviceInfo.getClli());
889         } else {
890             nodeInfoBldr.setNodeClli("defaultCLLI");
891         }
892         if (deviceInfo.getModel() != null) {
893             nodeInfoBldr.setNodeModel(deviceInfo.getModel());
894         }
895         if (deviceInfo.getVendor() != null) {
896             nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
897         }
898         if (deviceInfo.getIpAddress() != null) {
899             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
900         }
901
902         return nodeInfoBldr.build();
903     }
904
905 }