Adapt TransportPCE code to Sulfur
[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.OrgOpenroadmDeviceData;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.port.Interfaces;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.AdminStates;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.States;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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
66             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
67             .child(Interface.class, new InterfaceKey(ifBuilder.getName()))
68             .build();
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/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
89             // this check is not needed during the delete operation
90             // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
91             if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
92                 boolean devicePortIsUptodated = false;
93                 while (!devicePortIsUptodated) {
94                     devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
95                 }
96                 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
97                     ifBuilder.getName(), ifBuilder.getSupportingPort());
98             }
99             timer.interrupt();
100         } catch (InterruptedException | ExecutionException e) {
101             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
102                 .getName(), nodeId), e);
103         }
104     }
105
106
107     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
108         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
109             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
110             .child(Interface.class, new InterfaceKey(interfaceName))
111             .build();
112         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
113             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
114     }
115
116
117     public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
118         Optional<Interface> intf2DeleteOpt;
119         try {
120             intf2DeleteOpt = getInterface(nodeId, interfaceName);
121         } catch (OpenRoadmInterfaceException e) {
122             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
123                 interfaceName, nodeId), e);
124         }
125         if (intf2DeleteOpt.isPresent()) {
126             Interface intf2Delete = intf2DeleteOpt.get();
127             // State admin state to out of service
128             InterfaceBuilder ifBuilder = new InterfaceBuilder()
129                 .setAdministrativeState(AdminStates.OutOfService)
130                 .setName(intf2Delete.getName())
131                 .setType(intf2Delete.getType());
132             // post interface with updated admin state
133             try {
134                 postInterface(nodeId, ifBuilder);
135             } catch (OpenRoadmInterfaceException ex) {
136                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
137                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
138             }
139
140             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
141                 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
142                 .child(Interface.class, new InterfaceKey(interfaceName))
143                 .build();
144             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
145                 nodeId);
146             DeviceTransaction deviceTx;
147             try {
148                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
149                 if (deviceTxOpt.isPresent()) {
150                     deviceTx = deviceTxOpt.get();
151                 } else {
152                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
153                         nodeId));
154                 }
155             } catch (InterruptedException | ExecutionException e) {
156                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
157                     nodeId), e);
158             }
159
160             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
161             FluentFuture<? extends @NonNull CommitInfo> commit =
162                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
163
164             try {
165                 commit.get();
166                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
167             } catch (InterruptedException | ExecutionException e) {
168                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
169                     interfaceName, nodeId), e);
170             }
171             // change the equipment state on circuit pack if xpdr node
172             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
173                 StringConstants.NETWORK_TOKEN)) {
174                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
175             }
176
177         } else {
178             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
179         }
180     }
181
182     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
183         throws OpenRoadmInterfaceException {
184
185         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier
186             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
187             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName))
188             .build();
189         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
190             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
191             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
192         CircuitPacks cp = null;
193         if (cpOpt.isPresent()) {
194             cp = cpOpt.get();
195         } else {
196             throw new OpenRoadmInterfaceException(String.format(
197                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
198         }
199         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
200         boolean change = false;
201         if (activate) {
202             if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedInuse)) {
203                 cpBldr.setEquipmentState(States.NotReservedInuse);
204                 change = true;
205             }
206         } else if (
207             (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
208             cpBldr.setEquipmentState(States.NotReservedAvailable);
209             change = true;
210         }
211         if (change) {
212             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
213                 nodeId);
214             DeviceTransaction deviceTx;
215             try {
216                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
217                 if (deviceTxOpt.isPresent()) {
218                     deviceTx = deviceTxOpt.get();
219                 } else {
220                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
221                         nodeId));
222                 }
223             } catch (InterruptedException | ExecutionException e) {
224                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
225                     nodeId), e);
226             }
227             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
228             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
229                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
230             try {
231                 txSubmitFuture.get();
232                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
233             } catch (InterruptedException | ExecutionException e) {
234                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
235                     nodeId), e);
236             }
237         }
238     }
239
240     public String getSupportedInterface(String nodeId, String interf) {
241         Optional<Interface> supInterfOpt;
242         try {
243             supInterfOpt = getInterface(nodeId, interf);
244             if (supInterfOpt.isPresent()) {
245                 return supInterfOpt.get().getSupportingInterface();
246             } else {
247                 return null;
248             }
249         } catch (OpenRoadmInterfaceException e) {
250             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
251             return null;
252         }
253     }
254
255     private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
256         InstanceIdentifier<Ports> portIID = InstanceIdentifier
257             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
258             .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
259             .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()))
260             .build();
261         Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
262             portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
263         if (port.getInterfaces() == null) {
264             return false;
265         }
266         for (Interfaces interf : port.getInterfaces()) {
267             if (interf.getInterfaceName().equals(ifBuilder.getName())) {
268                 return true;
269             }
270         }
271         return false;
272     }
273 }