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