2 * Copyright © 2020 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.transportpce.common.mapping.PortMapping;
23 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220114.mapping.Mapping;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.Interface;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.port.Interfaces;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.States;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class OpenRoadmInterfacesImpl710 {
44 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl710.class);
46 private final DeviceTransactionManager deviceTransactionManager;
47 private final PortMapping portMapping;
48 private final PortMappingVersion710 portMapping710;
50 public OpenRoadmInterfacesImpl710(DeviceTransactionManager deviceTransactionManager,
51 PortMapping portMapping, PortMappingVersion710 portMapping710) {
52 this.deviceTransactionManager = deviceTransactionManager;
53 this.portMapping = portMapping;
54 this.portMapping710 = portMapping710;
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 LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
76 ifBuilder.getType().toString());
77 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
78 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
79 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
80 // TODO: instead of using this infinite loop coupled with this timeout,
81 // it would be better to use a notification mechanism from the device to be advertised
82 // that the new created interface is present in the device circuit-pack/port
83 final Thread current = Thread.currentThread();
84 Thread timer = new Thread() {
89 } catch (InterruptedException e) {
90 LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
96 LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
97 boolean devicePortIsUptodated = false;
98 while (!devicePortIsUptodated) {
99 devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
101 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
102 ifBuilder.getName(), ifBuilder.getSupportingPort());
104 } catch (InterruptedException | ExecutionException e) {
105 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
106 .getName(), nodeId), e);
111 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
112 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
113 .child(Interface.class, new InterfaceKey(interfaceName));
114 return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
115 interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
119 public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
120 LOG.info("deleting interface {} on device71 {}", interfaceName, nodeId);
121 Optional<Interface> intf2DeleteOpt;
123 intf2DeleteOpt = getInterface(nodeId, interfaceName);
124 } catch (OpenRoadmInterfaceException e) {
125 throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
126 interfaceName, nodeId), e);
128 if (intf2DeleteOpt.isPresent()) {
129 Interface intf2Delete = intf2DeleteOpt.get();
130 // set the name and set the type. Having these two lines will post the interface with just
131 // name, type and admin-state, without all the default values such as maint-testsignal
132 // delete the interfaces successfully
133 // just build a new Interface builder without the arguments for inter2Delete
134 InterfaceBuilder ifBuilder = new InterfaceBuilder()
135 .setAdministrativeState(AdminStates.OutOfService)
136 // Though these could be redundant, but 'when' statements are causing problem,
137 // when deleting the interfaces trying to be deleted
138 .setName(intf2Delete.getName())
139 .setType(intf2Delete.getType())
140 // CP name and the ports are needed, since the post interface is validated
141 .setSupportingCircuitPackName(intf2Delete.getSupportingCircuitPackName())
142 .setSupportingPort(intf2Delete.getSupportingPort());
144 // post interface with updated admin state
146 postInterface(nodeId, ifBuilder);
147 } catch (OpenRoadmInterfaceException ex) {
148 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
149 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
152 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
153 Interface.class, new InterfaceKey(interfaceName));
154 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
156 DeviceTransaction deviceTx;
158 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
159 if (deviceTxOpt.isPresent()) {
160 deviceTx = deviceTxOpt.get();
162 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
165 } catch (InterruptedException | ExecutionException e) {
166 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
170 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
171 FluentFuture<? extends @NonNull CommitInfo> commit =
172 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
176 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
177 } catch (InterruptedException | ExecutionException e) {
178 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
179 interfaceName, nodeId), e);
181 // change the equipment state on circuit pack if xpdr node
182 if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
183 StringConstants.NETWORK_TOKEN)) {
184 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
185 // Here we update the port-mapping data after the interface delete
186 Mapping oldMapping = this.portMapping.getMapping(
187 nodeId, intf2Delete.getSupportingCircuitPackName(), intf2Delete.getSupportingPort());
188 this.portMapping.deleteMapping(nodeId, oldMapping.getLogicalConnectionPoint());
189 this.portMapping710.updateMapping(nodeId, oldMapping);
193 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
197 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
198 throws OpenRoadmInterfaceException {
199 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
200 CircuitPacks.class, new CircuitPacksKey(circuitPackName));
201 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
202 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
203 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
204 CircuitPacks cp = null;
205 if (cpOpt.isPresent()) {
208 throw new OpenRoadmInterfaceException(String.format(
209 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
211 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
212 boolean change = false;
214 if (cpBldr.getEquipmentState() != null
215 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
216 cpBldr.setEquipmentState(States.NotReservedInuse);
219 } else if ((cpBldr.getEquipmentState() != null
220 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
221 cpBldr.setEquipmentState(States.NotReservedAvailable);
225 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
227 DeviceTransaction deviceTx;
229 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
230 if (deviceTxOpt.isPresent()) {
231 deviceTx = deviceTxOpt.get();
233 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
236 } catch (InterruptedException | ExecutionException e) {
237 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
240 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
241 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
242 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
244 txSubmitFuture.get();
245 LOG.info("Successfully posted equipment state change on node {}", nodeId);
246 } catch (InterruptedException | ExecutionException e) {
247 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
253 public String getSupportedInterface(String nodeId, String interf) {
254 Optional<Interface> supInterfOpt;
256 supInterfOpt = getInterface(nodeId, interf);
257 if (supInterfOpt.isPresent()) {
258 return supInterfOpt.get().getSupportingInterfaceList().get(0);
262 } catch (OpenRoadmInterfaceException e) {
263 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
268 private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
269 KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
270 .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
271 .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
272 Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
273 portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
274 if (port.getInterfaces() == null) {
277 for (Interfaces interf : port.getInterfaces()) {
278 if (interf.getInterfaceName().equals(ifBuilder.getName())) {