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 java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Locale;
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);
87 private static final Map<Direction, String> SUFFIX;
89 private final DataBroker dataBroker;
90 private final DeviceTransactionManager deviceTransactionManager;
91 private final OpenRoadmInterfaces openRoadmInterfaces;
97 Direction.Bidirectional, "TXRX");
100 public PortMappingVersion121(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
101 OpenRoadmInterfaces openRoadmInterfaces) {
102 this.dataBroker = dataBroker;
103 this.deviceTransactionManager = deviceTransactionManager;
104 this.openRoadmInterfaces = openRoadmInterfaces;
107 public boolean createMappingData(String nodeId) {
108 LOG.info("{} : OpenROADM version 1.2.1 node - Creating Mapping Data", nodeId);
109 List<Mapping> portMapList = new ArrayList<>();
110 InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
111 Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType
112 .OPERATIONAL, infoIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
113 if (!deviceInfoOptional.isPresent()) {
114 LOG.warn("{} : Device info subtree is absent", nodeId);
117 Info deviceInfo = deviceInfoOptional.get();
118 NodeInfo nodeInfo = createNodeInfo(deviceInfo);
119 if (nodeInfo == null) {
122 postPortMapping(nodeId, nodeInfo, null, null);
124 switch (deviceInfo.getNodeType()) {
127 // Get TTP port mapping
128 if (!createTtpPortMapping(nodeId, deviceInfo, portMapList)) {
129 // return false if mapping creation for TTP's failed
130 LOG.warn("{} : Unable to create mapping for TTP's", nodeId);
134 // Get PP port mapping
135 if (!createPpPortMapping(nodeId, deviceInfo, portMapList)) {
136 // return false if mapping creation for PP's failed
137 LOG.warn("{} : Unable to create mapping for PP's", nodeId);
142 if (!createXpdrPortMapping(nodeId, portMapList)) {
143 LOG.warn("{} : Unable to create mapping for Xponder", nodeId);
148 LOG.error("{} : unknown nodetype - Unable to create mapping", nodeId);
152 return postPortMapping(nodeId, nodeInfo, portMapList, null);
155 public boolean updateMapping(String nodeId, Mapping oldMapping) {
156 InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
157 .child(CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName()))
158 .child(Ports.class, new PortsKey(oldMapping.getSupportingPort()));
159 if ((oldMapping == null) || (nodeId == null)) {
160 LOG.error("Impossible to update mapping");
164 Optional<Ports> portObject = deviceTransactionManager.getDataFromDevice(nodeId,
165 LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
166 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
167 if (!portObject.isPresent()) {
170 Ports port = portObject.get();
171 Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
172 oldMapping.getLogicalConnectionPoint());
173 LOG.debug("{} : Updating old mapping Data {} for {} by new mapping data {}",
174 nodeId, oldMapping, oldMapping.getLogicalConnectionPoint(), newMapping);
175 final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
176 InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class)
177 .child(Nodes.class, new NodesKey(nodeId))
178 .child(Mapping.class, new MappingKey(oldMapping.getLogicalConnectionPoint()));
179 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
180 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
183 } catch (InterruptedException | ExecutionException e) {
184 LOG.error("{} : exception when updating Mapping {} - ",
185 nodeId, oldMapping.getLogicalConnectionPoint(), e);
190 private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
191 // Creating for Xponder Line and Client Ports
192 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
193 Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
194 LogicalDatastoreType.OPERATIONAL, deviceIID,
195 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
196 if (!deviceObject.isPresent()) {
197 LOG.error("{} : Impossible to get device configuration", nodeId);
200 OrgOpenroadmDevice device = deviceObject.get();
201 if (device.getCircuitPacks() == null) {
202 LOG.warn("{} : Circuit Packs not present", nodeId);
205 // Variable to keep track of number of line ports
207 // Variable to keep track of number of client ports
209 Map<String, String> lcpMap = new HashMap<>();
210 Map<String, Mapping> mappingMap = new HashMap<>();
211 List<CircuitPacks> circuitPackList = new ArrayList<>(device.nonnullCircuitPacks().values());
212 circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
214 for (CircuitPacks cp : circuitPackList) {
215 String circuitPackName = cp.getCircuitPackName();
216 if (cp.getPorts() == null) {
217 LOG.warn("{} : Ports were not found for circuit pack {}", nodeId, circuitPackName);
221 // com.google.common.collect.ImmutableList implementation of List
222 List<Ports> portList = new ArrayList<>(cp.nonnullPorts().values());
223 portList.sort(Comparator.comparing(Ports::getPortName));
224 for (Ports port : portList) {
225 if (port.getPortQual() == null) {
226 LOG.warn("{} : port {} on {} - PortQual was not found",
227 nodeId, port.getPortName(), circuitPackName);
231 switch (port.getPortQual()) {
234 String lcp0 = createXpdrLogicalConnectionPort(1, client, StringConstants.CLIENT_TOKEN);
235 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp0);
237 createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null));
243 switch (port.getPortDirection()) {
246 String lcp = createXpdrLogicalConnectionPort(1, line, StringConstants.NETWORK_TOKEN);
247 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
249 createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
255 StringBuilder circuitPackName2 = new StringBuilder();
256 Ports port2 = getPort2(port, nodeId, circuitPackName, circuitPackName2,
257 circuitPackList, lcpMap);
259 //key already present or an error occured and was logged
262 putXpdrLcpsInMaps(line, nodeId, 1,
263 circuitPackName, circuitPackName2.toString(), port, port2,
269 LOG.error("{} : port {} on {} - unsupported Direction {}",
270 nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
275 LOG.error("{} : port {} on {} - unsupported PortQual {}",
276 nodeId, port.getPortName(), circuitPackName, port.getPortQual());
281 Collection<ConnectionMap> connectionMap = deviceObject.get().nonnullConnectionMap().values();
282 for (ConnectionMap cm : connectionMap) {
283 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
284 String slcp = lcpMap.containsKey(skey) ? lcpMap.get(skey) : null;
285 Destination destination0 = cm.nonnullDestination().values().iterator().next();
286 String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
288 LOG.error("{} : Error in connection-map analysis for source {} and destination (CP+port) {}",
292 String dlcp = lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null;
293 Mapping mapping = mappingMap.get(slcp);
294 mappingMap.remove(slcp);
295 portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
298 if (!mappingMap.isEmpty()) {
299 for (Mapping m : mappingMap.values()) {
306 private boolean checkPartnerPortNotNull(Ports port) {
307 if (port.getPartnerPort() == null
308 || port.getPartnerPort().getCircuitPackName() == null
309 || port.getPartnerPort().getPortName() == null) {
315 private boolean checkPartnerPortNoDir(String circuitPackName, Ports port1, Ports port2) {
316 if (!checkPartnerPortNotNull(port2)
317 || !port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
318 || !port2.getPartnerPort().getPortName().equals(port1.getPortName())) {
324 private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
325 if (!checkPartnerPortNoDir(circuitPackName, port1, port2)
326 || ((Direction.Rx.getIntValue() != port1.getPortDirection().getIntValue()
327 || Direction.Tx.getIntValue() != port2.getPortDirection().getIntValue())
329 (Direction.Tx.getIntValue() != port1.getPortDirection().getIntValue()
330 || Direction.Rx.getIntValue() != port2.getPortDirection().getIntValue()))) {
336 private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
337 .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
338 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
339 .CircuitPacks>> cpPerSrg = new HashMap<>();
340 // Get value for max Srg from info subtree, required for iteration
341 // if not present assume to be 20 (temporary)
342 Integer maxSrg = ordmInfo.getMaxSrgs() == null ? 20 : ordmInfo.getMaxSrgs().toJava();
343 for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
344 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
346 LOG.debug("{} : Getting Circuitpacks for Srg Number {}", deviceId, srgCounter);
347 InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
348 .child(SharedRiskGroup.class, new SharedRiskGroupKey(Uint16.valueOf(srgCounter)));
349 Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
350 LogicalDatastoreType.OPERATIONAL, srgIID,
351 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
352 if (ordmSrgObject.isPresent()) {
353 srgCps.addAll(ordmSrgObject.get().nonnullCircuitPacks().values());
354 cpPerSrg.put(ordmSrgObject.get().getSrgNumber().toJava(), srgCps);
357 LOG.info("{} : Device has {} Srg", deviceId, cpPerSrg.size());
361 private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
362 // Creating mapping data for SRG's PP
363 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCps
364 = getSrgCps(nodeId, deviceInfo);
366 for (Entry<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>>
367 srgCpEntry : srgCps.entrySet()) {
368 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> cpList =
369 srgCps.get(srgCpEntry.getKey());
370 List<String> keys = new ArrayList<>();
371 for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cp : cpList) {
372 String circuitPackName = cp.getCircuitPackName();
373 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
374 .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
375 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
376 LogicalDatastoreType.OPERATIONAL, cpIID,
377 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
379 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
380 LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
383 // com.google.common.collect.ImmutableList implementation of List
385 List<Ports> portList = new ArrayList<>(circuitPackObject.get().nonnullPorts().values());
386 Collections.sort(portList, new SortPort121ByName());
388 for (Ports port : portList) {
389 String currentKey = circuitPackName + "-" + port.getPortName();
390 if (port.getPortQual() == null) {
394 if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
395 LOG.debug("{} : port {} on {} is not roadm-external - cannot assign logicalConnectionPoint.",
396 nodeId, port.getPortName(), circuitPackName);
400 if (keys.contains(currentKey)) {
401 LOG.debug("{} : port {} on {} has already been handled - cannot assign logicalConnectionPoint.",
402 nodeId, port.getPortName(), circuitPackName);
406 switch (port.getPortDirection()) {
409 String lcp = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
410 LOG.info("{} : port {} on {} - associated Logical Connection Point is {}",
411 nodeId, port.getPortName(), circuitPackName, lcp);
412 portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp));
414 keys.add(currentKey);
419 if (!checkPartnerPortNotNull(port)) {
420 LOG.info("{} : port {} on {} is unidirectional but has no valid partnerPort"
421 + " - cannot assign logicalConnectionPoint.",
422 nodeId, port.getPortName(), circuitPackName);
426 String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
427 LOG.info("{} : port {} on {} - associated Logical Connection Point is {}",
428 nodeId, port.getPortName(), circuitPackName, lcp1);
429 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
430 .child(CircuitPacks.class,
431 new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
432 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
433 Optional<Ports> port2Object = this.deviceTransactionManager
434 .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
435 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
436 if (!port2Object.isPresent()
437 || port2Object.get().getPortQual().getIntValue()
438 != Port.PortQual.RoadmExternal.getIntValue()) {
439 LOG.error("{} : port {} on {} - error getting partner",
440 nodeId, port.getPartnerPort().getPortName().toString(),
441 port.getPartnerPort().getCircuitPackName());
445 Ports port2 = port2Object.get();
446 if (!checkPartnerPort(circuitPackName, port, port2)) {
447 LOG.error("{} : port {} on {} - Error with partner port configuration",
448 nodeId, port.getPortName(), circuitPackName);
452 String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
453 LOG.info("{} : port {} on {} - associated Logical Connection Point is {}",
454 nodeId, port2.getPortName(), circuitPackName, lcp2);
455 portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp1));
457 createMappingObject(nodeId ,port2, port.getPartnerPort().getCircuitPackName(), lcp2));
459 keys.add(currentKey);
460 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
464 LOG.error("{} : port {} on {} - unsupported Direction {}"
465 + " - cannot assign logicalConnectionPoint.",
466 nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
475 private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
476 if (SUFFIX.containsKey(port.getPortDirection())) {
477 return String.join("-", "SRG" + index, "PP" + portIndex, SUFFIX.get(port.getPortDirection()));
479 LOG.error("port {} : Unsupported port direction {}", port, port.getPortDirection());
483 private String createXpdrLogicalConnectionPort(int xponderNb, int lcpNb, String token) {
484 return new StringBuilder("XPDR").append(xponderNb)
486 .append(token).append(lcpNb)
490 private Map<Integer, Degree> getDegreesMap(String deviceId, Info ordmInfo) {
491 Map<Integer, Degree> degrees = new HashMap<>();
493 // Get value for max degree from info subtree, required for iteration
494 // if not present assume to be 20 (temporary)
495 Integer maxDegree = ordmInfo.getMaxDegrees() == null ? 20 : ordmInfo.getMaxDegrees().toJava();
497 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
498 LOG.debug("{} : Getting Connection ports for Degree Number {}", deviceId, degreeCounter);
499 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
500 .child(Degree.class, new DegreeKey(Uint16.valueOf(degreeCounter)));
501 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
502 LogicalDatastoreType.OPERATIONAL, deviceIID,
503 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
504 if (ordmDegreeObject.isPresent()) {
505 degrees.put(degreeCounter, ordmDegreeObject.get());
508 LOG.info("{} : Device has {} degree(s)", deviceId, degrees.size());
512 private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
513 Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
514 getDegreesMap(deviceId, ordmInfo).forEach(
515 (index, degree) -> conPortMap.put(index, new ArrayList<>(degree.nonnullConnectionPorts().values())));
519 private Map<String, String> getEthInterfaceList(String nodeId) {
520 LOG.info("{} : It is calling get ethernet interface", nodeId);
521 InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
522 .child(Protocols.class);
523 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
524 LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
525 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
526 if (!protocolObject.isPresent() || protocolObject.get().augmentation(Protocols1.class).getLldp() == null) {
527 LOG.warn("{} : Couldnt find port config under LLDP - Processiong is done.. now returning..", nodeId);
528 return new HashMap<>();
530 Map<String, String> cpToInterfaceMap = new HashMap<>();
531 Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
532 for (PortConfig portConfig : lldp.nonnullPortConfig().values()) {
533 if (!portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
536 InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
537 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
538 Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
539 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
540 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
541 if (!interfaceObject.isPresent() || (interfaceObject.get().getSupportingCircuitPackName() == null)) {
544 String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
545 cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
546 InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
547 .create(OrgOpenroadmDevice.class)
548 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
549 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
550 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
551 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
552 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getParentCircuitPack() == null)) {
555 cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
556 portConfig.getIfName());
558 LOG.info("{} : Processiong is done.. now returning..", nodeId);
559 return cpToInterfaceMap;
562 private List<CpToDegree> getCpToDegreeList(Map<Integer, Degree> degrees, Map<String, String> interfaceList) {
563 List<CpToDegree> cpToDegreeList = new ArrayList<>();
564 for (Degree degree : degrees.values()) {
565 LOG.debug("Inside CP to degree list");
566 cpToDegreeList.addAll(degree.nonnullCircuitPacks().values().stream()
567 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
568 degree.getDegreeNumber().toString(), interfaceList))
569 .collect(Collectors.toList()));
571 return cpToDegreeList;
574 private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
575 List<CpToDegree> cp2DegreeList) {
576 NodesBuilder nodesBldr = new NodesBuilder().withKey(new NodesKey(nodeId)).setNodeId(nodeId);
577 if (nodeInfo != null) {
578 nodesBldr.setNodeInfo(nodeInfo);
580 if (portMapList != null) {
581 Map<MappingKey, Mapping> mappingMap = new HashMap<>();
582 // No element in the list below should be null at this stage
583 for (Mapping mapping: portMapList) {
584 mappingMap.put(mapping.key(), mapping);
586 nodesBldr.setMapping(mappingMap);
588 if (cp2DegreeList != null) {
589 Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
590 // No element in the list below should be null at this stage
591 for (CpToDegree cp2Degree: cp2DegreeList) {
592 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
594 nodesBldr.setCpToDegree(cpToDegreeMap);
597 Map<NodesKey,Nodes> nodesList = new HashMap<>();
598 Nodes nodes = nodesBldr.build();
599 nodesList.put(nodes.key(),nodes);
601 Network network = new NetworkBuilder().setNodes(nodesList).build();
603 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
604 InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
605 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
606 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
610 } catch (InterruptedException | ExecutionException e) {
611 LOG.warn("Failed to post {}", network, e);
616 private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber,
617 Map<String, String> interfaceList) {
618 return new CpToDegreeBuilder()
619 .withKey(new CpToDegreeKey(circuitPackName))
620 .setCircuitPackName(circuitPackName)
621 .setDegreeNumber(Uint32.valueOf(degreeNumber))
622 .setInterfaceName(interfaceList.get(circuitPackName)).build();
625 private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
626 String logicalConnectionPoint) {
627 MappingBuilder mpBldr = new MappingBuilder()
628 .withKey(new MappingKey(logicalConnectionPoint))
629 .setLogicalConnectionPoint(logicalConnectionPoint)
630 .setSupportingCircuitPackName(circuitPackName)
631 .setSupportingPort(port.getPortName())
632 .setPortDirection(port.getPortDirection().getName());
634 if (!logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) || (port.getInterfaces() == null)) {
635 return mpBldr.build();
638 // Get OMS and OTS interface provisioned on the TTP's
639 for (Interfaces interfaces : port.getInterfaces()) {
641 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
642 interfaces.getInterfaceName());
643 if (!openRoadmInterface.isPresent()) {
644 LOG.warn("{} : Interface {} was null!", nodeId, interfaces.getInterfaceName());
647 LOG.debug("{} : interface get from device is {} and of type {}",
648 nodeId, openRoadmInterface.get().getName(), openRoadmInterface.get().getType());
649 Class<? extends InterfaceType> interfaceType
650 = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
651 // Check if interface type is OMS or OTS
652 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
653 mpBldr.setSupportingOms(interfaces.getInterfaceName());
655 if (interfaceType.equals(OpticalTransport.class)) {
656 mpBldr.setSupportingOts(interfaces.getInterfaceName());
658 } catch (OpenRoadmInterfaceException ex) {
659 LOG.warn("{} : Error while getting interface {} - ",
660 nodeId, interfaces.getInterfaceName(), ex);
663 return mpBldr.build();
666 private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
667 String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
669 if (mapping != null && assoLcp != null) {
670 // update existing mapping
671 return new MappingBuilder(mapping).setConnectionMapLcp(assoLcp).build();
674 // create a new mapping
675 String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
676 MappingBuilder mpBldr = new MappingBuilder()
677 .withKey(new MappingKey(logicalConnectionPoint))
678 .setLogicalConnectionPoint(logicalConnectionPoint)
679 .setSupportingCircuitPackName(circuitPackName)
680 .setSupportingPort(port.getPortName())
681 .setPortDirection(port.getPortDirection().getName())
682 .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
683 if (port.getPortQual() != null) {
684 mpBldr.setPortQual(port.getPortQual().getName());
686 if (partnerLcp != null) {
687 mpBldr.setPartnerLcp(partnerLcp);
690 return mpBldr.build();
693 private Ports getPort2(Ports port, String nodeId, String circuitPackName, StringBuilder circuitPackName2,
694 //circuitPackName2 will be updated by reference contrary to circuitPackName
695 List<CircuitPacks> circuitPackList, Map<String, String> lcpMap) {
696 if (!checkPartnerPortNotNull(port)) {
697 LOG.warn("{} : port {} on {} - Error in the configuration ",
698 nodeId, port.getPortName(), circuitPackName);
701 if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
704 Optional<CircuitPacks> cpOpt = circuitPackList.stream()
705 .filter(cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
707 if (!cpOpt.isPresent()) {
708 LOG.error("{} : Error fetching circuit-pack {}",
709 nodeId, port.getPartnerPort().getCircuitPackName());
712 Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
713 .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName().toString()))
715 if (!poOpt.isPresent()) {
716 LOG.error("{} : Error fetching port {} on {}",
717 nodeId, port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName());
720 Ports port2 = poOpt.get();
721 circuitPackName2.append(cpOpt.get().getCircuitPackName());
722 if (!checkPartnerPort(circuitPackName, port, port2)) {
723 LOG.error("{} : port {} on {} is not a correct partner port of {} on {}",
724 nodeId, port2.getPortName(), circuitPackName2, port.getPortName(), circuitPackName);
730 private void putXpdrLcpsInMaps(int line, String nodeId,
732 String circuitPackName, String circuitPackName2, Ports port, Ports port2,
733 Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
734 String lcp1 = createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
735 String lcp2 = createXpdrLogicalConnectionPort(xponderNb, line + 1, StringConstants.NETWORK_TOKEN);
736 if (lcpMap.containsKey(lcp1) || lcpMap.containsKey(lcp2)) {
737 LOG.warn("{} : mapping already exists for {} or {}", nodeId, lcp1, lcp2);
740 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
741 lcpMap.put(circuitPackName2 + '+' + port2.getPortName(), lcp2);
743 createXpdrMappingObject(nodeId, port, circuitPackName, lcp1, lcp2, null, null));
745 createXpdrMappingObject(nodeId, port2, circuitPackName2, lcp2, lcp1, null, null));
750 private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
751 // Creating mapping data for degree TTP's
752 Map<Integer, Degree> degrees = getDegreesMap(nodeId, deviceInfo);
753 Map<String, String> interfaceList = getEthInterfaceList(nodeId);
754 List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, interfaceList);
755 LOG.info("{} : Map looks like this {}", nodeId, interfaceList);
756 postPortMapping(nodeId, null, null, cpToDegreeList);
758 Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
759 for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
760 switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
762 // port is bidirectional
763 InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
764 .child(CircuitPacks.class,
765 new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName()))
767 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
768 LOG.debug("{} : Fetching connection-port {} at circuit pack {}",
770 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(),
771 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
772 Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
773 LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
774 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
775 if (!portObject.isPresent()) {
776 LOG.error("{} : No port {} on circuit pack {}",
778 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(),
779 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
782 Ports port = portObject.get();
783 if (port.getPortQual() == null) {
786 if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()
787 || Direction.Bidirectional.getIntValue() != port.getPortDirection().getIntValue()) {
788 LOG.error("{} : port {} on {} - Impossible to create logical connection point"
789 + " - Error in configuration with port-qual or port-direction",
790 nodeId, port.getPortName(),
791 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
794 String logicalConnectionPoint = new StringBuilder("DEG")
795 .append(cpMapEntry.getKey())
798 LOG.info("{} : Logical Connection Point for {} on {} is {}",
800 port.getPortName(), connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
801 logicalConnectionPoint);
802 portMapList.add(createMappingObject(nodeId, port,
803 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
804 logicalConnectionPoint));
807 // ports are unidirectionals
808 String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
809 String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
810 InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
811 .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
813 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
814 LOG.debug("{} : Fetching connection-port {} at circuit pack {}",
815 nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cp1Name);
816 Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
817 LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
818 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
819 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
820 .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
822 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString()));
823 LOG.debug("{} : Fetching connection-port {} at circuit pack {}",
824 nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName(), cp2Name);
825 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
826 LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
827 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
828 if (!port1Object.isPresent() || !port2Object.isPresent()) {
829 LOG.error("No port {} on circuit pack {} for node {}",
830 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
831 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
835 Ports port1 = port1Object.get();
836 Ports port2 = port2Object.get();
837 if (port1.getPortQual() == null || port2.getPortQual() == null) {
840 if (Port.PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()
841 || Port.PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()) {
842 LOG.error("{} : Impossible to create logical connection point for port {} or port {}"
843 + " - Error in configuration with port-qual",
844 nodeId, port1.getPortName(), port2.getPortName());
847 if (!checkPartnerPort(cp1Name, port1, port2)) {
848 LOG.error("{} : port {} on {} is not a correct partner port of {} on {}",
849 nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
852 // Directions checks are the same for cp1 and cp2, no need to check them twice.
853 if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
854 LOG.error("{} : port {} on {} is not a correct partner port of {} on {}",
855 nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
859 String logicalConnectionPoint1 = new StringBuilder("DEG")
860 .append(cpMapEntry.getKey())
862 .append(port1.getPortDirection().getName().toUpperCase(Locale.getDefault()))
864 LOG.info("{} : Logical Connection Point for {} {} is {}",
866 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
867 port1.getPortName(), logicalConnectionPoint1);
868 portMapList.add(createMappingObject(nodeId, port1,
869 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
870 logicalConnectionPoint1));
871 String logicalConnectionPoint2 = new StringBuilder("DEG")
872 .append(cpMapEntry.getKey())
874 .append(port2.getPortDirection().getName().toUpperCase(Locale.getDefault()))
876 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
877 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
878 port2.getPortName(), logicalConnectionPoint2);
879 portMapList.add(createMappingObject(nodeId, port2,
880 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
881 logicalConnectionPoint2));
884 LOG.error("{} : Number of connection port for DEG{} is incorrect", nodeId, cpMapEntry.getKey());
891 private NodeInfo createNodeInfo(Info deviceInfo) {
893 if (deviceInfo.getNodeType() == null) {
894 // TODO make mandatory in yang
895 LOG.error("Node type field is missing");
899 NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
900 .setOpenroadmVersion(OpenroadmVersion._121);
901 // TODO check if we can use here .setNodeType(NodeTypes.forValue(..) such as with 221
902 switch (deviceInfo.getNodeType().getIntValue()) {
905 nodeInfoBldr.setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
908 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
909 // TODO: is this protection useful ? it is not present in Portmapping 221
911 if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
912 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
914 nodeInfoBldr.setNodeClli("defaultCLLI");
916 if (deviceInfo.getModel() != null) {
917 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
919 if (deviceInfo.getVendor() != null) {
920 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
922 if (deviceInfo.getIpAddress() != null) {
923 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
926 return nodeInfoBldr.build();