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.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.api.MountPoint;
19 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.fixedflex.GridConstant;
26 import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
27 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceData;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.Decimal64;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class CrossConnectImpl121 {
48 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
50 private final DeviceTransactionManager deviceTransactionManager;
52 public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
53 this.deviceTransactionManager = deviceTransactionManager;
56 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
57 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
58 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
59 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
62 public Optional<String> postCrossConnect(String deviceId, String srcTp, String destTp,
63 SpectrumInformation spectrumInformation) {
64 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
65 String connectionNumber = spectrumInformation.getIdentifierFromParams(srcTp, destTp);
66 rdmConnBldr.setConnectionNumber(connectionNumber);
67 rdmConnBldr.setWavelengthNumber(spectrumInformation.getWaveLength());
68 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
69 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(spectrumInformation.getIdentifierFromParams(srcTp))
71 rdmConnBldr.setDestination(new DestinationBuilder()
72 .setDstIf(spectrumInformation.getIdentifierFromParams(destTp))
74 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier
75 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
76 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()))
79 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
80 DeviceTransaction deviceTx;
82 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
83 if (deviceTxOpt.isPresent()) {
84 deviceTx = deviceTxOpt.get();
86 LOG.error("Device transaction for device {} was not found!", deviceId);
87 return Optional.empty();
89 } catch (InterruptedException | ExecutionException e) {
90 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
91 return Optional.empty();
94 // post the cross connect on the device
95 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
96 FluentFuture<? extends @NonNull CommitInfo> commit =
97 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
100 LOG.info("Roadm-connection successfully created: {}-{}-{}-{}", srcTp, destTp,
101 spectrumInformation.getLowerSpectralSlotNumber(),
102 spectrumInformation.getHigherSpectralSlotNumber());
103 return Optional.of(connectionNumber);
104 } catch (InterruptedException | ExecutionException e) {
105 LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
107 return Optional.empty();
110 public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
111 List<String> interfList = new ArrayList<>();
112 Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
113 //Check if cross connect exists before delete
114 if (xc.isPresent()) {
115 interfList.add(xc.get().getSource().getSrcIf());
116 interfList.add(xc.get().getDestination().getDstIf());
118 LOG.warn("Cross connect does not exist, halting delete");
121 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
122 DeviceTransaction deviceTx;
124 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
125 if (deviceTxOpt.isPresent()) {
126 deviceTx = deviceTxOpt.get();
128 LOG.error("Device transaction for device {} was not found!", deviceId);
131 } catch (InterruptedException | ExecutionException e) {
132 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
136 // post the cross connect on the device
137 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
138 FluentFuture<? extends @NonNull CommitInfo> commit =
139 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
142 LOG.info("Roadm connection successfully deleted ");
144 } catch (InterruptedException | ExecutionException e) {
145 LOG.warn("Failed to delete {}", connectionNumber, e);
151 public List<Ports> getConnectionPortTrail(String nodeId, String srcTp, String destTp,
152 int lowerSpectralSlotNumber, int higherSpectralSlotNumber) throws OpenRoadmInterfaceException {
153 String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
154 String.valueOf(lowerSpectralSlotNumber), String.valueOf(higherSpectralSlotNumber));
155 String connectionName = generateConnectionNumber(srcTp, destTp, spectralSlotName);
156 Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
157 List<Ports> ports = null;
158 MountPoint mountPoint;
159 if (mountPointOpt.isPresent()) {
160 mountPoint = mountPointOpt.get();
162 LOG.error("Failed to obtain mount point for device {}!", nodeId);
163 return Collections.emptyList();
165 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
166 if (!service.isPresent()) {
167 LOG.error("Failed to get RpcService for node {}", nodeId);
169 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
170 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
171 portTrainInputBuilder.setConnectionNumber(connectionName);
172 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
173 portTrainInputBuilder.build());
174 if (portTrailOutput != null) {
176 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
177 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
178 if (connectionPortTrailOutput == null) {
179 throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
180 + " node %s returned null!", nodeId));
182 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
183 ports = connectionPortTrailOutput.getPorts();
184 for (Ports port : ports) {
185 LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
187 } catch (InterruptedException | ExecutionException e) {
188 LOG.warn("Exception caught", e);
191 LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
193 return ports != null ? ports : Collections.emptyList();
196 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
197 return InstanceIdentifier.builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
198 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber))
202 private String generateConnectionNumber(String srcTp, String destTp, String spectralSlotName) {
203 return String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, destTp, spectralSlotName);
206 public boolean setPowerLevel(String deviceId, OpticalControlMode mode, Decimal64 powerValue, String ctNumber) {
208 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctNumber);
209 if (rdmConnOpt.isPresent()) {
210 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get())
211 .setOpticalControlMode(mode);
212 if (powerValue != null) {
213 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
215 RoadmConnections newRdmConn = rdmConnBldr.build();
217 Future<Optional<DeviceTransaction>> deviceTxFuture =
218 deviceTransactionManager.getDeviceTransaction(deviceId);
219 DeviceTransaction deviceTx;
221 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
222 if (deviceTxOpt.isPresent()) {
223 deviceTx = deviceTxOpt.get();
225 LOG.error("Transaction for device {} was not found!", deviceId);
228 } catch (InterruptedException | ExecutionException e) {
229 LOG.error("Unable to get transaction for device {}!", deviceId, e);
233 // post the cross connect on the device
234 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier
235 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
236 .child(RoadmConnections.class, new RoadmConnectionsKey(ctNumber))
238 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
239 FluentFuture<? extends @NonNull CommitInfo> commit =
240 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
243 LOG.info("Roadm connection power level successfully set ");
245 } catch (InterruptedException | ExecutionException ex) {
246 LOG.warn("Failed to post {}", newRdmConn, ex);
250 LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);