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