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