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.FluentFuture;
11 import java.math.BigDecimal;
12 import java.util.ArrayList;
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;
18 import org.eclipse.jdt.annotation.NonNull;
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.mdsal.common.api.CommitInfo;
23 import org.opendaylight.transportpce.common.Timeouts;
24 import org.opendaylight.transportpce.common.device.DeviceTransaction;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class CrossConnectImpl121 {
45 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
47 private final DeviceTransactionManager deviceTransactionManager;
49 public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
50 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);
59 public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
60 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
61 String connectionNumber = generateConnectionNumber(srcTp, destTp, waveNumber);
62 rdmConnBldr.setConnectionNumber(connectionNumber);
63 rdmConnBldr.setWavelengthNumber(waveNumber);
64 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
65 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
66 rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
67 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
68 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
70 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
71 DeviceTransaction deviceTx;
73 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
74 if (deviceTxOpt.isPresent()) {
75 deviceTx = deviceTxOpt.get();
77 LOG.error("Device transaction for device {} was not found!", deviceId);
78 return Optional.empty();
80 } catch (InterruptedException | ExecutionException e) {
81 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
82 return Optional.empty();
85 // post the cross connect on the device
86 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
87 FluentFuture<? extends @NonNull CommitInfo> commit =
88 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
91 LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
92 return Optional.of(connectionNumber);
93 } catch (InterruptedException | ExecutionException e) {
94 LOG.warn("Failed to post {}. Exception: {}", rdmConnBldr.build(), e);
96 return Optional.empty();
99 public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
100 List<String> interfList = new ArrayList<>();
101 Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
102 //Check if cross connect exists before delete
103 if (xc.isPresent()) {
104 interfList.add(xc.get().getSource().getSrcIf());
105 interfList.add(xc.get().getDestination().getDstIf());
107 LOG.warn("Cross connect does not exist, halting delete");
110 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
111 DeviceTransaction deviceTx;
113 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
114 if (deviceTxOpt.isPresent()) {
115 deviceTx = deviceTxOpt.get();
117 LOG.error("Device transaction for device {} was not found!", deviceId);
120 } catch (InterruptedException | ExecutionException e) {
121 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
125 // post the cross connect on the device
126 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
127 FluentFuture<? extends @NonNull CommitInfo> commit =
128 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
131 LOG.info("Roadm connection successfully deleted ");
133 } catch (InterruptedException | ExecutionException e) {
134 LOG.warn("Failed to delete {}", connectionNumber, e);
140 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
141 throws OpenRoadmInterfaceException {
142 String connectionName = generateConnectionNumber(srcTp, destTp, waveNumber);
143 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
144 List<Ports> ports = null;
145 MountPoint mountPoint;
146 if (mountPointOpt.isPresent()) {
147 mountPoint = mountPointOpt.get();
149 LOG.error("Failed to obtain mount point for device {}!", nodeId);
150 return Collections.emptyList();
152 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
153 if (!service.isPresent()) {
154 LOG.error("Failed to get RpcService for node {}", nodeId);
156 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
157 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
158 portTrainInputBuilder.setConnectionNumber(connectionName);
159 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
160 portTrainInputBuilder.build());
161 if (portTrailOutput != null) {
163 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
164 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
165 if (connectionPortTrailOutput == null) {
166 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
167 + " node %s returned null!", nodeId));
169 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
170 ports = connectionPortTrailOutput.getPorts();
171 for (Ports port : ports) {
172 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
174 } catch (InterruptedException | ExecutionException e) {
175 LOG.warn("Exception caught", e);
178 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
180 return ports != null ? ports : Collections.emptyList();
183 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
184 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
185 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
188 private String generateConnectionNumber(String srcTp, String destTp, Long waveNumber) {
189 return srcTp + "-" + destTp + "-" + waveNumber;
192 public boolean setPowerLevel(String deviceId, Enum mode, BigDecimal powerValue, String connectionNumber) {
194 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, connectionNumber);
195 if (rdmConnOpt.isPresent()) {
196 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
197 rdmConnBldr.setOpticalControlMode(OpticalControlMode.class.cast(mode));
198 if (powerValue != null) {
199 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
201 RoadmConnections newRdmConn = rdmConnBldr.build();
203 Future<Optional<DeviceTransaction>> deviceTxFuture =
204 deviceTransactionManager.getDeviceTransaction(deviceId);
205 DeviceTransaction deviceTx;
207 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
208 if (deviceTxOpt.isPresent()) {
209 deviceTx = deviceTxOpt.get();
211 LOG.error("Transaction for device {} was not found!", deviceId);
214 } catch (InterruptedException | ExecutionException e) {
215 LOG.error("Unable to get transaction for device {}!", deviceId, e);
219 // post the cross connect on the device
220 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
221 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
222 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
223 FluentFuture<? extends @NonNull CommitInfo> commit =
224 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
227 LOG.info("Roadm connection power level successfully set ");
229 } catch (InterruptedException | ExecutionException ex) {
230 LOG.warn("Failed to post {}", newRdmConn, ex);
234 LOG.warn("Roadm-Connection is null in set power level ({})", connectionNumber);