Add a portmapping method to map degrees
[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 Map<Integer, Degree> getDegreesMap(String deviceId, Info ordmInfo) {
519         Map<Integer, Degree> degrees = new HashMap<>();
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.put(degreeCounter, 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         getDegreesMap(deviceId, ordmInfo).forEach(
543             (index, degree) -> conPortMap.put(index, new ArrayList<>(degree.nonnullConnectionPorts().values())));
544         return conPortMap;
545     }
546
547     private Map<String, String> getEthInterfaceList(String nodeId) {
548         LOG.info("It is calling get ethernet interface");
549         InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
550             .child(Protocols.class);
551         Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
552             LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
553             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
554         if (!protocolObject.isPresent() || protocolObject.get().augmentation(Protocols1.class).getLldp() == null) {
555             LOG.warn("Couldnt find port config under LLDP for Node : {} - Processiong is done.. now returning..",
556                 nodeId);
557             return new HashMap<>();
558         }
559         Map<String, String> cpToInterfaceMap = new HashMap<>();
560         Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
561         for (PortConfig portConfig : lldp.nonnullPortConfig().values()) {
562             if (!portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
563                 continue;
564             }
565             InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
566                 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
567             Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
568                 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
569                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
570             if (!interfaceObject.isPresent() || (interfaceObject.get().getSupportingCircuitPackName() == null)) {
571                 continue;
572             }
573             String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
574             cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
575             InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
576                 .create(OrgOpenroadmDevice.class)
577                 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
578             Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
579                 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
580                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
581             if (!circuitPackObject.isPresent() || (circuitPackObject.get().getParentCircuitPack() == null)) {
582                 continue;
583             }
584             cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
585                 portConfig.getIfName());
586         }
587         LOG.info("Processiong is done.. now returning..");
588         return cpToInterfaceMap;
589     }
590
591     private List<CpToDegree> getCpToDegreeList(Map<Integer, Degree> degrees, Map<String, String> interfaceList) {
592         List<CpToDegree> cpToDegreeList = new ArrayList<>();
593         for (Degree degree : degrees.values()) {
594             LOG.info("Inside CP to degree list");
595             cpToDegreeList.addAll(degree.nonnullCircuitPacks().values().stream()
596                 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
597                     degree.getDegreeNumber().toString(), interfaceList))
598                 .collect(Collectors.toList()));
599         }
600         return cpToDegreeList;
601     }
602
603     private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
604             List<CpToDegree> cp2DegreeList) {
605         NodesBuilder nodesBldr = new NodesBuilder().withKey(new NodesKey(nodeId)).setNodeId(nodeId);
606         if (nodeInfo != null) {
607             nodesBldr.setNodeInfo(nodeInfo);
608         }
609         if (portMapList != null) {
610             Map<MappingKey, Mapping> mappingMap = new HashMap<>();
611             // No element in the list below should be null at this stage
612             for (Mapping mapping: portMapList) {
613                 mappingMap.put(mapping.key(), mapping);
614             }
615             nodesBldr.setMapping(mappingMap);
616         }
617         if (cp2DegreeList != null) {
618             Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
619             // No element in the list below should be null at this stage
620             for (CpToDegree cp2Degree: cp2DegreeList) {
621                 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
622             }
623             nodesBldr.setCpToDegree(cpToDegreeMap);
624         }
625
626         Map<NodesKey,Nodes> nodesList = new HashMap<>();
627         Nodes nodes = nodesBldr.build();
628         nodesList.put(nodes.key(),nodes);
629
630         Network network = new NetworkBuilder().setNodes(nodesList).build();
631
632         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
633         InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
634         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
635         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
636         try {
637             commit.get();
638             return true;
639         } catch (InterruptedException | ExecutionException e) {
640             LOG.warn("Failed to post {}", network, e);
641             return false;
642         }
643     }
644
645     private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber,
646             Map<String, String> interfaceList) {
647         return new CpToDegreeBuilder()
648             .withKey(new CpToDegreeKey(circuitPackName))
649             .setCircuitPackName(circuitPackName)
650             .setDegreeNumber(Uint32.valueOf(degreeNumber))
651             .setInterfaceName(interfaceList.get(circuitPackName)).build();
652     }
653
654     private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
655             String logicalConnectionPoint) {
656         MappingBuilder mpBldr = new MappingBuilder()
657                 .withKey(new MappingKey(logicalConnectionPoint))
658                 .setLogicalConnectionPoint(logicalConnectionPoint)
659                 .setSupportingCircuitPackName(circuitPackName)
660                 .setSupportingPort(port.getPortName())
661                 .setPortDirection(port.getPortDirection().getName());
662
663         if (!logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) || (port.getInterfaces() == null)) {
664             return mpBldr.build();
665         }
666
667         // Get OMS and OTS interface provisioned on the TTP's
668         for (Interfaces interfaces : port.getInterfaces()) {
669             try {
670                 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
671                     interfaces.getInterfaceName());
672                 if (!openRoadmInterface.isPresent()) {
673                     LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
674                     continue;
675                 }
676                 Class<? extends InterfaceType> interfaceType
677                     = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
678                 // Check if interface type is OMS or OTS
679                 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
680                     mpBldr.setSupportingOms(interfaces.getInterfaceName());
681                 }
682                 if (interfaceType.equals(OpticalTransport.class)) {
683                     mpBldr.setSupportingOts(interfaces.getInterfaceName());
684                 }
685             } catch (OpenRoadmInterfaceException ex) {
686                 LOG.warn("Error while getting interface {} from node {}!",
687                     interfaces.getInterfaceName(), nodeId, ex);
688             }
689         }
690         return mpBldr.build();
691     }
692
693     private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
694             String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
695
696         if (mapping != null && assoLcp != null) {
697             // update existing mapping
698             return new MappingBuilder(mapping).setConnectionMapLcp(assoLcp).build();
699         }
700
701         // create a new mapping
702         String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
703         MappingBuilder mpBldr = new MappingBuilder()
704                 .withKey(new MappingKey(logicalConnectionPoint))
705                 .setLogicalConnectionPoint(logicalConnectionPoint)
706                 .setSupportingCircuitPackName(circuitPackName)
707                 .setSupportingPort(port.getPortName())
708                 .setPortDirection(port.getPortDirection().getName())
709                 .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
710         if (port.getPortQual() != null) {
711             mpBldr.setPortQual(port.getPortQual().getName());
712         }
713         if (partnerLcp != null) {
714             mpBldr.setPartnerLcp(partnerLcp);
715         }
716
717         return mpBldr.build();
718     }
719
720     private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
721         // Creating mapping data for degree TTP's
722         Map<Integer, Degree> degrees = getDegreesMap(nodeId, deviceInfo);
723         Map<String, String> interfaceList = getEthInterfaceList(nodeId);
724         List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, interfaceList);
725         LOG.info("Map looks like this {}", interfaceList);
726         postPortMapping(nodeId, null, null, cpToDegreeList);
727
728         Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
729         for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
730             switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
731                 case 1:
732                     // port is bidirectional
733                     InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
734                         .child(CircuitPacks.class,
735                             new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName()))
736                         .child(Ports.class,
737                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
738                     LOG.info("Fetching connection-port {} at circuit pack {}",
739                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
740                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
741                     Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
742                         LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
743                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
744                     if (!portObject.isPresent()) {
745                         LOG.error("No port {} on circuit pack {} for node {}",
746                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
747                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
748                         return false;
749                     }
750                     Ports port = portObject.get();
751                     if (port.getPortQual() == null) {
752                         continue;
753                     }
754                     if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()
755                         || Direction.Bidirectional.getIntValue() != port.getPortDirection().getIntValue()) {
756                         LOG.error(
757                             "Impossible to create logical connection point for port {} of {} on node {}"
758                             + " - Error in configuration with port-qual or port-direction",
759                             port.getPortName(),
760                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
761                         continue;
762                     }
763                     String logicalConnectionPoint = new StringBuilder("DEG")
764                         .append(cpMapEntry.getKey())
765                         .append("-TTP-TXRX")
766                         .toString();
767                     LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
768                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
769                         port.getPortName(), logicalConnectionPoint);
770                     portMapList.add(createMappingObject(nodeId, port,
771                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
772                         logicalConnectionPoint));
773                     break;
774                 case 2:
775                     // ports are unidirectionals
776                     String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
777                     String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
778                     InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
779                         .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
780                         .child(Ports.class,
781                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
782                     LOG.info("Fetching connection-port {} at circuit pack {}",
783                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
784                         cp1Name);
785                     Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
786                         LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
787                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
788                     InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
789                         .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
790                         .child(Ports.class,
791                             new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString()));
792                     LOG.info("Fetching connection-port {} at circuit pack {}",
793                         connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString(), cp2Name);
794                     Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
795                         LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
796                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
797                     if (!port1Object.isPresent() || !port2Object.isPresent()) {
798                         LOG.error("No port {} on circuit pack {} for node {}",
799                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
800                             connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
801                         return false;
802                     }
803
804                     Ports port1 = port1Object.get();
805                     Ports port2 = port2Object.get();
806                     if (port1.getPortQual() == null || port2.getPortQual() == null) {
807                         continue;
808                     }
809                     if (Port.PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()
810                         || Port.PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()) {
811                         LOG.error("Impossible to create logical connection point for port {} or port {} on node {}"
812                                 + " - Error in configuration with port-qual",
813                             port1.getPortName(), port2.getPortName(), nodeId);
814                         continue;
815                     }
816                     if (!checkPartnerPort(cp1Name, port1, port2)) {
817                         LOG.error("port {} on {} is not a correct partner port of {} on  {}",
818                             port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
819                         continue;
820                     }
821                     // Directions checks are the same for cp1 and cp2, no need to check them twice.
822                     if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
823                         LOG.error("port {} on {} is not a correct partner port of {} on  {}",
824                             port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
825                         continue;
826                     }
827
828                     String logicalConnectionPoint1 = new StringBuilder("DEG")
829                         .append(cpMapEntry.getKey())
830                         .append("-TTP-")
831                         .append(port1.getPortDirection().getName().toUpperCase(Locale.getDefault()))
832                         .toString();
833                     LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
834                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
835                         port1.getPortName(), logicalConnectionPoint1);
836                     portMapList.add(createMappingObject(nodeId, port1,
837                         connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
838                         logicalConnectionPoint1));
839                     String logicalConnectionPoint2 = new StringBuilder("DEG")
840                         .append(cpMapEntry.getKey())
841                         .append("-TTP-")
842                         .append(port2.getPortDirection().getName().toUpperCase(Locale.getDefault()))
843                         .toString();
844                     LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
845                         connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
846                         port2.getPortName(), logicalConnectionPoint2);
847                     portMapList.add(createMappingObject(nodeId, port2,
848                         connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
849                         logicalConnectionPoint2));
850                     break;
851                 default:
852                     LOG.error("Number of connection port for DEG{} on {} is incorrect", cpMapEntry.getKey(), nodeId);
853                     continue;
854             }
855         }
856         return true;
857     }
858
859     private NodeInfo createNodeInfo(Info deviceInfo) {
860
861         if (deviceInfo.getNodeType() == null) {
862             // TODO make mandatory in yang
863             LOG.error("Node type field is missing");
864             return null;
865         }
866
867         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
868                 .setOpenroadmVersion(OpenroadmVersion._121);
869         // TODO check if we can use here .setNodeType(NodeTypes.forValue(..) such as with 221
870         switch (deviceInfo.getNodeType().getIntValue()) {
871             case 1:
872             case 2:
873                 nodeInfoBldr.setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
874                 break;
875             default:
876                 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
877                 // TODO: is this protection useful ? it is not present in Portmapping 221
878         }
879         if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
880             nodeInfoBldr.setNodeClli(deviceInfo.getClli());
881         } else {
882             nodeInfoBldr.setNodeClli("defaultCLLI");
883         }
884         if (deviceInfo.getModel() != null) {
885             nodeInfoBldr.setNodeModel(deviceInfo.getModel());
886         }
887         if (deviceInfo.getVendor() != null) {
888             nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
889         }
890         if (deviceInfo.getIpAddress() != null) {
891             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
892         }
893
894         return nodeInfoBldr.build();
895     }
896
897 }