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.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOdu;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOtu;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev161014.maint.loopback.MaintLoopbackBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev161014.maint.testsignal.MaintTestsignalBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.container.OduBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class OpenRoadmInterfacesImpl121 {
49 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl121.class);
51 private final DeviceTransactionManager deviceTransactionManager;
53 public OpenRoadmInterfacesImpl121(DeviceTransactionManager deviceTransactionManager) {
54 this.deviceTransactionManager = deviceTransactionManager;
57 public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
58 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
59 DeviceTransaction deviceTx;
61 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
62 if (deviceTxOpt.isPresent()) {
63 deviceTx = deviceTxOpt.get();
65 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
68 } catch (InterruptedException | ExecutionException e) {
69 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
73 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
74 Interface.class, new InterfaceKey(ifBuilder.getName()));
75 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
76 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
77 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
78 // TODO: instead of using this infinite loop coupled with this timeout,
79 // it would be better to use a notification mechanism from the device to be advertised
80 // that the new created interface is present in the device circuit-pack/port
81 final Thread current = Thread.currentThread();
82 Thread timer = new Thread() {
87 } catch (InterruptedException e) {
88 LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
94 LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
95 boolean devicePortIsUptodated = false;
96 while (!devicePortIsUptodated) {
97 devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
99 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
100 ifBuilder.getName(), ifBuilder.getSupportingPort());
102 } catch (InterruptedException | ExecutionException e) {
103 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
104 .getName(), nodeId), e);
109 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
110 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
111 .child(Interface.class, new InterfaceKey(interfaceName));
112 return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
113 interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
117 public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
118 Optional<Interface> intf2DeleteOpt;
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);
125 if (intf2DeleteOpt.isPresent()) {
126 Interface intf2Delete = intf2DeleteOpt.get();
127 // State admin state to out of service
128 InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
129 if (ifBuilder.getType() == OtnOdu.class) {
130 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
131 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
132 if (odu.getMaintTestsignal() != null) {
133 MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
134 maintSignalBuilder.setEnabled(false);
135 odu.setMaintTestsignal(maintSignalBuilder.build());
137 oduBuilder.setOdu(odu.build());
138 ifBuilder.addAugmentation(oduBuilder.build());
139 } else if (ifBuilder.getType() == OtnOtu.class) {
140 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder
142 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder(
143 intf2Delete.augmentation(
144 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1
146 OtuBuilder otu = new OtuBuilder(otuBuilder.getOtu());
147 if (otu.getMaintLoopback() != null) {
148 MaintLoopbackBuilder maintLoopBackBuilder = new MaintLoopbackBuilder();
149 maintLoopBackBuilder.setEnabled(false);
150 otu.setMaintLoopback(maintLoopBackBuilder.build());
152 otuBuilder.setOtu(otu.build());
153 ifBuilder.addAugmentation(otuBuilder.build());
155 ifBuilder.setAdministrativeState(AdminStates.OutOfService);
156 // post interface with updated admin state
158 postInterface(nodeId, ifBuilder);
159 } catch (OpenRoadmInterfaceException ex) {
160 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
161 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
164 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
165 Interface.class, new InterfaceKey(interfaceName));
166 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
168 DeviceTransaction deviceTx;
170 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
171 if (deviceTxOpt.isPresent()) {
172 deviceTx = deviceTxOpt.get();
174 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
177 } catch (InterruptedException | ExecutionException e) {
178 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
182 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
183 FluentFuture<? extends @NonNull CommitInfo> commit =
184 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
188 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
189 } catch (InterruptedException | ExecutionException e) {
190 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
191 interfaceName, nodeId), e);
193 // change the equipment state on circuit pack if xpdr node
194 if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
195 StringConstants.NETWORK_TOKEN)) {
196 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
200 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
204 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
205 throws OpenRoadmInterfaceException {
207 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
208 CircuitPacks.class, new CircuitPacksKey(circuitPackName));
209 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
210 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
211 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
212 CircuitPacks cp = null;
213 if (cpOpt.isPresent()) {
216 throw new OpenRoadmInterfaceException(String.format(
217 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
219 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
220 boolean change = false;
222 if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedInuse)) {
223 cpBldr.setEquipmentState(States.NotReservedInuse);
227 (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
228 cpBldr.setEquipmentState(States.NotReservedAvailable);
232 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
234 DeviceTransaction deviceTx;
236 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
237 if (deviceTxOpt.isPresent()) {
238 deviceTx = deviceTxOpt.get();
240 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
243 } catch (InterruptedException | ExecutionException e) {
244 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
247 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
248 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
249 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
251 txSubmitFuture.get();
252 LOG.info("Successfully posted equipment state change on node {}", nodeId);
253 } catch (InterruptedException | ExecutionException e) {
254 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
260 public String getSupportedInterface(String nodeId, String interf) {
261 Optional<Interface> supInterfOpt;
263 supInterfOpt = getInterface(nodeId, interf);
264 if (supInterfOpt.isPresent()) {
265 return supInterfOpt.get().getSupportingInterface();
269 } catch (OpenRoadmInterfaceException e) {
270 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
275 private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
276 KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
277 .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
278 .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
279 Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
280 portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
281 if (port.getInterfaces() == null) {
284 for (Interfaces interf : port.getInterfaces()) {
285 if (interf.getInterfaceName().equals(ifBuilder.getName())) {