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 interfList.add(xc.get().getSource().getSrcIf());
124 interfList.add(xc.get().getDestination().getDstIf());
125 interfList.add(xc.get().getSource().getSrcIf().replace("nmc", "mc"));
126 interfList.add(xc.get().getDestination().getDstIf().replace("nmc", "mc"));
127 } else if (otnXc.isPresent()) {
128 interfList.add(otnXc.get().getSource().getSrcIf());
129 interfList.add(otnXc.get().getDestination().getDstIf());
131 LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
134 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
135 DeviceTransaction deviceTx;
137 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
138 if (deviceTxOpt.isPresent()) {
139 deviceTx = deviceTxOpt.get();
141 LOG.error("Device transaction for device {} was not found!", deviceId);
144 } catch (InterruptedException | ExecutionException e) {
145 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
149 // post the cross connect on the device
151 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateOduConnectionIID(connectionName));
153 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionName));
155 FluentFuture<? extends @NonNull CommitInfo> commit =
156 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
159 LOG.info("Connection {} successfully deleted on {}", connectionName, deviceId);
161 } catch (InterruptedException | ExecutionException e) {
162 LOG.warn("Failed to delete {}", connectionName, e);
168 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
169 throws OpenRoadmInterfaceException {
170 String connectionName = generateConnectionName(srcTp, destTp, waveNumber);
171 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
172 List<Ports> ports = null;
173 MountPoint mountPoint;
174 if (mountPointOpt.isPresent()) {
175 mountPoint = mountPointOpt.get();
177 LOG.error("Failed to obtain mount point for device {}!", nodeId);
178 return Collections.emptyList();
180 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
181 if (!service.isPresent()) {
182 LOG.error("Failed to get RpcService for node {}", nodeId);
184 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
185 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
186 portTrainInputBuilder.setConnectionName(connectionName);
187 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
188 portTrainInputBuilder.build());
189 if (portTrailOutput != null) {
191 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
192 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
193 if (connectionPortTrailOutput == null) {
194 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
195 + " node %s returned null!", nodeId));
197 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
198 ports = connectionPortTrailOutput.getPorts();
199 for (Ports port : ports) {
200 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
202 } catch (InterruptedException | ExecutionException e) {
203 LOG.warn("Exception caught", e);
206 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
208 return ports != null ? ports : Collections.emptyList();
212 public boolean setPowerLevel(String deviceId, OpticalControlMode mode, BigDecimal powerValue, String ctName) {
213 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctName);
214 if (rdmConnOpt.isPresent()) {
215 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
216 rdmConnBldr.setOpticalControlMode(mode);
217 if (powerValue != null) {
218 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
220 RoadmConnections newRdmConn = rdmConnBldr.build();
222 Future<Optional<DeviceTransaction>> deviceTxFuture =
223 deviceTransactionManager.getDeviceTransaction(deviceId);
224 DeviceTransaction deviceTx;
226 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
227 if (deviceTxOpt.isPresent()) {
228 deviceTx = deviceTxOpt.get();
230 LOG.error("Transaction for device {} was not found!", deviceId);
233 } catch (InterruptedException | ExecutionException e) {
234 LOG.error("Unable to get transaction for device {}!", deviceId, e);
238 // post the cross connect on the device
239 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
240 .child(RoadmConnections.class, new RoadmConnectionsKey(ctName));
241 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
242 FluentFuture<? extends @NonNull CommitInfo> commit =
243 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
246 LOG.info("Roadm connection power level successfully set ");
248 } catch (InterruptedException | ExecutionException ex) {
249 LOG.warn("Failed to post {}", newRdmConn, ex);
253 LOG.warn("Roadm-Connection is null in set power level ({})", ctName);
258 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
259 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
260 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
263 private InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
264 .container.org.openroadm.device.OduConnection> generateOduConnectionIID(String connectionNumber) {
265 return InstanceIdentifier.create(OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org
266 .openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnection.class,
267 new OduConnectionKey(connectionNumber));
270 private String generateConnectionName(String srcTp, String destTp, Long waveNumber) {
271 return srcTp + "-" + destTp + "-" + waveNumber;
274 public Optional<String> postOtnCrossConnect(List<String> createdOduInterfaces, Nodes node) {
275 String deviceId = node.getNodeId();
276 String srcTp = createdOduInterfaces.get(0);
277 String dstTp = createdOduInterfaces.get(1);
278 OduConnectionBuilder oduConnectionBuilder = new OduConnectionBuilder()
279 .setConnectionName(srcTp + "-x-" + dstTp)
280 .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
281 .DestinationBuilder().setDstIf(dstTp).build())
282 .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
283 .SourceBuilder().setSrcIf(srcTp).build())
284 .setDirection(Direction.Bidirectional);
286 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
287 .container.org.openroadm.device.OduConnection> oduConnectionIID =
288 InstanceIdentifier.create(OrgOpenroadmDevice.class)
289 .child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
290 .container.org.openroadm.device.OduConnection.class,
291 new OduConnectionKey(oduConnectionBuilder.getConnectionName())
294 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
295 DeviceTransaction deviceTx;
297 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
298 if (deviceTxOpt.isPresent()) {
299 deviceTx = deviceTxOpt.get();
301 LOG.error("Device transaction for device {} was not found!", deviceId);
302 return Optional.empty();
304 } catch (InterruptedException | ExecutionException e) {
305 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
306 return Optional.empty();
309 // post the cross connect on the device
310 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, oduConnectionIID, oduConnectionBuilder.build());
311 FluentFuture<? extends @NonNull CommitInfo> commit =
312 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
315 LOG.info("Otn-connection successfully created: {}-{}", srcTp, dstTp);
316 return Optional.of(srcTp + "-x-" + dstTp);
317 } catch (InterruptedException | ExecutionException e) {
318 LOG.warn("Failed to post {}.", oduConnectionBuilder.build(), e);
320 return Optional.empty();