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.transportpce.common.mapping.PortMapping;
23 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
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.rev181019.circuit.pack.Ports;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsKey;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.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 OpenRoadmInterfacesImpl221 {
44 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl221.class);
46 private final DeviceTransactionManager deviceTransactionManager;
47 private final PortMapping portMapping;
48 private final PortMappingVersion221 portMapping221;
50 public OpenRoadmInterfacesImpl221(DeviceTransactionManager deviceTransactionManager,
51 PortMapping portMapping, PortMappingVersion221 portMapping221) {
52 this.deviceTransactionManager = deviceTransactionManager;
53 this.portMapping = portMapping;
54 this.portMapping221 = portMapping221;
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/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
97 // this check is not needed during the delete operation
98 // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
99 if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
100 boolean devicePortIsUptodated = false;
101 while (!devicePortIsUptodated) {
102 devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
104 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
105 ifBuilder.getName(), ifBuilder.getSupportingPort());
108 } catch (InterruptedException | ExecutionException e) {
109 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
110 .getName(), nodeId), e);
115 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
116 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
117 .child(Interface.class, new InterfaceKey(interfaceName));
118 return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
119 interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
123 public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
124 LOG.info("deleting interface {} on device221 {}", interfaceName, nodeId);
125 Optional<Interface> intf2DeleteOpt;
127 intf2DeleteOpt = getInterface(nodeId, interfaceName);
128 } catch (OpenRoadmInterfaceException e) {
129 throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
130 interfaceName, nodeId), e);
132 if (intf2DeleteOpt.isPresent()) {
133 Interface intf2Delete = intf2DeleteOpt.get();
134 // State admin state to out of service
135 InterfaceBuilder ifBuilder = new InterfaceBuilder()
136 .setAdministrativeState(AdminStates.OutOfService)
137 .setName(intf2Delete.getName())
138 .setType(intf2Delete.getType());
139 // post interface with updated admin state
141 postInterface(nodeId, ifBuilder);
142 } catch (OpenRoadmInterfaceException ex) {
143 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
144 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
147 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
148 Interface.class, new InterfaceKey(interfaceName));
149 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
151 DeviceTransaction deviceTx;
153 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
154 if (deviceTxOpt.isPresent()) {
155 deviceTx = deviceTxOpt.get();
157 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
160 } catch (InterruptedException | ExecutionException e) {
161 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
165 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
166 FluentFuture<? extends @NonNull CommitInfo> commit =
167 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
171 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
172 } catch (InterruptedException | ExecutionException e) {
173 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
174 interfaceName, nodeId), e);
176 // change the equipment state on circuit pack if xpdr node
177 if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
178 StringConstants.NETWORK_TOKEN)) {
179 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
180 Mapping oldmapping = this.portMapping.getMapping(nodeId, intf2Delete.getSupportingCircuitPackName(),
181 intf2Delete.getSupportingPort());
182 this.portMapping.deleteMapping(nodeId, oldmapping.getLogicalConnectionPoint());
183 this.portMapping221.updateMapping(nodeId, oldmapping);
187 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
191 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
192 throws OpenRoadmInterfaceException {
193 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
194 CircuitPacks.class, new CircuitPacksKey(circuitPackName));
195 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
196 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
197 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
198 CircuitPacks cp = null;
199 if (cpOpt.isPresent()) {
202 throw new OpenRoadmInterfaceException(String.format(
203 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
205 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
206 boolean change = false;
208 if (cpBldr.getEquipmentState() != null
209 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
210 cpBldr.setEquipmentState(States.NotReservedInuse);
213 } else if ((cpBldr.getEquipmentState() != null
214 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
215 cpBldr.setEquipmentState(States.NotReservedAvailable);
219 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
221 DeviceTransaction deviceTx;
223 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
224 if (deviceTxOpt.isPresent()) {
225 deviceTx = deviceTxOpt.get();
227 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
230 } catch (InterruptedException | ExecutionException e) {
231 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
234 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
235 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
236 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
238 txSubmitFuture.get();
239 LOG.info("Successfully posted equipment state change on node {}", nodeId);
240 } catch (InterruptedException | ExecutionException e) {
241 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
247 public String getSupportedInterface(String nodeId, String interf) {
248 Optional<Interface> supInterfOpt;
250 supInterfOpt = getInterface(nodeId, interf);
251 if (supInterfOpt.isPresent()) {
252 return supInterfOpt.get().getSupportingInterface();
256 } catch (OpenRoadmInterfaceException e) {
257 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
262 private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
263 KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
264 .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
265 .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
266 Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
267 portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
268 if (port.getInterfaces() == null) {
271 for (Interfaces interf : port.getInterfaces()) {
272 if (interf.getInterfaceName().equals(ifBuilder.getName())) {