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;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.api.MountPoint;
20 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.transportpce.common.Timeouts;
24 import org.opendaylight.transportpce.common.device.DeviceTransaction;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
27 import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
28 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
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.RpcResult;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class CrossConnectImpl121 {
47 private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
49 private final DeviceTransactionManager deviceTransactionManager;
51 public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
52 this.deviceTransactionManager = deviceTransactionManager;
55 public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
56 return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
57 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
58 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
61 public Optional<String> postCrossConnect(String deviceId, String srcTp, String destTp,
62 SpectrumInformation spectrumInformation) {
63 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
64 String connectionNumber = spectrumInformation.getIdentifierFromParams(srcTp, destTp);
65 rdmConnBldr.setConnectionNumber(connectionNumber);
66 rdmConnBldr.setWavelengthNumber(spectrumInformation.getWaveLength());
67 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
68 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(spectrumInformation.getIdentifierFromParams(srcTp))
70 rdmConnBldr.setDestination(new DestinationBuilder()
71 .setDstIf(spectrumInformation.getIdentifierFromParams(destTp))
73 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
74 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
76 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
77 DeviceTransaction deviceTx;
79 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
80 if (deviceTxOpt.isPresent()) {
81 deviceTx = deviceTxOpt.get();
83 LOG.error("Device transaction for device {} was not found!", deviceId);
84 return Optional.empty();
86 } catch (InterruptedException | ExecutionException e) {
87 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
88 return Optional.empty();
91 // post the cross connect on the device
92 deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
93 FluentFuture<? extends @NonNull CommitInfo> commit =
94 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
97 LOG.info("Roadm-connection successfully created: {}-{}-{}-{}", srcTp, destTp,
98 spectrumInformation.getLowerSpectralSlotNumber(),
99 spectrumInformation.getHigherSpectralSlotNumber());
100 return Optional.of(connectionNumber);
101 } catch (InterruptedException | ExecutionException e) {
102 LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
104 return Optional.empty();
107 public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
108 List<String> interfList = new ArrayList<>();
109 Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
110 //Check if cross connect exists before delete
111 if (xc.isPresent()) {
112 interfList.add(xc.get().getSource().getSrcIf());
113 interfList.add(xc.get().getDestination().getDstIf());
115 LOG.warn("Cross connect does not exist, halting delete");
118 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
119 DeviceTransaction deviceTx;
121 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
122 if (deviceTxOpt.isPresent()) {
123 deviceTx = deviceTxOpt.get();
125 LOG.error("Device transaction for device {} was not found!", deviceId);
128 } catch (InterruptedException | ExecutionException e) {
129 LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
133 // post the cross connect on the device
134 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
135 FluentFuture<? extends @NonNull CommitInfo> commit =
136 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
139 LOG.info("Roadm connection successfully deleted ");
141 } catch (InterruptedException | ExecutionException e) {
142 LOG.warn("Failed to delete {}", connectionNumber, e);
148 public List<Ports> getConnectionPortTrail(String nodeId, String srcTp, String destTp,
149 int lowerSpectralSlotNumber, int higherSpectralSlotNumber) throws OpenRoadmInterfaceException {
150 String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
151 String.valueOf(lowerSpectralSlotNumber), String.valueOf(higherSpectralSlotNumber));
152 String connectionName = generateConnectionNumber(srcTp, destTp, spectralSlotName);
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);
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.setConnectionNumber(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();
193 private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
194 return InstanceIdentifier.create(OrgOpenroadmDevice.class)
195 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
198 private String generateConnectionNumber(String srcTp, String destTp, String spectralSlotName) {
199 return String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, destTp, spectralSlotName);
202 public boolean setPowerLevel(String deviceId, OpticalControlMode mode, BigDecimal powerValue, String ctNumber) {
204 Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctNumber);
205 if (rdmConnOpt.isPresent()) {
206 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get())
207 .setOpticalControlMode(mode);
208 if (powerValue != null) {
209 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
211 RoadmConnections newRdmConn = rdmConnBldr.build();
213 Future<Optional<DeviceTransaction>> deviceTxFuture =
214 deviceTransactionManager.getDeviceTransaction(deviceId);
215 DeviceTransaction deviceTx;
217 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
218 if (deviceTxOpt.isPresent()) {
219 deviceTx = deviceTxOpt.get();
221 LOG.error("Transaction for device {} was not found!", deviceId);
224 } catch (InterruptedException | ExecutionException e) {
225 LOG.error("Unable to get transaction for device {}!", deviceId, e);
229 // post the cross connect on the device
230 InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
231 .child(RoadmConnections.class, new RoadmConnectionsKey(ctNumber));
232 deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
233 FluentFuture<? extends @NonNull CommitInfo> commit =
234 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
237 LOG.info("Roadm connection power level successfully set ");
239 } catch (InterruptedException | ExecutionException ex) {
240 LOG.warn("Failed to post {}", newRdmConn, ex);
244 LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);