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.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.Interface;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.States;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class OpenRoadmInterfacesImpl710 {
37 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl710.class);
39 private final DeviceTransactionManager deviceTransactionManager;
41 public OpenRoadmInterfacesImpl710(DeviceTransactionManager deviceTransactionManager) {
42 this.deviceTransactionManager = deviceTransactionManager;
45 public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
46 Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
47 DeviceTransaction deviceTx;
49 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
50 if (deviceTxOpt.isPresent()) {
51 deviceTx = deviceTxOpt.get();
53 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
56 } catch (InterruptedException | ExecutionException e) {
57 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
61 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
62 Interface.class, new InterfaceKey(ifBuilder.getName()));
63 LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
64 ifBuilder.getType().toString());
65 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
66 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
67 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
70 LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
71 } catch (InterruptedException | ExecutionException e) {
72 throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
73 .getName(), nodeId), e);
78 public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
79 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
80 .child(Interface.class, new InterfaceKey(interfaceName));
81 return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
82 interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
86 public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
87 LOG.info("deleting interface {} on device71 {}", interfaceName, nodeId);
88 Optional<Interface> intf2DeleteOpt;
90 intf2DeleteOpt = getInterface(nodeId, interfaceName);
91 } catch (OpenRoadmInterfaceException e) {
92 throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
93 interfaceName, nodeId), e);
95 if (intf2DeleteOpt.isPresent()) {
96 Interface intf2Delete = intf2DeleteOpt.get();
97 // State admin state to out of service
98 InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
99 ifBuilder.setAdministrativeState(AdminStates.OutOfService);
100 // post interface with updated admin state
102 postInterface(nodeId, ifBuilder);
103 } catch (OpenRoadmInterfaceException ex) {
104 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
105 + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
108 InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
109 Interface.class, new InterfaceKey(interfaceName));
110 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
112 DeviceTransaction deviceTx;
114 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
115 if (deviceTxOpt.isPresent()) {
116 deviceTx = deviceTxOpt.get();
118 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
121 } catch (InterruptedException | ExecutionException e) {
122 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
126 deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
127 FluentFuture<? extends @NonNull CommitInfo> commit =
128 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
132 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
133 } catch (InterruptedException | ExecutionException e) {
134 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
135 interfaceName, nodeId), e);
137 // change the equipment state on circuit pack if xpdr node
138 if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
139 StringConstants.NETWORK_TOKEN)) {
140 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
144 LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
148 public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
149 throws OpenRoadmInterfaceException {
150 InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
151 CircuitPacks.class, new CircuitPacksKey(circuitPackName));
152 Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
153 LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
154 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
155 CircuitPacks cp = null;
156 if (cpOpt.isPresent()) {
159 throw new OpenRoadmInterfaceException(String.format(
160 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
162 CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
163 boolean change = false;
165 if (cpBldr.getEquipmentState() != null
166 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
167 cpBldr.setEquipmentState(States.NotReservedInuse);
170 } else if ((cpBldr.getEquipmentState() != null
171 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
172 cpBldr.setEquipmentState(States.NotReservedAvailable);
176 Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
178 DeviceTransaction deviceTx;
180 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
181 if (deviceTxOpt.isPresent()) {
182 deviceTx = deviceTxOpt.get();
184 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
187 } catch (InterruptedException | ExecutionException e) {
188 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
191 deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
192 FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
193 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
195 txSubmitFuture.get();
196 LOG.info("Successfully posted equipment state change on node {}", nodeId);
197 } catch (InterruptedException | ExecutionException e) {
198 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
204 public String getSupportedInterface(String nodeId, String interf) {
205 Optional<Interface> supInterfOpt;
207 supInterfOpt = getInterface(nodeId, interf);
208 if (supInterfOpt.isPresent()) {
209 return supInterfOpt.get().getSupportingInterfaceList().get(0);
213 } catch (OpenRoadmInterfaceException e) {
214 LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);