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