3141e51422f6cfcd0ab2a179e65466b05b893dde
[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             String name = xc.get().getSource().getSrcIf().replace("nmc", "mc");
124             interfList.add(xc.get().getSource().getSrcIf());
125             interfList.add(xc.get().getDestination().getDstIf());
126             interfList.add(xc.get().getSource().getSrcIf().replace("nmc", "mc"));
127             interfList.add(xc.get().getDestination().getDstIf().replace("nmc", "mc"));
128         } else if (otnXc.isPresent()) {
129             interfList.add(otnXc.get().getSource().getSrcIf());
130             interfList.add(otnXc.get().getDestination().getDstIf());
131         } else {
132             LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
133             return null;
134         }
135         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
136         DeviceTransaction deviceTx;
137         try {
138             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
139             if (deviceTxOpt.isPresent()) {
140                 deviceTx = deviceTxOpt.get();
141             } else {
142                 LOG.error("Device transaction for device {} was not found!", deviceId);
143                 return null;
144             }
145         } catch (InterruptedException | ExecutionException e) {
146             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
147             return null;
148         }
149
150         // post the cross connect on the device
151         if (isOtn) {
152             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateOduConnectionIID(connectionName));
153         } else {
154             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionName));
155         }
156         FluentFuture<? extends @NonNull CommitInfo> commit =
157                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
158         try {
159             commit.get();
160             LOG.info("Connection {} successfully deleted on {}", connectionName, deviceId);
161             return interfList;
162         } catch (InterruptedException | ExecutionException e) {
163             LOG.warn("Failed to delete {}", connectionName, e);
164         }
165         return null;
166     }
167
168
169     public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
170             throws OpenRoadmInterfaceException {
171         String connectionName = generateConnectionName(srcTp, destTp, waveNumber);
172         Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
173         List<Ports> ports = null;
174         MountPoint mountPoint;
175         if (mountPointOpt.isPresent()) {
176             mountPoint = mountPointOpt.get();
177         } else {
178             LOG.error("Failed to obtain mount point for device {}!", nodeId);
179             return Collections.emptyList();
180         }
181         final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
182         if (!service.isPresent()) {
183             LOG.error("Failed to get RpcService for node {}", nodeId);
184         }
185         final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
186         final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
187         portTrainInputBuilder.setConnectionName(connectionName);
188         final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
189                 portTrainInputBuilder.build());
190         if (portTrailOutput != null) {
191             try {
192                 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
193                 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
194                 if (connectionPortTrailOutput == null) {
195                     throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
196                             + " node %s returned null!", nodeId));
197                 }
198                 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
199                 ports = connectionPortTrailOutput.getPorts();
200                 for (Ports port : ports) {
201                     LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
202                 }
203             } catch (InterruptedException | ExecutionException e) {
204                 LOG.warn("Exception caught", e);
205             }
206         } else {
207             LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
208         }
209         return ports != null ? ports : Collections.emptyList();
210     }
211
212
213     public boolean setPowerLevel(String deviceId, Enum mode, BigDecimal powerValue, String connectionName) {
214         Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, connectionName);
215         if (rdmConnOpt.isPresent()) {
216             RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
217             rdmConnBldr.setOpticalControlMode(OpticalControlMode.values()[mode.ordinal()]);
218             if (powerValue != null) {
219                 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
220             }
221             RoadmConnections newRdmConn = rdmConnBldr.build();
222
223             Future<Optional<DeviceTransaction>> deviceTxFuture =
224                     deviceTransactionManager.getDeviceTransaction(deviceId);
225             DeviceTransaction deviceTx;
226             try {
227                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
228                 if (deviceTxOpt.isPresent()) {
229                     deviceTx = deviceTxOpt.get();
230                 } else {
231                     LOG.error("Transaction for device {} was not found!", deviceId);
232                     return false;
233                 }
234             } catch (InterruptedException | ExecutionException e) {
235                 LOG.error("Unable to get transaction for device {}!", deviceId, e);
236                 return false;
237             }
238
239             // post the cross connect on the device
240             InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
241                     .child(RoadmConnections.class, new RoadmConnectionsKey(connectionName));
242             deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
243             FluentFuture<? extends @NonNull CommitInfo> commit =
244                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
245             try {
246                 commit.get();
247                 LOG.info("Roadm connection power level successfully set ");
248                 return true;
249             } catch (InterruptedException | ExecutionException ex) {
250                 LOG.warn("Failed to post {}", newRdmConn, ex);
251             }
252
253         } else {
254             LOG.warn("Roadm-Connection is null in set power level ({})", connectionName);
255         }
256         return false;
257     }
258
259     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
260         return InstanceIdentifier.create(OrgOpenroadmDevice.class)
261                 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
262     }
263
264     private InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
265         .container.org.openroadm.device.OduConnection> generateOduConnectionIID(String connectionNumber) {
266         return InstanceIdentifier.create(OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org
267             .openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnection.class,
268             new OduConnectionKey(connectionNumber));
269     }
270
271     private String generateConnectionName(String srcTp, String destTp, Long waveNumber) {
272         return srcTp + "-" + destTp + "-" + waveNumber;
273     }
274
275     public Optional<String> postOtnCrossConnect(List<String> createdOduInterfaces, Nodes node) {
276         String deviceId = node.getNodeId();
277         String srcTp = createdOduInterfaces.get(0);
278         String dstTp = createdOduInterfaces.get(1);
279         OduConnectionBuilder oduConnectionBuilder = new OduConnectionBuilder()
280             .setConnectionName(srcTp + "-x-" + dstTp)
281             .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
282                 .DestinationBuilder().setDstIf(dstTp).build())
283             .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
284                 .SourceBuilder().setSrcIf(srcTp).build())
285             .setDirection(Direction.Bidirectional);
286
287         InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
288                 .container.org.openroadm.device.OduConnection> oduConnectionIID =
289             InstanceIdentifier.create(OrgOpenroadmDevice.class)
290                 .child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
291                         .container.org.openroadm.device.OduConnection.class,
292                     new OduConnectionKey(oduConnectionBuilder.getConnectionName())
293                 );
294
295         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
296         DeviceTransaction deviceTx;
297         try {
298             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
299             if (deviceTxOpt.isPresent()) {
300                 deviceTx = deviceTxOpt.get();
301             } else {
302                 LOG.error("Device transaction for device {} was not found!", deviceId);
303                 return Optional.empty();
304             }
305         } catch (InterruptedException | ExecutionException e) {
306             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
307             return Optional.empty();
308         }
309
310         // post the cross connect on the device
311         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, oduConnectionIID, oduConnectionBuilder.build());
312         FluentFuture<? extends @NonNull CommitInfo> commit =
313                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
314         try {
315             commit.get();
316             LOG.info("Otn-connection successfully created: {}-{}", srcTp, dstTp);
317             return Optional.of(srcTp + "-x-" + dstTp);
318         } catch (InterruptedException | ExecutionException e) {
319             LOG.warn("Failed to post {}. Exception: {}", oduConnectionBuilder.build(), e);
320         }
321         return Optional.empty();
322     }
323 }