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