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