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