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