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;
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.transportpce.common.Timeouts;
23 import org.opendaylight.transportpce.common.device.DeviceTransaction;
24 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
25 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.OpticalControlMode;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.PowerDBm;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrailInputBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrailOutput;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceService;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.connection.DestinationBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.connection.SourceBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.get.connection.port.trail.output.Ports;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class CrossConnectImpl221 {
45 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl221.class);
46 private final DeviceTransactionManager deviceTransactionManager;
48 public CrossConnectImpl221(DeviceTransactionManager deviceTransactionManager) {
49 this.deviceTransactionManager = deviceTransactionManager;
52 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
53 //TODO Change it to Operational later for real device
54 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.CONFIGURATION,
55 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
56 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
60 public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
61 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
62 String connectionNumber = generateConnectionName(srcTp, destTp, waveNumber);
63 rdmConnBldr.setConnectionName(connectionNumber);
65 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
67 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-nmc-" + waveNumber).build());
69 rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-nmc-" + waveNumber).build());
72 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
73 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()));
75 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
76 DeviceTransaction deviceTx;
78 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
79 if (deviceTxOpt.isPresent()) {
80 deviceTx = deviceTxOpt.get();
82 LOG.error("Device transaction for device {} was not found!", deviceId);
83 return Optional.empty();
85 } catch (InterruptedException | ExecutionException e) {
86 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
87 return Optional.empty();
90 // post the cross connect on the device
91 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
92 ListenableFuture<Void> submit =
93 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
96 LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
97 return Optional.of(connectionNumber);
98 } catch (InterruptedException | ExecutionException e) {
99 LOG.warn("Failed to post {}. Exception: {}", rdmConnBldr.build(), e);
101 return Optional.empty();
105 public List<String> deleteCrossConnect(String deviceId, String connectionName) {
106 List<String> interfList = new ArrayList<>();
107 Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionName);
108 //Check if cross connect exists before delete
109 if (xc.isPresent()) {
110 String name = xc.get().getSource().getSrcIf();
111 name.replace("nmc", "mc");
112 interfList.add(xc.get().getSource().getSrcIf());
113 interfList.add(xc.get().getDestination().getDstIf());
114 interfList.add(xc.get().getSource().getSrcIf().replace("nmc", "mc"));
115 interfList.add(xc.get().getDestination().getDstIf().replace("nmc", "mc"));
117 LOG.warn("Cross connect does not exist, halting delete");
120 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
121 DeviceTransaction deviceTx;
123 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
124 if (deviceTxOpt.isPresent()) {
125 deviceTx = deviceTxOpt.get();
127 LOG.error("Device transaction for device {} was not found!", deviceId);
130 } catch (InterruptedException | ExecutionException e) {
131 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
135 // post the cross connect on the device
136 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionName));
137 ListenableFuture<Void> submit =
138 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
141 LOG.info("Roadm connection {} successfully deleted on {}", connectionName, deviceId);
143 } catch (InterruptedException | ExecutionException e) {
144 LOG.warn("Failed to delete {}", connectionName, e);
150 public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
151 throws OpenRoadmInterfaceException {
152 String connectionName = generateConnectionName(srcTp, destTp, waveNumber);
153 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
154 List<Ports> ports = null;
155 MountPoint mountPoint;
156 if (mountPointOpt.isPresent()) {
157 mountPoint = mountPointOpt.get();
159 LOG.error("Failed to obtain mount point for device {}!", nodeId);
160 return Collections.emptyList();
162 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
163 if (!service.isPresent()) {
164 LOG.error("Failed to get RpcService for node {}", nodeId);
166 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
167 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
168 portTrainInputBuilder.setConnectionName(connectionName);
169 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
170 portTrainInputBuilder.build());
171 if (portTrailOutput != null) {
173 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
174 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
175 if (connectionPortTrailOutput == null) {
176 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
177 + " node %s returned null!", nodeId));
179 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
180 ports = connectionPortTrailOutput.getPorts();
181 for (Ports port : ports) {
182 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
184 } catch (InterruptedException | ExecutionException e) {
185 LOG.warn("Exception caught", e);
188 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
190 return ports != null ? ports : Collections.emptyList();
194 public boolean setPowerLevel(String deviceId, Enum mode, BigDecimal powerValue, String connectionName) {
195 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, connectionName);
196 if (rdmConnOpt.isPresent()) {
197 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
198 rdmConnBldr.setOpticalControlMode(OpticalControlMode.values()[mode.ordinal()]);
199 if (powerValue != null) {
200 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
202 RoadmConnections newRdmConn = rdmConnBldr.build();
204 Future<Optional<DeviceTransaction>> deviceTxFuture =
205 deviceTransactionManager.getDeviceTransaction(deviceId);
206 DeviceTransaction deviceTx;
208 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
209 if (deviceTxOpt.isPresent()) {
210 deviceTx = deviceTxOpt.get();
212 LOG.error("Transaction for device {} was not found!", deviceId);
215 } catch (InterruptedException | ExecutionException e) {
216 LOG.error("Unable to get transaction for device {}!", deviceId, e);
220 // post the cross connect on the device
221 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
222 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionName));
223 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
224 ListenableFuture<Void> submit = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
225 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
228 LOG.info("Roadm connection power level successfully set ");
230 } catch (InterruptedException | ExecutionException ex) {
231 LOG.warn("Failed to post {}", newRdmConn, ex);
235 LOG.warn("Roadm-Connection is null in set power level ({})", connectionName);
240 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
241 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
242 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
245 private String generateConnectionName(String srcTp, String destTp, Long waveNumber) {
246 return srcTp + "-" + destTp + "-" + waveNumber;