2 * Copyright © 2017 AT&T and others. All rights reserved.
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
9 package org.opendaylight.transportpce.common.mapping;
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;
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;
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 {
86 private static final Logger LOG = LoggerFactory.getLogger(PortMappingVersion121.class);
88 private final DataBroker dataBroker;
89 private final DeviceTransactionManager deviceTransactionManager;
90 private final OpenRoadmInterfaces openRoadmInterfaces;
92 public PortMappingVersion121(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
93 OpenRoadmInterfaces openRoadmInterfaces) {
94 this.dataBroker = dataBroker;
95 this.deviceTransactionManager = deviceTransactionManager;
96 this.openRoadmInterfaces = openRoadmInterfaces;
99 public boolean createMappingData(String nodeId) {
100 LOG.info("Create Mapping Data for node 1.2.1 {}", nodeId);
101 List<Mapping> portMapList = new ArrayList<>();
102 InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
103 Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType
104 .OPERATIONAL, infoIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
105 if (!deviceInfoOptional.isPresent()) {
106 LOG.warn("Device info subtree is absent for {}", nodeId);
109 Info deviceInfo = deviceInfoOptional.get();
110 NodeInfo nodeInfo = createNodeInfo(deviceInfo);
111 if (nodeInfo == null) {
114 postPortMapping(nodeId, nodeInfo, null, null);
116 switch (deviceInfo.getNodeType()) {
119 // Get TTP port mapping
120 if (!createTtpPortMapping(nodeId, deviceInfo, portMapList)) {
121 // return false if mapping creation for TTP's failed
122 LOG.warn("Unable to create mapping for TTP's on node {}", nodeId);
126 // Get PP port mapping
127 if (!createPpPortMapping(nodeId, deviceInfo, portMapList)) {
128 // return false if mapping creation for PP's failed
129 LOG.warn("Unable to create mapping for PP's on node {}", nodeId);
134 if (!createXpdrPortMapping(nodeId, portMapList)) {
135 LOG.warn("Unable to create mapping for Xponder on node {}", nodeId);
140 LOG.error("Unable to create mapping for node {} : unknown nodetype ", nodeId);
144 return postPortMapping(nodeId, nodeInfo, portMapList, null);
147 public boolean updateMapping(String nodeId, Mapping oldMapping) {
148 InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
149 .child(CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName()))
150 .child(Ports.class, new PortsKey(oldMapping.getSupportingPort()));
151 if ((oldMapping == null) || (nodeId == null)) {
152 LOG.error("Impossible to update mapping");
156 Optional<Ports> portObject = deviceTransactionManager.getDataFromDevice(nodeId,
157 LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
158 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
159 if (!portObject.isPresent()) {
162 Ports port = portObject.get();
163 Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
164 oldMapping.getLogicalConnectionPoint());
165 LOG.info("Updating old mapping Data {} for {} of {} by new mapping data {}",
166 oldMapping, oldMapping.getLogicalConnectionPoint(), nodeId, newMapping);
167 final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
168 InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class)
169 .child(Nodes.class, new NodesKey(nodeId))
170 .child(Mapping.class, new MappingKey(oldMapping.getLogicalConnectionPoint()));
171 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
172 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
175 } catch (InterruptedException | ExecutionException e) {
176 LOG.error("Error updating Mapping {} for node {}", oldMapping.getLogicalConnectionPoint(), nodeId, e);
181 private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
182 // Creating for Xponder Line and Client Ports
183 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
184 Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
185 LogicalDatastoreType.OPERATIONAL, deviceIID,
186 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
187 if (!deviceObject.isPresent()) {
188 LOG.error("Impossible to get device configuration for node {}", nodeId);
191 OrgOpenroadmDevice device = deviceObject.get();
192 if (device.getCircuitPacks() == null) {
193 LOG.warn("Circuit Packs are not present for {}", nodeId);
196 // Variable to keep track of number of line ports
198 // Variable to keep track of number of client ports
200 Map<String, String> lcpMap = new HashMap<>();
201 Map<String, Mapping> mappingMap = new HashMap<>();
202 List<CircuitPacks> circuitPackList = new ArrayList<>(device.nonnullCircuitPacks().values());
203 circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
205 for (CircuitPacks cp : circuitPackList) {
206 String circuitPackName = cp.getCircuitPackName();
207 if (cp.getPorts() == null) {
208 LOG.warn("Ports were not found for circuit pack: {}", circuitPackName);
212 // com.google.common.collect.ImmutableList implementation of List
213 List<Ports> portList = new ArrayList<>(cp.nonnullPorts().values());
214 portList.sort(Comparator.comparing(Ports::getPortName));
215 for (Ports port : portList) {
216 if (port.getPortQual() == null) {
217 LOG.warn("PortQual was not found for port {} on circuit pack: {}", port.getPortName(),
221 if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
222 && port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
223 String lcp = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
224 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
225 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
227 } else if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
228 && port.getPortDirection().getIntValue() != Direction.Bidirectional.getIntValue()
229 && port.getPartnerPort() != null
230 && port.getPartnerPort().getCircuitPackName() != null
231 && port.getPartnerPort().getPortName() != null) {
232 if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
235 String lcp1 = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
236 Optional<CircuitPacks> cpOpt = circuitPackList.stream().filter(cP -> cP.getCircuitPackName()
237 .equals(port.getPartnerPort().getCircuitPackName())).findFirst();
238 if (cpOpt.isPresent()) {
239 Optional<Ports> poOpt = cpOpt.get().nonnullPorts()
240 .values().stream().filter(p -> p.getPortName().equals(port
241 .getPartnerPort().getPortName().toString())).findFirst();
242 if (poOpt.isPresent()) {
243 Ports port2 = poOpt.get();
244 if ((Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
245 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
246 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
247 && port2.getPartnerPort().getPortName() != null
248 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
249 && port2.getPartnerPort().getPortName().equals(port.getPortName()))
251 (Direction.Tx.getIntValue() == port.getPortDirection().getIntValue()
252 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
253 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
254 && port2.getPartnerPort().getPortName() != null
255 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
256 && port2.getPartnerPort().getPortName().equals(port.getPortName()))) {
257 String lcp2 = new StringBuilder("XPDR1-").append(StringConstants.NETWORK_TOKEN)
258 .append(line + 1).toString();
259 if (!lcpMap.containsKey(lcp1) && !lcpMap.containsKey(lcp2)) {
260 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
261 lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
262 mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName, lcp1,
264 mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2,
265 cpOpt.get().getCircuitPackName(), lcp2, lcp1, null, null));
267 LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
271 LOG.error("port {} on {} is not a correct partner port of {} on {}",
272 port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
276 LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
277 port.getPartnerPort().getCircuitPackName(), nodeId);
280 LOG.error("Error fetching circuit-pack {} for {}", port.getPartnerPort().getCircuitPackName(),
283 } else if (Port.PortQual.XpdrClient.getIntValue() == port.getPortQual().getIntValue()) {
284 String lcp = "XPDR1-" + StringConstants.CLIENT_TOKEN + client;
285 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
286 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
289 LOG.warn("Error in the configuration of port {} of {} for {}", port.getPortName(), circuitPackName,
295 Collection<ConnectionMap> connectionMap = deviceObject.get().nonnullConnectionMap().values();
296 for (ConnectionMap cm : connectionMap) {
299 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
300 if (lcpMap.containsKey(skey)) {
301 slcp = lcpMap.get(skey);
303 Destination destination0 = cm.nonnullDestination().values().iterator().next();
304 String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
305 if (lcpMap.containsKey(dkey)) {
306 dlcp = lcpMap.get(dkey);
309 LOG.error("Error in connection-map analysis for source {} and destination (circuitpack+port) {}",
313 Mapping mapping = mappingMap.get(slcp);
314 mappingMap.remove(slcp);
315 portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
318 if (!mappingMap.isEmpty()) {
319 for (Mapping m : mappingMap.values()) {
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<>();
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();
338 for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
339 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
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);
352 LOG.info("Device {} has {} Srg", deviceId, cpPerSrg.size());
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);
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);
375 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
376 LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
379 // com.google.common.collect.ImmutableList implementation of List
381 List<Ports> portList = new ArrayList<>(circuitPackObject.get().nonnullPorts().values());
382 Collections.sort(portList, new SortPort121ByName());
384 for (Ports port : portList) {
385 String currentKey = circuitPackName + "-" + port.getPortName();
386 if (port.getPortQual() == null) {
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));
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(),
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()))
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(),
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));
436 keys.add(currentKey);
437 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
439 LOG.error("Error with partner port configuration for port {} of {} - {}",
440 port.getPortName(), circuitPackName, nodeId);
444 LOG.error("error getting partner port {} of {} - {}",
445 port.getPartnerPort().getPortName().toString(),
446 port.getPartnerPort().getCircuitPackName(), nodeId);
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);
459 private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
461 switch (port.getPortDirection()) {
463 lcp = "SRG" + index + "-PP" + portIndex + "-TX";
466 lcp = "SRG" + index + "-PP" + portIndex + "-RX";
469 lcp = "SRG" + index + "-PP" + portIndex + "-TXRX";
472 LOG.error("Unsupported port direction for port {} : {}", port, port.getPortDirection());
477 private List<Degree> getDegrees(String deviceId, Info ordmInfo) {
478 List<Degree> degrees = new ArrayList<>();
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();
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());
500 LOG.info("Device {} has {} degree", deviceId, degrees.size());
504 private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
505 Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
508 if (ordmInfo.getMaxDegrees() != null) {
509 maxDegree = ordmInfo.getMaxDegrees().toJava();
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()));
525 LOG.info("Device {} has {} degree", deviceId, conPortMap.size());
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());
563 LOG.warn("Couldnt find port config under LLDP for Node : {}", nodeId);
565 LOG.info("Processiong is done.. now returning..");
566 return cpToInterfaceMap;
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()));
581 return cpToDegreeList;
584 private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
585 List<CpToDegree> cp2DegreeList) {
586 NodesBuilder nodesBldr = new NodesBuilder().withKey(new NodesKey(nodeId)).setNodeId(nodeId);
587 if (nodeInfo != null) {
588 nodesBldr.setNodeInfo(nodeInfo);
590 if (portMapList != null) {
591 Map<MappingKey, Mapping> mappingMap = new HashMap<>();
592 for (Mapping mapping: portMapList) {
593 if (mapping != null) {
594 mappingMap.put(mapping.key(), mapping);
597 nodesBldr.setMapping(mappingMap);
599 if (cp2DegreeList != null) {
600 Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
601 for (CpToDegree cp2Degree: cp2DegreeList) {
602 if (cp2Degree != null) {
603 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
606 nodesBldr.setCpToDegree(cpToDegreeMap);
609 Map<NodesKey,Nodes> nodesList = new HashMap<>();
610 Nodes nodes = nodesBldr.build();
611 nodesList.put(nodes.key(),nodes);
613 Network network = new NetworkBuilder().setNodes(nodesList).build();
615 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
616 InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
617 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
618 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
622 } catch (InterruptedException | ExecutionException e) {
623 LOG.warn("Failed to post {}", network, e);
628 private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber, String nodeId,
629 Map<String, String> interfaceList) {
630 String interfaceName = null;
631 if (interfaceList.get(circuitPackName) != null) {
632 interfaceName = interfaceList.get(circuitPackName);
634 return new CpToDegreeBuilder().withKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
635 .setDegreeNumber(Uint32.valueOf(degreeNumber)).setInterfaceName(interfaceName).build();
638 private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
639 String logicalConnectionPoint) {
641 MappingBuilder mpBldr = new MappingBuilder()
642 .withKey(new MappingKey(logicalConnectionPoint))
643 .setLogicalConnectionPoint(logicalConnectionPoint)
644 .setSupportingCircuitPackName(circuitPackName)
645 .setSupportingPort(port.getPortName())
646 .setPortDirection(port.getPortDirection().getName());
648 // Get OMS and OTS interface provisioned on the TTP's
649 if (logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) && (port.getInterfaces() != null)) {
650 for (Interfaces interfaces : port.getInterfaces()) {
652 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
653 interfaces.getInterfaceName());
654 if (openRoadmInterface.isPresent()) {
655 Class<? extends InterfaceType> interfaceType
656 = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
657 // Check if interface type is OMS or OTS
658 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
659 mpBldr.setSupportingOms(interfaces.getInterfaceName());
661 if (interfaceType.equals(OpticalTransport.class)) {
662 mpBldr.setSupportingOts(interfaces.getInterfaceName());
665 LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
667 } catch (OpenRoadmInterfaceException ex) {
668 LOG.warn("Error while getting interface {} from node {}!", interfaces.getInterfaceName(), nodeId,
673 return mpBldr.build();
676 private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
677 String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
679 if (mapping != null && assoLcp != null) {
680 // update existing mapping
681 return new MappingBuilder(mapping).setConnectionMapLcp(assoLcp).build();
684 // create a new mapping
685 String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
686 MappingBuilder mpBldr = new MappingBuilder()
687 .withKey(new MappingKey(logicalConnectionPoint))
688 .setLogicalConnectionPoint(logicalConnectionPoint)
689 .setSupportingCircuitPackName(circuitPackName)
690 .setSupportingPort(port.getPortName())
691 .setPortDirection(port.getPortDirection().getName())
692 .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
693 if (port.getPortQual() != null) {
694 mpBldr.setPortQual(port.getPortQual().getName());
696 if (partnerLcp != null) {
697 mpBldr.setPartnerLcp(partnerLcp);
700 return mpBldr.build();
703 // some LOG messages are too long
704 @SuppressWarnings("checkstyle:linelength")
705 @SuppressFBWarnings("DM_CONVERT_CASE")
706 private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
707 // Creating mapping data for degree TTP's
708 List<Degree> degrees = getDegrees(nodeId, deviceInfo);
709 Map<String, String> interfaceList = getEthInterfaceList(nodeId);
710 List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, nodeId, interfaceList);
711 LOG.info("Map looks like this {}", interfaceList);
712 postPortMapping(nodeId, null, null, cpToDegreeList);
714 Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
715 for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
716 switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
718 // port is bidirectional
719 InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
720 .child(CircuitPacks.class, new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0)
721 .getCircuitPackName()))
722 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0)
723 .getPortName().toString()));
724 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap
725 .get(cpMapEntry.getKey()).get(0).getPortName().toString(), connectionPortMap
726 .get(cpMapEntry.getKey()).get(0).getCircuitPackName());
727 Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
728 LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
729 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
730 if (portObject.isPresent()) {
731 Ports port = portObject.get();
732 if (port.getPortQual() == null) {
734 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
735 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()) {
736 String logicalConnectionPoint = new StringBuilder("DEG").append(cpMapEntry.getKey())
737 .append("-TTP-TXRX").toString();
738 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
739 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
740 port.getPortName(), logicalConnectionPoint);
741 portMapList.add(createMappingObject(nodeId, port,
742 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
743 logicalConnectionPoint));
746 "Impossible to create logical connection point for port {} of {} on node {} - Error in configuration with port-qual or port-direction",
747 port.getPortName(), connectionPortMap
748 .get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
751 LOG.error("No port {} on circuit pack {} for node {}",
752 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
753 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
758 // ports are unidirectionals
759 String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
760 String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
761 InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
762 .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
763 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()
765 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap
766 .get(cpMapEntry.getKey()).get(0).getPortName().toString(), cp1Name);
767 Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
768 LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
769 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
770 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
771 .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
772 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName()
774 LOG.info("Fetching connection-port {} at circuit pack {}",
775 connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString(), cp2Name);
776 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
777 LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
778 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
779 if (port1Object.isPresent() && port2Object.isPresent()) {
780 Ports port1 = port1Object.get();
781 Ports port2 = port2Object.get();
782 if (port1.getPortQual() == null || port2.getPortQual() == null) {
784 } else if ((Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
785 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
786 && Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
787 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
788 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
789 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
790 && port1.getPartnerPort().getPortName().equals(port2.getPortName())
791 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
792 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))
794 (Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
795 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
796 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
797 && Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
798 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
799 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
800 && port1.getPartnerPort().getPortName().equals(port2.getPortName())
801 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
802 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))) {
803 String logicalConnectionPoint1 = new StringBuilder("DEG").append(cpMapEntry.getKey())
804 .append("-TTP-").append(port1.getPortDirection().getName().toUpperCase()).toString();
805 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
806 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
807 port1.getPortName(), logicalConnectionPoint1);
808 portMapList.add(createMappingObject(nodeId, port1, connectionPortMap
809 .get(cpMapEntry.getKey()).get(0).getCircuitPackName(), logicalConnectionPoint1));
810 String logicalConnectionPoint2 = new StringBuilder("DEG").append(cpMapEntry.getKey())
811 .append("-TTP-").append(port2.getPortDirection().getName().toUpperCase()).toString();
812 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
813 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
814 port2.getPortName(), logicalConnectionPoint2);
815 portMapList.add(createMappingObject(nodeId, port2, connectionPortMap
816 .get(cpMapEntry.getKey()).get(1).getCircuitPackName(), logicalConnectionPoint2));
819 "Impossible to create logical connection point for port {} or port {} on node {} - Error in configuration with port-qual, port-direction or partner-port configuration",
820 port1.getPortName(), port2.getPortName(), nodeId);
823 LOG.error("No port {} on circuit pack {} for node {}",
824 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
825 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
831 LOG.error("Number of connection port for DEG{} on {} is incorrect", cpMapEntry.getKey(), nodeId);
838 private NodeInfo createNodeInfo(Info deviceInfo) {
840 if (deviceInfo.getNodeType() == null) {
841 // TODO make mandatory in yang
842 LOG.error("Node type field is missing");
846 NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
847 .setOpenroadmVersion(OpenroadmVersion._121);
848 // TODO check if we can use here .setNodeType(NodeTypes.forValue(..) such as with 221
849 switch (deviceInfo.getNodeType().getIntValue()) {
852 nodeInfoBldr.setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
855 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
856 // TODO: is this protection useful ? it is not present in Portmapping 221
858 if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
859 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
861 nodeInfoBldr.setNodeClli("defaultCLLI");
863 if (deviceInfo.getModel() != null) {
864 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
866 if (deviceInfo.getVendor() != null) {
867 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
869 if (deviceInfo.getIpAddress() != null) {
870 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
873 return nodeInfoBldr.build();