2 * Copyright © 2017 AT&T and others. All rights reserved.
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
9 package org.opendaylight.transportpce.common.openroadminterfaces;
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;
39 public class OpenRoadmInterfacesImpl121 {
41 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl121.class);
43 private final DeviceTransactionManager deviceTransactionManager;
45 public OpenRoadmInterfacesImpl121(DeviceTransactionManager deviceTransactionManager) {
46 this.deviceTransactionManager = deviceTransactionManager;
49 public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
50 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
51 DeviceTransaction deviceTx;
53 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
54 if (deviceTxOpt.isPresent()) {
55 deviceTx = deviceTxOpt.get();
57 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
60 } catch (InterruptedException | ExecutionException e) {
61 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
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() {
79 } catch (InterruptedException e) {
80 LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
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);
94 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
95 ifBuilder.getName(), ifBuilder.getSupportingPort());
98 } catch (InterruptedException | ExecutionException e) {
99 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
100 .getName(), nodeId), e);
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);
113 public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
114 Optional<Interface> intf2DeleteOpt;
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);
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
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);
136 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
137 Interface.class, new InterfaceKey(interfaceName));
138 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
140 DeviceTransaction deviceTx;
142 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
143 if (deviceTxOpt.isPresent()) {
144 deviceTx = deviceTxOpt.get();
146 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
149 } catch (InterruptedException | ExecutionException e) {
150 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
154 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
155 FluentFuture<? extends @NonNull CommitInfo> commit =
156 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
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);
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);
172 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
176 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
177 throws OpenRoadmInterfaceException {
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()) {
188 throw new OpenRoadmInterfaceException(String.format(
189 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
191 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
192 boolean change = false;
194 if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedInuse)) {
195 cpBldr.setEquipmentState(States.NotReservedInuse);
199 (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
200 cpBldr.setEquipmentState(States.NotReservedAvailable);
204 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
206 DeviceTransaction deviceTx;
208 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
209 if (deviceTxOpt.isPresent()) {
210 deviceTx = deviceTxOpt.get();
212 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
215 } catch (InterruptedException | ExecutionException e) {
216 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
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);
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!",
232 public String getSupportedInterface(String nodeId, String interf) {
233 Optional<Interface> supInterfOpt;
235 supInterfOpt = getInterface(nodeId, interf);
236 if (supInterfOpt.isPresent()) {
237 return supInterfOpt.get().getSupportingInterface();
241 } catch (OpenRoadmInterfaceException e) {
242 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
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) {
256 for (Interfaces interf : port.getInterfaces()) {
257 if (interf.getInterfaceName().equals(ifBuilder.getName())) {