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.Base64;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.Optional;
24 import java.util.concurrent.ExecutionException;
25 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.common.api.CommitInfo;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.transportpce.common.StringConstants;
33 import org.opendaylight.transportpce.common.Timeouts;
34 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
35 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
36 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.Network;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.NetworkBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.Nodes;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.NodesBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.NodesKey;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.CpToDegree;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.CpToDegreeBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.CpToDegreeKey;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.Mapping;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.MappingBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.MappingKey;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfo;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfo.OpenroadmVersion;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfoBuilder;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.Direction;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.NodeTypes;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.CircuitPack;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.Port;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPorts;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.ConnectionMap;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Degree;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroup;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroupKey;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.port.Interfaces;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.InterfaceType;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpenROADMOpticalMultiplex;
73 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpticalTransport;
74 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
75 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.Lldp;
76 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.PortConfig;
77 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
81 public class PortMappingVersion121 {
83 private static final Logger LOG = LoggerFactory.getLogger(PortMappingVersion121.class);
85 private final DataBroker dataBroker;
86 private final DeviceTransactionManager deviceTransactionManager;
87 private final OpenRoadmInterfaces openRoadmInterfaces;
89 //FNV1 64 bit hash constants
90 private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16);
91 private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16);
92 private static final BigInteger FNV_MOD = new BigInteger("2").pow(64);
94 public PortMappingVersion121(DataBroker dataBroker, DeviceTransactionManager deviceTransactionManager,
95 OpenRoadmInterfaces openRoadmInterfaces) {
96 this.dataBroker = dataBroker;
97 this.deviceTransactionManager = deviceTransactionManager;
98 this.openRoadmInterfaces = openRoadmInterfaces;
101 public boolean createMappingData(String nodeId) {
102 LOG.info("Create Mapping Data for node 1.2.1 {}", nodeId);
103 List<Mapping> portMapList = new ArrayList<>();
104 InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
105 Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType
106 .OPERATIONAL, infoIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
109 if (deviceInfoOptional.isPresent()) {
110 deviceInfo = deviceInfoOptional.get();
111 nodeInfo = createNodeInfo(deviceInfo);
112 if (nodeInfo == null) {
115 postPortMapping(nodeId, nodeInfo, null, null);
118 LOG.warn("Device info subtree is absent for {}", nodeId);
122 switch (deviceInfo.getNodeType()) {
125 // Get TTP port mapping
126 if (!createTtpPortMapping(nodeId, deviceInfo, portMapList)) {
127 // return false if mapping creation for TTP's failed
128 LOG.warn("Unable to create mapping for TTP's on node {}", nodeId);
132 // Get PP port mapping
133 if (!createPpPortMapping(nodeId, deviceInfo, portMapList)) {
134 // return false if mapping creation for PP's failed
135 LOG.warn("Unable to create mapping for PP's on node {}", nodeId);
140 if (!createXpdrPortMapping(nodeId, portMapList)) {
141 LOG.warn("Unable to create mapping for Xponder on node {}", nodeId);
146 LOG.error("Unable to create mapping for node {} : unknown nodetype ", nodeId);
150 return postPortMapping(nodeId, nodeInfo, portMapList, null);
153 public boolean updateMapping(String nodeId, Mapping oldMapping) {
154 InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class)
155 .child(CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName()))
156 .child(Ports.class, new PortsKey(oldMapping.getSupportingPort()));
157 if ((oldMapping != null) && (nodeId != null)) {
159 Optional<Ports> portObject = deviceTransactionManager.getDataFromDevice(nodeId,
160 LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
161 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
162 if (portObject.isPresent()) {
163 Ports port = portObject.get();
164 Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
165 oldMapping.getLogicalConnectionPoint());
166 LOG.info("Updating old mapping Data {} for {} of {} by new mapping data {}", oldMapping,
167 oldMapping.getLogicalConnectionPoint(), nodeId, newMapping);
168 final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
169 InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class)
170 .child(Nodes.class, new NodesKey(nodeId))
171 .child(Mapping.class, new MappingKey(oldMapping.getLogicalConnectionPoint()));
172 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
173 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
178 } catch (InterruptedException | ExecutionException e) {
179 LOG.error("Error updating Mapping {} for node {}", oldMapping.getLogicalConnectionPoint(), nodeId, e);
183 LOG.error("Impossible to update mapping");
188 private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
189 // Creating for Xponder Line and Client Ports
190 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
191 Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
192 LogicalDatastoreType.OPERATIONAL, deviceIID,
193 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
195 // Variable to keep track of number of line ports
197 // Variable to keep track of number of client ports
199 if (!deviceObject.isPresent() || deviceObject.get().getCircuitPacks() == null) {
200 LOG.warn("Circuit Packs are not present for {}", nodeId);
202 // TODO return false or continue?
204 Map<String, String> lcpMap = new HashMap<>();
205 Map<String, Mapping> mappingMap = new HashMap<>();
207 // com.google.common.collect.ImmutableList implementation of List
208 List<CircuitPacks> circuitPackList = new ArrayList<>(deviceObject.get().getCircuitPacks());
209 circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
211 for (CircuitPacks cp : circuitPackList) {
212 String circuitPackName = cp.getCircuitPackName();
213 if (cp.getPorts() == null) {
214 LOG.warn("Ports were not found for circuit pack: {}", circuitPackName);
218 // com.google.common.collect.ImmutableList implementation of List
219 List<Ports> portList = new ArrayList<>(cp.getPorts());
220 portList.sort(Comparator.comparing(Ports::getPortName));
221 for (Ports port : portList) {
222 if (port.getPortQual() == null) {
223 LOG.warn("PortQual was not found for port {} on circuit pack: {}", port.getPortName(),
227 if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
228 && port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
229 String lcp = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
230 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
231 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
233 } else if (Port.PortQual.XpdrNetwork.getIntValue() == port.getPortQual().getIntValue()
234 && port.getPortDirection().getIntValue() != Direction.Bidirectional.getIntValue()
235 && port.getPartnerPort() != null
236 && port.getPartnerPort().getCircuitPackName() != null
237 && port.getPartnerPort().getPortName() != null) {
238 if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
241 String lcp1 = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
242 Optional<CircuitPacks> cpOpt = circuitPackList.stream().filter(cP -> cP.getCircuitPackName()
243 .equals(port.getPartnerPort().getCircuitPackName())).findFirst();
244 if (cpOpt.isPresent()) {
245 Optional<Ports> poOpt = cpOpt.get().getPorts().stream().filter(p -> p.getPortName().equals(port
246 .getPartnerPort().getPortName().toString())).findFirst();
247 if (poOpt.isPresent()) {
248 Ports port2 = poOpt.get();
249 if ((Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
250 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
251 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
252 && port2.getPartnerPort().getPortName() != null
253 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
254 && port2.getPartnerPort().getPortName().equals(port.getPortName()))
256 (Direction.Tx.getIntValue() == port.getPortDirection().getIntValue()
257 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
258 && port2.getPartnerPort() != null && port2.getPartnerPort().getCircuitPackName() != null
259 && port2.getPartnerPort().getPortName() != null
260 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
261 && port2.getPartnerPort().getPortName().equals(port.getPortName()))) {
262 String lcp2 = new StringBuilder("XPDR1-").append(StringConstants.NETWORK_TOKEN)
263 .append(line + 1).toString();
264 if (!lcpMap.containsKey(lcp1) && !lcpMap.containsKey(lcp2)) {
265 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
266 lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
267 mappingMap.put(lcp1, createXpdrMappingObject(nodeId, port, circuitPackName, lcp1,
269 mappingMap.put(lcp2, createXpdrMappingObject(nodeId, port2,
270 cpOpt.get().getCircuitPackName(), lcp2, lcp1, null, null));
272 LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
276 LOG.error("port {} on {} is not a correct partner port of {} on {}",
277 port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
281 LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
282 port.getPartnerPort().getCircuitPackName(), nodeId);
285 LOG.error("Error fetching circuit-pack {} for {}", port.getPartnerPort().getCircuitPackName(),
288 } else if (Port.PortQual.XpdrClient.getIntValue() == port.getPortQual().getIntValue()) {
289 String lcp = "XPDR1-" + StringConstants.CLIENT_TOKEN + client;
290 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
291 mappingMap.put(lcp, createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null));
294 LOG.warn("Error in the configuration of port {} of {} for {}", port.getPortName(), circuitPackName,
299 List<ConnectionMap> connectionMap = deviceObject.get().getConnectionMap();
302 for (ConnectionMap cm : connectionMap) {
303 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
304 if (lcpMap.containsKey(skey)) {
305 slcp = lcpMap.get(skey);
307 String dkey = cm.getDestination().get(0).getCircuitPackName() + "+"
308 + cm.getDestination().get(0).getPortName();
309 if (lcpMap.containsKey(dkey)) {
310 dlcp = lcpMap.get(dkey);
313 Mapping mapping = mappingMap.get(slcp);
314 mappingMap.remove(slcp);
315 portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp));
317 LOG.error("Error in connection-map analysis");
320 if (!mappingMap.isEmpty()) {
321 for (Mapping m : mappingMap.values()) {
328 private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
329 .CircuitPacks>> getSrgCps(String deviceId, Info ordmInfo) {
330 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
331 .CircuitPacks>> cpPerSrg = new HashMap<>();
333 // Get value for max Srg from info subtree, required for iteration
334 // if not present assume to be 20 (temporary)
335 if (ordmInfo.getMaxSrgs() != null) {
336 maxSrg = ordmInfo.getMaxSrgs().toJava();
340 for (int srgCounter = 1; srgCounter <= maxSrg; srgCounter++) {
341 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> srgCps
343 LOG.info("Getting Circuitpacks for Srg Number {}", srgCounter);
344 InstanceIdentifier<SharedRiskGroup> srgIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
345 .child(SharedRiskGroup.class, new SharedRiskGroupKey(srgCounter));
346 Optional<SharedRiskGroup> ordmSrgObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
347 LogicalDatastoreType.OPERATIONAL, srgIID,
348 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
349 if (ordmSrgObject.isPresent()) {
350 srgCps.addAll(ordmSrgObject.get().getCircuitPacks());
351 cpPerSrg.put(ordmSrgObject.get().getSrgNumber().toJava(), srgCps);
354 LOG.info("Device {} has {} Srg", deviceId, cpPerSrg.size());
358 //last LOG info message in this method is too long
359 @SuppressWarnings("checkstyle:linelength")
360 private boolean createPpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
361 // Creating mapping data for SRG's PP
362 HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCps
363 = getSrgCps(nodeId, deviceInfo);
365 for (Entry<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>> srgCpEntry : srgCps.entrySet()) {
366 List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> cpList =
367 srgCps.get(srgCpEntry.getKey());
368 List<String> keys = new ArrayList<>();
369 for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks cp : cpList) {
370 String circuitPackName = cp.getCircuitPackName();
371 InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
372 .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
373 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
374 LogicalDatastoreType.OPERATIONAL, cpIID,
375 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
377 if (!circuitPackObject.isPresent() || (circuitPackObject.get().getPorts() == null)) {
378 LOG.warn("{} : Circuit pack {} not found or without ports.", nodeId, circuitPackName);
381 // com.google.common.collect.ImmutableList implementation of List
383 List<Ports> portList = new ArrayList<>(circuitPackObject.get().getPorts());
384 Collections.sort(portList, new SortPort121ByName());
386 for (Ports port : portList) {
387 String currentKey = circuitPackName + "-" + port.getPortName();
388 if (port.getPortQual() == null) {
390 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
391 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()
392 && !keys.contains(currentKey)) {
393 String logicalConnectionPoint = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
394 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
395 port.getPortName(), logicalConnectionPoint);
396 portMapList.add(createMappingObject(nodeId, port, circuitPackName, logicalConnectionPoint));
398 keys.add(currentKey);
399 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
400 && (Direction.Rx.getIntValue() == port.getPortDirection().getIntValue()
401 || Direction.Tx.getIntValue() == port.getPortDirection().getIntValue())
402 && !keys.contains(currentKey)
403 && port.getPartnerPort() != null) {
404 String logicalConnectionPoint1 = createLogicalConnectionPort(port, srgCpEntry.getKey(),
406 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
407 port.getPortName(), logicalConnectionPoint1);
408 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
409 .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
410 .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName().toString()));
411 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
412 LogicalDatastoreType.OPERATIONAL, port2ID,
413 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
414 if (port2Object.isPresent()
415 && port2Object.get().getPortQual().getIntValue()
416 == Port.PortQual.RoadmExternal.getIntValue()) {
417 Ports port2 = port2Object.get();
418 if ((port.getPortDirection().getIntValue() == Direction.Rx.getIntValue()
419 && port2.getPortDirection().getIntValue() == Direction.Tx.getIntValue()
420 && port2.getPartnerPort() != null
421 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
422 && port2.getPartnerPort().getPortName().toString().equals(port.getPortName()))
424 (port.getPortDirection().getIntValue() == Direction.Tx.getIntValue()
425 && port2.getPortDirection().getIntValue() == Direction.Rx.getIntValue()
426 && port2.getPartnerPort() != null
427 && port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
428 && port2.getPartnerPort().getPortName().toString().equals(port.getPortName()))) {
429 String logicalConnectionPoint2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),
431 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId, circuitPackName,
432 port2.getPortName(), logicalConnectionPoint2);
433 portMapList.add(createMappingObject(nodeId, port, circuitPackName,
434 logicalConnectionPoint1));
435 portMapList.add(createMappingObject(nodeId, port2, port.getPartnerPort()
436 .getCircuitPackName(), logicalConnectionPoint2));
438 keys.add(currentKey);
439 keys.add(port.getPartnerPort().getCircuitPackName() + "-" + port2.getPortName());
441 LOG.error("Error with partner port configuration for port {} of {} - {}",
442 port.getPortName(), circuitPackName, nodeId);
446 LOG.error("error getting partner port {} of {} - {}",
447 port.getPartnerPort().getPortName().toString(),
448 port.getPartnerPort().getCircuitPackName(), nodeId);
452 LOG.info("{} : port {} on {} is not roadm-external or has already been handled. No logicalConnectionPoint assignment for this port.",
453 nodeId, port.getPortName(), circuitPackName);
461 private String createLogicalConnectionPort(Ports port, int index, int portIndex) {
463 switch (port.getPortDirection()) {
465 lcp = "SRG" + index + "-PP" + portIndex + "-TX";
468 lcp = "SRG" + index + "-PP" + portIndex + "-RX";
471 lcp = "SRG" + index + "-PP" + portIndex + "-TXRX";
474 LOG.error("Unsupported port direction for port {} : {}", port, port.getPortDirection());
479 private List<Degree> getDegrees(String deviceId, Info ordmInfo) {
480 List<Degree> degrees = new ArrayList<>();
483 // Get value for max degree from info subtree, required for iteration
484 // if not present assume to be 20 (temporary)
485 if (ordmInfo.getMaxDegrees() != null) {
486 maxDegree = ordmInfo.getMaxDegrees().toJava();
491 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
492 LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
493 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
494 .child(Degree.class, new DegreeKey(degreeCounter));
495 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
496 LogicalDatastoreType.OPERATIONAL, deviceIID,
497 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
498 if (ordmDegreeObject.isPresent()) {
499 degrees.add(ordmDegreeObject.get());
502 LOG.info("Device {} has {} degree", deviceId, degrees.size());
506 private Map<Integer, List<ConnectionPorts>> getPerDegreePorts(String deviceId, Info ordmInfo) {
507 Map<Integer, List<ConnectionPorts>> conPortMap = new HashMap<>();
510 if (ordmInfo.getMaxDegrees() != null) {
511 maxDegree = ordmInfo.getMaxDegrees().toJava();
515 for (int degreeCounter = 1; degreeCounter <= maxDegree; degreeCounter++) {
516 LOG.info("Getting Connection ports for Degree Number {}", degreeCounter);
517 InstanceIdentifier<Degree> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
518 .child(Degree.class, new DegreeKey(degreeCounter));
519 Optional<Degree> ordmDegreeObject = this.deviceTransactionManager.getDataFromDevice(deviceId,
520 LogicalDatastoreType.OPERATIONAL, deviceIID,
521 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
522 if (ordmDegreeObject.isPresent()) {
523 conPortMap.put(degreeCounter, ordmDegreeObject.get().getConnectionPorts());
526 LOG.info("Device {} has {} degree", deviceId, conPortMap.size());
530 private Map<String, String> getEthInterfaceList(String nodeId) {
531 LOG.info("It is calling get ethernet interface");
532 Map<String, String> cpToInterfaceMap = new HashMap<>();
533 InstanceIdentifier<Protocols> protocoliid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
534 .child(Protocols.class);
535 Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
536 LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
537 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
538 if (protocolObject.isPresent() && protocolObject.get().augmentation(Protocols1.class).getLldp() != null) {
539 Lldp lldp = protocolObject.get().augmentation(Protocols1.class).getLldp();
540 for (PortConfig portConfig : lldp.getPortConfig()) {
541 if (portConfig.getAdminStatus().equals(PortConfig.AdminStatus.Txandrx)) {
542 InstanceIdentifier<Interface> interfaceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
543 .child(Interface.class, new InterfaceKey(portConfig.getIfName()));
544 Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
545 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
546 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
547 if (interfaceObject.isPresent() && (interfaceObject.get().getSupportingCircuitPackName() != null)) {
548 String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
549 cpToInterfaceMap.put(supportingCircuitPackName, portConfig.getIfName());
550 InstanceIdentifier<CircuitPacks> circuitPacksIID = InstanceIdentifier
551 .create(OrgOpenroadmDevice.class)
552 .child(CircuitPacks.class, new CircuitPacksKey(supportingCircuitPackName));
553 Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
554 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
555 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
556 if (circuitPackObject.isPresent() && (circuitPackObject.get().getParentCircuitPack() != null)) {
557 cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
558 portConfig.getIfName());
564 LOG.warn("Couldnt find port config under LLDP for Node : {}", nodeId);
566 LOG.info("Processiong is done.. now returning..");
567 return cpToInterfaceMap;
570 private List<CpToDegree> getCpToDegreeList(List<Degree> degrees, String nodeId,
571 Map<String, String> interfaceList) {
572 List<CpToDegree> cpToDegreeList = new ArrayList<>();
573 for (Degree degree : degrees) {
574 if (degree.getCircuitPacks() != null) {
575 LOG.info("Inside CP to degree list");
576 cpToDegreeList.addAll(degree.getCircuitPacks().stream()
577 .map(cp -> createCpToDegreeObject(cp.getCircuitPackName(),
578 degree.getDegreeNumber().toString(), nodeId, interfaceList))
579 .collect(Collectors.toList()));
582 return cpToDegreeList;
585 private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
586 List<CpToDegree> cp2DegreeList) {
587 NodesBuilder nodesBldr = new NodesBuilder();
588 nodesBldr.withKey(new NodesKey(nodeId)).setNodeId(nodeId);
589 if (nodeInfo != null) {
590 nodesBldr.setNodeInfo(nodeInfo);
592 if (portMapList != null) {
593 nodesBldr.setMapping(portMapList);
595 if (cp2DegreeList != null) {
596 nodesBldr.setCpToDegree(cp2DegreeList);
599 List<Nodes> nodesList = new ArrayList<>();
600 nodesList.add(nodesBldr.build());
602 NetworkBuilder nwBldr = new NetworkBuilder();
603 nwBldr.setNodes(nodesList);
605 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
606 InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
607 Network network = nwBldr.build();
608 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
609 FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
614 } catch (InterruptedException | ExecutionException e) {
615 LOG.warn("Failed to post {}", network, e);
620 private CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber, String nodeId,
621 Map<String, String> interfaceList) {
622 String interfaceName = null;
623 if (interfaceList.get(circuitPackName) != null) {
624 interfaceName = interfaceList.get(circuitPackName);
626 return new CpToDegreeBuilder().withKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
627 .setDegreeNumber(Long.valueOf(degreeNumber)).setInterfaceName(interfaceName).build();
630 private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
631 String logicalConnectionPoint) {
632 MappingBuilder mpBldr = new MappingBuilder();
633 mpBldr.withKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
634 .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName())
635 .setPortDirection(port.getPortDirection().getName());
637 // Get OMS and OTS interface provisioned on the TTP's
638 if (logicalConnectionPoint.contains(StringConstants.TTP_TOKEN) && (port.getInterfaces() != null)) {
639 for (Interfaces interfaces : port.getInterfaces()) {
641 Optional<Interface> openRoadmInterface = this.openRoadmInterfaces.getInterface(nodeId,
642 interfaces.getInterfaceName());
643 if (openRoadmInterface.isPresent()) {
644 Class<? extends InterfaceType> interfaceType
645 = (Class<? extends InterfaceType>) openRoadmInterface.get().getType();
646 // Check if interface type is OMS or OTS
647 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
648 mpBldr.setSupportingOms(interfaces.getInterfaceName());
650 if (interfaceType.equals(OpticalTransport.class)) {
651 mpBldr.setSupportingOts(interfaces.getInterfaceName());
654 LOG.warn("Interface {} from node {} was null!", interfaces.getInterfaceName(), nodeId);
656 } catch (OpenRoadmInterfaceException ex) {
657 LOG.warn("Error while getting interface {} from node {}!", interfaces.getInterfaceName(), nodeId,
662 return mpBldr.build();
665 private Mapping createXpdrMappingObject(String nodeId, Ports port, String circuitPackName,
666 String logicalConnectionPoint, String partnerLcp, Mapping mapping, String assoLcp) {
667 MappingBuilder mpBldr;
669 if (mapping != null && assoLcp != null) {
670 // update existing mapping
671 mpBldr = new MappingBuilder(mapping);
672 mpBldr.setConnectionMapLcp(assoLcp);
674 // create a new mapping
675 mpBldr = new MappingBuilder();
676 String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
677 mpBldr.withKey(new MappingKey(logicalConnectionPoint))
678 .setLogicalConnectionPoint(logicalConnectionPoint)
679 .setSupportingCircuitPackName(circuitPackName)
680 .setSupportingPort(port.getPortName())
681 .setPortDirection(port.getPortDirection().getName())
682 .setLcpHashVal(fnv(nodeIdLcp));
683 if (port.getPortQual() != null) {
684 mpBldr.setPortQual(port.getPortQual().getName());
686 if (partnerLcp != null) {
687 mpBldr.setPartnerLcp(partnerLcp);
690 return mpBldr.build();
693 // some LOG messages are too long
694 @SuppressWarnings("checkstyle:linelength")
695 @SuppressFBWarnings("DM_CONVERT_CASE")
696 private boolean createTtpPortMapping(String nodeId, Info deviceInfo, List<Mapping> portMapList) {
697 // Creating mapping data for degree TTP's
698 List<Degree> degrees = getDegrees(nodeId, deviceInfo);
699 Map<String, String> interfaceList = getEthInterfaceList(nodeId);
700 List<CpToDegree> cpToDegreeList = getCpToDegreeList(degrees, nodeId, interfaceList);
701 LOG.info("Map looks like this {}", interfaceList);
702 postPortMapping(nodeId, null, null, cpToDegreeList);
704 Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
705 for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
706 switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
708 // port is bidirectional
709 InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
710 .child(CircuitPacks.class, new CircuitPacksKey(connectionPortMap.get(cpMapEntry.getKey()).get(0)
711 .getCircuitPackName()))
712 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0)
713 .getPortName().toString()));
714 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap
715 .get(cpMapEntry.getKey()).get(0).getPortName().toString(), connectionPortMap
716 .get(cpMapEntry.getKey()).get(0).getCircuitPackName());
717 Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
718 LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
719 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
720 if (portObject.isPresent()) {
721 Ports port = portObject.get();
722 if (port.getPortQual() == null) {
724 } else if (Port.PortQual.RoadmExternal.getIntValue() == port.getPortQual().getIntValue()
725 && Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue()) {
726 String logicalConnectionPoint = new StringBuilder("DEG").append(cpMapEntry.getKey())
727 .append("-TTP-TXRX").toString();
728 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
729 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
730 port.getPortName(), logicalConnectionPoint);
731 portMapList.add(createMappingObject(nodeId, port,
732 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
733 logicalConnectionPoint));
736 "Impossible to create logical connection point for port {} of {} on node {} - Error in configuration with port-qual or port-direction",
737 port.getPortName(), connectionPortMap
738 .get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
741 LOG.error("No port {} on circuit pack {} for node {}",
742 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
743 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
748 // ports are unidirectionals
749 String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
750 String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
751 InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
752 .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
753 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()
755 LOG.info("Fetching connection-port {} at circuit pack {}", connectionPortMap
756 .get(cpMapEntry.getKey()).get(0).getPortName().toString(), cp1Name);
757 Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
758 LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
759 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
760 InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
761 .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
762 .child(Ports.class, new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName()
764 LOG.info("Fetching connection-port {} at circuit pack {}",
765 connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName().toString(), cp2Name);
766 Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
767 LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
768 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
769 if (port1Object.isPresent() && port2Object.isPresent()) {
770 Ports port1 = port1Object.get();
771 Ports port2 = port2Object.get();
772 if (port1.getPortQual() == null || port2.getPortQual() == null) {
774 } else if ((Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
775 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
776 && Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
777 && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue()
778 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
779 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
780 && port1.getPartnerPort().getPortName().equals(port2.getPortName())
781 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
782 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))
784 (Port.PortQual.RoadmExternal.getIntValue() == port1.getPortQual().getIntValue()
785 && Port.PortQual.RoadmExternal.getIntValue() == port2.getPortQual().getIntValue()
786 && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue()
787 && Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
788 && port1.getPartnerPort() != null && port2.getPartnerPort() != null
789 && port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
790 && port1.getPartnerPort().getPortName().equals(port2.getPortName())
791 && port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
792 && port2.getPartnerPort().getPortName().equals(port1.getPortName()))) {
793 String logicalConnectionPoint1 = new StringBuilder("DEG").append(cpMapEntry.getKey())
794 .append("-TTP-").append(port1.getPortDirection().getName().toUpperCase()).toString();
795 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
796 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(),
797 port1.getPortName(), logicalConnectionPoint1);
798 portMapList.add(createMappingObject(nodeId, port1, connectionPortMap
799 .get(cpMapEntry.getKey()).get(0).getCircuitPackName(), logicalConnectionPoint1));
800 String logicalConnectionPoint2 = new StringBuilder("DEG").append(cpMapEntry.getKey())
801 .append("-TTP-").append(port2.getPortDirection().getName().toUpperCase()).toString();
802 LOG.info("{} : Logical Connection Point for {} {} is {}", nodeId,
803 connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName(),
804 port2.getPortName(), logicalConnectionPoint2);
805 portMapList.add(createMappingObject(nodeId, port2, connectionPortMap
806 .get(cpMapEntry.getKey()).get(1).getCircuitPackName(), logicalConnectionPoint2));
809 "Impossible to create logical connection point for port {} or port {} on node {} - Error in configuration with port-qual, port-direction or partner-port configuration",
810 port1.getPortName(), port2.getPortName(), nodeId);
813 LOG.error("No port {} on circuit pack {} for node {}",
814 connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName().toString(),
815 connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName(), nodeId);
821 LOG.error("Number of connection port for DEG{} on {} is incorrect", cpMapEntry.getKey(), nodeId);
828 private NodeInfo createNodeInfo(Info deviceInfo) {
829 NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder();
830 if (deviceInfo.getNodeType() != null) {
831 nodeInfoBldr.setOpenroadmVersion(OpenroadmVersion._121);
832 if (deviceInfo.getNodeType().getIntValue() == 1) {
833 nodeInfoBldr.setNodeType(NodeTypes.Rdm);
834 } else if (deviceInfo.getNodeType().getIntValue() == 2) {
835 nodeInfoBldr.setNodeType(NodeTypes.Xpdr);
837 LOG.error("Error with node-type of {}", deviceInfo.getNodeId());
839 if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
840 nodeInfoBldr.setNodeClli(deviceInfo.getClli());
842 nodeInfoBldr.setNodeClli("defaultCLLI");
844 if (deviceInfo.getModel() != null) {
845 nodeInfoBldr.setNodeModel(deviceInfo.getModel());
847 if (deviceInfo.getVendor() != null) {
848 nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
850 if (deviceInfo.getIpAddress() != null) {
851 nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
854 // TODO make mandatory in yang
855 LOG.error("Node type field is missing");
858 return nodeInfoBldr.build();
862 * Implements the FNV-1 64bit algorithm.
863 * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding.
864 * Otherwise, the hash cannot be stored in a UTF-8 string.
865 * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
866 * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
867 * @param stringdata the String to be hashed
868 * @return the base64 formatted hash string
870 private String fnv(String stringdata) {
871 BigInteger hash = FNV_INIT;
872 byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
874 for (byte b : data) {
875 hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
876 hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
879 return Base64.getEncoder().encodeToString(hash.toByteArray());