2 * Copyright © 2017 Orange, Inc. 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.Collections;
14 import java.util.Comparator;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.WriteTransaction;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.transportpce.common.StringConstants;
27 import org.opendaylight.transportpce.common.Timeouts;
28 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
29 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
30 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.Network;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.NetworkBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.Nodes;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.NodesBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.NodesKey;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.CpToDegree;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.CpToDegreeBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.CpToDegreeKey;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.Mapping;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.MappingBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.MappingKey;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.NodeInfo;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.NodeInfo.OpenroadmVersion;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev190702.network.nodes.NodeInfoBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.Direction;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.Port;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.CircuitPack;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsKey;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.degree.ConnectionPorts;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.ConnectionMap;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Degree;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.DegreeKey;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Info;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.Protocols;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.SharedRiskGroup;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.SharedRiskGroupKey;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.InterfaceType;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpenROADMOpticalMultiplex;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpticalTransport;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.Lldp;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.PortConfig;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
74 public class PortMappingVersion221 {
75 private static final Logger LOG = LoggerFactory.getLogger(PortMappingImpl.class);
77 private final DataBroker dataBroker;
78 private final DeviceTransactionManager deviceTransactionManager;
79 private final OpenRoadmInterfaces openRoadmInterfaces;
81 public PortMappingVersion221(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
82 OpenRoadmInterfaces openRoadmInterfaces) {
83 this.dataBroker = dataBroker;
84 this.deviceTransactionManager = deviceTransactionManager;
85 this.openRoadmInterfaces = openRoadmInterfaces;
88 public boolean createMappingData(String nodeId) {
89 LOG.info("Create Mapping Data for node 2.2.1 {}", nodeId);
90 List<Mapping> portMapList = new ArrayList<>();
91 InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
92 Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType
93 .OPERATIONAL, infoIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
96 if (deviceInfoOptional.isPresent()) {
97 deviceInfo = deviceInfoOptional.get();
98 nodeInfo = createNodeInfo(deviceInfo);
99 if (nodeInfo == null) {
102 postPortMapping(nodeId, nodeInfo, null, null);
105 LOG.warn("Device info subtree is absent for {}", nodeId);
109 switch (deviceInfo.getNodeType()) {
112 // Get TTP port mapping
113 if (!createTtpPortMapping(nodeId, deviceInfo, portMapList)) {
114 // return false if mapping creation for TTP's failed
115 LOG.warn("Unable to create mapping for TTP's on node {}", nodeId);
119 // Get PP port mapping
120 if (!createPpPortMapping(nodeId, deviceInfo, portMapList)) {
121 // return false if mapping creation for PP's failed
122 LOG.warn("Unable to create mapping for PP's on node {}", nodeId);
127 if (!createXpdrPortMapping(nodeId, portMapList)) {
128 LOG.warn("Unable to create mapping for Xponder on node {}", nodeId);
133 LOG.error("Unable to create mapping for node {} : unknown nodetype ", nodeId);
137 return postPortMapping(nodeId, nodeInfo, portMapList, null);
140 public boolean updateMapping(String nodeId, Mapping oldMapping) {
141 LOG.info("Updating Mapping Data {} for node {}", oldMapping, nodeId);
142 InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
143 .child(CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName()))
144 .child(Ports.class, new PortsKey(oldMapping.getSupportingPort()));
145 if ((oldMapping != null) && (nodeId != null)) {
147 Optional<Ports> portObject = deviceTransactionManager.getDataFromDevice(nodeId,
148 LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
149 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
150 if (portObject.isPresent()) {
151 Ports port = portObject.get();
152 Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
153 oldMapping.getLogicalConnectionPoint());
155 final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
156 InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class)
157 .child(Nodes.class, new NodesKey(nodeId))
158 .child(Mapping.class, new MappingKey(oldMapping.getLogicalConnectionPoint()));
159 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
160 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
165 } catch (InterruptedException | ExecutionException e) {
166 LOG.error("Error updating Mapping {} for node {}", oldMapping.getLogicalConnectionPoint(), nodeId, e);
170 LOG.error("Impossible to update mapping");
175 private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
176 // Creating for Xponder Line and Client Ports
177 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
178 Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
179 LogicalDatastoreType.OPERATIONAL, deviceIID,
180 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
182 // Variable to keep track of number of line ports
184 // Variable to keep track of number of client ports
186 if (!deviceObject.isPresent() || deviceObject.get().getCircuitPacks() == null) {
187 LOG.warn("Circuit Packs are not present for {}", nodeId);
189 // TODO return false or continue?
191 Map<String, String> lcpMap = new HashMap<>();
192 Map<String, Mapping> mappingMap = new HashMap<>();
194 List<CircuitPacks> circuitPackList = deviceObject.get().getCircuitPacks();
195 circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
197 for (CircuitPacks cp : circuitPackList) {
198 String circuitPackName = cp.getCircuitPackName();
199 if (cp.getPorts() == null) {
200 LOG.warn("Ports were not found for circuit pack: {}", circuitPackName);
203 List<Ports> portList = cp.getPorts();
204 portList.sort(Comparator.comparing(Ports::getPortName));
205 for (Ports port : portList) {
206 if (port.getPortQual() == null) {
207 LOG.warn("PortQual was not found for port {} on circuit pack: {}", port.getPortName(),
211 if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
212 && port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
213 String lcp = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
214 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
215 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
217 } else if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
218 && port.getPortDirection().getIntValue() != Direction.Bidirectional.getIntValue()
219 && port.getPartnerPort() != null
220 && port.getPartnerPort().getCircuitPackName() != null
221 && port.getPartnerPort().getPortName() != null) {
222 if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
225 String lcp1 = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
226 Optional<CircuitPacks> cpOpt = circuitPackList.stream().filter(cP -> cP.getCircuitPackName()
227 .equals(port.getPartnerPort().getCircuitPackName())).findFirst();
228 if (cpOpt.isPresent()) {
229 Optional<Ports> poOpt = cpOpt.get().getPorts().stream().filter(p -> p.getPortName().equals(port
230 .getPartnerPort().getPortName().toString())).findFirst();
231 if (poOpt.isPresent()) {
232 Ports port2 = poOpt.get();
233 if ((Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
234 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
235 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
236 && port2.getPartnerPort().getPortName() != null
237 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
238 && port2.getPartnerPort().getPortName().equals(port.getPortName()))
240 (Direction.Tx.getIntValue() == port.getPortDirection().getIntValue()
241 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
242 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
243 && port2.getPartnerPort().getPortName() != null
244 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
245 && port2.getPartnerPort().getPortName().equals(port.getPortName()))) {
246 String lcp2 = new StringBuilder("XPDR1-").append(StringConstants.NETWORK_TOKEN)
247 .append(line + 1).toString();
248 if (!lcpMap.containsKey(lcp1) && !lcpMap.containsKey(lcp2)) {
249 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
250 lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
251 mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName, lcp1,
253 mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2, cpOpt.get()
254 .getCircuitPackName(), lcp2, lcp1, null, null));
256 LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
260 LOG.error("port {} on {} is not a correct partner port of {} on {}",
261 port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
265 LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
266 port.getPartnerPort().getCircuitPackName(), nodeId);
269 LOG.error("Error fetching circuit-pack {} for {}", port.getPartnerPort().getCircuitPackName(),
272 } else if (Port.PortQual.XpdrClient.getIntValue() == port.getPortQual().getIntValue()) {
273 String lcp = "XPDR1-" + StringConstants.CLIENT_TOKEN + client;
274 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
275 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
278 LOG.warn("Error in the configuration of port {} of {} for {}", port.getPortName(), circuitPackName,
283 List<ConnectionMap> connectionMap = deviceObject.get().getConnectionMap();
286 for (ConnectionMap cm : connectionMap) {
287 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
288 if (lcpMap.containsKey(skey)) {
289 slcp = lcpMap.get(skey);
291 String dkey = cm.getDestination().get(0).getCircuitPackName() + "+"
292 + cm.getDestination().get(0).getPortName();
293 if (lcpMap.containsKey(dkey)) {
294 dlcp = lcpMap.get(dkey);
297 Mapping mapping = mappingMap.get(slcp);
298 mappingMap.remove(slcp);
299 portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
301 LOG.error("Error in connection-map analysis");
304 if (!mappingMap.isEmpty()) {
305 for (Mapping m : mappingMap.values()) {
312 private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg
313 .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
314 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg
315 .CircuitPacks>> cpPerSrg = new HashMap<>();
317 // Get value for max Srg from info subtree, required for iteration
318 // if not present assume to be 20 (temporary)
319 if (ordmInfo.getMaxSrgs() != null) {
320 maxSrg = ordmInfo.getMaxSrgs();
324 for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
325 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks> srgCps
327 LOG.info("Getting Circuitpacks for Srg Number {}", srgCounter);
328 InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
329 .child(SharedRiskGroup.class, new SharedRiskGroupKey(srgCounter));
330 Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
331 LogicalDatastoreType.OPERATIONAL, srgIID,
332 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
333 if (ordmSrgObject.isPresent()) {
334 srgCps.addAll(ordmSrgObject.get().getCircuitPacks());
335 cpPerSrg.put(ordmSrgObject.get().getSrgNumber(), srgCps);
338 LOG.info("Device {} has {} Srg", deviceId, cpPerSrg.size());
342 //last LOG info message in this method is too long
343 @SuppressWarnings("checkstyle:linelength")
344 private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
345 // Creating mapping data for SRG's PP
346 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks>> srgCps
347 = getSrgCps(nodeId, deviceInfo);
349 for (Integer k : srgCps.keySet()) {
350 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks> cpList =
352 List<String> keys = new ArrayList<>();
353 for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.srg.CircuitPacks cp : cpList) {
354 String circuitPackName = cp.getCircuitPackName();
355 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
356 .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
357 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
358 LogicalDatastoreType.OPERATIONAL, cpIID,
359 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
361 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
362 LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
365 List<Ports> portList = circuitPackObject.get().getPorts();
366 Collections.sort(portList, new SortPort221ByName());
368 for (Ports port : portList) {
369 String currentKey = circuitPackName + "-" + port.getPortName();
370 if (port.getPortQual() == null) {
372 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
373 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()
374 && !keys.contains(currentKey)) {
375 String logicalConnectionPoint = createLogicalConnectionPort(port, k, portIndex);
376 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
377 port.getPortName(), logicalConnectionPoint);
378 portMapList.add(createMappingObject(nodeId, port, circuitPackName, logicalConnectionPoint));
380 keys.add(currentKey);
381 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
382 && (Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
383 || Direction.Tx.getIntValue() == port.getPortDirection().getIntValue())
384 && !keys.contains(currentKey)
385 && port.getPartnerPort() != null) {
386 String logicalConnectionPoint1 = createLogicalConnectionPort(port, k, portIndex);
387 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
388 port.getPortName(), logicalConnectionPoint1);
389 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
390 .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
391 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
392 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
393 LogicalDatastoreType.OPERATIONAL, port2ID,
394 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
395 if (port2Object.isPresent()
396 && port2Object.get().getPortQual().getIntValue()
397 == Port.PortQual.RoadmExternal.getIntValue()) {
398 Ports port2 = port2Object.get();
399 if ((port.getPortDirection().getIntValue() == Direction.Rx.getIntValue()
400 && port2.getPortDirection().getIntValue() == Direction.Tx.getIntValue()
401 && port2.getPartnerPort() != null
402 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
403 && port2.getPartnerPort().getPortName().toString().equals(port.getPortName()))
405 (port.getPortDirection().getIntValue() == Direction.Tx.getIntValue()
406 && port2.getPortDirection().getIntValue() == Direction.Rx.getIntValue()
407 && port2.getPartnerPort() != null
408 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
409 && port2.getPartnerPort().getPortName().toString().equals(port.getPortName()))) {
410 String logicalConnectionPoint2 = createLogicalConnectionPort(port2, k, portIndex);
411 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
412 port2.getPortName(), logicalConnectionPoint2);
413 portMapList.add(createMappingObject(nodeId, port, circuitPackName,
414 logicalConnectionPoint1));
415 portMapList.add(createMappingObject(nodeId,port2,
416 port.getPartnerPort().getCircuitPackName(), logicalConnectionPoint2));
418 keys.add(currentKey);
419 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
421 LOG.error("Error with partner port configuration for port {} of {} - {}",
422 port.getPortName(), circuitPackName, nodeId);
426 LOG.error("error getting partner port {} of {} - {}", port.getPartnerPort().getPortName()
427 .toString(), port.getPartnerPort().getCircuitPackName(), nodeId);
431 LOG.info("{} : port {} on {} is not roadm-external or has already been handled. No logicalConnectionPoint assignment for this port.",
432 nodeId, port.getPortName(), circuitPackName);
440 private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
442 switch (port.getPortDirection()) {
444 lcp = "SRG" + index + "-PP" + portIndex + "-TX";
447 lcp = "SRG" + index + "-PP" + portIndex + "-RX";
450 lcp = "SRG" + index + "-PP" + portIndex + "-TXRX";
453 LOG.error("Unsupported port direction for port {} : {}", port, port.getPortDirection());
458 private List<Degree> getDegrees(String deviceId, Info ordmInfo) {
459 List<Degree> degrees = new ArrayList<>();
462 // Get value for max degree from info subtree, required for iteration
463 // if not present assume to be 20 (temporary)
464 if (ordmInfo.getMaxDegrees() != null) {
465 maxDegree = ordmInfo.getMaxDegrees();
470 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
471 LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
472 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
473 .child(Degree.class, new DegreeKey(degreeCounter));
474 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
475 LogicalDatastoreType.OPERATIONAL, deviceIID,
476 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
477 if (ordmDegreeObject.isPresent()) {
478 degrees.add(ordmDegreeObject.get());
481 LOG.info("Device {} has {} degree", degrees.size());
485 private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
486 Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
489 if (ordmInfo.getMaxDegrees() != null) {
490 maxDegree = ordmInfo.getMaxDegrees();
494 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
495 LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
496 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
497 .child(Degree.class, new DegreeKey(degreeCounter));
498 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
499 LogicalDatastoreType.OPERATIONAL, deviceIID,
500 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
501 if (ordmDegreeObject.isPresent()) {
502 conPortMap.put(degreeCounter, ordmDegreeObject.get().getConnectionPorts());
505 LOG.info("Device {} has {} degree", deviceId, conPortMap.size());
509 private Map<String, String> getEthInterfaceList(String nodeId) {
510 LOG.info("It is calling get ethernet interface");
511 Map<String, String> cpToInterfaceMap = new HashMap<>();
512 InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
513 .child(Protocols.class);
514 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
515 LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
516 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
517 if (protocolObject.isPresent() && protocolObject.get().augmentation(Protocols1.class).getLldp() != null) {
518 Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
519 for (PortConfig portConfig : lldp.getPortConfig()) {
520 if (portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
521 InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
522 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
523 Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
524 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
525 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
526 if (interfaceObject.isPresent() && (interfaceObject.get().getSupportingCircuitPackName() != null)) {
527 String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
528 cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
529 InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
530 .create(OrgOpenroadmDevice.class)
531 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
532 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
533 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
534 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
535 if (circuitPackObject.isPresent() && (circuitPackObject.get().getParentCircuitPack() != null)) {
536 cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
537 portConfig.getIfName());
543 LOG.warn("Couldnt find port config under LLDP for Node : {}", nodeId);
545 LOG.info("Processiong is done.. now returning..");
546 return cpToInterfaceMap;
549 private List<CpToDegree> getCpToDegreeList(List<Degree> degrees, String nodeId,
550 Map<String, String> interfaceList) {
551 List<CpToDegree> cpToDegreeList = new ArrayList<>();
552 for (Degree degree : degrees) {
553 if (degree.getCircuitPacks() != null) {
554 LOG.info("Inside CP to degree list");
555 cpToDegreeList.addAll(degree.getCircuitPacks().stream()
556 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
557 degree.getDegreeNumber().toString(), nodeId, interfaceList))
558 .collect(Collectors.toList()));
561 return cpToDegreeList;
564 private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
565 List<CpToDegree> cp2DegreeList) {
566 NodesBuilder nodesBldr = new NodesBuilder();
567 nodesBldr.withKey(new NodesKey(nodeId)).setNodeId(nodeId);
568 if (nodeInfo != null) {
569 nodesBldr.setNodeInfo(nodeInfo);
571 if (portMapList != null) {
572 nodesBldr.setMapping(portMapList);
574 if (cp2DegreeList != null) {
575 nodesBldr.setCpToDegree(cp2DegreeList);
578 List<Nodes> nodesList = new ArrayList<>();
579 nodesList.add(nodesBldr.build());
581 NetworkBuilder nwBldr = new NetworkBuilder();
582 nwBldr.setNodes(nodesList);
584 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
585 InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
586 Network network = nwBldr.build();
587 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
588 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
593 } catch (InterruptedException | ExecutionException e) {
594 LOG.warn("Failed to post {}", network, e);
599 private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber, String nodeId,
600 Map<String, String> interfaceList) {
601 String interfaceName = null;
602 if (interfaceList.get(circuitPackName) != null) {
603 interfaceName = interfaceList.get(circuitPackName);
605 return new CpToDegreeBuilder().withKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
606 .setDegreeNumber(new Long(degreeNumber)).setInterfaceName(interfaceName).build();
609 private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
610 String logicalConnectionPoint) {
611 MappingBuilder mpBldr = new MappingBuilder();
612 mpBldr.withKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
613 .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName())
614 .setPortDirection(port.getPortDirection().getName());
616 // Get OMS and OTS interface provisioned on the TTP's
617 if (logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) && (port.getInterfaces() != null)) {
618 for (Interfaces interfaces : port.getInterfaces()) {
620 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
621 interfaces.getInterfaceName());
622 if (openRoadmInterface.isPresent()) {
623 Class<? extends InterfaceType> interfaceType
624 = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
625 // Check if interface type is OMS or OTS
626 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
627 mpBldr.setSupportingOms(interfaces.getInterfaceName());
629 if (interfaceType.equals(OpticalTransport.class)) {
630 mpBldr.setSupportingOts(interfaces.getInterfaceName());
633 LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
635 } catch (OpenRoadmInterfaceException ex) {
636 LOG.warn("Error while getting interface {} from node {}!", interfaces.getInterfaceName(), nodeId,
641 return mpBldr.build();
644 private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
645 String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
646 MappingBuilder mpBldr;
647 if (mapping != null && assoLcp != null) {
648 // update existing mapping
649 mpBldr = new MappingBuilder(mapping);
650 mpBldr.setAssociatedLcp(assoLcp);
652 // create a new mapping
653 mpBldr = new MappingBuilder();
654 mpBldr.withKey(new MappingKey(logicalConnectionPoint))
655 .setLogicalConnectionPoint(logicalConnectionPoint)
656 .setSupportingCircuitPackName(circuitPackName)
657 .setSupportingPort(port.getPortName())
658 .setPortDirection(port.getPortDirection().getName());
659 if (port.getPortQual() != null) {
660 mpBldr.setPortQual(port.getPortQual().getName());
662 if (partnerLcp != null) {
663 mpBldr.setPartnerLcp(partnerLcp);
666 return mpBldr.build();
669 //some LOG messages are too long
670 @SuppressWarnings("checkstyle:linelength")
671 private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
672 // Creating mapping data for degree TTP's
673 List<Degree> degrees = getDegrees(nodeId, deviceInfo);
674 Map<String, String> interfaceList = getEthInterfaceList(nodeId);
675 List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, nodeId, interfaceList);
676 LOG.info("Map looks like this {}", interfaceList);
677 postPortMapping(nodeId, null, null, cpToDegreeList);
679 Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
680 for (Integer k : connectionPortMap.keySet()) {
681 switch (connectionPortMap.get(k).size()) {
683 // port is bidirectional
684 InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
685 .child(CircuitPacks.class, new CircuitPacksKey(connectionPortMap.get(k).get(0)
686 .getCircuitPackName()))
687 .child(Ports.class, new PortsKey(connectionPortMap.get(k).get(0)
688 .getPortName().toString()));
689 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap.get(k).get(0)
690 .getPortName().toString(), connectionPortMap.get(k).get(0).getCircuitPackName());
691 Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
692 LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
693 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
694 if (portObject.isPresent()) {
695 Ports port = portObject.get();
696 if (port.getPortQual() == null) {
698 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
699 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()) {
700 String logicalConnectionPoint = new StringBuilder("DEG").append(k).append("-TTP-TXRX")
702 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
703 connectionPortMap.get(k).get(0).getCircuitPackName(), port.getPortName(),
704 logicalConnectionPoint);
705 portMapList.add(createMappingObject(nodeId, port,
706 connectionPortMap.get(k).get(0).getCircuitPackName(), logicalConnectionPoint));
709 "Impossible to create logical connection point for port {} of {} on node {} - Error in configuration with port-qual or port-direction",
710 port.getPortName(), connectionPortMap.get(k).get(0).getCircuitPackName(), nodeId);
713 LOG.error("No port {} on circuit pack {} for node {}",
714 connectionPortMap.get(k).get(0).getPortName().toString(),
715 connectionPortMap.get(k).get(0).getCircuitPackName(), nodeId);
720 // ports are unidirectionals
721 String cp1Name = connectionPortMap.get(k).get(0).getCircuitPackName();
722 String cp2Name = connectionPortMap.get(k).get(1).getCircuitPackName();
723 InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
724 .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
725 .child(Ports.class, new PortsKey(connectionPortMap.get(k).get(0).getPortName().toString()));
726 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap.get(k).get(0)
727 .getPortName().toString(), cp1Name);
728 Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
729 LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
730 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
731 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
732 .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
733 .child(Ports.class, new PortsKey(connectionPortMap.get(k).get(1).getPortName().toString()));
734 LOG.info("Fetching connection-port {} at circuit pack {}",
735 connectionPortMap.get(k).get(1).getPortName().toString(), cp2Name);
736 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
737 LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
738 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
739 if (port1Object.isPresent() && port2Object.isPresent()) {
740 Ports port1 = port1Object.get();
741 Ports port2 = port2Object.get();
742 if (port1.getPortQual() == null || port2.getPortQual() == null) {
744 } else if ((Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
745 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
746 && Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
747 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
748 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
749 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
750 && port1.getPartnerPort().getPortName().equals(port2.getPortName().toString())
751 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
752 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))
754 (Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
755 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
756 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
757 && Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
758 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
759 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
760 && port1.getPartnerPort().getPortName().equals(port2.getPortName().toString())
761 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
762 && port2.getPartnerPort().getPortName().equals(port1.getPortName().toString()))) {
763 String logicalConnectionPoint1 = new StringBuilder("DEG").append(k).append("-TTP-")
764 .append(port1.getPortDirection().getName().toUpperCase()).toString();
765 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
766 connectionPortMap.get(k).get(0).getCircuitPackName(), port1.getPortName(),
767 logicalConnectionPoint1);
768 portMapList.add(createMappingObject(nodeId, port1, connectionPortMap.get(k).get(0)
769 .getCircuitPackName(), logicalConnectionPoint1));
770 String logicalConnectionPoint2 = new StringBuilder("DEG").append(k).append("-TTP-")
771 .append(port2.getPortDirection().getName().toUpperCase()).toString();
772 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
773 connectionPortMap.get(k).get(1).getCircuitPackName(), port2.getPortName(),
774 logicalConnectionPoint2);
775 portMapList.add(createMappingObject(nodeId, port2, connectionPortMap.get(k).get(1)
776 .getCircuitPackName(), logicalConnectionPoint2));
779 "impossible to create logical connection point for port {} or port {} on node {} - Error in configuration with port-qual, port-direction or partner-port configuration",
780 port1.getPortName(), port2.getPortName(), nodeId);
783 LOG.error("No port {} on circuit pack {} for node {}",
784 connectionPortMap.get(k).get(0).getPortName().toString(),
785 connectionPortMap.get(k).get(0).getCircuitPackName(), nodeId);
791 LOG.error("Number of connection port for DEG{} on {} is incorrect", k, nodeId);
798 private NodeInfo createNodeInfo(Info deviceInfo) {
799 NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder();
800 if (deviceInfo.getNodeType() != null) {
801 nodeInfoBldr = new NodeInfoBuilder()
802 .setOpenroadmVersion(OpenroadmVersion._221)
803 .setNodeType(deviceInfo.getNodeType());
804 if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
805 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
807 nodeInfoBldr.setNodeClli("defaultCLLI");
809 if (deviceInfo.getModel() != null) {
810 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
812 if (deviceInfo.getVendor() != null) {
813 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
815 if (deviceInfo.getIpAddress() != null) {
816 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
819 // TODO make mandatory in yang
820 LOG.error("Node type field is missing");
823 return nodeInfoBldr.build();