Bump upstream dependencies to Ca
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl121.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.common.crossconnect;
9
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.RpcService;
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.GetConnectionPortTrail;
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.OrgOpenroadmDeviceData;
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;
46
47 public class CrossConnectImpl121 {
48     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
49
50     private final DeviceTransactionManager deviceTransactionManager;
51
52     public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
53         this.deviceTransactionManager = deviceTransactionManager;
54     }
55
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);
60     }
61
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))
70                 .build());
71         rdmConnBldr.setDestination(new DestinationBuilder()
72                 .setDstIf(spectrumInformation.getIdentifierFromParams(destTp))
73                 .build());
74         InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier
75             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
76             .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()))
77             .build();
78
79         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
80         DeviceTransaction deviceTx;
81         try {
82             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
83             if (deviceTxOpt.isPresent()) {
84                 deviceTx = deviceTxOpt.orElseThrow();
85             } else {
86                 LOG.error("Device transaction for device {} was not found!", deviceId);
87                 return Optional.empty();
88             }
89         } catch (InterruptedException | ExecutionException e) {
90             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
91             return Optional.empty();
92         }
93
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);
98         try {
99             commit.get();
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);
106         }
107         return Optional.empty();
108     }
109
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.orElseThrow().getSource().getSrcIf());
116             interfList.add(xc.orElseThrow().getDestination().getDstIf());
117         } else {
118             LOG.warn("Cross connect does not exist, halting delete");
119             return null;
120         }
121         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
122         DeviceTransaction deviceTx;
123         try {
124             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
125             if (deviceTxOpt.isPresent()) {
126                 deviceTx = deviceTxOpt.orElseThrow();
127             } else {
128                 LOG.error("Device transaction for device {} was not found!", deviceId);
129                 return null;
130             }
131         } catch (InterruptedException | ExecutionException e) {
132             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
133             return null;
134         }
135
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);
140         try {
141             commit.get();
142             LOG.info("Roadm connection successfully deleted ");
143             return interfList;
144         } catch (InterruptedException | ExecutionException e) {
145             LOG.warn("Failed to delete {}", connectionNumber, e);
146         }
147         return null;
148     }
149
150
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.orElseThrow();
161         } else {
162             LOG.error("Failed to obtain mount point for device {}!", nodeId);
163             return Collections.emptyList();
164         }
165         final Optional<RpcService> service = mountPoint.getService(RpcService.class);
166         if (!service.isPresent()) {
167             LOG.error("Failed to get RpcService for node {}", nodeId);
168         }
169         final GetConnectionPortTrail rpcService = service.orElseThrow().getRpc(GetConnectionPortTrail.class);
170         final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.invoke(
171                 new GetConnectionPortTrailInputBuilder()
172                     .setConnectionNumber(connectionName)
173                     .build());
174         if (portTrailOutput != null) {
175             try {
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));
181                 }
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());
186                 }
187             } catch (InterruptedException | ExecutionException e) {
188                 LOG.warn("Exception caught", e);
189             }
190         } else {
191             LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
192         }
193         return ports != null ? ports : Collections.emptyList();
194     }
195
196     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
197         return InstanceIdentifier.builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
198             .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber))
199             .build();
200     }
201
202     private String generateConnectionNumber(String srcTp, String destTp, String spectralSlotName) {
203         return String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, destTp, spectralSlotName);
204     }
205
206     public boolean setPowerLevel(String deviceId, OpticalControlMode mode, Decimal64 powerValue, String ctNumber) {
207
208         Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctNumber);
209         if (rdmConnOpt.isPresent()) {
210             RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.orElseThrow())
211                     .setOpticalControlMode(mode);
212             if (powerValue != null) {
213                 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
214             }
215             RoadmConnections newRdmConn = rdmConnBldr.build();
216
217             Future<Optional<DeviceTransaction>> deviceTxFuture =
218                     deviceTransactionManager.getDeviceTransaction(deviceId);
219             DeviceTransaction deviceTx;
220             try {
221                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
222                 if (deviceTxOpt.isPresent()) {
223                     deviceTx = deviceTxOpt.orElseThrow();
224                 } else {
225                     LOG.error("Transaction for device {} was not found!", deviceId);
226                     return false;
227                 }
228             } catch (InterruptedException | ExecutionException e) {
229                 LOG.error("Unable to get transaction for device {}!", deviceId, e);
230                 return false;
231             }
232
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))
237                 .build();
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);
241             try {
242                 commit.get();
243                 LOG.info("Roadm connection power level successfully set ");
244                 return true;
245             } catch (InterruptedException | ExecutionException ex) {
246                 LOG.warn("Failed to post {}", newRdmConn, ex);
247             }
248
249         } else {
250             LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);
251         }
252         return false;
253     }
254
255 }