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