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
8 package org.opendaylight.transportpce.common.crossconnect;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.math.BigDecimal;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.transportpce.common.device.DeviceTransaction;
22 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
23 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class CrossConnectImpl121 {
42 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
44 private final DeviceTransactionManager deviceTransactionManager;
46 public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
47 this.deviceTransactionManager = deviceTransactionManager;
50 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
51 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
52 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
53 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
56 public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
57 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
58 String connectionNumber = generateConnectionNumber(srcTp, destTp, waveNumber);
59 rdmConnBldr.setConnectionNumber(connectionNumber);
60 rdmConnBldr.setWavelengthNumber(waveNumber);
61 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
62 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
63 rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
64 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
65 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
67 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
68 DeviceTransaction deviceTx;
70 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
71 if (deviceTxOpt.isPresent()) {
72 deviceTx = deviceTxOpt.get();
74 LOG.error("Device transaction for device {} was not found!", deviceId);
75 return Optional.empty();
77 } catch (InterruptedException | ExecutionException e) {
78 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
79 return Optional.empty();
82 // post the cross connect on the device
83 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
84 ListenableFuture<Void> submit =
85 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
88 LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
89 return Optional.of(connectionNumber);
90 } catch (InterruptedException | ExecutionException e) {
91 LOG.warn("Failed to post {}. Exception: {}", rdmConnBldr.build(), e);
93 return Optional.empty();
96 public boolean deleteCrossConnect(String deviceId, String connectionNumber) {
97 //Check if cross connect exists before delete
98 if (!getCrossConnect(deviceId, connectionNumber).isPresent()) {
99 LOG.warn("Cross connect does not exist, halting delete");
102 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
103 DeviceTransaction deviceTx;
105 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
106 if (deviceTxOpt.isPresent()) {
107 deviceTx = deviceTxOpt.get();
109 LOG.error("Device transaction for device {} was not found!", deviceId);
112 } catch (InterruptedException | ExecutionException e) {
113 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
117 // post the cross connect on the device
118 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
119 ListenableFuture<Void> submit =
120 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
123 LOG.info("Roadm connection successfully deleted ");
125 } catch (InterruptedException | ExecutionException e) {
126 LOG.warn("Failed to delete {}", connectionNumber, e);
132 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
133 throws OpenRoadmInterfaceException {
134 String connectionName = generateConnectionNumber(srcTp, destTp, waveNumber);
135 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
136 List<Ports> ports = null;
137 MountPoint mountPoint;
138 if (mountPointOpt.isPresent()) {
139 mountPoint = mountPointOpt.get();
141 LOG.error("Failed to obtain mount point for device {}!", nodeId);
142 return Collections.emptyList();
144 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
145 if (!service.isPresent()) {
146 LOG.error("Failed to get RpcService for node {}", nodeId);
148 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
149 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
150 portTrainInputBuilder.setConnectionNumber(connectionName);
151 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
152 portTrainInputBuilder.build());
153 if (portTrailOutput != null) {
155 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
156 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
157 if (connectionPortTrailOutput == null) {
158 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
159 + " node %s returned null!", nodeId));
161 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
162 ports = connectionPortTrailOutput.getPorts();
163 for (Ports port : ports) {
164 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
166 } catch (InterruptedException | ExecutionException e) {
167 LOG.warn("Exception caught", e);
170 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
172 return ports != null ? ports : Collections.emptyList();
175 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
176 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
177 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
180 private String generateConnectionNumber(String srcTp, String destTp, Long waveNumber) {
181 return srcTp + "-" + destTp + "-" + waveNumber;
184 public boolean setPowerLevel(String deviceId, Enum mode, BigDecimal powerValue, String connectionNumber) {
186 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, connectionNumber);
187 if (rdmConnOpt.isPresent()) {
188 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
189 rdmConnBldr.setOpticalControlMode(OpticalControlMode.class.cast(mode));
190 if (powerValue != null) {
191 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
193 RoadmConnections newRdmConn = rdmConnBldr.build();
195 Future<Optional<DeviceTransaction>> deviceTxFuture =
196 deviceTransactionManager.getDeviceTransaction(deviceId);
197 DeviceTransaction deviceTx;
199 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
200 if (deviceTxOpt.isPresent()) {
201 deviceTx = deviceTxOpt.get();
203 LOG.error("Transaction for device {} was not found!", deviceId);
206 } catch (InterruptedException | ExecutionException e) {
207 LOG.error("Unable to get transaction for device {}!", deviceId, e);
211 // post the cross connect on the device
212 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
213 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
214 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
215 ListenableFuture<Void> submit = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
216 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
219 LOG.info("Roadm connection power level successfully set ");
221 } catch (InterruptedException | ExecutionException ex) {
222 LOG.warn("Failed to post {}", newRdmConn, ex);
226 LOG.warn("Roadm-Connection is null in set power level ({})", connectionNumber);