ef8fa13e0c4a42c5b8af386aaaa2999dc9ce33c1
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl710.java
1 /*
2  * Copyright © 2021 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.FluentFuture;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.transportpce.common.device.DeviceTransaction;
22 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OduConnection.Direction;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OrgOpenroadmDeviceData;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.org.openroadm.device.OduConnection;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.org.openroadm.device.OduConnectionBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.org.openroadm.device.OduConnectionKey;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210930.otn.renderer.nodes.Nodes;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class CrossConnectImpl710 {
35
36     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl710.class);
37     private final DeviceTransactionManager deviceTransactionManager;
38
39     public CrossConnectImpl710(DeviceTransactionManager deviceTransactionManager) {
40         this.deviceTransactionManager = deviceTransactionManager;
41     }
42
43     public Optional<OduConnection> getOtnCrossConnect(String deviceId, String connectionNumber) {
44         return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL,
45             generateOduConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
46             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
47     }
48
49     private InstanceIdentifier<OduConnection> generateOduConnectionIID(String connectionNumber) {
50         return InstanceIdentifier
51             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
52             .child(OduConnection.class, new OduConnectionKey(connectionNumber))
53             .build();
54     }
55
56     public Optional<String> postOtnCrossConnect(List<String> createdOduInterfaces, Nodes node) {
57         String deviceId = node.getNodeId();
58         String srcTp = createdOduInterfaces.get(0);
59         String dstTp = createdOduInterfaces.get(1);
60
61         OduConnectionBuilder oduConnectionBuilder = new OduConnectionBuilder()
62             .setConnectionName(srcTp + "-x-" + dstTp)
63             .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.odu.connection
64                 .DestinationBuilder().setDstIf(dstTp).build())
65             .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.odu.connection
66                 .SourceBuilder().setSrcIf(srcTp).build())
67             .setDirection(Direction.Bidirectional);
68
69         InstanceIdentifier<OduConnection> oduConnectionIID = InstanceIdentifier
70             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
71             .child(OduConnection.class, new OduConnectionKey(oduConnectionBuilder.getConnectionName()))
72             .build();
73
74         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
75         DeviceTransaction deviceTx;
76         try {
77             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
78             if (deviceTxOpt.isPresent()) {
79                 deviceTx = deviceTxOpt.get();
80             } else {
81                 LOG.error("Device transaction for device {} was not found!", deviceId);
82                 return Optional.empty();
83             }
84         } catch (InterruptedException | ExecutionException e) {
85             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
86             return Optional.empty();
87         }
88
89         // post the cross connect on the device
90         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, oduConnectionIID, oduConnectionBuilder.build());
91         FluentFuture<? extends @NonNull CommitInfo> commit =
92             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
93         try {
94             commit.get();
95             LOG.info("Otn-connection successfully created: {}-{}", srcTp, dstTp);
96             return Optional.of(srcTp + "-x-" + dstTp);
97         } catch (InterruptedException | ExecutionException e) {
98             LOG.warn("Failed to post {}.", oduConnectionBuilder.build(), e);
99         }
100         return Optional.empty();
101
102     }
103
104     public List<String> deleteOtnCrossConnect(String deviceId, String connectionName) {
105         List<String> interfList = new ArrayList<>();
106         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device
107             .container.org.openroadm.device.OduConnection> otnXc = getOtnCrossConnect(deviceId, connectionName);
108
109         if (otnXc.isPresent()) {
110             interfList.add(otnXc.get().getSource().getSrcIf());
111             interfList.add(otnXc.get().getDestination().getDstIf());
112         } else {
113             LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
114             return null;
115         }
116         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
117         DeviceTransaction deviceTx;
118         try {
119             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
120             if (deviceTxOpt.isPresent()) {
121                 deviceTx = deviceTxOpt.get();
122             } else {
123                 LOG.error("Device transaction for device {} was not found!", deviceId);
124                 return null;
125             }
126         } catch (InterruptedException | ExecutionException e) {
127             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
128             return null;
129         }
130
131         // delete the cross connect on the device
132         deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateOduConnectionIID(connectionName));
133         FluentFuture<? extends @NonNull CommitInfo> commit =
134             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
135         try {
136             commit.get();
137             LOG.info("Connection {} successfully deleted on {}", connectionName, deviceId);
138             return interfList;
139         } catch (InterruptedException | ExecutionException e) {
140             LOG.warn("Failed to delete {}", connectionName, e);
141         }
142         return null;
143     }
144
145 }