Fix spotbugs issues in common module
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl221.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.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
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;
48
49 public class CrossConnectImpl221 {
50
51     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl221.class);
52     private final DeviceTransactionManager deviceTransactionManager;
53
54     public CrossConnectImpl221(DeviceTransactionManager deviceTransactionManager) {
55         this.deviceTransactionManager = deviceTransactionManager;
56     }
57
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);
63     }
64
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);
71     }
72
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)
80                 .build());
81
82         InstanceIdentifier<RoadmConnections> rdmConnectionIID =
83                 InstanceIdentifier.create(OrgOpenroadmDevice.class)
84                     .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()));
85
86         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
87         DeviceTransaction deviceTx;
88         try {
89             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
90             if (deviceTxOpt.isPresent()) {
91                 deviceTx = deviceTxOpt.get();
92             } else {
93                 LOG.error("Device transaction for device {} was not found!", deviceId);
94                 return Optional.empty();
95             }
96         } catch (InterruptedException | ExecutionException e) {
97             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
98             return Optional.empty();
99         }
100
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);
105         try {
106             commit.get();
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);
111         }
112         return Optional.empty();
113     }
114
115
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());
130         } else {
131             LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
132             return null;
133         }
134         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
135         DeviceTransaction deviceTx;
136         try {
137             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
138             if (deviceTxOpt.isPresent()) {
139                 deviceTx = deviceTxOpt.get();
140             } else {
141                 LOG.error("Device transaction for device {} was not found!", deviceId);
142                 return null;
143             }
144         } catch (InterruptedException | ExecutionException e) {
145             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
146             return null;
147         }
148
149         // post the cross connect on the device
150         if (isOtn) {
151             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateOduConnectionIID(connectionName));
152         } else {
153             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionName));
154         }
155         FluentFuture<? extends @NonNull CommitInfo> commit =
156                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
157         try {
158             commit.get();
159             LOG.info("Connection {} successfully deleted on {}", connectionName, deviceId);
160             return interfList;
161         } catch (InterruptedException | ExecutionException e) {
162             LOG.warn("Failed to delete {}", connectionName, e);
163         }
164         return null;
165     }
166
167
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();
176         } else {
177             LOG.error("Failed to obtain mount point for device {}!", nodeId);
178             return Collections.emptyList();
179         }
180         final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
181         if (!service.isPresent()) {
182             LOG.error("Failed to get RpcService for node {}", nodeId);
183         }
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) {
190             try {
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));
196                 }
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());
201                 }
202             } catch (InterruptedException | ExecutionException e) {
203                 LOG.warn("Exception caught", e);
204             }
205         } else {
206             LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
207         }
208         return ports != null ? ports : Collections.emptyList();
209     }
210
211
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));
219             }
220             RoadmConnections newRdmConn = rdmConnBldr.build();
221
222             Future<Optional<DeviceTransaction>> deviceTxFuture =
223                     deviceTransactionManager.getDeviceTransaction(deviceId);
224             DeviceTransaction deviceTx;
225             try {
226                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
227                 if (deviceTxOpt.isPresent()) {
228                     deviceTx = deviceTxOpt.get();
229                 } else {
230                     LOG.error("Transaction for device {} was not found!", deviceId);
231                     return false;
232                 }
233             } catch (InterruptedException | ExecutionException e) {
234                 LOG.error("Unable to get transaction for device {}!", deviceId, e);
235                 return false;
236             }
237
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);
244             try {
245                 commit.get();
246                 LOG.info("Roadm connection power level successfully set ");
247                 return true;
248             } catch (InterruptedException | ExecutionException ex) {
249                 LOG.warn("Failed to post {}", newRdmConn, ex);
250             }
251
252         } else {
253             LOG.warn("Roadm-Connection is null in set power level ({})", ctName);
254         }
255         return false;
256     }
257
258     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
259         return InstanceIdentifier.create(OrgOpenroadmDevice.class)
260                 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
261     }
262
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));
268     }
269
270     private String generateConnectionName(String srcTp, String destTp, Long waveNumber) {
271         return srcTp + "-" + destTp + "-" + waveNumber;
272     }
273
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);
285
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())
292                 );
293
294         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
295         DeviceTransaction deviceTx;
296         try {
297             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
298             if (deviceTxOpt.isPresent()) {
299                 deviceTx = deviceTxOpt.get();
300             } else {
301                 LOG.error("Device transaction for device {} was not found!", deviceId);
302                 return Optional.empty();
303             }
304         } catch (InterruptedException | ExecutionException e) {
305             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
306             return Optional.empty();
307         }
308
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);
313         try {
314             commit.get();
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);
319         }
320         return Optional.empty();
321     }
322 }