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 int[] counters = fillXpdrLcpsMaps(line, client, nodeId,
226 1, circuitPackName, port,
227 circuitPackList, lcpMap, mappingMap);
229 client = counters[1];
233 Collection<ConnectionMap> connectionMap = deviceObject.get().nonnullConnectionMap().values();
234 for (ConnectionMap cm : connectionMap) {
235 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
236 String slcp = lcpMap.containsKey(skey) ? lcpMap.get(skey) : null;
237 Destination destination0 = cm.nonnullDestination().values().iterator().next();
238 String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
240 LOG.error("{} : Error in connection-map analysis for source {} and destination (CP+port) {}",
244 String dlcp = lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null;
245 Mapping mapping = mappingMap.get(slcp);
246 mappingMap.remove(slcp);
247 portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
250 if (!mappingMap.isEmpty()) {
251 for (Mapping m : mappingMap.values()) {
258 private boolean checkPartnerPortNotNull(Ports port) {
259 if (port.getPartnerPort() == null
260 || port.getPartnerPort().getCircuitPackName() == null
261 || port.getPartnerPort().getPortName() == null) {
267 private boolean checkPartnerPortNoDir(String circuitPackName, Ports port1, Ports port2) {
268 if (!checkPartnerPortNotNull(port2)
269 || !port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
270 || !port2.getPartnerPort().getPortName().equals(port1.getPortName())) {
276 private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
277 if (!checkPartnerPortNoDir(circuitPackName, port1, port2)
278 || ((Direction.Rx.getIntValue() != port1.getPortDirection().getIntValue()
279 || Direction.Tx.getIntValue() != port2.getPortDirection().getIntValue())
281 (Direction.Tx.getIntValue() != port1.getPortDirection().getIntValue()
282 || Direction.Rx.getIntValue() != port2.getPortDirection().getIntValue()))) {
288 private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
289 .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
290 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
291 .CircuitPacks>> cpPerSrg = new HashMap<>();
292 // Get value for max Srg from info subtree, required for iteration
293 // if not present assume to be 20 (temporary)
294 Integer maxSrg = ordmInfo.getMaxSrgs() == null ? 20 : ordmInfo.getMaxSrgs().toJava();
295 for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
296 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
298 LOG.debug("{} : Getting Circuitpacks for Srg Number {}", deviceId, srgCounter);
299 InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
300 .child(SharedRiskGroup.class, new SharedRiskGroupKey(Uint16.valueOf(srgCounter)));
301 Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
302 LogicalDatastoreType.OPERATIONAL, srgIID,
303 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
304 if (ordmSrgObject.isPresent()) {
305 srgCps.addAll(ordmSrgObject.get().nonnullCircuitPacks().values());
306 cpPerSrg.put(ordmSrgObject.get().getSrgNumber().toJava(), srgCps);
309 LOG.info("{} : Device has {} Srg", deviceId, cpPerSrg.size());
313 private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
314 // Creating mapping data for SRG's PP
315 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCps
316 = getSrgCps(nodeId, deviceInfo);
318 for (Entry<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>>
319 srgCpEntry : srgCps.entrySet()) {
320 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> cpList =
321 srgCps.get(srgCpEntry.getKey());
322 List<String> keys = new ArrayList<>();
323 for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cp : cpList) {
324 String circuitPackName = cp.getCircuitPackName();
325 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
326 .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
327 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
328 LogicalDatastoreType.OPERATIONAL, cpIID,
329 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
331 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
332 LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
335 // com.google.common.collect.ImmutableList implementation of List
337 List<Ports> portList = new ArrayList<>(circuitPackObject.get().nonnullPorts().values());
338 Collections.sort(portList, new SortPort121ByName());
340 for (Ports port : portList) {
341 String currentKey = circuitPackName + "-" + port.getPortName();
342 if (port.getPortQual() == null) {
346 if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
347 LOG.debug("{} : port {} on {} is not roadm-external - cannot assign logicalConnectionPoint.",
348 nodeId, port.getPortName(), circuitPackName);
352 if (keys.contains(currentKey)) {
353 LOG.debug("{} : port {} on {} has already been handled - cannot assign logicalConnectionPoint.",
354 nodeId, port.getPortName(), circuitPackName);
358 switch (port.getPortDirection()) {
361 String lcp = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
362 LOG.info("{} : port {} on {} - associated Logical Connection Point is {}",
363 nodeId, port.getPortName(), circuitPackName, lcp);
364 portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp));
366 keys.add(currentKey);
371 if (!checkPartnerPortNotNull(port)) {
372 LOG.info("{} : port {} on {} is unidirectional but has no valid partnerPort"
373 + " - cannot assign logicalConnectionPoint.",
374 nodeId, port.getPortName(), circuitPackName);
378 String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
379 LOG.info("{} : port {} on {} - associated Logical Connection Point is {}",
380 nodeId, port.getPortName(), circuitPackName, lcp1);
381 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
382 .child(CircuitPacks.class,
383 new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
384 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
385 Optional<Ports> port2Object = this.deviceTransactionManager
386 .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
387 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
388 if (!port2Object.isPresent()
389 || port2Object.get().getPortQual().getIntValue()
390 != Port.PortQual.RoadmExternal.getIntValue()) {
391 LOG.error("{} : port {} on {} - error getting partner",
392 nodeId, port.getPartnerPort().getPortName().toString(),
393 port.getPartnerPort().getCircuitPackName());
397 Ports port2 = port2Object.get();
398 if (!checkPartnerPort(circuitPackName, port, port2)) {
399 LOG.error("{} : port {} on {} - Error with partner port configuration",
400 nodeId, port.getPortName(), circuitPackName);
404 String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
405 LOG.info("{} : port {} on {} - associated Logical Connection Point is {}",
406 nodeId, port2.getPortName(), circuitPackName, lcp2);
407 portMapList.add(createMappingObject(nodeId, port, circuitPackName, lcp1));
409 createMappingObject(nodeId ,port2, port.getPartnerPort().getCircuitPackName(), lcp2));
411 keys.add(currentKey);
412 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
416 LOG.error("{} : port {} on {} - unsupported Direction {}"
417 + " - cannot assign logicalConnectionPoint.",
418 nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
427 private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
428 if (SUFFIX.containsKey(port.getPortDirection())) {
429 return String.join("-", "SRG" + index, "PP" + portIndex, SUFFIX.get(port.getPortDirection()));
431 LOG.error("port {} : Unsupported port direction {}", port, port.getPortDirection());
435 private String createXpdrLogicalConnectionPort(int xponderNb, int lcpNb, String token) {
436 return new StringBuilder("XPDR").append(xponderNb)
438 .append(token).append(lcpNb)
442 private Map<Integer, Degree> getDegreesMap(String deviceId, Info ordmInfo) {
443 Map<Integer, Degree> degrees = new HashMap<>();
445 // Get value for max degree from info subtree, required for iteration
446 // if not present assume to be 20 (temporary)
447 Integer maxDegree = ordmInfo.getMaxDegrees() == null ? 20 : ordmInfo.getMaxDegrees().toJava();
449 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
450 LOG.debug("{} : Getting Connection ports for Degree Number {}", deviceId, degreeCounter);
451 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
452 .child(Degree.class, new DegreeKey(Uint16.valueOf(degreeCounter)));
453 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
454 LogicalDatastoreType.OPERATIONAL, deviceIID,
455 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
456 if (ordmDegreeObject.isPresent()) {
457 degrees.put(degreeCounter, ordmDegreeObject.get());
460 LOG.info("{} : Device has {} degree(s)", deviceId, degrees.size());
464 private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
465 Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
466 getDegreesMap(deviceId, ordmInfo).forEach(
467 (index, degree) -> conPortMap.put(index, new ArrayList<>(degree.nonnullConnectionPorts().values())));
471 private Map<String, String> getEthInterfaceList(String nodeId) {
472 LOG.info("{} : It is calling get ethernet interface", nodeId);
473 InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
474 .child(Protocols.class);
475 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
476 LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
477 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
478 if (!protocolObject.isPresent() || protocolObject.get().augmentation(Protocols1.class).getLldp() == null) {
479 LOG.warn("{} : Couldnt find port config under LLDP - Processiong is done.. now returning..", nodeId);
480 return new HashMap<>();
482 Map<String, String> cpToInterfaceMap = new HashMap<>();
483 Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
484 for (PortConfig portConfig : lldp.nonnullPortConfig().values()) {
485 if (!portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
488 InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
489 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
490 Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
491 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
492 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
493 if (!interfaceObject.isPresent() || (interfaceObject.get().getSupportingCircuitPackName() == null)) {
496 String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
497 cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
498 InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
499 .create(OrgOpenroadmDevice.class)
500 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
501 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
502 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
503 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
504 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getParentCircuitPack() == null)) {
507 cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
508 portConfig.getIfName());
510 LOG.info("{} : Processiong is done.. now returning..", nodeId);
511 return cpToInterfaceMap;
514 private List<CpToDegree> getCpToDegreeList(Map<Integer, Degree> degrees, Map<String, String> interfaceList) {
515 List<CpToDegree> cpToDegreeList = new ArrayList<>();
516 for (Degree degree : degrees.values()) {
517 LOG.debug("Inside CP to degree list");
518 cpToDegreeList.addAll(degree.nonnullCircuitPacks().values().stream()
519 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
520 degree.getDegreeNumber().toString(), interfaceList))
521 .collect(Collectors.toList()));
523 return cpToDegreeList;
526 private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
527 List<CpToDegree> cp2DegreeList) {
528 NodesBuilder nodesBldr = new NodesBuilder().withKey(new NodesKey(nodeId)).setNodeId(nodeId);
529 if (nodeInfo != null) {
530 nodesBldr.setNodeInfo(nodeInfo);
532 if (portMapList != null) {
533 Map<MappingKey, Mapping> mappingMap = new HashMap<>();
534 // No element in the list below should be null at this stage
535 for (Mapping mapping: portMapList) {
536 mappingMap.put(mapping.key(), mapping);
538 nodesBldr.setMapping(mappingMap);
540 if (cp2DegreeList != null) {
541 Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
542 // No element in the list below should be null at this stage
543 for (CpToDegree cp2Degree: cp2DegreeList) {
544 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
546 nodesBldr.setCpToDegree(cpToDegreeMap);
549 Map<NodesKey,Nodes> nodesList = new HashMap<>();
550 Nodes nodes = nodesBldr.build();
551 nodesList.put(nodes.key(),nodes);
553 Network network = new NetworkBuilder().setNodes(nodesList).build();
555 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
556 InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
557 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
558 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
562 } catch (InterruptedException | ExecutionException e) {
563 LOG.warn("Failed to post {}", network, e);
568 private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber,
569 Map<String, String> interfaceList) {
570 return new CpToDegreeBuilder()
571 .withKey(new CpToDegreeKey(circuitPackName))
572 .setCircuitPackName(circuitPackName)
573 .setDegreeNumber(Uint32.valueOf(degreeNumber))
574 .setInterfaceName(interfaceList.get(circuitPackName)).build();
577 private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
578 String logicalConnectionPoint) {
579 MappingBuilder mpBldr = new MappingBuilder()
580 .withKey(new MappingKey(logicalConnectionPoint))
581 .setLogicalConnectionPoint(logicalConnectionPoint)
582 .setSupportingCircuitPackName(circuitPackName)
583 .setSupportingPort(port.getPortName())
584 .setPortDirection(port.getPortDirection().getName());
586 if (!logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) || (port.getInterfaces() == null)) {
587 return mpBldr.build();
590 // Get OMS and OTS interface provisioned on the TTP's
591 for (Interfaces interfaces : port.getInterfaces()) {
593 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
594 interfaces.getInterfaceName());
595 if (!openRoadmInterface.isPresent()) {
596 LOG.warn("{} : Interface {} was null!", nodeId, interfaces.getInterfaceName());
599 LOG.debug("{} : interface get from device is {} and of type {}",
600 nodeId, openRoadmInterface.get().getName(), openRoadmInterface.get().getType());
601 Class<? extends InterfaceType> interfaceType
602 = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
603 // Check if interface type is OMS or OTS
604 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
605 mpBldr.setSupportingOms(interfaces.getInterfaceName());
607 if (interfaceType.equals(OpticalTransport.class)) {
608 mpBldr.setSupportingOts(interfaces.getInterfaceName());
610 } catch (OpenRoadmInterfaceException ex) {
611 LOG.warn("{} : Error while getting interface {} - ",
612 nodeId, interfaces.getInterfaceName(), ex);
615 return mpBldr.build();
618 private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
619 String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
621 if (mapping != null && assoLcp != null) {
622 // update existing mapping
623 return new MappingBuilder(mapping).setConnectionMapLcp(assoLcp).build();
626 // create a new mapping
627 String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
628 MappingBuilder mpBldr = new MappingBuilder()
629 .withKey(new MappingKey(logicalConnectionPoint))
630 .setLogicalConnectionPoint(logicalConnectionPoint)
631 .setSupportingCircuitPackName(circuitPackName)
632 .setSupportingPort(port.getPortName())
633 .setPortDirection(port.getPortDirection().getName())
634 .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
635 if (port.getPortQual() != null) {
636 mpBldr.setPortQual(port.getPortQual().getName());
638 if (partnerLcp != null) {
639 mpBldr.setPartnerLcp(partnerLcp);
642 return mpBldr.build();
645 private Ports getPort2(Ports port, String nodeId, String circuitPackName, StringBuilder circuitPackName2,
646 //circuitPackName2 will be updated by reference contrary to circuitPackName
647 List<CircuitPacks> circuitPackList, Map<String, String> lcpMap) {
648 if (!checkPartnerPortNotNull(port)) {
649 LOG.warn("{} : port {} on {} - Error in the configuration ",
650 nodeId, port.getPortName(), circuitPackName);
653 if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
656 Optional<CircuitPacks> cpOpt = circuitPackList.stream()
657 .filter(cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
659 if (!cpOpt.isPresent()) {
660 LOG.error("{} : Error fetching circuit-pack {}",
661 nodeId, port.getPartnerPort().getCircuitPackName());
664 Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
665 .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName().toString()))
667 if (!poOpt.isPresent()) {
668 LOG.error("{} : Error fetching port {} on {}",
669 nodeId, port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName());
672 Ports port2 = poOpt.get();
673 circuitPackName2.append(cpOpt.get().getCircuitPackName());
674 if (!checkPartnerPort(circuitPackName, port, port2)) {
675 LOG.error("{} : port {} on {} is not a correct partner port of {} on {}",
676 nodeId, port2.getPortName(), circuitPackName2, port.getPortName(), circuitPackName);
682 private void putXpdrLcpsInMaps(int line, String nodeId,
684 String circuitPackName, String circuitPackName2, Ports port, Ports port2,
685 Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
686 String lcp1 = createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
687 String lcp2 = createXpdrLogicalConnectionPort(xponderNb, line + 1, StringConstants.NETWORK_TOKEN);
688 if (lcpMap.containsKey(lcp1) || lcpMap.containsKey(lcp2)) {
689 LOG.warn("{} : mapping already exists for {} or {}", nodeId, lcp1, lcp2);
692 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
693 lcpMap.put(circuitPackName2 + '+' + port2.getPortName(), lcp2);
695 createXpdrMappingObject(nodeId, port, circuitPackName, lcp1, lcp2, null, null));
697 createXpdrMappingObject(nodeId, port2, circuitPackName2, lcp2, lcp1, null, null));
701 private int[] fillXpdrLcpsMaps(int line, int client, String nodeId,
703 String circuitPackName, Ports port,
704 List<CircuitPacks> circuitPackList, Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
706 if (port.getPortQual() == null) {
707 LOG.warn("{} : port {} on {} - PortQual was not found",
708 nodeId, port.getPortName(), circuitPackName);
709 return new int[] {line, client};
712 switch (port.getPortQual()) {
715 String lcp0 = createXpdrLogicalConnectionPort(xponderNb, client, StringConstants.CLIENT_TOKEN);
716 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp0);
718 createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null));
723 line = fillXpdrNetworkLcpsMaps(line, nodeId,
725 circuitPackName, port,
726 circuitPackList, lcpMap, mappingMap);
730 LOG.error("{} : port {} on {} - unsupported PortQual {}",
731 nodeId, port.getPortName(), circuitPackName, port.getPortQual());
733 return new int[] {line, client};
736 private int fillXpdrNetworkLcpsMaps(int line, String nodeId,
738 String circuitPackName, Ports port,
739 List<CircuitPacks> circuitPackList, Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
741 switch (port.getPortDirection()) {
744 String lcp = createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
745 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
747 createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
753 StringBuilder circuitPackName2 = new StringBuilder();
754 Ports port2 = getPort2(port, nodeId, circuitPackName, circuitPackName2,
755 circuitPackList, lcpMap);
758 //key already present or an error occured and was logged
762 putXpdrLcpsInMaps(line, nodeId, xponderNb,
763 circuitPackName, circuitPackName2.toString(), port, port2,
770 LOG.error("{} : port {} on {} - unsupported Direction {}",
771 nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
777 private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
778 // Creating mapping data for degree TTP's
779 Map<Integer, Degree> degrees = getDegreesMap(nodeId, deviceInfo);
780 Map<String, String> interfaceList = getEthInterfaceList(nodeId);
781 List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, interfaceList);
782 LOG.info("{} : Map looks like this {}", nodeId, interfaceList);
783 postPortMapping(nodeId, null, null, cpToDegreeList);
785 Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
786 for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
787 switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
789 // port is bidirectional
790 InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
791 .child(CircuitPacks.class,
792 new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName()))
794 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
795 LOG.debug("{} : Fetching connection-port {} at circuit pack {}",
797 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(),
798 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
799 Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
800 LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
801 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
802 if (!portObject.isPresent()) {
803 LOG.error("{} : No port {} on circuit pack {}",
805 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(),
806 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
809 Ports port = portObject.get();
810 if (port.getPortQual() == null) {
813 if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()
814 || Direction.Bidirectional.getIntValue() != port.getPortDirection().getIntValue()) {
815 LOG.error("{} : port {} on {} - Impossible to create logical connection point"
816 + " - Error in configuration with port-qual or port-direction",
817 nodeId, port.getPortName(),
818 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName());
821 String logicalConnectionPoint = new StringBuilder("DEG")
822 .append(cpMapEntry.getKey())
825 LOG.info("{} : Logical Connection Point for {} on {} is {}",
827 port.getPortName(), connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
828 logicalConnectionPoint);
829 portMapList.add(createMappingObject(nodeId, port,
830 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
831 logicalConnectionPoint));
834 // ports are unidirectionals
835 String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
836 String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
837 InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
838 .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
840 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString()));
841 LOG.debug("{} : Fetching connection-port {} at circuit pack {}",
842 nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cp1Name);
843 Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
844 LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
845 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
846 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
847 .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
849 new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString()));
850 LOG.debug("{} : Fetching connection-port {} at circuit pack {}",
851 nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName(), cp2Name);
852 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
853 LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
854 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
855 if (!port1Object.isPresent() || !port2Object.isPresent()) {
856 LOG.error("No port {} on circuit pack {} for node {}",
857 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
858 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
862 Ports port1 = port1Object.get();
863 Ports port2 = port2Object.get();
864 if (port1.getPortQual() == null || port2.getPortQual() == null) {
867 if (Port.PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()
868 || Port.PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()) {
869 LOG.error("{} : Impossible to create logical connection point for port {} or port {}"
870 + " - Error in configuration with port-qual",
871 nodeId, port1.getPortName(), port2.getPortName());
874 if (!checkPartnerPort(cp1Name, port1, port2)) {
875 LOG.error("{} : port {} on {} is not a correct partner port of {} on {}",
876 nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
879 // Directions checks are the same for cp1 and cp2, no need to check them twice.
880 if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
881 LOG.error("{} : port {} on {} is not a correct partner port of {} on {}",
882 nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
886 String logicalConnectionPoint1 = new StringBuilder("DEG")
887 .append(cpMapEntry.getKey())
889 .append(port1.getPortDirection().getName().toUpperCase(Locale.getDefault()))
891 LOG.info("{} : Logical Connection Point for {} {} is {}",
893 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
894 port1.getPortName(), logicalConnectionPoint1);
895 portMapList.add(createMappingObject(nodeId, port1,
896 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
897 logicalConnectionPoint1));
898 String logicalConnectionPoint2 = new StringBuilder("DEG")
899 .append(cpMapEntry.getKey())
901 .append(port2.getPortDirection().getName().toUpperCase(Locale.getDefault()))
903 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
904 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
905 port2.getPortName(), logicalConnectionPoint2);
906 portMapList.add(createMappingObject(nodeId, port2,
907 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
908 logicalConnectionPoint2));
911 LOG.error("{} : Number of connection port for DEG{} is incorrect", nodeId, cpMapEntry.getKey());
918 private NodeInfo createNodeInfo(Info deviceInfo) {
920 if (deviceInfo.getNodeType() == null) {
921 // TODO make mandatory in yang
922 LOG.error("Node type field is missing");
926 NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
927 .setOpenroadmVersion(OpenroadmVersion._121);
928 // TODO check if we can use here .setNodeType(NodeTypes.forValue(..) such as with 221
929 switch (deviceInfo.getNodeType().getIntValue()) {
932 nodeInfoBldr.setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
935 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
936 // TODO: is this protection useful ? it is not present in Portmapping 221
938 if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
939 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
941 nodeInfoBldr.setNodeClli("defaultCLLI");
943 if (deviceInfo.getModel() != null) {
944 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
946 if (deviceInfo.getVendor() != null) {
947 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
949 if (deviceInfo.getIpAddress() != null) {
950 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
953 return nodeInfoBldr.build();