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.rev220316.mapping.Mapping;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OrgOpenroadmDeviceData;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.Interface;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.port.Interfaces;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.States;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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
74 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
75 .child(Interface.class, new InterfaceKey(ifBuilder.getName()))
77 LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
78 ifBuilder.getType().toString());
79 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
80 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
81 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
82 // TODO: instead of using this infinite loop coupled with this timeout,
83 // it would be better to use a notification mechanism from the device to be advertised
84 // that the new created interface is present in the device circuit-pack/port
85 final Thread current = Thread.currentThread();
86 Thread timer = new Thread() {
91 } catch (InterruptedException e) {
92 LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
98 LOG.info("Successfully posted/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
99 // this check is not needed during the delete operation
100 // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
101 if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
102 boolean devicePortIsUptodated = false;
103 while (!devicePortIsUptodated) {
104 devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
106 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
107 ifBuilder.getName(), ifBuilder.getSupportingPort());
110 } catch (InterruptedException | ExecutionException e) {
111 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
112 .getName(), nodeId), e);
117 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
118 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
119 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
120 .child(Interface.class, new InterfaceKey(interfaceName))
122 return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
123 interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
127 public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
128 LOG.info("deleting interface {} on device71 {}", interfaceName, nodeId);
129 Optional<Interface> intf2DeleteOpt;
131 intf2DeleteOpt = getInterface(nodeId, interfaceName);
132 } catch (OpenRoadmInterfaceException e) {
133 throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
134 interfaceName, nodeId), e);
136 if (intf2DeleteOpt.isPresent()) {
137 Interface intf2Delete = intf2DeleteOpt.get();
138 // set the name and set the type. Having these two lines will post the interface with just
139 // name, type and admin-state, without all the default values such as maint-testsignal
140 // delete the interfaces successfully
141 // just build a new Interface builder without the arguments for inter2Delete
142 InterfaceBuilder ifBuilder = new InterfaceBuilder()
143 .setAdministrativeState(AdminStates.OutOfService)
144 // Though these could be redundant, but 'when' statements are causing problem,
145 // when deleting the interfaces trying to be deleted
146 .setName(intf2Delete.getName())
147 .setType(intf2Delete.getType());
149 // post interface with updated admin state
151 postInterface(nodeId, ifBuilder);
152 } catch (OpenRoadmInterfaceException ex) {
153 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
154 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
157 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
158 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
159 .child(Interface.class, new InterfaceKey(interfaceName))
161 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
163 DeviceTransaction deviceTx;
165 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
166 if (deviceTxOpt.isPresent()) {
167 deviceTx = deviceTxOpt.get();
169 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
172 } catch (InterruptedException | ExecutionException e) {
173 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
177 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
178 FluentFuture<? extends @NonNull CommitInfo> commit =
179 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
183 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
184 } catch (InterruptedException | ExecutionException e) {
185 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
186 interfaceName, nodeId), e);
188 // change the equipment state on circuit pack if xpdr node
189 if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
190 StringConstants.NETWORK_TOKEN)) {
191 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
192 // Here we update the port-mapping data after the interface delete
193 Mapping oldMapping = this.portMapping.getMapping(
194 nodeId, intf2Delete.getSupportingCircuitPackName(), intf2Delete.getSupportingPort());
195 this.portMapping.deleteMapping(nodeId, oldMapping.getLogicalConnectionPoint());
196 this.portMapping710.updateMapping(nodeId, oldMapping);
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 {
206 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier
207 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
208 .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName))
210 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
211 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
212 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
213 CircuitPacks cp = null;
214 if (cpOpt.isPresent()) {
217 throw new OpenRoadmInterfaceException(String.format(
218 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
220 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
221 boolean change = false;
223 if (cpBldr.getEquipmentState() != null
224 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
225 cpBldr.setEquipmentState(States.NotReservedInuse);
228 } else if ((cpBldr.getEquipmentState() != null
229 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
230 cpBldr.setEquipmentState(States.NotReservedAvailable);
234 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
236 DeviceTransaction deviceTx;
238 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
239 if (deviceTxOpt.isPresent()) {
240 deviceTx = deviceTxOpt.get();
242 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
245 } catch (InterruptedException | ExecutionException e) {
246 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
249 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
250 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
251 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
253 txSubmitFuture.get();
254 LOG.info("Successfully posted equipment state change on node {}", nodeId);
255 } catch (InterruptedException | ExecutionException e) {
256 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
262 public String getSupportedInterface(String nodeId, String interf) {
263 Optional<Interface> supInterfOpt;
265 supInterfOpt = getInterface(nodeId, interf);
266 if (supInterfOpt.isPresent()) {
267 return supInterfOpt.get().getSupportingInterfaceList().stream().findFirst().get();
271 } catch (OpenRoadmInterfaceException e) {
272 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
277 private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
278 InstanceIdentifier<Ports> portIID = InstanceIdentifier
279 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
280 .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
281 .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()))
283 Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
284 portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
285 if (port.getInterfaces() == null) {
288 for (Interfaces interf : port.getInterfaces()) {
289 if (interf.getInterfaceName().equals(ifBuilder.getName())) {