2 * Copyright © 2017 AT&T and others. All rights reserved.
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
9 package org.opendaylight.transportpce.renderer.provisiondevice;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
19 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.transportpce.renderer.mapping.PortMapping;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.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;
43 public class CrossConnect {
45 private final DataBroker deviceDb;
46 private static final Logger LOG = LoggerFactory.getLogger(CrossConnect.class);
47 private final String connectionNumber;
48 private final InstanceIdentifier<RoadmConnections> rdmConnectionIID;
50 public CrossConnect(DataBroker deviceDb) {
51 this.deviceDb = deviceDb;
52 connectionNumber = null;
53 rdmConnectionIID = null;
56 public CrossConnect(DataBroker deviceDb, String connectionNumber) {
57 this.deviceDb = deviceDb;
58 this.connectionNumber = connectionNumber;
59 rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(RoadmConnections.class,
60 new RoadmConnectionsKey(connectionNumber));
64 * This method return the RoadmConnection subtree for a given connection
67 * @param connectionNumber
68 * Name of the cross connect.
70 * @return Roadm connection subtree from the device.
72 public RoadmConnections getCrossConnect(String connectionNumber) {
73 if (connectionNumber == null && this.connectionNumber != null) {
74 connectionNumber = this.connectionNumber;
76 if (deviceDb != null) {
77 ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
78 Optional<RoadmConnections> roadmConnectionsObject;
80 roadmConnectionsObject = rtx.read(LogicalDatastoreType.OPERATIONAL, rdmConnectionIID).get();
81 if (roadmConnectionsObject.isPresent()) {
82 return roadmConnectionsObject.get();
84 } catch (InterruptedException | ExecutionException ex) {
85 LOG.info("Error getting roadm-connection subtree from the device for " + connectionNumber, ex);
93 * This method does a post(edit-config) on roadm connection subtree for a
94 * given connection number.
99 * Name of source termination point.
101 * Name of destination termination point.
102 * @return true/false based on status of operation.
104 public boolean postCrossConnect(Long waveNumber, String srcTp, String destTp) {
106 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
107 rdmConnBldr.setConnectionNumber(srcTp + "-" + destTp + "-" + waveNumber);
108 rdmConnBldr.setWavelengthNumber(waveNumber);
109 rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
110 rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
111 rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
112 InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
113 .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
115 if (deviceDb != null) {
116 final WriteTransaction writeTransaction = deviceDb.newWriteOnlyTransaction();
117 // post the cross connect on the device
118 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
119 final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
122 LOG.info("Roadm-connection successfully created: " + srcTp + "-" + destTp + "-" + waveNumber);
124 } catch (TransactionCommitFailedException ex) {
125 LOG.info("Failed to post {} ", rdmConnBldr.build(), ex);
129 LOG.error("Invalid device databroker");
135 * This method does a delete(edit-config) on roadm connection subtree for a
136 * given connection number.
138 * @param connectionNumber
139 * Name of the cross connect.
140 * @return true/false based on status of operation.
143 public boolean deleteCrossConnect(String connectionNumber) {
144 if (connectionNumber == null && this.connectionNumber != null) {
145 connectionNumber = this.connectionNumber;
147 return deleteCrossConnect();
151 * This method does a delete(edit-config) on roadm connection subtree for a
152 * given connection number.
154 * @return true/false based on status of operation.
157 public boolean deleteCrossConnect() {
159 //Check if cross connect exists before delete
160 if (getCrossConnect(connectionNumber) == null) {
161 LOG.info("Cross connect does not exist, halting delete");
164 if (deviceDb != null) {
165 final WriteTransaction writeTransaction = deviceDb.newWriteOnlyTransaction();
166 // post the cross connect on the device
167 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID);
168 final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
171 LOG.info("Roadm connection successfully deleted ");
173 } catch (TransactionCommitFailedException ex) {
174 LOG.info("Failed to delete {} ", connectionNumber, ex);
178 LOG.error("Invalid device databroker");
184 * This method does an edit-config on roadm connection subtree for a given
185 * connection number in order to set power level for use by the optical
189 * Optical control modelcan be off, power or gainLoss.
191 * Power value in DBm.
192 * @param connectionNumber
193 * Name of the cross connect.
194 * @return true/false based on status of operation.
196 public boolean setPowerLevel(OpticalControlMode mode, PowerDBm value, String connectionNumber) {
197 if (connectionNumber == null && this.connectionNumber != null) {
198 connectionNumber = this.connectionNumber;
200 return setPowerLevel(mode, value);
204 * This method does an edit-config on roadm connection subtree for a given
205 * connection number in order to set power level for use by the optical
209 * Optical control modelcan be off, power or gainLoss.
211 * Power value in DBm.
212 * @return true/false based on status of operation.
214 public boolean setPowerLevel(OpticalControlMode mode, PowerDBm value) {
216 RoadmConnections rdmConn = getCrossConnect(connectionNumber);
217 if (rdmConn != null) {
218 RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConn);
219 rdmConnBldr.setOpticalControlMode(mode);
220 rdmConnBldr.setTargetOutputPower(value);
221 if (deviceDb != null) {
222 final WriteTransaction writeTransaction = deviceDb.newWriteOnlyTransaction();
223 // post the cross connect on the device
224 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
225 final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
228 LOG.info("Roadm connection power level successfully set ");
230 } catch (TransactionCommitFailedException ex) {
231 LOG.info("Failed to post {} ", rdmConnBldr.build(), ex);
235 LOG.error("Invalid device databroker");
239 LOG.info("Roadm-Connection does not exist");
245 * This public method returns the list of ports (port-trail) for a roadm's
246 * cross connect. It calls rpc get-port-trail on device. To be used store
247 * detailed path description.
251 * @param mountService
252 * Reference to mount point service.
256 * Source logical connection point.
258 * Destination logical connection point.
260 * @return list of Ports object type.
262 public Ports getConnectionPortTrail(String nodeId, MountPointService mountService, Long waveNumber, String srcTp,
265 String connectionName = srcTp + "-" + destTp + "-" + waveNumber;
266 MountPoint mountPoint = PortMapping.getDeviceMountPoint(nodeId, mountService);
268 final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
269 if (!service.isPresent()) {
270 LOG.error("Failed to get RpcService for node {}", nodeId);
272 final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
273 final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
274 portTrainInputBuilder.setConnectionNumber(connectionName);
275 final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
276 portTrainInputBuilder.build());
277 if (portTrailOutput != null) {
279 LOG.info("Getting port trail for node " + nodeId + "'s connection number " + connectionName);
280 for (Ports ports : portTrailOutput.get().getResult().getPorts()) {
281 LOG.info(nodeId + " - " + "Circuit pack " + ports.getCircuitPackName() + "- Port " + ports
284 } catch (InterruptedException | ExecutionException e) {
285 LOG.info("Exception caught", e);
288 LOG.info("Port trail is null");