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.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.opendaylight.controller.md.sal.binding.api.MountPoint;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
21 import org.opendaylight.transportpce.common.Timeouts;
22 import org.opendaylight.transportpce.common.device.DeviceTransaction;
23 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
24 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
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 CrossConnectImpl121 {
43 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
45 private final DeviceTransactionManager deviceTransactionManager;
47 public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
48 this.deviceTransactionManager = deviceTransactionManager;
51 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
52 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
53 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
54 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
57 public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
58 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
59 String connectionNumber = generateConnectionNumber(srcTp, destTp, waveNumber);
60 rdmConnBldr.setConnectionNumber(connectionNumber);
61 rdmConnBldr.setWavelengthNumber(waveNumber);
62 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
63 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
64 rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
65 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
66 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
68 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
69 DeviceTransaction deviceTx;
71 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
72 if (deviceTxOpt.isPresent()) {
73 deviceTx = deviceTxOpt.get();
75 LOG.error("Device transaction for device {} was not found!", deviceId);
76 return Optional.empty();
78 } catch (InterruptedException | ExecutionException e) {
79 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
80 return Optional.empty();
83 // post the cross connect on the device
84 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
85 ListenableFuture<Void> submit =
86 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
89 LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
90 return Optional.of(connectionNumber);
91 } catch (InterruptedException | ExecutionException e) {
92 LOG.warn("Failed to post {}. Exception: {}", rdmConnBldr.build(), e);
94 return Optional.empty();
97 public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
98 List<String> interfList = new ArrayList<>();
99 Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
100 //Check if cross connect exists before delete
101 if (xc.isPresent()) {
102 interfList.add(xc.get().getSource().getSrcIf());
103 interfList.add(xc.get().getDestination().getDstIf());
105 LOG.warn("Cross connect does not exist, halting delete");
108 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
109 DeviceTransaction deviceTx;
111 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
112 if (deviceTxOpt.isPresent()) {
113 deviceTx = deviceTxOpt.get();
115 LOG.error("Device transaction for device {} was not found!", deviceId);
118 } catch (InterruptedException | ExecutionException e) {
119 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
123 // post the cross connect on the device
124 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
125 ListenableFuture<Void> submit =
126 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
129 LOG.info("Roadm connection successfully deleted ");
131 } catch (InterruptedException | ExecutionException e) {
132 LOG.warn("Failed to delete {}", connectionNumber, e);
138 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
139 throws OpenRoadmInterfaceException {
140 String connectionName = generateConnectionNumber(srcTp, destTp, waveNumber);
141 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
142 List<Ports> ports = null;
143 MountPoint mountPoint;
144 if (mountPointOpt.isPresent()) {
145 mountPoint = mountPointOpt.get();
147 LOG.error("Failed to obtain mount point for device {}!", nodeId);
148 return Collections.emptyList();
150 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
151 if (!service.isPresent()) {
152 LOG.error("Failed to get RpcService for node {}", nodeId);
154 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
155 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
156 portTrainInputBuilder.setConnectionNumber(connectionName);
157 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
158 portTrainInputBuilder.build());
159 if (portTrailOutput != null) {
161 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
162 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
163 if (connectionPortTrailOutput == null) {
164 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
165 + " node %s returned null!", nodeId));
167 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
168 ports = connectionPortTrailOutput.getPorts();
169 for (Ports port : ports) {
170 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
172 } catch (InterruptedException | ExecutionException e) {
173 LOG.warn("Exception caught", e);
176 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
178 return ports != null ? ports : Collections.emptyList();
181 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
182 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
183 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
186 private String generateConnectionNumber(String srcTp, String destTp, Long waveNumber) {
187 return srcTp + "-" + destTp + "-" + waveNumber;
190 public boolean setPowerLevel(String deviceId, Enum mode, BigDecimal powerValue, String connectionNumber) {
192 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, connectionNumber);
193 if (rdmConnOpt.isPresent()) {
194 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
195 rdmConnBldr.setOpticalControlMode(OpticalControlMode.class.cast(mode));
196 if (powerValue != null) {
197 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
199 RoadmConnections newRdmConn = rdmConnBldr.build();
201 Future<Optional<DeviceTransaction>> deviceTxFuture =
202 deviceTransactionManager.getDeviceTransaction(deviceId);
203 DeviceTransaction deviceTx;
205 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
206 if (deviceTxOpt.isPresent()) {
207 deviceTx = deviceTxOpt.get();
209 LOG.error("Transaction for device {} was not found!", deviceId);
212 } catch (InterruptedException | ExecutionException e) {
213 LOG.error("Unable to get transaction for device {}!", deviceId, e);
217 // post the cross connect on the device
218 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
219 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
220 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
221 ListenableFuture<Void> submit = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
222 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
225 LOG.info("Roadm connection power level successfully set ");
227 } catch (InterruptedException | ExecutionException ex) {
228 LOG.warn("Failed to post {}", newRdmConn, ex);
232 LOG.warn("Roadm-Connection is null in set power level ({})", connectionNumber);