Fix spotbugs issues in common module
[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.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.openroadminterfaces.OpenRoadmInterfaceException;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class CrossConnectImpl121 {
45     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
46
47     private final DeviceTransactionManager deviceTransactionManager;
48
49     public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
50         this.deviceTransactionManager = deviceTransactionManager;
51     }
52
53     public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
54         return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
55                 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
56                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
57     }
58
59     public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp) {
60         RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
61         String connectionNumber = generateConnectionNumber(srcTp, destTp, waveNumber);
62         rdmConnBldr.setConnectionNumber(connectionNumber);
63         rdmConnBldr.setWavelengthNumber(waveNumber);
64         rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
65         rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
66         rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
67         InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
68                 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
69
70         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
71         DeviceTransaction deviceTx;
72         try {
73             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
74             if (deviceTxOpt.isPresent()) {
75                 deviceTx = deviceTxOpt.get();
76             } else {
77                 LOG.error("Device transaction for device {} was not found!", deviceId);
78                 return Optional.empty();
79             }
80         } catch (InterruptedException | ExecutionException e) {
81             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
82             return Optional.empty();
83         }
84
85         // post the cross connect on the device
86         deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
87         FluentFuture<? extends @NonNull CommitInfo> commit =
88                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
89         try {
90             commit.get();
91             LOG.info("Roadm-connection successfully created: {}-{}-{}", srcTp, destTp, waveNumber);
92             return Optional.of(connectionNumber);
93         } catch (InterruptedException | ExecutionException e) {
94             LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
95         }
96         return Optional.empty();
97     }
98
99     public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
100         List<String> interfList = new ArrayList<>();
101         Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
102         //Check if cross connect exists before delete
103         if (xc.isPresent()) {
104             interfList.add(xc.get().getSource().getSrcIf());
105             interfList.add(xc.get().getDestination().getDstIf());
106         } else {
107             LOG.warn("Cross connect does not exist, halting delete");
108             return null;
109         }
110         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
111         DeviceTransaction deviceTx;
112         try {
113             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
114             if (deviceTxOpt.isPresent()) {
115                 deviceTx = deviceTxOpt.get();
116             } else {
117                 LOG.error("Device transaction for device {} was not found!", deviceId);
118                 return null;
119             }
120         } catch (InterruptedException | ExecutionException e) {
121             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
122             return null;
123         }
124
125         // post the cross connect on the device
126         deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
127         FluentFuture<? extends @NonNull CommitInfo> commit =
128                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
129         try {
130             commit.get();
131             LOG.info("Roadm connection successfully deleted ");
132             return interfList;
133         } catch (InterruptedException | ExecutionException e) {
134             LOG.warn("Failed to delete {}", connectionNumber, e);
135         }
136         return null;
137     }
138
139
140     public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
141             throws OpenRoadmInterfaceException {
142         String connectionName = generateConnectionNumber(srcTp, destTp, waveNumber);
143         Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
144         List<Ports> ports = null;
145         MountPoint mountPoint;
146         if (mountPointOpt.isPresent()) {
147             mountPoint = mountPointOpt.get();
148         } else {
149             LOG.error("Failed to obtain mount point for device {}!", nodeId);
150             return Collections.emptyList();
151         }
152         final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
153         if (!service.isPresent()) {
154             LOG.error("Failed to get RpcService for node {}", nodeId);
155         }
156         final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
157         final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
158         portTrainInputBuilder.setConnectionNumber(connectionName);
159         final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
160                 portTrainInputBuilder.build());
161         if (portTrailOutput != null) {
162             try {
163                 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
164                 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
165                 if (connectionPortTrailOutput == null) {
166                     throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
167                             + " node %s returned null!", nodeId));
168                 }
169                 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
170                 ports = connectionPortTrailOutput.getPorts();
171                 for (Ports port : ports) {
172                     LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
173                 }
174             } catch (InterruptedException | ExecutionException e) {
175                 LOG.warn("Exception caught", e);
176             }
177         } else {
178             LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
179         }
180         return ports != null ? ports : Collections.emptyList();
181     }
182
183     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
184         return InstanceIdentifier.create(OrgOpenroadmDevice.class)
185                 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
186     }
187
188     private String generateConnectionNumber(String srcTp, String destTp, Long waveNumber) {
189         return srcTp + "-" + destTp + "-" + waveNumber;
190     }
191
192     public boolean setPowerLevel(String deviceId, OpticalControlMode mode, BigDecimal powerValue, String ctNumber) {
193
194         Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctNumber);
195         if (rdmConnOpt.isPresent()) {
196             RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get())
197                     .setOpticalControlMode(mode);
198             if (powerValue != null) {
199                 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
200             }
201             RoadmConnections newRdmConn = rdmConnBldr.build();
202
203             Future<Optional<DeviceTransaction>> deviceTxFuture =
204                     deviceTransactionManager.getDeviceTransaction(deviceId);
205             DeviceTransaction deviceTx;
206             try {
207                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
208                 if (deviceTxOpt.isPresent()) {
209                     deviceTx = deviceTxOpt.get();
210                 } else {
211                     LOG.error("Transaction for device {} was not found!", deviceId);
212                     return false;
213                 }
214             } catch (InterruptedException | ExecutionException e) {
215                 LOG.error("Unable to get transaction for device {}!", deviceId, e);
216                 return false;
217             }
218
219             // post the cross connect on the device
220             InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
221                     .child(RoadmConnections.class, new RoadmConnectionsKey(ctNumber));
222             deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
223             FluentFuture<? extends @NonNull CommitInfo> commit =
224                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
225             try {
226                 commit.get();
227                 LOG.info("Roadm connection power level successfully set ");
228                 return true;
229             } catch (InterruptedException | ExecutionException ex) {
230                 LOG.warn("Failed to post {}", newRdmConn, ex);
231             }
232
233         } else {
234             LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);
235         }
236         return false;
237     }
238
239 }