b67f76c6fdbc3bd162bed1f1afe515a5e93f76ce
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl.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
9 package org.opendaylight.transportpce.common.crossconnect;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12
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.rev161014.OpticalControlMode;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class CrossConnectImpl implements CrossConnect {
43
44     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl.class);
45
46     private final DeviceTransactionManager deviceTransactionManager;
47
48     public CrossConnectImpl(DeviceTransactionManager deviceTransactionManager) {
49         this.deviceTransactionManager = deviceTransactionManager;
50     }
51
52     @Override
53     public Optional<RoadmConnections> getCrossConnect(String deviceId, String connectionNumber) {
54         return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
55                 generateRdmConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
56                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
57     }
58
59     @Override
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(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         ListenableFuture<Void> submit =
89                 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
90         try {
91             submit.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     @Override
101     public boolean deleteCrossConnect(String deviceId, String connectionNumber) {
102         //Check if cross connect exists before delete
103         if (!getCrossConnect(deviceId, connectionNumber).isPresent()) {
104             LOG.warn("Cross connect does not exist, halting delete");
105             return false;
106         }
107         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
108         DeviceTransaction deviceTx;
109         try {
110             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
111             if (deviceTxOpt.isPresent()) {
112                 deviceTx = deviceTxOpt.get();
113             } else {
114                 LOG.error("Device transaction for device {} was not found!", deviceId);
115                 return false;
116             }
117         } catch (InterruptedException | ExecutionException e) {
118             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
119             return false;
120         }
121
122         // post the cross connect on the device
123         deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
124         ListenableFuture<Void> submit =
125                 deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
126         try {
127             submit.get();
128             LOG.info("Roadm connection successfully deleted ");
129             return true;
130         } catch (InterruptedException | ExecutionException e) {
131             LOG.warn("Failed to delete {}", connectionNumber, e);
132         }
133         return false;
134     }
135
136     @Override
137     public List<Ports> getConnectionPortTrail(String nodeId, Long waveNumber, String srcTp, String destTp)
138             throws OpenRoadmInterfaceException {
139         String connectionName = generateConnectionNumber(srcTp, destTp, waveNumber);
140         Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
141         List<Ports> ports = null;
142         MountPoint mountPoint;
143         if (mountPointOpt.isPresent()) {
144             mountPoint = mountPointOpt.get();
145         } else {
146             LOG.error("Failed to obtain mount point for device {}!", nodeId);
147             return Collections.emptyList();
148         }
149         final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class).toJavaUtil();
150         if (!service.isPresent()) {
151             LOG.error("Failed to get RpcService for node {}", nodeId);
152         }
153         final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
154         final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
155         portTrainInputBuilder.setConnectionNumber(connectionName);
156         final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
157             portTrainInputBuilder.build());
158         if (portTrailOutput != null) {
159             try {
160                 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
161                 GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
162                 if (connectionPortTrailOutput == null) {
163                     throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
164                             + " node %s returned null!", nodeId));
165                 }
166                 LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
167                 ports = connectionPortTrailOutput.getPorts();
168                 for (Ports port : ports) {
169                     LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
170                 }
171             } catch (InterruptedException | ExecutionException e) {
172                 LOG.warn("Exception caught", e);
173             }
174         } else {
175             LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
176         }
177         return ports != null ? ports : Collections.emptyList();
178     }
179
180     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
181         return InstanceIdentifier.create(OrgOpenroadmDevice.class)
182                 .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
183     }
184
185     private String generateConnectionNumber(String srcTp, String destTp, Long waveNumber) {
186         return srcTp + "-" + destTp + "-" + waveNumber;
187     }
188 }