Merge "Refactor ConvertORTopoToTapiTopoTest"
[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.rev220926.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         LOG.debug("Client TP: {}, Network TP: {}, Network2TP: {} SrcTP: {}, DstTP: {}",
61                 node.getClientTp(), node.getNetworkTp(), node.getNetwork2Tp(), srcTp, dstTp);
62         if (!srcTp.contains(node.getClientTp())) {
63             // If the src-tp does not contain client port, then we swap src-tp & dest-tp
64             String tmp;
65             tmp = dstTp;
66             dstTp = srcTp;
67             srcTp = tmp;
68             LOG.debug("After swap, SrcTP: {}, DstTP: {}", srcTp, dstTp);
69         }
70         // Strip the service name from the src and dst
71         String oduXConnectionName = srcTp.split(":")[0] + "-x-" + dstTp.split(":")[0];
72         OduConnectionBuilder oduConnectionBuilder = new OduConnectionBuilder()
73             .setConnectionName(oduXConnectionName)
74             .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.odu.connection
75                 .DestinationBuilder().setDstIf(dstTp).build())
76             .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.odu.connection
77                 .SourceBuilder().setSrcIf(srcTp).build())
78             .setDirection(Direction.Bidirectional);
79
80         InstanceIdentifier<OduConnection> oduConnectionIID = InstanceIdentifier
81             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
82             .child(OduConnection.class, new OduConnectionKey(oduConnectionBuilder.getConnectionName()))
83             .build();
84
85         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
86         DeviceTransaction deviceTx;
87         try {
88             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
89             if (deviceTxOpt.isPresent()) {
90                 deviceTx = deviceTxOpt.orElseThrow();
91             } else {
92                 LOG.error("Device transaction for device {} was not found!", deviceId);
93                 return Optional.empty();
94             }
95         } catch (InterruptedException | ExecutionException e) {
96             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
97             return Optional.empty();
98         }
99
100         // post the cross connect on the device
101         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, oduConnectionIID, oduConnectionBuilder.build());
102         FluentFuture<? extends @NonNull CommitInfo> commit =
103             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
104         try {
105             commit.get();
106             LOG.info("Otn-connection successfully created: {}", oduXConnectionName);
107             return Optional.of(oduXConnectionName);
108         } catch (InterruptedException | ExecutionException e) {
109             LOG.warn("Failed to post {}.", oduConnectionBuilder.build(), e);
110         }
111         return Optional.empty();
112
113     }
114
115     public List<String> deleteOtnCrossConnect(String deviceId, String connectionName) {
116         List<String> interfList = new ArrayList<>();
117         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device
118             .container.org.openroadm.device.OduConnection> otnXc = getOtnCrossConnect(deviceId, connectionName);
119
120         if (otnXc.isPresent()) {
121             interfList.add(otnXc.orElseThrow().getSource().getSrcIf());
122             interfList.add(otnXc.orElseThrow().getDestination().getDstIf());
123         } else {
124             LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
125             return null;
126         }
127         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
128         DeviceTransaction deviceTx;
129         try {
130             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
131             if (deviceTxOpt.isPresent()) {
132                 deviceTx = deviceTxOpt.orElseThrow();
133             } else {
134                 LOG.error("Device transaction for device {} was not found!", deviceId);
135                 return null;
136             }
137         } catch (InterruptedException | ExecutionException e) {
138             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
139             return null;
140         }
141
142         // delete the cross connect on the device
143         deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateOduConnectionIID(connectionName));
144         FluentFuture<? extends @NonNull CommitInfo> commit =
145             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
146         try {
147             commit.get();
148             LOG.info("Connection {} successfully deleted on {}", connectionName, deviceId);
149             return interfList;
150         } catch (InterruptedException | ExecutionException e) {
151             LOG.warn("Failed to delete {}", connectionName, e);
152         }
153         return null;
154     }
155
156 }