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.crossconnect;
11 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
19 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.transportpce.common.Timeouts;
23 import org.opendaylight.transportpce.common.device.DeviceTransaction;
24 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
25 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class CrossConnectImpl implements CrossConnect {
44 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl.class);
46 private final DeviceTransactionManager deviceTransactionManager;
48 public CrossConnectImpl(DeviceTransactionManager deviceTransactionManager) {
49 this.deviceTransactionManager = deviceTransactionManager;
53 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
54 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
55 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
56 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
60 public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
61 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
62 String connectionNumber = generateConnectionNumber(srcTp, destTp, waveNumber);
63 rdmConnBldr.setConnectionNumber(connectionNumber);
64 rdmConnBldr.setWavelengthNumber(waveNumber);
65 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
66 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
67 rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
68 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
69 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
71 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
72 DeviceTransaction deviceTx;
74 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
75 if (deviceTxOpt.isPresent()) {
76 deviceTx = deviceTxOpt.get();
78 LOG.error("Device transaction for device {} was not found!", deviceId);
79 return Optional.empty();
81 } catch (InterruptedException | ExecutionException e) {
82 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
83 return Optional.empty();
86 // post the cross connect on the device
87 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
88 ListenableFuture<Void> submit =
89 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
92 LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
93 return Optional.of(connectionNumber);
94 } catch (InterruptedException | ExecutionException e) {
95 LOG.warn("Failed to post {}. Exception: {}", rdmConnBldr.build(), e);
97 return Optional.empty();
101 public boolean deleteCrossConnect(String deviceId, String connectionNumber) {
102 //Check if cross connect exists before delete
103 if (!getCrossConnect(deviceId, connectionNumber).isPresent()) {
104 LOG.warn("Cross connect does not exist, halting delete");
107 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
108 DeviceTransaction deviceTx;
110 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
111 if (deviceTxOpt.isPresent()) {
112 deviceTx = deviceTxOpt.get();
114 LOG.error("Device transaction for device {} was not found!", deviceId);
117 } catch (InterruptedException | ExecutionException e) {
118 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
122 // post the cross connect on the device
123 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
124 ListenableFuture<Void> submit =
125 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
128 LOG.info("Roadm connection successfully deleted ");
130 } catch (InterruptedException | ExecutionException e) {
131 LOG.warn("Failed to delete {}", connectionNumber, e);
137 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
138 throws OpenRoadmInterfaceException {
139 String connectionName = generateConnectionNumber(srcTp, destTp, waveNumber);
140 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
141 List<Ports> ports = null;
142 MountPoint mountPoint;
143 if (mountPointOpt.isPresent()) {
144 mountPoint = mountPointOpt.get();
146 LOG.error("Failed to obtain mount point for device {}!", nodeId);
147 return Collections.emptyList();
149 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
150 if (!service.isPresent()) {
151 LOG.error("Failed to get RpcService for node {}", nodeId);
153 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
154 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
155 portTrainInputBuilder.setConnectionNumber(connectionName);
156 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
157 portTrainInputBuilder.build());
158 if (portTrailOutput != null) {
160 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
161 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
162 if (connectionPortTrailOutput == null) {
163 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
164 + " node %s returned null!", nodeId));
166 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
167 ports = connectionPortTrailOutput.getPorts();
168 for (Ports port : ports) {
169 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
171 } catch (InterruptedException | ExecutionException e) {
172 LOG.warn("Exception caught", e);
175 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
177 return ports != null ? ports : Collections.emptyList();
180 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
181 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
182 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
185 private String generateConnectionNumber(String srcTp, String destTp, Long waveNumber) {
186 return srcTp + "-" + destTp + "-" + waveNumber;