Simplify delete operations
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl121.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.common.openroadminterfaces;
10
11 import com.google.common.util.concurrent.FluentFuture;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.transportpce.common.StringConstants;
19 import org.opendaylight.transportpce.common.Timeouts;
20 import org.opendaylight.transportpce.common.device.DeviceTransaction;
21 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.port.Interfaces;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.AdminStates;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.States;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class OpenRoadmInterfacesImpl121 {
40
41     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl121.class);
42
43     private final DeviceTransactionManager deviceTransactionManager;
44
45     public OpenRoadmInterfacesImpl121(DeviceTransactionManager deviceTransactionManager) {
46         this.deviceTransactionManager = deviceTransactionManager;
47     }
48
49     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
50         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
51         DeviceTransaction deviceTx;
52         try {
53             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
54             if (deviceTxOpt.isPresent()) {
55                 deviceTx = deviceTxOpt.get();
56             } else {
57                 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
58                     nodeId));
59             }
60         } catch (InterruptedException | ExecutionException e) {
61             throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
62                 nodeId), e);
63         }
64
65         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
66             Interface.class, new InterfaceKey(ifBuilder.getName()));
67         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
68         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
69             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
70         // TODO: instead of using this infinite loop coupled with this timeout,
71         // it would be better to use a notification mechanism from the device to be advertised
72         // that the new created interface is present in the device circuit-pack/port
73         final Thread current = Thread.currentThread();
74         Thread timer = new Thread() {
75             public void run() {
76                 try {
77                     Thread.sleep(3000);
78                     current.interrupt();
79                 } catch (InterruptedException e) {
80                     LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
81                 }
82             }
83         };
84         try {
85             txSubmitFuture.get();
86             LOG.info("Successfully posted/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
87             // this check is not needed during the delete operation
88             // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
89             if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
90                 boolean devicePortIsUptodated = false;
91                 while (!devicePortIsUptodated) {
92                     devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
93                 }
94                 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
95                     ifBuilder.getName(), ifBuilder.getSupportingPort());
96             }
97             timer.interrupt();
98         } catch (InterruptedException | ExecutionException e) {
99             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
100                 .getName(), nodeId), e);
101         }
102     }
103
104
105     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
106         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
107             .child(Interface.class, new InterfaceKey(interfaceName));
108         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
109             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
110     }
111
112
113     public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
114         Optional<Interface> intf2DeleteOpt;
115         try {
116             intf2DeleteOpt = getInterface(nodeId, interfaceName);
117         } catch (OpenRoadmInterfaceException e) {
118             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
119                 interfaceName, nodeId), e);
120         }
121         if (intf2DeleteOpt.isPresent()) {
122             Interface intf2Delete = intf2DeleteOpt.get();
123             // State admin state to out of service
124             InterfaceBuilder ifBuilder = new InterfaceBuilder()
125                 .setAdministrativeState(AdminStates.OutOfService)
126                 .setName(intf2Delete.getName())
127                 .setType(intf2Delete.getType());
128             // post interface with updated admin state
129             try {
130                 postInterface(nodeId, ifBuilder);
131             } catch (OpenRoadmInterfaceException ex) {
132                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
133                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
134             }
135
136             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
137                 Interface.class, new InterfaceKey(interfaceName));
138             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
139                 nodeId);
140             DeviceTransaction deviceTx;
141             try {
142                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
143                 if (deviceTxOpt.isPresent()) {
144                     deviceTx = deviceTxOpt.get();
145                 } else {
146                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
147                         nodeId));
148                 }
149             } catch (InterruptedException | ExecutionException e) {
150                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
151                     nodeId), e);
152             }
153
154             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
155             FluentFuture<? extends @NonNull CommitInfo> commit =
156                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
157
158             try {
159                 commit.get();
160                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
161             } catch (InterruptedException | ExecutionException e) {
162                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
163                     interfaceName, nodeId), e);
164             }
165             // change the equipment state on circuit pack if xpdr node
166             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
167                 StringConstants.NETWORK_TOKEN)) {
168                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
169             }
170
171         } else {
172             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
173         }
174     }
175
176     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
177         throws OpenRoadmInterfaceException {
178
179         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
180             CircuitPacks.class, new CircuitPacksKey(circuitPackName));
181         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
182             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
183             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
184         CircuitPacks cp = null;
185         if (cpOpt.isPresent()) {
186             cp = cpOpt.get();
187         } else {
188             throw new OpenRoadmInterfaceException(String.format(
189                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
190         }
191         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
192         boolean change = false;
193         if (activate) {
194             if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedInuse)) {
195                 cpBldr.setEquipmentState(States.NotReservedInuse);
196                 change = true;
197             }
198         } else if (
199             (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
200             cpBldr.setEquipmentState(States.NotReservedAvailable);
201             change = true;
202         }
203         if (change) {
204             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
205                 nodeId);
206             DeviceTransaction deviceTx;
207             try {
208                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
209                 if (deviceTxOpt.isPresent()) {
210                     deviceTx = deviceTxOpt.get();
211                 } else {
212                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
213                         nodeId));
214                 }
215             } catch (InterruptedException | ExecutionException e) {
216                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
217                     nodeId), e);
218             }
219             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
220             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
221                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
222             try {
223                 txSubmitFuture.get();
224                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
225             } catch (InterruptedException | ExecutionException e) {
226                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
227                     nodeId), e);
228             }
229         }
230     }
231
232     public String getSupportedInterface(String nodeId, String interf) {
233         Optional<Interface> supInterfOpt;
234         try {
235             supInterfOpt = getInterface(nodeId, interf);
236             if (supInterfOpt.isPresent()) {
237                 return supInterfOpt.get().getSupportingInterface();
238             } else {
239                 return null;
240             }
241         } catch (OpenRoadmInterfaceException e) {
242             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
243             return null;
244         }
245     }
246
247     private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
248         KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
249             .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
250             .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
251         Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
252             portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
253         if (port.getInterfaces() == null) {
254             return false;
255         }
256         for (Interfaces interf : port.getInterfaces()) {
257             if (interf.getInterfaceName().equals(ifBuilder.getName())) {
258                 return true;
259             }
260         }
261         return false;
262     }
263 }