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