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;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.api.MountPoint;
21 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.transportpce.common.Timeouts;
25 import org.opendaylight.transportpce.common.device.DeviceTransaction;
26 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
27 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.OpticalControlMode;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.PowerDBm;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrailInputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrailOutput;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OduConnection.Direction;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceService;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.connection.DestinationBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.connection.SourceBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.get.connection.port.trail.output.Ports;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnectionBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnectionKey;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200128.otn.renderer.input.Nodes;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class CrossConnectImpl221 {
51 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl221.class);
52 private final DeviceTransactionManager deviceTransactionManager;
54 public CrossConnectImpl221(DeviceTransactionManager deviceTransactionManager) {
55 this.deviceTransactionManager = deviceTransactionManager;
58 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
59 //TODO Change it to Operational later for real device
60 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.CONFIGURATION,
61 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
62 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
65 public Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
66 .openroadm.device.OduConnection> getOtnCrossConnect(String deviceId, String connectionNumber) {
67 //TODO Change it to Operational later for real device
68 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.CONFIGURATION,
69 generateOduConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
70 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
73 public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
74 String connectionNumber = generateConnectionName(srcTp, destTp, waveNumber);
75 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder()
76 .setConnectionName(connectionNumber)
77 .setOpticalControlMode(OpticalControlMode.Off)
78 .setSource(new SourceBuilder().setSrcIf(srcTp + "-nmc-" + waveNumber).build())
79 .setDestination(new DestinationBuilder().setDstIf(destTp + "-nmc-" + waveNumber)
82 InstanceIdentifier<RoadmConnections> rdmConnectionIID =
83 InstanceIdentifier.create(OrgOpenroadmDevice.class)
84 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()));
86 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
87 DeviceTransaction deviceTx;
89 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
90 if (deviceTxOpt.isPresent()) {
91 deviceTx = deviceTxOpt.get();
93 LOG.error("Device transaction for device {} was not found!", deviceId);
94 return Optional.empty();
96 } catch (InterruptedException | ExecutionException e) {
97 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
98 return Optional.empty();
101 // post the cross connect on the device
102 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
103 FluentFuture<? extends @NonNull CommitInfo> commit =
104 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
107 LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
108 return Optional.of(connectionNumber);
109 } catch (InterruptedException | ExecutionException e) {
110 LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
112 return Optional.empty();
116 public List<String> deleteCrossConnect(String deviceId, String connectionName, boolean isOtn) {
117 List<String> interfList = new ArrayList<>();
118 Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionName);
119 Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
120 .openroadm.device.OduConnection> otnXc = getOtnCrossConnect(deviceId, connectionName);
121 //Check if cross connect exists before delete
122 if (xc.isPresent()) {
123 String name = xc.get().getSource().getSrcIf().replace("nmc", "mc");
124 interfList.add(xc.get().getSource().getSrcIf());
125 interfList.add(xc.get().getDestination().getDstIf());
126 interfList.add(xc.get().getSource().getSrcIf().replace("nmc", "mc"));
127 interfList.add(xc.get().getDestination().getDstIf().replace("nmc", "mc"));
128 } else if (otnXc.isPresent()) {
129 interfList.add(otnXc.get().getSource().getSrcIf());
130 interfList.add(otnXc.get().getDestination().getDstIf());
132 LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
135 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
136 DeviceTransaction deviceTx;
138 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
139 if (deviceTxOpt.isPresent()) {
140 deviceTx = deviceTxOpt.get();
142 LOG.error("Device transaction for device {} was not found!", deviceId);
145 } catch (InterruptedException | ExecutionException e) {
146 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
150 // post the cross connect on the device
152 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateOduConnectionIID(connectionName));
154 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionName));
156 FluentFuture<? extends @NonNull CommitInfo> commit =
157 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
160 LOG.info("Connection {} successfully deleted on {}", connectionName, deviceId);
162 } catch (InterruptedException | ExecutionException e) {
163 LOG.warn("Failed to delete {}", connectionName, e);
169 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
170 throws OpenRoadmInterfaceException {
171 String connectionName = generateConnectionName(srcTp, destTp, waveNumber);
172 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
173 List<Ports> ports = null;
174 MountPoint mountPoint;
175 if (mountPointOpt.isPresent()) {
176 mountPoint = mountPointOpt.get();
178 LOG.error("Failed to obtain mount point for device {}!", nodeId);
179 return Collections.emptyList();
181 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
182 if (!service.isPresent()) {
183 LOG.error("Failed to get RpcService for node {}", nodeId);
185 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
186 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
187 portTrainInputBuilder.setConnectionName(connectionName);
188 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
189 portTrainInputBuilder.build());
190 if (portTrailOutput != null) {
192 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
193 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
194 if (connectionPortTrailOutput == null) {
195 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
196 + " node %s returned null!", nodeId));
198 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
199 ports = connectionPortTrailOutput.getPorts();
200 for (Ports port : ports) {
201 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
203 } catch (InterruptedException | ExecutionException e) {
204 LOG.warn("Exception caught", e);
207 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
209 return ports != null ? ports : Collections.emptyList();
213 public boolean setPowerLevel(String deviceId, Enum mode, BigDecimal powerValue, String connectionName) {
214 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, connectionName);
215 if (rdmConnOpt.isPresent()) {
216 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
217 rdmConnBldr.setOpticalControlMode(OpticalControlMode.values()[mode.ordinal()]);
218 if (powerValue != null) {
219 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
221 RoadmConnections newRdmConn = rdmConnBldr.build();
223 Future<Optional<DeviceTransaction>> deviceTxFuture =
224 deviceTransactionManager.getDeviceTransaction(deviceId);
225 DeviceTransaction deviceTx;
227 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
228 if (deviceTxOpt.isPresent()) {
229 deviceTx = deviceTxOpt.get();
231 LOG.error("Transaction for device {} was not found!", deviceId);
234 } catch (InterruptedException | ExecutionException e) {
235 LOG.error("Unable to get transaction for device {}!", deviceId, e);
239 // post the cross connect on the device
240 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
241 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionName));
242 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
243 FluentFuture<? extends @NonNull CommitInfo> commit =
244 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
247 LOG.info("Roadm connection power level successfully set ");
249 } catch (InterruptedException | ExecutionException ex) {
250 LOG.warn("Failed to post {}", newRdmConn, ex);
254 LOG.warn("Roadm-Connection is null in set power level ({})", connectionName);
259 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
260 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
261 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
264 private InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
265 .container.org.openroadm.device.OduConnection> generateOduConnectionIID(String connectionNumber) {
266 return InstanceIdentifier.create(OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org
267 .openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnection.class,
268 new OduConnectionKey(connectionNumber));
271 private String generateConnectionName(String srcTp, String destTp, Long waveNumber) {
272 return srcTp + "-" + destTp + "-" + waveNumber;
275 public Optional<String> postOtnCrossConnect(List<String> createdOduInterfaces, Nodes node) {
276 String deviceId = node.getNodeId();
277 String srcTp = createdOduInterfaces.get(0);
278 String dstTp = createdOduInterfaces.get(1);
279 OduConnectionBuilder oduConnectionBuilder = new OduConnectionBuilder()
280 .setConnectionName(srcTp + "-x-" + dstTp)
281 .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
282 .DestinationBuilder().setDstIf(dstTp).build())
283 .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
284 .SourceBuilder().setSrcIf(srcTp).build())
285 .setDirection(Direction.Bidirectional);
287 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
288 .container.org.openroadm.device.OduConnection> oduConnectionIID =
289 InstanceIdentifier.create(OrgOpenroadmDevice.class)
290 .child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
291 .container.org.openroadm.device.OduConnection.class,
292 new OduConnectionKey(oduConnectionBuilder.getConnectionName())
295 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
296 DeviceTransaction deviceTx;
298 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
299 if (deviceTxOpt.isPresent()) {
300 deviceTx = deviceTxOpt.get();
302 LOG.error("Device transaction for device {} was not found!", deviceId);
303 return Optional.empty();
305 } catch (InterruptedException | ExecutionException e) {
306 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
307 return Optional.empty();
310 // post the cross connect on the device
311 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, oduConnectionIID, oduConnectionBuilder.build());
312 FluentFuture<? extends @NonNull CommitInfo> commit =
313 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
316 LOG.info("Otn-connection successfully created: {}-{}", srcTp, dstTp);
317 return Optional.of(srcTp + "-x-" + dstTp);
318 } catch (InterruptedException | ExecutionException e) {
319 LOG.warn("Failed to post {}. Exception: {}", oduConnectionBuilder.build(), e);
321 return Optional.empty();