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