fd964b4cc9224cd01556e1dce32d28ee89268c31
[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 = createXpdrLogicalConnectionPort(1, client, StringConstants.CLIENT_TOKEN);
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 = createXpdrLogicalConnectionPort(1, line, StringConstants.NETWORK_TOKEN);
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
251                         if (!checkPartnerPortNotNull(port)) {
252                             LOG.warn("Error in the configuration of port {} of {} for {}",
253                                 port.getPortName(), circuitPackName, nodeId);
254                             continue;
255                         }
256
257                         if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
258                             continue;
259                         }
260
261                         Optional<CircuitPacks> cpOpt = circuitPackList.stream()
262                             .filter(cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
263                             .findFirst();
264                         if (!cpOpt.isPresent()) {
265                             LOG.error("Error fetching circuit-pack {} for {}",
266                                 port.getPartnerPort().getCircuitPackName(), nodeId);
267                             continue;
268                         }
269
270                         Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
271                             .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName().toString()))
272                             .findFirst();
273                         if (!poOpt.isPresent()) {
274                             LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
275                                 port.getPartnerPort().getCircuitPackName(), nodeId);
276                             continue;
277                         }
278                         Ports port2 = poOpt.get();
279                         if (!checkPartnerPort(circuitPackName, port, port2)) {
280                             LOG.error("port {} on {} is not a correct partner port of {} on  {}",
281                                 port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
282                                 circuitPackName);
283                             continue;
284                         }
285                         String lcp1 = createXpdrLogicalConnectionPort(1, line, StringConstants.NETWORK_TOKEN);
286                         String lcp2 = createXpdrLogicalConnectionPort(1, line + 1, StringConstants.NETWORK_TOKEN);
287                         if (lcpMap.containsKey(lcp1) || lcpMap.containsKey(lcp2)) {
288                             LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
289                             line += 2;
290                             continue;
291                         }
292                         lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
293                         lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
294                         mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName, lcp1,
295                             lcp2, null, null));
296                         mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2,
297                             cpOpt.get().getCircuitPackName(), lcp2, lcp1, null, null));
298                         line += 2;
299                         break;
300
301                     default:
302                         LOG.warn(
303                             "Error in the configuration of port {} of {} for {} - unsupported PortQual {}",
304                             port.getPortName(), circuitPackName, nodeId, port.getPortQual().getIntValue());
305                 }
306             }
307         }
308
309         Collection<ConnectionMap> connectionMap = deviceObject.get().nonnullConnectionMap().values();
310         for (ConnectionMap cm : connectionMap) {
311             String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
312             String slcp = lcpMap.containsKey(skey) ? lcpMap.get(skey) : null;
313             Destination destination0 = cm.nonnullDestination().values().iterator().next();
314             String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
315             if (slcp == null) {
316                 LOG.error("Error in connection-map analysis for source {} and destination (circuitpack+port) {}",
317                     skey, dkey);
318                 continue;
319             }
320             String dlcp = lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null;
321             Mapping mapping = mappingMap.get(slcp);
322             mappingMap.remove(slcp);
323             portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
324         }
325
326         if (!mappingMap.isEmpty()) {
327             for (Mapping m : mappingMap.values()) {
328                 portMapList.add(m);
329             }
330         }
331         return true;
332     }
333
334     private boolean checkPartnerPortNotNull(Ports port) {
335         if (port.getPartnerPort() == null
336             || port.getPartnerPort().getCircuitPackName() == null
337             || port.getPartnerPort().getPortName() == null) {
338             return false;
339         }
340         return true;
341     }
342
343     private boolean checkPartnerPortNoDir(String circuitPackName, Ports port1, Ports port2) {
344         if (!checkPartnerPortNotNull(port2)
345             || !port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
346             || !port2.getPartnerPort().getPortName().equals(port1.getPortName())) {
347             return false;
348         }
349         return true;
350     }
351
352     private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
353         if (!checkPartnerPortNoDir(circuitPackName, port1, port2)
354             || ((Direction.Rx.getIntValue() != port1.getPortDirection().getIntValue()
355                     || Direction.Tx.getIntValue() != port2.getPortDirection().getIntValue())
356                 &&
357                 (Direction.Tx.getIntValue() != port1.getPortDirection().getIntValue()
358                     || Direction.Rx.getIntValue() != port2.getPortDirection().getIntValue()))) {
359             return false;
360         }
361         return true;
362     }
363
364     private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
365             .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
366         HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
367             .CircuitPacks>> cpPerSrg = new HashMap<>();
368         // Get value for max Srg from info subtree, required for iteration
369         // if not present assume to be 20 (temporary)
370         Integer maxSrg = ordmInfo.getMaxSrgs() == null ? 20 : ordmInfo.getMaxSrgs().toJava();
371         for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
372             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
373                 = new ArrayList<>();
374             LOG.info("Getting Circuitpacks for Srg Number {}", srgCounter);
375             InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
376                 .child(SharedRiskGroup.class, new SharedRiskGroupKey(Uint16.valueOf(srgCounter)));
377             Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
378                 LogicalDatastoreType.OPERATIONAL, srgIID,
379                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
380             if (ordmSrgObject.isPresent()) {
381                 srgCps.addAll(ordmSrgObject.get().nonnullCircuitPacks().values());
382                 cpPerSrg.put(ordmSrgObject.get().getSrgNumber().toJava(), srgCps);
383             }
384         }
385         LOG.info("Device {} has {} Srg", deviceId, cpPerSrg.size());
386         return cpPerSrg;
387     }
388
389     private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
390         // Creating mapping data for SRG's PP
391         HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCps
392             = getSrgCps(nodeId, deviceInfo);
393
394         for (Entry<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>>
395                 srgCpEntry : srgCps.entrySet()) {
396             List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> cpList =
397                 srgCps.get(srgCpEntry.getKey());
398             List<String> keys = new ArrayList<>();
399             for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cp : cpList) {
400                 String circuitPackName = cp.getCircuitPackName();
401                 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
402                     .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
403                 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
404                     LogicalDatastoreType.OPERATIONAL, cpIID,
405                     Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
406
407                 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
408                     LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
409                     continue;
410                 }
411                 // com.google.common.collect.ImmutableList implementation of List
412                 @Nullable
413                 List<Ports> portList = new ArrayList<>(circuitPackObject.get().nonnullPorts().values());
414                 Collections.sort(portList, new SortPort121ByName());
415                 int portIndex = 1;
416                 for (Ports port : portList) {
417                     String currentKey = circuitPackName + "-" + port.getPortName();
418                     if (port.getPortQual() == null) {
419                         continue;
420                     }
421
422                     if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
423                         LOG.info("{} : port {} on {} is not roadm-external - cannot assign logicalConnectionPoint.",
424                             nodeId, port.getPortName(), circuitPackName);
425                         continue;
426                     }
427
428                     if (keys.contains(currentKey)) {
429                         LOG.info("{} : port {} on {} has already been handled - cannot assign logicalConnectionPoint.",
430                             nodeId, port.getPortName(), circuitPackName);
431                         continue;
432                     }
433
434                     switch (port.getPortDirection()) {
435
436                         case Bidirectional:
437                             String lcp = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
438                             LOG.info("{} : Logical Connection Point for {} {} is {}",
439                                 nodeId, circuitPackName, port.getPortName(), lcp);
440                             portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp));
441                             portIndex++;
442                             keys.add(currentKey);
443                             break;
444
445                         case Rx:
446                         case Tx:
447                             if (!checkPartnerPortNotNull(port)) {
448                                 LOG.info("{} : port {} on {} is unidirectional but has no valid partnerPort"
449                                     + " - cannot assign  logicalConnectionPoint.",
450                                     nodeId, port.getPortName(), circuitPackName);
451                                 continue;
452                             }
453
454                             String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
455                             LOG.info("{} : Logical Connection Point for {} {} is {}",
456                                 nodeId, circuitPackName, port.getPortName(), lcp1);
457                             InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
458                                 .child(CircuitPacks.class,
459                                     new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
460                                 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
461                             Optional<Ports> port2Object = this.deviceTransactionManager
462                                 .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
463                                     Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
464                             if (!port2Object.isPresent()
465                                 || port2Object.get().getPortQual().getIntValue()
466                                     != Port.PortQual.RoadmExternal.getIntValue()) {
467                                 LOG.error("error getting partner port {} of  {} - {}",
468                                     port.getPartnerPort().getPortName().toString(),
469                                     port.getPartnerPort().getCircuitPackName(), nodeId);
470                                 continue;
471                             }
472
473                             Ports port2 = port2Object.get();
474                             if (!checkPartnerPort(circuitPackName, port, port2)) {
475                                 LOG.error("Error with partner port configuration for port {} of  {} - {}",
476                                     port.getPortName(), circuitPackName, nodeId);
477                                 portIndex++;
478                                 continue;
479                             }
480                             String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
481                             LOG.info("{} : Logical Connection Point for {} {} is {}",
482                                 nodeId, circuitPackName, port2.getPortName(), lcp2);
483                             portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp1));
484                             portMapList.add(
485                                 createMappingObject(nodeId ,port2, port.getPartnerPort().getCircuitPackName(), lcp2));
486                             portIndex++;
487                             keys.add(currentKey);
488                             keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
489                             break;
490
491                         default:
492                             LOG.info("{} : port {} on {} - unsupported Direction"
493                                     + " - cannot assign  logicalConnectionPoint.",
494                                 nodeId, port.getPortName(), circuitPackName);
495
496                     }
497                 }
498             }
499         }
500         return true;
501     }
502
503     private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
504         if (SUFFIX.containsKey(port.getPortDirection())) {
505             return String.join("-", "SRG" + index, "PP" + portIndex, SUFFIX.get(port.getPortDirection()));
506         }
507         LOG.error("port {} : Unsupported port direction {}", port, port.getPortDirection());
508         return null;
509     }
510
511     private String createXpdrLogicalConnectionPort(int xponderNb, int lcpNb, String token) {
512         return new StringBuilder("XPDR").append(xponderNb)
513                 .append("-")
514                 .append(token).append(lcpNb)
515                 .toString();
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 }