Refactor to remove useless wavelength reference
[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.fixedflex.GridConstant;
27 import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
28 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class CrossConnectImpl121 {
47     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl121.class);
48
49     private final DeviceTransactionManager deviceTransactionManager;
50
51     public CrossConnectImpl121(DeviceTransactionManager deviceTransactionManager) {
52         this.deviceTransactionManager = deviceTransactionManager;
53     }
54
55     public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
56         return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
57                 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
58                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
59     }
60
61     public Optional<String> postCrossConnect(String deviceId, String srcTp, String destTp,
62             SpectrumInformation spectrumInformation) {
63         RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
64         String connectionNumber = spectrumInformation.getIdentifierFromParams(srcTp, destTp);
65         rdmConnBldr.setConnectionNumber(connectionNumber);
66         rdmConnBldr.setWavelengthNumber(spectrumInformation.getWaveLength());
67         rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
68         rdmConnBldr.setSource(new SourceBuilder().setSrcIf(spectrumInformation.getIdentifierFromParams(srcTp))
69                 .build());
70         rdmConnBldr.setDestination(new DestinationBuilder()
71                 .setDstIf(spectrumInformation.getIdentifierFromParams(destTp))
72                 .build());
73         InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
74                 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
75
76         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
77         DeviceTransaction deviceTx;
78         try {
79             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
80             if (deviceTxOpt.isPresent()) {
81                 deviceTx = deviceTxOpt.get();
82             } else {
83                 LOG.error("Device transaction for device {} was not found!", deviceId);
84                 return Optional.empty();
85             }
86         } catch (InterruptedException | ExecutionException e) {
87             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
88             return Optional.empty();
89         }
90
91         // post the cross connect on the device
92         deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
93         FluentFuture<? extends @NonNull CommitInfo> commit =
94                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
95         try {
96             commit.get();
97             LOG.info("Roadm-connection successfully created: {}-{}-{}-{}", srcTp, destTp,
98                     spectrumInformation.getLowerSpectralSlotNumber(),
99                     spectrumInformation.getHigherSpectralSlotNumber());
100             return Optional.of(connectionNumber);
101         } catch (InterruptedException | ExecutionException e) {
102             LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
103         }
104         return Optional.empty();
105     }
106
107     public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
108         List<String> interfList = new ArrayList<>();
109         Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
110         //Check if cross connect exists before delete
111         if (xc.isPresent()) {
112             interfList.add(xc.get().getSource().getSrcIf());
113             interfList.add(xc.get().getDestination().getDstIf());
114         } else {
115             LOG.warn("Cross connect does not exist, halting delete");
116             return null;
117         }
118         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
119         DeviceTransaction deviceTx;
120         try {
121             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
122             if (deviceTxOpt.isPresent()) {
123                 deviceTx = deviceTxOpt.get();
124             } else {
125                 LOG.error("Device transaction for device {} was not found!", deviceId);
126                 return null;
127             }
128         } catch (InterruptedException | ExecutionException e) {
129             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
130             return null;
131         }
132
133         // post the cross connect on the device
134         deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
135         FluentFuture<? extends @NonNull CommitInfo> commit =
136                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
137         try {
138             commit.get();
139             LOG.info("Roadm connection successfully deleted ");
140             return interfList;
141         } catch (InterruptedException | ExecutionException e) {
142             LOG.warn("Failed to delete {}", connectionNumber, e);
143         }
144         return null;
145     }
146
147
148     public List<Ports> getConnectionPortTrail(String nodeId, String srcTp, String destTp,
149             int lowerSpectralSlotNumber, int higherSpectralSlotNumber) throws OpenRoadmInterfaceException {
150         String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
151                 String.valueOf(lowerSpectralSlotNumber), String.valueOf(higherSpectralSlotNumber));
152         String connectionName = generateConnectionNumber(srcTp, destTp, spectralSlotName);
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);
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.setConnectionNumber(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     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
194         return InstanceIdentifier.create(OrgOpenroadmDevice.class)
195                 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
196     }
197
198     private String generateConnectionNumber(String srcTp, String destTp, String spectralSlotName) {
199         return String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, destTp, spectralSlotName);
200     }
201
202     public boolean setPowerLevel(String deviceId, OpticalControlMode mode, BigDecimal powerValue, String ctNumber) {
203
204         Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctNumber);
205         if (rdmConnOpt.isPresent()) {
206             RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get())
207                     .setOpticalControlMode(mode);
208             if (powerValue != null) {
209                 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
210             }
211             RoadmConnections newRdmConn = rdmConnBldr.build();
212
213             Future<Optional<DeviceTransaction>> deviceTxFuture =
214                     deviceTransactionManager.getDeviceTransaction(deviceId);
215             DeviceTransaction deviceTx;
216             try {
217                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
218                 if (deviceTxOpt.isPresent()) {
219                     deviceTx = deviceTxOpt.get();
220                 } else {
221                     LOG.error("Transaction for device {} was not found!", deviceId);
222                     return false;
223                 }
224             } catch (InterruptedException | ExecutionException e) {
225                 LOG.error("Unable to get transaction for device {}!", deviceId, e);
226                 return false;
227             }
228
229             // post the cross connect on the device
230             InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
231                     .child(RoadmConnections.class, new RoadmConnectionsKey(ctNumber));
232             deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
233             FluentFuture<? extends @NonNull CommitInfo> commit =
234                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
235             try {
236                 commit.get();
237                 LOG.info("Roadm connection power level successfully set ");
238                 return true;
239             } catch (InterruptedException | ExecutionException ex) {
240                 LOG.warn("Failed to post {}", newRdmConn, ex);
241             }
242
243         } else {
244             LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);
245         }
246         return false;
247     }
248
249 }