af220f7bbd05757e206c205acd0dbd8a32198c75
[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      * @param connectionNumber
68      *            Name of the cross connect.
69      *
70      * @return Roadm connection subtree from the device.
71      */
72     public RoadmConnections getCrossConnect(String connectionNumber) {
73         if (connectionNumber == null && this.connectionNumber != null) {
74             connectionNumber = this.connectionNumber;
75         }
76         if (deviceDb != null) {
77             ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
78             Optional<RoadmConnections> roadmConnectionsObject;
79             try {
80                 roadmConnectionsObject = rtx.read(LogicalDatastoreType.OPERATIONAL, rdmConnectionIID).get();
81                 if (roadmConnectionsObject.isPresent()) {
82                     return roadmConnectionsObject.get();
83                 }
84             } catch (InterruptedException | ExecutionException ex) {
85                 LOG.info("Error getting roadm-connection subtree from the device for " + connectionNumber, ex);
86                 return null;
87             }
88         }
89         return null;
90     }
91
92     /**
93      * This method does a post(edit-config) on roadm connection subtree for a
94      * given connection number.
95      *
96      * @param waveNumber
97      *            Wavelength number.
98      * @param srcTp
99      *            Name of source termination point.
100      * @param destTp
101      *            Name of destination termination point.
102      * @return true/false based on status of operation.
103      */
104     public boolean postCrossConnect(Long waveNumber, String srcTp, String destTp) {
105
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()));
114
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();
120             try {
121                 submit.checkedGet();
122                 LOG.info("Roadm-connection successfully created: " + srcTp + "-" + destTp + "-" + waveNumber);
123                 return true;
124             } catch (TransactionCommitFailedException ex) {
125                 LOG.info("Failed to post {} ", rdmConnBldr.build(), ex);
126                 return false;
127             }
128         } else {
129             LOG.error("Invalid device databroker");
130             return false;
131         }
132     }
133
134     /**
135      * This method does a delete(edit-config) on roadm connection subtree for a
136      * given connection number.
137      *
138      * @param connectionNumber
139      *            Name of the cross connect.
140      * @return true/false based on status of operation.
141      */
142
143     public boolean deleteCrossConnect(String connectionNumber) {
144         if (connectionNumber == null && this.connectionNumber != null) {
145             connectionNumber = this.connectionNumber;
146         }
147         return deleteCrossConnect();
148     }
149
150     /**
151      * This method does a delete(edit-config) on roadm connection subtree for a
152      * given connection number.
153      *
154      * @return true/false based on status of operation.
155      */
156
157     public boolean deleteCrossConnect() {
158
159         //Check if cross connect exists before delete
160         if (getCrossConnect(connectionNumber) == null) {
161             LOG.info("Cross connect does not exist, halting delete");
162             return false;
163         }
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();
169             try {
170                 submit.checkedGet();
171                 LOG.info("Roadm connection successfully deleted ");
172                 return true;
173             } catch (TransactionCommitFailedException ex) {
174                 LOG.info("Failed to delete {} ", connectionNumber, ex);
175                 return false;
176             }
177         } else {
178             LOG.error("Invalid device databroker");
179             return false;
180         }
181     }
182
183     /**
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
186      * power control.
187      *
188      * @param mode
189      *            Optical control modelcan be off, power or gainLoss.
190      * @param value
191      *            Power value in DBm.
192      * @param connectionNumber
193      *            Name of the cross connect.
194      * @return true/false based on status of operation.
195      */
196     public boolean setPowerLevel(OpticalControlMode mode, PowerDBm value, String connectionNumber) {
197         if (connectionNumber == null && this.connectionNumber != null) {
198             connectionNumber = this.connectionNumber;
199         }
200         return setPowerLevel(mode, value);
201     }
202
203     /**
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
206      * power control.
207      *
208      * @param mode
209      *            Optical control modelcan be off, power or gainLoss.
210      * @param value
211      *            Power value in DBm.
212      * @return true/false based on status of operation.
213      */
214     public boolean setPowerLevel(OpticalControlMode mode, PowerDBm value) {
215
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();
226                 try {
227                     submit.checkedGet();
228                     LOG.info("Roadm connection power level successfully set ");
229                     return true;
230                 } catch (TransactionCommitFailedException ex) {
231                     LOG.info("Failed to post {} ", rdmConnBldr.build(), ex);
232                     return false;
233                 }
234             } else {
235                 LOG.error("Invalid device databroker");
236                 return false;
237             }
238         } else {
239             LOG.info("Roadm-Connection does not exist");
240             return false;
241         }
242     }
243
244     /**
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.
248      *
249      * @param nodeId
250      *            node-id of NE.
251      * @param mountService
252      *            Reference to mount point service.
253      * @param waveNumber
254      *            Wavelength number.
255      * @param srcTp
256      *            Source logical connection point.
257      * @param destTp
258      *            Destination logical connection point.
259      *
260      * @return list of Ports object type.
261      */
262     public Ports getConnectionPortTrail(String nodeId, MountPointService mountService, Long waveNumber, String srcTp,
263         String destTp) {
264
265         String connectionName = srcTp + "-" + destTp + "-" + waveNumber;
266         MountPoint mountPoint = PortMapping.getDeviceMountPoint(nodeId, mountService);
267
268         final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
269         if (!service.isPresent()) {
270             LOG.error("Failed to get RpcService for node {}", nodeId);
271         }
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) {
278             try {
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
282                         .getPortName());
283                 }
284             } catch (InterruptedException | ExecutionException e) {
285                 LOG.info("Exception caught", e);
286             }
287         } else {
288             LOG.info("Port trail is null");
289         }
290         return null;
291     }
292 }