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