993a6a7622d92afbd09c244f0368fe137ddae5b7
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / CrossConnect.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.renderer.provisiondevice;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16
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;
42
43 public class CrossConnect {
44
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;
49
50     public CrossConnect(DataBroker deviceDb) {
51         this.deviceDb = deviceDb;
52         connectionNumber = null;
53         rdmConnectionIID = null;
54     }
55
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));
61     }
62
63     /**
64      * This method return the RoadmConnection subtree for a given connection
65      * number.
66      *
67      * @return Roadm connection subtree from the device.
68      */
69     public RoadmConnections getCrossConnect() {
70
71         if (deviceDb != null && connectionNumber != null) {
72             ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
73             Optional<RoadmConnections> roadmConnectionsObject;
74             try {
75                 roadmConnectionsObject = rtx.read(LogicalDatastoreType.OPERATIONAL, rdmConnectionIID).get();
76                 if (roadmConnectionsObject.isPresent()) {
77                     return roadmConnectionsObject.get();
78                 }
79             } catch (InterruptedException | ExecutionException ex) {
80                 LOG.info("Error getting roadm-connection subtree from the device for " + connectionNumber, ex);
81                 return null;
82             }
83         }
84         return null;
85     }
86
87     /**
88      * This method does a post(edit-config) on roadm connection subtree for a
89      * given connection number.
90      *
91      * @param waveNumber
92      *            Wavelength number.
93      * @param srcTp
94      *            Name of source termination point.
95      * @param destTp
96      *            Name of destination termination point.
97      * @return true/false based on status of operation.
98      */
99     public boolean postCrossConnect(Long waveNumber, String srcTp, String destTp) {
100
101         RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
102         rdmConnBldr.setConnectionNumber(srcTp + "-" + destTp + "-" + waveNumber);
103         rdmConnBldr.setWavelengthNumber(waveNumber);
104         rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
105         rdmConnBldr.setSource(new SourceBuilder().setSrcIf(srcTp + "-" + waveNumber.toString()).build());
106         rdmConnBldr.setDestination(new DestinationBuilder().setDstIf(destTp + "-" + waveNumber.toString()).build());
107         InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
108             .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
109
110         if (deviceDb != null) {
111             final WriteTransaction writeTransaction = deviceDb.newWriteOnlyTransaction();
112             // post the cross connect on the device
113             writeTransaction.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
114             final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
115             try {
116                 submit.checkedGet();
117                 LOG.info("Roadm-connection successfully created: " + srcTp + "-" + destTp + "-" + waveNumber);
118                 return true;
119             } catch (TransactionCommitFailedException ex) {
120                 LOG.info("Failed to post {} ", rdmConnBldr.build(), ex);
121                 return false;
122             }
123         } else {
124             LOG.error("Invalid device databroker");
125             return false;
126         }
127     }
128
129
130
131     /**
132      * This method does a delete(edit-config) on roadm connection subtree for a
133      * given connection number.
134      *
135      * @return true/false based on status of operation.
136      */
137
138     public boolean deleteCrossConnect() {
139
140         //Check if cross connect exists before delete
141         if (getCrossConnect() == null) {
142             LOG.info("Cross connect does not exist, halting delete");
143             return false;
144         }
145         if (deviceDb != null) {
146             final WriteTransaction writeTransaction = deviceDb.newWriteOnlyTransaction();
147             // post the cross connect on the device
148             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID);
149             final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
150             try {
151                 submit.checkedGet();
152                 LOG.info("Roadm connection successfully deleted ");
153                 return true;
154             } catch (TransactionCommitFailedException ex) {
155                 LOG.info("Failed to delete {} ", connectionNumber, ex);
156                 return false;
157             }
158         } else {
159             LOG.error("Invalid device databroker");
160             return false;
161         }
162     }
163
164
165
166     /**
167      * This method does an edit-config on roadm connection subtree for a given
168      * connection number in order to set power level for use by the optical
169      * power control.
170      *
171      * @param mode
172      *            Optical control modelcan be off, power or gainLoss.
173      * @param value
174      *            Power value in DBm.
175      * @return true/false based on status of operation.
176      */
177     public boolean setPowerLevel(OpticalControlMode mode, PowerDBm value) {
178
179         RoadmConnections rdmConn = getCrossConnect();
180         if (rdmConn != null) {
181             RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConn);
182             rdmConnBldr.setOpticalControlMode(mode);
183             rdmConnBldr.setTargetOutputPower(value);
184             if (deviceDb != null) {
185                 final WriteTransaction writeTransaction = deviceDb.newWriteOnlyTransaction();
186                 // post the cross connect on the device
187                 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
188                 final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
189                 try {
190                     submit.checkedGet();
191                     LOG.info("Roadm connection power level successfully set ");
192                     return true;
193                 } catch (TransactionCommitFailedException ex) {
194                     LOG.info("Failed to post {} ", rdmConnBldr.build(), ex);
195                     return false;
196                 }
197             } else {
198                 LOG.error("Invalid device databroker");
199                 return false;
200             }
201         } else {
202             LOG.info("Roadm-Connection does not exist");
203             return false;
204         }
205     }
206
207     /**
208      * This public method returns the list of ports (port-trail) for a roadm's
209      * cross connect. It calls rpc get-port-trail on device. To be used store
210      * detailed path description.
211      *
212      * @param nodeId
213      *            node-id of NE.
214      * @param mountService
215      *            Reference to mount point service.
216      * @param waveNumber
217      *            Wavelength number.
218      * @param srcTp
219      *            Source logical connection point.
220      * @param destTp
221      *            Destination logical connection point.
222      *
223      * @return list of Ports object type.
224      */
225     public Ports getConnectionPortTrail(String nodeId, MountPointService mountService, Long waveNumber, String srcTp,
226         String destTp) {
227
228         String connectionName = srcTp + "-" + destTp + "-" + waveNumber;
229         MountPoint mountPoint = PortMapping.getDeviceMountPoint(nodeId, mountService);
230
231         final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
232         if (!service.isPresent()) {
233             LOG.error("Failed to get RpcService for node {}", nodeId);
234         }
235         final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
236         final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
237         portTrainInputBuilder.setConnectionNumber(connectionName);
238         final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
239             portTrainInputBuilder.build());
240         if (portTrailOutput != null) {
241             try {
242                 LOG.info("Getting port trail for node " + nodeId + "'s connection number " + connectionName);
243                 for (Ports ports : portTrailOutput.get().getResult().getPorts()) {
244                     LOG.info(nodeId + " - " + "Circuit pack " + ports.getCircuitPackName() + "- Port " + ports
245                         .getPortName());
246                 }
247             } catch (InterruptedException | ExecutionException e) {
248                 LOG.info("Exception caught", e);
249             }
250         } else {
251             LOG.info("Port trail is null");
252         }
253         return null;
254     }
255 }