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.rev220114.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.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOtu;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev171215.maint.loopback.MaintLoopbackBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev171215.maint.testsignal.MaintTestsignalBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.otu.container.OtuBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class OpenRoadmInterfacesImpl221 {
52 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl221.class);
54 private final DeviceTransactionManager deviceTransactionManager;
55 private final PortMapping portMapping;
56 private final PortMappingVersion221 portMapping221;
58 public OpenRoadmInterfacesImpl221(DeviceTransactionManager deviceTransactionManager,
59 PortMapping portMapping, PortMappingVersion221 portMapping221) {
60 this.deviceTransactionManager = deviceTransactionManager;
61 this.portMapping = portMapping;
62 this.portMapping221 = portMapping221;
65 public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
66 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
67 DeviceTransaction deviceTx;
69 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
70 if (deviceTxOpt.isPresent()) {
71 deviceTx = deviceTxOpt.get();
73 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
76 } catch (InterruptedException | ExecutionException e) {
77 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
81 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
82 Interface.class, new InterfaceKey(ifBuilder.getName()));
83 LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
84 ifBuilder.getType().toString());
85 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
86 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
87 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
88 // TODO: instead of using this infinite loop coupled with this timeout,
89 // it would be better to use a notification mechanism from the device to be advertised
90 // that the new created interface is present in the device circuit-pack/port
91 final Thread current = Thread.currentThread();
92 Thread timer = new Thread() {
97 } catch (InterruptedException e) {
98 LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
103 txSubmitFuture.get();
104 LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
105 boolean devicePortIsUptodated = false;
106 while (!devicePortIsUptodated) {
107 devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
109 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
110 ifBuilder.getName(), ifBuilder.getSupportingPort());
112 } catch (InterruptedException | ExecutionException e) {
113 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
114 .getName(), nodeId), e);
119 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
120 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
121 .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 device221 {}", 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 // State admin state to out of service
139 InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
140 if (ifBuilder.getType() == OtnOdu.class) {
141 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
142 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
143 if (odu.getMaintTestsignal() != null) {
144 MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
145 maintSignalBuilder.setEnabled(false);
146 odu.setMaintTestsignal(maintSignalBuilder.build());
148 oduBuilder.setOdu(odu.build());
149 ifBuilder.addAugmentation(oduBuilder.build());
150 } else if (ifBuilder.getType() == OtnOtu.class) {
151 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder
153 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder(
154 intf2Delete.augmentation(
155 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1
157 OtuBuilder otu = new OtuBuilder(otuBuilder.getOtu());
158 if (otu.getMaintLoopback() != null) {
159 MaintLoopbackBuilder maintLoopBackBuilder = new MaintLoopbackBuilder();
160 maintLoopBackBuilder.setEnabled(false);
161 otu.setMaintLoopback(maintLoopBackBuilder.build());
163 otuBuilder.setOtu(otu.build());
164 ifBuilder.addAugmentation(otuBuilder.build());
166 ifBuilder.setAdministrativeState(AdminStates.OutOfService);
167 // post interface with updated admin state
169 postInterface(nodeId, ifBuilder);
170 } catch (OpenRoadmInterfaceException ex) {
171 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
172 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
175 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
176 Interface.class, new InterfaceKey(interfaceName));
177 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
179 DeviceTransaction deviceTx;
181 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
182 if (deviceTxOpt.isPresent()) {
183 deviceTx = deviceTxOpt.get();
185 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
188 } catch (InterruptedException | ExecutionException e) {
189 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
193 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
194 FluentFuture<? extends @NonNull CommitInfo> commit =
195 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
199 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
200 } catch (InterruptedException | ExecutionException e) {
201 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
202 interfaceName, nodeId), e);
204 // change the equipment state on circuit pack if xpdr node
205 if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
206 StringConstants.NETWORK_TOKEN)) {
207 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
208 Mapping oldmapping = this.portMapping.getMapping(nodeId, intf2Delete.getSupportingCircuitPackName(),
209 intf2Delete.getSupportingPort());
210 this.portMapping.deleteMapping(nodeId, oldmapping.getLogicalConnectionPoint());
211 this.portMapping221.updateMapping(nodeId, oldmapping);
215 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
219 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
220 throws OpenRoadmInterfaceException {
221 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
222 CircuitPacks.class, new CircuitPacksKey(circuitPackName));
223 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
224 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
225 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
226 CircuitPacks cp = null;
227 if (cpOpt.isPresent()) {
230 throw new OpenRoadmInterfaceException(String.format(
231 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
233 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
234 boolean change = false;
236 if (cpBldr.getEquipmentState() != null
237 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
238 cpBldr.setEquipmentState(States.NotReservedInuse);
241 } else if ((cpBldr.getEquipmentState() != null
242 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
243 cpBldr.setEquipmentState(States.NotReservedAvailable);
247 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
249 DeviceTransaction deviceTx;
251 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
252 if (deviceTxOpt.isPresent()) {
253 deviceTx = deviceTxOpt.get();
255 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
258 } catch (InterruptedException | ExecutionException e) {
259 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
262 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
263 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
264 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
266 txSubmitFuture.get();
267 LOG.info("Successfully posted equipment state change on node {}", nodeId);
268 } catch (InterruptedException | ExecutionException e) {
269 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
275 public String getSupportedInterface(String nodeId, String interf) {
276 Optional<Interface> supInterfOpt;
278 supInterfOpt = getInterface(nodeId, interf);
279 if (supInterfOpt.isPresent()) {
280 return supInterfOpt.get().getSupportingInterface();
284 } catch (OpenRoadmInterfaceException e) {
285 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
290 private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
291 KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
292 .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
293 .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
294 Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
295 portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
296 if (port.getInterfaces() == null) {
299 for (Interfaces interf : port.getInterfaces()) {
300 if (interf.getInterfaceName().equals(ifBuilder.getName())) {