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