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