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