Use constants for PortMapping log messages Step 3
[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(PortMappingUtils.UPDATE_MAPPING_LOGMSG,
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(PortMappingUtils.CONMAP_ISSUE_LOGMSG, nodeId, skey, dkey);
241                 continue;
242             }
243             String dlcp = lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null;
244             Mapping mapping = mappingMap.get(slcp);
245             mappingMap.remove(slcp);
246             portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
247         }
248
249         if (!mappingMap.isEmpty()) {
250             for (Mapping m : mappingMap.values()) {
251                 portMapList.add(m);
252             }
253         }
254         return true;
255     }
256
257     private boolean checkPartnerPortNotNull(Ports port) {
258         return (port.getPartnerPort() != null
259             && port.getPartnerPort().getCircuitPackName() != null
260             && port.getPartnerPort().getPortName() != null);
261     }
262
263     private boolean checkPartnerPortNoDir(String circuitPackName, Ports port1, Ports port2) {
264         return (checkPartnerPortNotNull(port2)
265             && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
266             && port2.getPartnerPort().getPortName().equals(port1.getPortName()));
267     }
268
269     private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
270         return (checkPartnerPortNoDir(circuitPackName, port1, port2)
271             && ((Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
272                     && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue())
273                 ||
274                 (Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
275                     && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue())));
276     }
277
278     private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
279             .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
280         HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
281             .CircuitPacks>> cpPerSrg = new HashMap<>();
282         // Get value for max Srg from info subtree, required for iteration
283         // if not present assume to be 20 (temporary)
284         Integer maxSrg = ordmInfo.getMaxSrgs() == null ? 20 : ordmInfo.getMaxSrgs().toJava();
285         for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
286             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
287                 = new ArrayList<>();
288             LOG.debug(PortMappingUtils.GETTING_CP_LOGMSG, deviceId, srgCounter);
289             InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
290                 .child(SharedRiskGroup.class, new SharedRiskGroupKey(Uint16.valueOf(srgCounter)));
291             Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
292                 LogicalDatastoreType.OPERATIONAL, srgIID,
293                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
294             if (ordmSrgObject.isPresent()) {
295                 srgCps.addAll(ordmSrgObject.get().nonnullCircuitPacks().values());
296                 cpPerSrg.put(ordmSrgObject.get().getSrgNumber().toJava(), srgCps);
297             }
298         }
299         LOG.info(PortMappingUtils.DEVICE_HAS_LOGMSG, deviceId, cpPerSrg.size(), "SRG");
300         return cpPerSrg;
301     }
302
303     private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
304         // Creating mapping data for SRG's PP
305         HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCps
306             = getSrgCps(nodeId, deviceInfo);
307
308         for (Entry<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>>
309                 srgCpEntry : srgCps.entrySet()) {
310             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> cpList =
311                 srgCps.get(srgCpEntry.getKey());
312             List<String> keys = new ArrayList<>();
313             for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cp : cpList) {
314                 String circuitPackName = cp.getCircuitPackName();
315                 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
316                     .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
317                 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
318                     LogicalDatastoreType.OPERATIONAL, cpIID,
319                     Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
320
321                 if (circuitPackObject.isEmpty()) {
322                     LOG.warn(PortMappingUtils.MISSING_CP_LOGMSG + PortMappingUtils.PORTMAPPING_IGNORE_LOGMSG,
323                         nodeId, circuitPackName);
324                     continue;
325                 }
326                 if (circuitPackObject.get().getPorts() == null) {
327                     LOG.warn(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, "found", circuitPackName);
328                     continue;
329                 }
330
331                 // com.google.common.collect.ImmutableList implementation of List
332                 @Nullable
333                 List<Ports> portList = new ArrayList<>(circuitPackObject.get().nonnullPorts().values());
334                 Collections.sort(portList, new SortPort121ByName());
335                 int portIndex = 1;
336                 for (Ports port : portList) {
337                     String currentKey = circuitPackName + "-" + port.getPortName();
338                     if (port.getPortQual() == null) {
339                         continue;
340                     }
341
342                     if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
343                         LOG.debug(PortMappingUtils.PORT_NOT_RDMEXT_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
344                             nodeId, port.getPortName(), circuitPackName);
345                         continue;
346                     }
347
348                     if (keys.contains(currentKey)) {
349                         LOG.debug(PortMappingUtils.PORT_ALREADY_HANDLED_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
350                             nodeId, port.getPortName(), circuitPackName);
351                         continue;
352                     }
353
354                     switch (port.getPortDirection()) {
355
356                         case Bidirectional:
357                             String lcp = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
358                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
359                                 nodeId, port.getPortName(), circuitPackName, lcp);
360                             portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp));
361                             portIndex++;
362                             keys.add(currentKey);
363                             break;
364
365                         case Rx:
366                         case Tx:
367                             if (!checkPartnerPortNotNull(port)) {
368                                 LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG
369                                         + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
370                                     nodeId, port.getPortName(), circuitPackName);
371                                 continue;
372                             }
373
374                             String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
375                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
376                                     nodeId, port.getPortName(), circuitPackName, lcp1);
377                             InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
378                                 .child(CircuitPacks.class,
379                                     new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
380                                 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
381                             Optional<Ports> port2Object = this.deviceTransactionManager
382                                 .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
383                                     Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
384                             if (port2Object.isEmpty()
385                                 || port2Object.get().getPortQual().getIntValue()
386                                     != Port.PortQual.RoadmExternal.getIntValue()) {
387                                 LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
388                                         + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
389                                     nodeId, port.getPartnerPort().getPortName(),
390                                     port.getPartnerPort().getCircuitPackName(),
391                                     port.getPortName(), circuitPackName);
392
393                                 continue;
394                             }
395
396                             Ports port2 = port2Object.get();
397                             if (!checkPartnerPort(circuitPackName, port, port2)) {
398                                 LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
399                                         + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
400                                     nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
401                                     port.getPortName(), circuitPackName);
402                                 portIndex++;
403                                 continue;
404                             }
405                             String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
406                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
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(PortMappingUtils.UNSUPPORTED_DIR_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
418                                 nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
419
420                     }
421                 }
422             }
423         }
424         return true;
425     }
426
427     private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
428         if (SUFFIX.containsKey(port.getPortDirection())) {
429             return String.join("-", "SRG" + index, "PP" + portIndex, SUFFIX.get(port.getPortDirection()));
430         }
431         LOG.error(PortMappingUtils.UNSUPPORTED_DIR_LOGMSG,
432             "createLogicalConnectionPort", port, "SRG" + index + "-PP" + portIndex, port.getPortDirection());
433         return null;
434     }
435
436     private Map<Integer, Degree> getDegreesMap(String deviceId, Info ordmInfo) {
437         Map<Integer, Degree> degrees = new HashMap<>();
438
439         // Get value for max degree from info subtree, required for iteration
440         // if not present assume to be 20 (temporary)
441         Integer maxDegree = ordmInfo.getMaxDegrees() == null ? 20 : ordmInfo.getMaxDegrees().toJava();
442
443         for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
444             LOG.debug(PortMappingUtils.GETTING_CONPORT_LOGMSG, deviceId, degreeCounter);
445             InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
446                 .child(Degree.class, new DegreeKey(Uint16.valueOf(degreeCounter)));
447             Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
448                 LogicalDatastoreType.OPERATIONAL, deviceIID,
449                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
450             if (ordmDegreeObject.isPresent()) {
451                 degrees.put(degreeCounter, ordmDegreeObject.get());
452             }
453         }
454         LOG.info(PortMappingUtils.DEVICE_HAS_LOGMSG,
455                 deviceId, degrees.size(), degrees.size() <= 1 ? "degree" : "degrees");
456         return degrees;
457     }
458
459     private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
460         Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
461         getDegreesMap(deviceId, ordmInfo).forEach(
462             (index, degree) -> conPortMap.put(index, new ArrayList<>(degree.nonnullConnectionPorts().values())));
463         return conPortMap;
464     }
465
466     private Map<String, String> getEthInterfaceList(String nodeId) {
467         LOG.info(PortMappingUtils.GETTING_ETH_LIST_LOGMSG, nodeId);
468         InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
469             .child(Protocols.class);
470         Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
471             LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
472             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
473         if (!protocolObject.isPresent() || protocolObject.get().augmentation(Protocols1.class).getLldp() == null) {
474             LOG.warn(PortMappingUtils.PROCESSING_DONE_LOGMSG, nodeId, PortMappingUtils.CANNOT_GET_LLDP_CONF_LOGMSG);
475             return new HashMap<>();
476         }
477         Map<String, String> cpToInterfaceMap = new HashMap<>();
478         Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
479         for (PortConfig portConfig : lldp.nonnullPortConfig().values()) {
480             if (!portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
481                 continue;
482             }
483             InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
484                 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
485             Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
486                 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
487                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
488             if (!interfaceObject.isPresent() || (interfaceObject.get().getSupportingCircuitPackName() == null)) {
489                 continue;
490             }
491             String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
492             cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
493             InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
494                 .create(OrgOpenroadmDevice.class)
495                 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
496             Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
497                 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
498                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
499             if (!circuitPackObject.isPresent() || (circuitPackObject.get().getParentCircuitPack() == null)) {
500                 continue;
501             }
502             cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
503                 portConfig.getIfName());
504         }
505         LOG.info(PortMappingUtils.PROCESSING_DONE_LOGMSG, nodeId, " - success");
506         return cpToInterfaceMap;
507     }
508
509     private List<CpToDegree> getCpToDegreeList(Map<Integer, Degree> degrees, Map<String, String> interfaceList) {
510         List<CpToDegree> cpToDegreeList = new ArrayList<>();
511         for (Degree degree : degrees.values()) {
512             cpToDegreeList.addAll(degree.nonnullCircuitPacks().values().stream()
513                 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
514                     degree.getDegreeNumber().toString(), interfaceList))
515                 .collect(Collectors.toList()));
516         }
517         return cpToDegreeList;
518     }
519
520     private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
521             List<CpToDegree> cp2DegreeList) {
522         NodesBuilder nodesBldr = new NodesBuilder().withKey(new NodesKey(nodeId)).setNodeId(nodeId);
523         if (nodeInfo != null) {
524             nodesBldr.setNodeInfo(nodeInfo);
525         }
526         if (portMapList != null) {
527             Map<MappingKey, Mapping> mappingMap = new HashMap<>();
528             // No element in the list below should be null at this stage
529             for (Mapping mapping: portMapList) {
530                 mappingMap.put(mapping.key(), mapping);
531             }
532             nodesBldr.setMapping(mappingMap);
533         }
534         if (cp2DegreeList != null) {
535             Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
536             // No element in the list below should be null at this stage
537             for (CpToDegree cp2Degree: cp2DegreeList) {
538                 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
539             }
540             nodesBldr.setCpToDegree(cpToDegreeMap);
541         }
542
543         Map<NodesKey,Nodes> nodesList = new HashMap<>();
544         Nodes nodes = nodesBldr.build();
545         nodesList.put(nodes.key(),nodes);
546
547         Network network = new NetworkBuilder().setNodes(nodesList).build();
548
549         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
550         InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
551         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
552         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
553         try {
554             commit.get();
555             return true;
556         } catch (InterruptedException | ExecutionException e) {
557             LOG.warn(PortMappingUtils.PORTMAPPING_POST_FAIL_LOGMSG, nodeId, network, e);
558             return false;
559         }
560     }
561
562     private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber,
563             Map<String, String> interfaceList) {
564         return new CpToDegreeBuilder()
565             .withKey(new CpToDegreeKey(circuitPackName))
566             .setCircuitPackName(circuitPackName)
567             .setDegreeNumber(Uint32.valueOf(degreeNumber))
568             .setInterfaceName(interfaceList.get(circuitPackName)).build();
569     }
570
571     private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
572             String logicalConnectionPoint) {
573         MappingBuilder mpBldr = new MappingBuilder()
574                 .withKey(new MappingKey(logicalConnectionPoint))
575                 .setLogicalConnectionPoint(logicalConnectionPoint)
576                 .setSupportingCircuitPackName(circuitPackName)
577                 .setSupportingPort(port.getPortName())
578                 .setPortDirection(port.getPortDirection().getName());
579         if (port.getAdministrativeState() != null) {
580             mpBldr.setPortAdminState(port.getAdministrativeState().name());
581         }
582         if (port.getOperationalState() != null) {
583             mpBldr.setPortOperState(port.getOperationalState().name());
584         }
585         if (!logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) || (port.getInterfaces() == null)) {
586             return mpBldr.build();
587         }
588         mpBldr = updateMappingInterfaces(nodeId, mpBldr, port);
589         return mpBldr.build();
590     }
591
592     private Mapping updateMappingObject(String nodeId, Ports port, Mapping oldmapping) {
593         MappingBuilder mpBldr = new MappingBuilder(oldmapping);
594         updateMappingStates(mpBldr, port, oldmapping);
595         if ((port.getInterfaces() == null)
596             || (!oldmapping.getLogicalConnectionPoint().contains(StringConstants.TTP_TOKEN)
597                 && !oldmapping.getLogicalConnectionPoint().contains(StringConstants.NETWORK_TOKEN))) {
598             return mpBldr.build();
599         }
600         // Get interfaces provisioned on the port
601         mpBldr = updateMappingInterfaces(nodeId, mpBldr, port);
602         return mpBldr.build();
603     }
604
605     private MappingBuilder updateMappingStates(MappingBuilder mpBldr, Ports port, Mapping oldmapping) {
606         if (port.getAdministrativeState() != null
607             && !port.getAdministrativeState().getName().equals(oldmapping.getPortAdminState())) {
608             mpBldr.setPortAdminState(port.getAdministrativeState().name());
609         }
610         if (port.getOperationalState() != null
611             && !port.getOperationalState().getName().equals(oldmapping.getPortOperState())) {
612             mpBldr.setPortOperState(port.getOperationalState().name());
613         }
614         return mpBldr;
615     }
616
617     private MappingBuilder updateMappingInterfaces(String nodeId, MappingBuilder mpBldr, Ports port) {
618         for (Interfaces interfaces : port.getInterfaces()) {
619             try {
620                 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
621                     interfaces.getInterfaceName());
622                 if (openRoadmInterface.isEmpty()) {
623                     LOG.warn(PortMappingUtils.INTF_ISSUE_LOGMSG,
624                         nodeId, interfaces.getInterfaceName() + "- empty interface");
625                     continue;
626                 }
627                 LOG.debug(PortMappingUtils.GOT_INTF_LOGMSG,
628                     nodeId, openRoadmInterface.get().getName(), openRoadmInterface.get().getType());
629                 Class<? extends InterfaceType> interfaceType
630                     = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
631                 // Check if interface type is OMS or OTS
632                 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
633                     mpBldr.setSupportingOms(interfaces.getInterfaceName());
634                 }
635                 if (interfaceType.equals(OpticalTransport.class)) {
636                     mpBldr.setSupportingOts(interfaces.getInterfaceName());
637                 }
638             } catch (OpenRoadmInterfaceException ex) {
639                 LOG.warn(PortMappingUtils.INTF_ISSUE_LOGMSG, nodeId, interfaces.getInterfaceName(), ex);
640             }
641         }
642         return mpBldr;
643     }
644
645     private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
646             String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
647
648         if (mapping != null && assoLcp != null) {
649             // update existing mapping
650             return new MappingBuilder(mapping).setConnectionMapLcp(assoLcp).build();
651         }
652
653         // create a new mapping
654         String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
655         MappingBuilder mpBldr = new MappingBuilder()
656                 .withKey(new MappingKey(logicalConnectionPoint))
657                 .setLogicalConnectionPoint(logicalConnectionPoint)
658                 .setSupportingCircuitPackName(circuitPackName)
659                 .setSupportingPort(port.getPortName())
660                 .setPortDirection(port.getPortDirection().getName())
661                 .setLcpHashVal(PortMappingUtils.fnv1size64(nodeIdLcp));
662         if (port.getPortQual() != null) {
663             mpBldr.setPortQual(port.getPortQual().getName());
664         }
665         if (partnerLcp != null) {
666             mpBldr.setPartnerLcp(partnerLcp);
667         }
668         if (port.getAdministrativeState() != null) {
669             mpBldr.setPortAdminState(port.getAdministrativeState().name());
670         }
671         if (port.getOperationalState() != null) {
672             mpBldr.setPortOperState(port.getOperationalState().name());
673         }
674         return mpBldr.build();
675     }
676
677     private Ports getPort2(Ports port, String nodeId, String circuitPackName, StringBuilder circuitPackName2,
678             //circuitPackName2 will be updated by reference contrary to circuitPackName
679             List<CircuitPacks> circuitPackList, Map<String, String> lcpMap) {
680         if (!checkPartnerPortNotNull(port)) {
681             LOG.warn(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG, nodeId, port.getPortName(), circuitPackName);
682             return null;
683         }
684         if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
685             return null;
686         }
687         Optional<CircuitPacks> cpOpt = circuitPackList.stream()
688             .filter(cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
689             .findFirst();
690         if (cpOpt.isEmpty()) {
691             LOG.error(PortMappingUtils.MISSING_CP_LOGMSG, nodeId, port.getPartnerPort().getCircuitPackName());
692             return null;
693         }
694         Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
695             .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName()))
696             .findFirst();
697         if (poOpt.isEmpty()) {
698             LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
699                 nodeId, port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName());
700             return null;
701         }
702         Ports port2 = poOpt.get();
703         circuitPackName2.append(cpOpt.get().getCircuitPackName());
704         if (!checkPartnerPort(circuitPackName, port, port2)) {
705             LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
706                 nodeId, port2.getPortName(), circuitPackName2, port.getPortName(), circuitPackName);
707             return null;
708         }
709         return port2;
710     }
711
712     private void putXpdrLcpsInMaps(int line, String nodeId,
713             Integer xponderNb,
714             String circuitPackName, String circuitPackName2, Ports port, Ports port2,
715             Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
716         String lcp1 =
717             PortMappingUtils.createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
718         if (lcpMap.containsKey(lcp1)) {
719             LOG.warn(PortMappingUtils.UNABLE_MAPPING_LOGMSG, nodeId, "add", lcp1 + " - already exist");
720             return;
721         }
722         String lcp2 =
723             PortMappingUtils.createXpdrLogicalConnectionPort(xponderNb, line + 1, StringConstants.NETWORK_TOKEN);
724         if (lcpMap.containsKey(lcp2)) {
725             LOG.warn(PortMappingUtils.UNABLE_MAPPING_LOGMSG, nodeId, "add", lcp2 + " - already exist");
726             return;
727         }
728         lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
729         lcpMap.put(circuitPackName2 + '+' + port2.getPortName(), lcp2);
730         mappingMap.put(lcp1,
731                 createXpdrMappingObject(nodeId, port, circuitPackName, lcp1, lcp2, null, null));
732         mappingMap.put(lcp2,
733                 createXpdrMappingObject(nodeId, port2, circuitPackName2, lcp2, lcp1, null, null));
734         return;
735     }
736
737     private int[] fillXpdrLcpsMaps(int line, int client, String nodeId,
738             Integer xponderNb,
739             String circuitPackName,  Ports port,
740             List<CircuitPacks> circuitPackList, Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
741
742         if (port.getPortQual() == null) {
743             LOG.warn(PortMappingUtils.PORTQUAL_LOGMSG, nodeId, port.getPortName(), circuitPackName, "not found");
744             return new int[] {line, client};
745         }
746
747         switch (port.getPortQual()) {
748
749             case XpdrClient:
750                 String lcp0 =
751                     PortMappingUtils.createXpdrLogicalConnectionPort(xponderNb, client, StringConstants.CLIENT_TOKEN);
752                 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp0);
753                 mappingMap.put(lcp0,
754                     createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null));
755                 client++;
756                 break;
757
758             case XpdrNetwork:
759                 line = fillXpdrNetworkLcpsMaps(line, nodeId,
760                         xponderNb,
761                         circuitPackName,  port,
762                         circuitPackList,  lcpMap, mappingMap);
763                 break;
764
765             default:
766                 LOG.error(PortMappingUtils.PORTQUAL_LOGMSG,
767                     nodeId, port.getPortName(), circuitPackName, port.getPortQual() + " not supported");
768         }
769         return new int[] {line, client};
770     }
771
772     private int fillXpdrNetworkLcpsMaps(int line, String nodeId,
773             Integer xponderNb,
774             String circuitPackName,  Ports port,
775             List<CircuitPacks> circuitPackList, Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
776
777         switch (port.getPortDirection()) {
778
779             case Bidirectional:
780                 String lcp =
781                     PortMappingUtils.createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
782                 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
783                 mappingMap.put(lcp,
784                     createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
785                 line++;
786                 break;
787
788             case Rx:
789             case Tx:
790                 StringBuilder circuitPackName2 = new StringBuilder();
791                 Ports port2 = getPort2(port, nodeId, circuitPackName, circuitPackName2,
792                         circuitPackList, lcpMap);
793
794                 if (port2 == null) {
795                      //key already present or an error occured and was logged
796                     return line;
797                 }
798
799                 putXpdrLcpsInMaps(line, nodeId, xponderNb,
800                         circuitPackName, circuitPackName2.toString(), port, port2,
801                         lcpMap, mappingMap);
802
803                 line += 2;
804                 break;
805
806             default:
807                 LOG.error(PortMappingUtils.UNSUPPORTED_DIR_LOGMSG,
808                      nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
809         }
810
811         return line;
812     }
813
814     private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
815         // Creating mapping data for degree TTP's
816         Map<Integer, Degree> degrees = getDegreesMap(nodeId, deviceInfo);
817         Map<String, String> interfaceList = getEthInterfaceList(nodeId);
818         List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, interfaceList);
819         LOG.info(PortMappingUtils.MAP_LOOKS_LOGMSG, nodeId, interfaceList);
820         postPortMapping(nodeId, null, null, cpToDegreeList);
821
822         Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
823         for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
824             switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
825                 case 1:
826                     // port is bidirectional
827                     String cpName = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
828                     InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
829                         .child(CircuitPacks.class, new CircuitPacksKey(cpName))
830                         .child(Ports.class,
831                                 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()));
832                     LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG,
833                         nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cpName);
834                     Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
835                         LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
836                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
837                     if (!portObject.isPresent()) {
838                         LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
839                             nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cpName);
840                         return false;
841                     }
842                     Ports port = portObject.get();
843                     if (port.getPortQual() == null) {
844                         continue;
845                     }
846                     if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
847                         LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
848                             nodeId, port.getPortName(), cpName);
849                         continue;
850                     }
851                     if (Direction.Bidirectional.getIntValue() != port.getPortDirection().getIntValue()) {
852                         LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTDIR_ERROR_LOGMSG,
853                             nodeId, port.getPortName(), cpName);
854                         continue;
855                     }
856
857                     String logicalConnectionPoint =
858                             PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(), "TXRX");
859                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
860                         nodeId, port.getPortName(), cpName, logicalConnectionPoint);
861                     portMapList.add(createMappingObject(nodeId, port, cpName, logicalConnectionPoint));
862                     break;
863                 case 2:
864                     // ports are unidirectionals
865                     String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
866                     String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
867                     InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
868                         .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
869                         .child(Ports.class,
870                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()));
871                     LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG,
872                         nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cp1Name);
873                     Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
874                         LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
875                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
876                     InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
877                         .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
878                         .child(Ports.class,
879                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName()));
880                     LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG,
881                         nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName(), cp2Name);
882                     Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
883                         LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
884                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
885                     if (!port1Object.isPresent() || !port2Object.isPresent()) {
886                         LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
887                             nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cp1Name);
888                         return false;
889                     }
890                     if (!port2Object.isPresent()) {
891                         LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
892                             nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName(), cp2Name);
893                         return false;
894                     }
895
896                     Ports port1 = port1Object.get();
897                     Ports port2 = port2Object.get();
898                     if (port1.getPortQual() == null || port2.getPortQual() == null) {
899                         continue;
900                     }
901                     if (Port.PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()) {
902                         LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
903                             nodeId, port1.getPortName(), cp1Name);
904                         continue;
905                     }
906                     if (Port.PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()) {
907                         LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
908                             nodeId, port2.getPortName(), cp2Name);
909                         continue;
910                     }
911
912                     if (!checkPartnerPort(cp1Name, port1, port2)) {
913                         LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
914                             nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
915                         continue;
916                     }
917                     // Directions checks are the same for cp1 and cp2, no need to check them twice.
918                     if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
919                         LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
920                             nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
921                         continue;
922                     }
923
924                     String logicalConnectionPoint1 = PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(),
925                             port1.getPortDirection().getName().toUpperCase(Locale.getDefault()));
926                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
927                         nodeId, port1.getPortName(), cp1Name, logicalConnectionPoint1);
928                     portMapList.add(createMappingObject(nodeId, port1, cp1Name, logicalConnectionPoint1));
929                     String logicalConnectionPoint2 = PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(),
930                             port2.getPortDirection().getName().toUpperCase(Locale.getDefault()));
931                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
932                         nodeId, port2.getPortName(), cp2Name, logicalConnectionPoint2);
933                     portMapList.add(createMappingObject(nodeId, port2, cp2Name, logicalConnectionPoint2));
934                     break;
935                 default:
936                     LOG.error(PortMappingUtils.NOT_CORRECT_CONPORT_LOGMSG, nodeId, cpMapEntry.getKey());
937                     continue;
938             }
939         }
940         return true;
941     }
942
943     private NodeInfo createNodeInfo(Info deviceInfo) {
944
945         if (deviceInfo.getNodeType() == null) {
946             // TODO make mandatory in yang
947             LOG.error(PortMappingUtils.NODE_TYPE_LOGMSG, deviceInfo.getNodeId(), "field missing");
948             return null;
949         }
950
951         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
952                 .setOpenroadmVersion(OpenroadmNodeVersion._121);
953         // TODO check if we can use here .setNodeType(NodeTypes.forValue(..) such as with 221
954         switch (deviceInfo.getNodeType().getIntValue()) {
955             case 1:
956             case 2:
957                 nodeInfoBldr.setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
958                 break;
959             default:
960                 LOG.error(PortMappingUtils.NODE_TYPE_LOGMSG, deviceInfo.getNodeId(), "value not supported");
961                 // TODO: is this protection useful ? it is not present in Portmapping 221
962         }
963         if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
964             nodeInfoBldr.setNodeClli(deviceInfo.getClli());
965         } else {
966             nodeInfoBldr.setNodeClli("defaultCLLI");
967         }
968         if (deviceInfo.getModel() != null) {
969             nodeInfoBldr.setNodeModel(deviceInfo.getModel());
970         }
971         if (deviceInfo.getVendor() != null) {
972             nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
973         }
974         if (deviceInfo.getIpAddress() != null) {
975             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
976         }
977
978         return nodeInfoBldr.build();
979     }
980
981 }