2 * Copyright © 2017 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.transportpce.common.mapping;
11 import com.google.common.util.concurrent.FluentFuture;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.math.BigInteger;
14 import java.nio.charset.StandardCharsets;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
24 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
28 import org.opendaylight.mdsal.binding.api.WriteTransaction;
29 import org.opendaylight.mdsal.common.api.CommitInfo;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.transportpce.common.StringConstants;
32 import org.opendaylight.transportpce.common.Timeouts;
33 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
34 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
35 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.Network;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.NetworkBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.Nodes;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.NodesBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.NodesKey;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.CpToDegree;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.CpToDegreeBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.CpToDegreeKey;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.Mapping;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.MappingBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.MappingKey;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.NodeInfo;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.NodeInfo.OpenroadmVersion;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.NodeInfoBuilder;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.Direction;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.NodeTypes;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.CircuitPack;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.Port;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPorts;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.ConnectionMap;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Degree;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroup;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroupKey;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.port.Interfaces;
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.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
80 public class PortMappingVersion121 {
82 private static final Logger LOG = LoggerFactory.getLogger(PortMappingVersion121.class);
84 private final DataBroker dataBroker;
85 private final DeviceTransactionManager deviceTransactionManager;
86 private final OpenRoadmInterfaces openRoadmInterfaces;
88 //FNV1 128 bit hash constants
89 private static final BigInteger FNV_PRIME = new BigInteger("309485009821345068724781371");
90 private static final BigInteger FNV_INIT = new BigInteger("6c62272e07bb014262b821756295c58d", 16);
91 private static final BigInteger FNV_MOD = new BigInteger("2").pow(128);
93 public PortMappingVersion121(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
94 OpenRoadmInterfaces openRoadmInterfaces) {
95 this.dataBroker = dataBroker;
96 this.deviceTransactionManager = deviceTransactionManager;
97 this.openRoadmInterfaces = openRoadmInterfaces;
100 public boolean createMappingData(String nodeId) {
101 LOG.info("Create Mapping Data for node 1.2.1 {}", nodeId);
102 List<Mapping> portMapList = new ArrayList<>();
103 InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
104 Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType
105 .OPERATIONAL, infoIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
108 if (deviceInfoOptional.isPresent()) {
109 deviceInfo = deviceInfoOptional.get();
110 nodeInfo = createNodeInfo(deviceInfo);
111 if (nodeInfo == null) {
114 postPortMapping(nodeId, nodeInfo, null, null);
117 LOG.warn("Device info subtree is absent for {}", nodeId);
121 switch (deviceInfo.getNodeType()) {
124 // Get TTP port mapping
125 if (!createTtpPortMapping(nodeId, deviceInfo, portMapList)) {
126 // return false if mapping creation for TTP's failed
127 LOG.warn("Unable to create mapping for TTP's on node {}", nodeId);
131 // Get PP port mapping
132 if (!createPpPortMapping(nodeId, deviceInfo, portMapList)) {
133 // return false if mapping creation for PP's failed
134 LOG.warn("Unable to create mapping for PP's on node {}", nodeId);
139 if (!createXpdrPortMapping(nodeId, portMapList)) {
140 LOG.warn("Unable to create mapping for Xponder on node {}", nodeId);
145 LOG.error("Unable to create mapping for node {} : unknown nodetype ", nodeId);
149 return postPortMapping(nodeId, nodeInfo, portMapList, null);
152 public boolean updateMapping(String nodeId, Mapping oldMapping) {
153 InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
154 .child(CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName()))
155 .child(Ports.class, new PortsKey(oldMapping.getSupportingPort()));
156 if ((oldMapping != null) && (nodeId != null)) {
158 Optional<Ports> portObject = deviceTransactionManager.getDataFromDevice(nodeId,
159 LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
160 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
161 if (portObject.isPresent()) {
162 Ports port = portObject.get();
163 Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
164 oldMapping.getLogicalConnectionPoint());
165 LOG.info("Updating old mapping Data {} for {} of {} by new mapping data {}", oldMapping,
166 oldMapping.getLogicalConnectionPoint(), nodeId, newMapping);
167 final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
168 InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class)
169 .child(Nodes.class, new NodesKey(nodeId))
170 .child(Mapping.class, new MappingKey(oldMapping.getLogicalConnectionPoint()));
171 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
172 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
177 } catch (InterruptedException | ExecutionException e) {
178 LOG.error("Error updating Mapping {} for node {}", oldMapping.getLogicalConnectionPoint(), nodeId, e);
182 LOG.error("Impossible to update mapping");
187 private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
188 // Creating for Xponder Line and Client Ports
189 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
190 Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
191 LogicalDatastoreType.OPERATIONAL, deviceIID,
192 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
194 // Variable to keep track of number of line ports
196 // Variable to keep track of number of client ports
198 if (!deviceObject.isPresent() || deviceObject.get().getCircuitPacks() == null) {
199 LOG.warn("Circuit Packs are not present for {}", nodeId);
201 // TODO return false or continue?
203 Map<String, String> lcpMap = new HashMap<>();
204 Map<String, Mapping> mappingMap = new HashMap<>();
206 // com.google.common.collect.ImmutableList implementation of List
207 List<CircuitPacks> circuitPackList = new ArrayList<>(deviceObject.get().getCircuitPacks());
208 circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
210 for (CircuitPacks cp : circuitPackList) {
211 String circuitPackName = cp.getCircuitPackName();
212 if (cp.getPorts() == null) {
213 LOG.warn("Ports were not found for circuit pack: {}", circuitPackName);
217 // com.google.common.collect.ImmutableList implementation of List
218 List<Ports> portList = new ArrayList<>(cp.getPorts());
219 portList.sort(Comparator.comparing(Ports::getPortName));
220 for (Ports port : portList) {
221 if (port.getPortQual() == null) {
222 LOG.warn("PortQual was not found for port {} on circuit pack: {}", port.getPortName(),
226 if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
227 && port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
228 String lcp = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
229 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
230 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
232 } else if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
233 && port.getPortDirection().getIntValue() != Direction.Bidirectional.getIntValue()
234 && port.getPartnerPort() != null
235 && port.getPartnerPort().getCircuitPackName() != null
236 && port.getPartnerPort().getPortName() != null) {
237 if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
240 String lcp1 = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
241 Optional<CircuitPacks> cpOpt = circuitPackList.stream().filter(cP -> cP.getCircuitPackName()
242 .equals(port.getPartnerPort().getCircuitPackName())).findFirst();
243 if (cpOpt.isPresent()) {
244 Optional<Ports> poOpt = cpOpt.get().getPorts().stream().filter(p -> p.getPortName().equals(port
245 .getPartnerPort().getPortName().toString())).findFirst();
246 if (poOpt.isPresent()) {
247 Ports port2 = poOpt.get();
248 if ((Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
249 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
250 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
251 && port2.getPartnerPort().getPortName() != null
252 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
253 && port2.getPartnerPort().getPortName().equals(port.getPortName()))
255 (Direction.Tx.getIntValue() == port.getPortDirection().getIntValue()
256 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
257 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
258 && port2.getPartnerPort().getPortName() != null
259 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
260 && port2.getPartnerPort().getPortName().equals(port.getPortName()))) {
261 String lcp2 = new StringBuilder("XPDR1-").append(StringConstants.NETWORK_TOKEN)
262 .append(line + 1).toString();
263 if (!lcpMap.containsKey(lcp1) && !lcpMap.containsKey(lcp2)) {
264 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
265 lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
266 mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName, lcp1,
268 mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2,
269 cpOpt.get().getCircuitPackName(), lcp2, lcp1, null, null));
271 LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
275 LOG.error("port {} on {} is not a correct partner port of {} on {}",
276 port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
280 LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
281 port.getPartnerPort().getCircuitPackName(), nodeId);
284 LOG.error("Error fetching circuit-pack {} for {}", port.getPartnerPort().getCircuitPackName(),
287 } else if (Port.PortQual.XpdrClient.getIntValue() == port.getPortQual().getIntValue()) {
288 String lcp = "XPDR1-" + StringConstants.CLIENT_TOKEN + client;
289 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
290 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
293 LOG.warn("Error in the configuration of port {} of {} for {}", port.getPortName(), circuitPackName,
298 List<ConnectionMap> connectionMap = deviceObject.get().getConnectionMap();
301 for (ConnectionMap cm : connectionMap) {
302 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
303 if (lcpMap.containsKey(skey)) {
304 slcp = lcpMap.get(skey);
306 String dkey = cm.getDestination().get(0).getCircuitPackName() + "+"
307 + cm.getDestination().get(0).getPortName();
308 if (lcpMap.containsKey(dkey)) {
309 dlcp = lcpMap.get(dkey);
312 Mapping mapping = mappingMap.get(slcp);
313 mappingMap.remove(slcp);
314 portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
316 LOG.error("Error in connection-map analysis");
319 if (!mappingMap.isEmpty()) {
320 for (Mapping m : mappingMap.values()) {
327 private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
328 .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
329 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
330 .CircuitPacks>> cpPerSrg = new HashMap<>();
332 // Get value for max Srg from info subtree, required for iteration
333 // if not present assume to be 20 (temporary)
334 if (ordmInfo.getMaxSrgs() != null) {
335 maxSrg = ordmInfo.getMaxSrgs().toJava();
339 for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
340 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
342 LOG.info("Getting Circuitpacks for Srg Number {}", srgCounter);
343 InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
344 .child(SharedRiskGroup.class, new SharedRiskGroupKey(srgCounter));
345 Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
346 LogicalDatastoreType.OPERATIONAL, srgIID,
347 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
348 if (ordmSrgObject.isPresent()) {
349 srgCps.addAll(ordmSrgObject.get().getCircuitPacks());
350 cpPerSrg.put(ordmSrgObject.get().getSrgNumber().toJava(), srgCps);
353 LOG.info("Device {} has {} Srg", deviceId, cpPerSrg.size());
357 //last LOG info message in this method is too long
358 @SuppressWarnings("checkstyle:linelength")
359 private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
360 // Creating mapping data for SRG's PP
361 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCps
362 = getSrgCps(nodeId, deviceInfo);
364 for (Entry<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCpEntry : srgCps.entrySet()) {
365 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> cpList =
366 srgCps.get(srgCpEntry.getKey());
367 List<String> keys = new ArrayList<>();
368 for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cp : cpList) {
369 String circuitPackName = cp.getCircuitPackName();
370 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
371 .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
372 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
373 LogicalDatastoreType.OPERATIONAL, cpIID,
374 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
376 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
377 LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
380 // com.google.common.collect.ImmutableList implementation of List
382 List<Ports> portList = new ArrayList<>(circuitPackObject.get().getPorts());
383 Collections.sort(portList, new SortPort121ByName());
385 for (Ports port : portList) {
386 String currentKey = circuitPackName + "-" + port.getPortName();
387 if (port.getPortQual() == null) {
389 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
390 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()
391 && !keys.contains(currentKey)) {
392 String logicalConnectionPoint = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
393 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
394 port.getPortName(), logicalConnectionPoint);
395 portMapList.add(createMappingObject(nodeId, port, circuitPackName, logicalConnectionPoint));
397 keys.add(currentKey);
398 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
399 && (Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
400 || Direction.Tx.getIntValue() == port.getPortDirection().getIntValue())
401 && !keys.contains(currentKey)
402 && port.getPartnerPort() != null) {
403 String logicalConnectionPoint1 = createLogicalConnectionPort(port, srgCpEntry.getKey(),
405 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
406 port.getPortName(), logicalConnectionPoint1);
407 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
408 .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
409 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
410 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
411 LogicalDatastoreType.OPERATIONAL, port2ID,
412 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
413 if (port2Object.isPresent()
414 && port2Object.get().getPortQual().getIntValue()
415 == Port.PortQual.RoadmExternal.getIntValue()) {
416 Ports port2 = port2Object.get();
417 if ((port.getPortDirection().getIntValue() == Direction.Rx.getIntValue()
418 && port2.getPortDirection().getIntValue() == Direction.Tx.getIntValue()
419 && port2.getPartnerPort() != null
420 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
421 && port2.getPartnerPort().getPortName().toString().equals(port.getPortName()))
423 (port.getPortDirection().getIntValue() == Direction.Tx.getIntValue()
424 && port2.getPortDirection().getIntValue() == Direction.Rx.getIntValue()
425 && port2.getPartnerPort() != null
426 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
427 && port2.getPartnerPort().getPortName().toString().equals(port.getPortName()))) {
428 String logicalConnectionPoint2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),
430 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
431 port2.getPortName(), logicalConnectionPoint2);
432 portMapList.add(createMappingObject(nodeId, port, circuitPackName,
433 logicalConnectionPoint1));
434 portMapList.add(createMappingObject(nodeId, port2, port.getPartnerPort()
435 .getCircuitPackName(), logicalConnectionPoint2));
437 keys.add(currentKey);
438 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
440 LOG.error("Error with partner port configuration for port {} of {} - {}",
441 port.getPortName(), circuitPackName, nodeId);
445 LOG.error("error getting partner port {} of {} - {}",
446 port.getPartnerPort().getPortName().toString(),
447 port.getPartnerPort().getCircuitPackName(), nodeId);
451 LOG.info("{} : port {} on {} is not roadm-external or has already been handled. No logicalConnectionPoint assignment for this port.",
452 nodeId, port.getPortName(), circuitPackName);
460 private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
462 switch (port.getPortDirection()) {
464 lcp = "SRG" + index + "-PP" + portIndex + "-TX";
467 lcp = "SRG" + index + "-PP" + portIndex + "-RX";
470 lcp = "SRG" + index + "-PP" + portIndex + "-TXRX";
473 LOG.error("Unsupported port direction for port {} : {}", port, port.getPortDirection());
478 private List<Degree> getDegrees(String deviceId, Info ordmInfo) {
479 List<Degree> degrees = new ArrayList<>();
482 // Get value for max degree from info subtree, required for iteration
483 // if not present assume to be 20 (temporary)
484 if (ordmInfo.getMaxDegrees() != null) {
485 maxDegree = ordmInfo.getMaxDegrees().toJava();
490 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
491 LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
492 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
493 .child(Degree.class, new DegreeKey(degreeCounter));
494 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
495 LogicalDatastoreType.OPERATIONAL, deviceIID,
496 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
497 if (ordmDegreeObject.isPresent()) {
498 degrees.add(ordmDegreeObject.get());
501 LOG.info("Device {} has {} degree", deviceId, degrees.size());
505 private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
506 Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
509 if (ordmInfo.getMaxDegrees() != null) {
510 maxDegree = ordmInfo.getMaxDegrees().toJava();
514 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
515 LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
516 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
517 .child(Degree.class, new DegreeKey(degreeCounter));
518 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
519 LogicalDatastoreType.OPERATIONAL, deviceIID,
520 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
521 if (ordmDegreeObject.isPresent()) {
522 conPortMap.put(degreeCounter, ordmDegreeObject.get().getConnectionPorts());
525 LOG.info("Device {} has {} degree", deviceId, conPortMap.size());
529 private Map<String, String> getEthInterfaceList(String nodeId) {
530 LOG.info("It is calling get ethernet interface");
531 Map<String, String> cpToInterfaceMap = new HashMap<>();
532 InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
533 .child(Protocols.class);
534 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
535 LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
536 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
537 if (protocolObject.isPresent() && protocolObject.get().augmentation(Protocols1.class).getLldp() != null) {
538 Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
539 for (PortConfig portConfig : lldp.getPortConfig()) {
540 if (portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
541 InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
542 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
543 Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
544 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
545 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
546 if (interfaceObject.isPresent() && (interfaceObject.get().getSupportingCircuitPackName() != null)) {
547 String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
548 cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
549 InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
550 .create(OrgOpenroadmDevice.class)
551 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
552 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
553 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
554 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
555 if (circuitPackObject.isPresent() && (circuitPackObject.get().getParentCircuitPack() != null)) {
556 cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
557 portConfig.getIfName());
563 LOG.warn("Couldnt find port config under LLDP for Node : {}", nodeId);
565 LOG.info("Processiong is done.. now returning..");
566 return cpToInterfaceMap;
569 private List<CpToDegree> getCpToDegreeList(List<Degree> degrees, String nodeId,
570 Map<String, String> interfaceList) {
571 List<CpToDegree> cpToDegreeList = new ArrayList<>();
572 for (Degree degree : degrees) {
573 if (degree.getCircuitPacks() != null) {
574 LOG.info("Inside CP to degree list");
575 cpToDegreeList.addAll(degree.getCircuitPacks().stream()
576 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
577 degree.getDegreeNumber().toString(), nodeId, interfaceList))
578 .collect(Collectors.toList()));
581 return cpToDegreeList;
584 private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
585 List<CpToDegree> cp2DegreeList) {
586 NodesBuilder nodesBldr = new NodesBuilder();
587 nodesBldr.withKey(new NodesKey(nodeId)).setNodeId(nodeId);
588 if (nodeInfo != null) {
589 nodesBldr.setNodeInfo(nodeInfo);
591 if (portMapList != null) {
592 nodesBldr.setMapping(portMapList);
594 if (cp2DegreeList != null) {
595 nodesBldr.setCpToDegree(cp2DegreeList);
598 List<Nodes> nodesList = new ArrayList<>();
599 nodesList.add(nodesBldr.build());
601 NetworkBuilder nwBldr = new NetworkBuilder();
602 nwBldr.setNodes(nodesList);
604 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
605 InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
606 Network network = nwBldr.build();
607 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
608 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
613 } catch (InterruptedException | ExecutionException e) {
614 LOG.warn("Failed to post {}", network, e);
619 private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber, String nodeId,
620 Map<String, String> interfaceList) {
621 String interfaceName = null;
622 if (interfaceList.get(circuitPackName) != null) {
623 interfaceName = interfaceList.get(circuitPackName);
625 return new CpToDegreeBuilder().withKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
626 .setDegreeNumber(Long.valueOf(degreeNumber)).setInterfaceName(interfaceName).build();
629 private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
630 String logicalConnectionPoint) {
631 MappingBuilder mpBldr = new MappingBuilder();
632 mpBldr.withKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
633 .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName())
634 .setPortDirection(port.getPortDirection().getName());
636 // Get OMS and OTS interface provisioned on the TTP's
637 if (logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) && (port.getInterfaces() != null)) {
638 for (Interfaces interfaces : port.getInterfaces()) {
640 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
641 interfaces.getInterfaceName());
642 if (openRoadmInterface.isPresent()) {
643 Class<? extends InterfaceType> interfaceType
644 = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
645 // Check if interface type is OMS or OTS
646 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
647 mpBldr.setSupportingOms(interfaces.getInterfaceName());
649 if (interfaceType.equals(OpticalTransport.class)) {
650 mpBldr.setSupportingOts(interfaces.getInterfaceName());
653 LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
655 } catch (OpenRoadmInterfaceException ex) {
656 LOG.warn("Error while getting interface {} from node {}!", interfaces.getInterfaceName(), nodeId,
661 return mpBldr.build();
664 private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
665 String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
666 MappingBuilder mpBldr;
668 if (mapping != null && assoLcp != null) {
669 // update existing mapping
670 mpBldr = new MappingBuilder(mapping);
671 mpBldr.setConnectionMapLcp(assoLcp);
673 // create a new mapping
674 mpBldr = new MappingBuilder();
675 String nodeIdLcp = nodeId + logicalConnectionPoint;
676 mpBldr.withKey(new MappingKey(logicalConnectionPoint))
677 .setLogicalConnectionPoint(logicalConnectionPoint)
678 .setSupportingCircuitPackName(circuitPackName)
679 .setSupportingPort(port.getPortName())
680 .setPortDirection(port.getPortDirection().getName())
681 .setLcpHashVal(fnv(nodeIdLcp));
682 if (port.getPortQual() != null) {
683 mpBldr.setPortQual(port.getPortQual().getName());
685 if (partnerLcp != null) {
686 mpBldr.setPartnerLcp(partnerLcp);
689 return mpBldr.build();
692 // some LOG messages are too long
693 @SuppressWarnings("checkstyle:linelength")
694 @SuppressFBWarnings("DM_CONVERT_CASE")
695 private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
696 // Creating mapping data for degree TTP's
697 List<Degree> degrees = getDegrees(nodeId, deviceInfo);
698 Map<String, String> interfaceList = getEthInterfaceList(nodeId);
699 List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, nodeId, interfaceList);
700 LOG.info("Map looks like this {}", interfaceList);
701 postPortMapping(nodeId, null, null, cpToDegreeList);
703 Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
704 for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
705 switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
707 // port is bidirectional
708 InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
709 .child(CircuitPacks.class, new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0)
710 .getCircuitPackName()))
711 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0)
712 .getPortName().toString()));
713 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap
714 .get(cpMapEntry.getKey()).get(0).getPortName().toString(), connectionPortMap
715 .get(cpMapEntry.getKey()).get(0).getCircuitPackName());
716 Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
717 LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
718 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
719 if (portObject.isPresent()) {
720 Ports port = portObject.get();
721 if (port.getPortQual() == null) {
723 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
724 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()) {
725 String logicalConnectionPoint = new StringBuilder("DEG").append(cpMapEntry.getKey())
726 .append("-TTP-TXRX").toString();
727 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
728 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
729 port.getPortName(), logicalConnectionPoint);
730 portMapList.add(createMappingObject(nodeId, port,
731 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
732 logicalConnectionPoint));
735 "Impossible to create logical connection point for port {} of {} on node {} - Error in configuration with port-qual or port-direction",
736 port.getPortName(), connectionPortMap
737 .get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
740 LOG.error("No port {} on circuit pack {} for node {}",
741 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
742 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
747 // ports are unidirectionals
748 String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
749 String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
750 InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
751 .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
752 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()
754 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap
755 .get(cpMapEntry.getKey()).get(0).getPortName().toString(), cp1Name);
756 Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
757 LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
758 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
759 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
760 .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
761 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName()
763 LOG.info("Fetching connection-port {} at circuit pack {}",
764 connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString(), cp2Name);
765 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
766 LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
767 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
768 if (port1Object.isPresent() && port2Object.isPresent()) {
769 Ports port1 = port1Object.get();
770 Ports port2 = port2Object.get();
771 if (port1.getPortQual() == null || port2.getPortQual() == null) {
773 } else if ((Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
774 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
775 && Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
776 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
777 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
778 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
779 && port1.getPartnerPort().getPortName().equals(port2.getPortName())
780 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
781 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))
783 (Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
784 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
785 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
786 && Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
787 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
788 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
789 && port1.getPartnerPort().getPortName().equals(port2.getPortName())
790 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
791 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))) {
792 String logicalConnectionPoint1 = new StringBuilder("DEG").append(cpMapEntry.getKey())
793 .append("-TTP-").append(port1.getPortDirection().getName().toUpperCase()).toString();
794 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
795 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
796 port1.getPortName(), logicalConnectionPoint1);
797 portMapList.add(createMappingObject(nodeId, port1, connectionPortMap
798 .get(cpMapEntry.getKey()).get(0).getCircuitPackName(), logicalConnectionPoint1));
799 String logicalConnectionPoint2 = new StringBuilder("DEG").append(cpMapEntry.getKey())
800 .append("-TTP-").append(port2.getPortDirection().getName().toUpperCase()).toString();
801 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
802 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
803 port2.getPortName(), logicalConnectionPoint2);
804 portMapList.add(createMappingObject(nodeId, port2, connectionPortMap
805 .get(cpMapEntry.getKey()).get(1).getCircuitPackName(), logicalConnectionPoint2));
808 "Impossible to create logical connection point for port {} or port {} on node {} - Error in configuration with port-qual, port-direction or partner-port configuration",
809 port1.getPortName(), port2.getPortName(), nodeId);
812 LOG.error("No port {} on circuit pack {} for node {}",
813 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
814 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
820 LOG.error("Number of connection port for DEG{} on {} is incorrect", cpMapEntry.getKey(), nodeId);
827 private NodeInfo createNodeInfo(Info deviceInfo) {
828 NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder();
829 if (deviceInfo.getNodeType() != null) {
830 nodeInfoBldr.setOpenroadmVersion(OpenroadmVersion._121);
831 if (deviceInfo.getNodeType().getIntValue() == 1) {
832 nodeInfoBldr.setNodeType(NodeTypes.Rdm);
833 } else if (deviceInfo.getNodeType().getIntValue() == 2) {
834 nodeInfoBldr.setNodeType(NodeTypes.Xpdr);
836 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
838 if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
839 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
841 nodeInfoBldr.setNodeClli("defaultCLLI");
843 if (deviceInfo.getModel() != null) {
844 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
846 if (deviceInfo.getVendor() != null) {
847 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
849 if (deviceInfo.getIpAddress() != null) {
850 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
853 // TODO make mandatory in yang
854 LOG.error("Node type field is missing");
857 return nodeInfoBldr.build();
861 * Implements the FNV-1 128bit algorithm.
862 * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
863 * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
864 * @param stringdata the String to be hashed
865 * @return the hash string
867 private String fnv(String stringdata) {
868 BigInteger hash = FNV_INIT;
869 byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
871 for (byte b : data) {
872 hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
873 hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
876 return hash.toString(16);