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.ListenableFuture;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.transportpce.common.Timeouts;
17 import org.opendaylight.transportpce.common.device.DeviceTransaction;
18 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.AdminStates;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.States;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOdu;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOtu;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev161014.maint.loopback.MaintLoopbackBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev161014.maint.testsignal.MaintTestsignalBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.container.OduBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class OpenRoadmInterfacesImpl implements OpenRoadmInterfaces {
42 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl.class);
44 // TODO move somewhere to constants
45 public static final String NETWORK_TOKEN = "NETWORK";
46 public static final String TTP_TOKEN = "TTP";
47 public static final String CLIENT_TOKEN = "CLIENT";
48 public static final String PP_TOKEN = "PP";
50 private final DeviceTransactionManager deviceTransactionManager;
52 public OpenRoadmInterfacesImpl(DeviceTransactionManager deviceTransactionManager) {
53 this.deviceTransactionManager = deviceTransactionManager;
57 public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
58 Future<Optional<DeviceTransaction>> deviceTxFuture = this.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.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
76 ListenableFuture<Void> txSubmitFuture = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
77 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
80 LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
81 } catch (InterruptedException | ExecutionException e) {
82 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
83 .getName(), nodeId), e);
88 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
89 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
90 Interface.class, new InterfaceKey(interfaceName));
91 return this.deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
92 interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
96 public void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
97 Optional<Interface> intf2DeleteOpt;
99 intf2DeleteOpt = getInterface(nodeId, interfaceName);
100 } catch (OpenRoadmInterfaceException e) {
101 throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
102 interfaceName, nodeId), e);
104 if (intf2DeleteOpt.isPresent()) {
105 Interface intf2Delete = intf2DeleteOpt.get();
106 // State admin state to out of service
107 InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
108 if (ifBuilder.getType() == OtnOdu.class) {
109 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
110 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
111 if (odu.getMaintTestsignal() != null) {
112 MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
113 maintSignalBuilder.setEnabled(false);
114 odu.setMaintTestsignal(maintSignalBuilder.build());
116 oduBuilder.setOdu(odu.build());
117 ifBuilder.addAugmentation(Interface1.class, oduBuilder.build());
118 } else if (ifBuilder.getType() == OtnOtu.class) {
119 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder
121 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder(
122 intf2Delete.augmentation(
123 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1.class));
124 OtuBuilder otu = new OtuBuilder(otuBuilder.getOtu());
125 if (otu.getMaintLoopback() != null) {
126 MaintLoopbackBuilder maintLoopBackBuilder = new MaintLoopbackBuilder();
127 maintLoopBackBuilder.setEnabled(false);
128 otu.setMaintLoopback(maintLoopBackBuilder.build());
130 otuBuilder.setOtu(otu.build());
131 ifBuilder.addAugmentation(
132 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1.class,
135 ifBuilder.setAdministrativeState(AdminStates.OutOfService);
136 // post interface with updated admin state
138 postInterface(nodeId, ifBuilder);
139 } catch (OpenRoadmInterfaceException ex) {
140 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
141 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
144 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
145 Interface.class, new InterfaceKey(interfaceName));
146 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
148 DeviceTransaction deviceTx;
150 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
151 if (deviceTxOpt.isPresent()) {
152 deviceTx = deviceTxOpt.get();
154 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
157 } catch (InterruptedException | ExecutionException e) {
158 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
162 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
163 ListenableFuture<Void> submit = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
164 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
168 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
169 } catch (InterruptedException | ExecutionException e) {
170 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
171 interfaceName, nodeId), e);
173 // change the equipment state on circuit pack if xpdr node
174 if (intf2Delete.getName().contains(OpenRoadmInterfacesImpl.CLIENT_TOKEN) || intf2Delete.getName().contains(
175 OpenRoadmInterfacesImpl.NETWORK_TOKEN)) {
176 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
180 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
185 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
186 throws OpenRoadmInterfaceException {
188 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
189 CircuitPacks.class, new CircuitPacksKey(circuitPackName));
190 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
191 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
192 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
193 CircuitPacks cp = null;
194 if (cpOpt.isPresent()) {
197 throw new OpenRoadmInterfaceException(String.format(
198 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
200 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
201 boolean change = false;
203 if (cpBldr.getEquipmentState()!=null && !cpBldr.getEquipmentState().equals(States.NotReservedInuse)) {
204 cpBldr.setEquipmentState(States.NotReservedInuse);
208 if ((cpBldr.getEquipmentState()!=null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
209 cpBldr.setEquipmentState(States.NotReservedAvailable);
214 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
216 DeviceTransaction deviceTx;
218 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
219 if (deviceTxOpt.isPresent()) {
220 deviceTx = deviceTxOpt.get();
222 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
225 } catch (InterruptedException | ExecutionException e) {
226 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
229 deviceTx.put(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
230 ListenableFuture<Void> txSubmitFuture = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
231 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
233 txSubmitFuture.get();
234 LOG.info("Successfully posted equipment state change on node {}", nodeId);
235 } catch (InterruptedException | ExecutionException e) {
236 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",